瀏覽代碼

优化因部门表结构不正确导致数据查询错误问题

Guos 6 天之前
父節點
當前提交
7fca44e8bc

+ 5 - 0
fs-service/src/main/java/com/fs/company/domain/CompanyDeptUserInfo.java

@@ -2,6 +2,8 @@ package com.fs.company.domain;
 
 import lombok.Data;
 
+import java.util.List;
+
 /**
  * @description:
  * @author: Guos
@@ -50,4 +52,7 @@ public class CompanyDeptUserInfo {
     */
    private String nickName;
 
+
+   private List<CompanyDeptUserInfo> children;
+
 }

+ 103 - 48
fs-service/src/main/java/com/fs/company/service/impl/StatisticManageServiceImpl.java

@@ -8,6 +8,7 @@ import com.fs.company.domain.CourseWatchLogStatistics;
 import com.fs.company.mapper.CourseWatchLogStatisticsMapper;
 import com.fs.company.mapper.StatisticManageMapper;
 import com.fs.company.service.IStatisticManageService;
+import java.util.function.Function;
 import com.fs.statis.dto.ComprehensiveStatisticsDTO;
 import com.fs.statis.param.ComprehensiveStatisticsParam;
 import com.google.common.collect.Lists;
@@ -45,38 +46,33 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
     @Override
     public Object statisticMain(ComprehensiveStatisticsParam param) {
         if(param.getDimension() == DimensionEnum.COMPANY.getValue()){
-            Assert.notNull(param.getId(), "按公司展示查询条件不能为空!");
-            return getStatisticNumByCompanyId(param);
+            return getStatisticNumByCompany(param);
         }
         if(param.getDimension() == DimensionEnum.DEPARTMENT.getValue()){
             Assert.notNull(param.getId(), "按部门展示查询条件不能为空!");
-            return getStatisticNumByDeptId(param);
+            return getStatisticNumByCompanyId(param);
         }
         if (param.getDimension() == DimensionEnum.PERSONAL.getValue()){
             Assert.notNull(param.getId(), "按个人展示查询条件不能为空!");
-            return getStatisticNumByPersonal(param);
+            return getStatisticNumByDeptId(param);
         }
         return null;
     }
 
     /**
-     * 获取个人统计数据
+     * 按照公司维度统计数据
      */
-    public List getStatisticNumByDeptId(ComprehensiveStatisticsParam param){
-        //先获取部门下的用户的信息
-        Long id = param.getId();
-        List<CompanyDeptUserInfo> searchUserInfo = statisticManageMapper.getSearchUserInfo(id);
+    public List getStatisticNumByCompany(ComprehensiveStatisticsParam param) {
+        List<CompanyDeptUserInfo> companyInfo = statisticManageMapper.getCompanyInfo();
         List result = Lists.newArrayList();
         List<Date> dates = generateDateList(param.getStartTime(), param.getEndTime());
         for (Date date : dates){
             param.setStartTime(date);
             param.setEndTime(date);
-            for (CompanyDeptUserInfo companyDeptUserInfo : searchUserInfo) {
-                String deptName = companyDeptUserInfo.getDeptName();
+            for (CompanyDeptUserInfo companyDeptUserInfo : companyInfo) {
                 String companyName = companyDeptUserInfo.getCompanyName();
-                Long deptId = companyDeptUserInfo.getDeptId();
                 Long companyId = companyDeptUserInfo.getCompanyId();
-                param.setUserId(companyDeptUserInfo.getUserId());
+                param.setCompanyId(companyId);
                 ComprehensiveStatisticsDTO statisticDataByDeptId =
                         statisticManageMapper.getStatisticDataByDeptId(param);
                 if(ObjectUtils.isEmpty(statisticDataByDeptId)){
@@ -85,11 +81,10 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
                     statisticDataByDeptId.setRedPacketNum(BigDecimal.ZERO.intValue());
                     statisticDataByDeptId.setSendCount(BigDecimal.ZERO.intValue());
                     statisticDataByDeptId.setRedPacketAmount(BigDecimal.ZERO);
+                    statisticDataByDeptId.setCompleteNum(BigDecimal.ZERO.intValue());
                 }
                 statisticDataByDeptId.setCompanyUserName(companyDeptUserInfo.getNickName());
                 statisticDataByDeptId.setCompanyUserId(companyDeptUserInfo.getUserId());
-                statisticDataByDeptId.setDeptName(deptName);
-                statisticDataByDeptId.setDeptId(deptId.intValue());
                 statisticDataByDeptId.setCompanyName(companyName);
                 statisticDataByDeptId.setCompanyId(companyId);
                 statisticDataByDeptId.setStatisticsTime(date);
@@ -100,54 +95,74 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
     }
 
     /**
-     * 获取个人统计数据
+     * 根据部门id获取个人统计数据
      */
-    public List getStatisticNumByPersonal(ComprehensiveStatisticsParam param){
-        //先获取用户的信息
+    public List getStatisticNumByDeptId(ComprehensiveStatisticsParam param){
+        //先获取部门下的用户的信息
         Long id = param.getId();
-        CompanyDeptUserInfo userInfoByUserId = statisticManageMapper.getUserInfoByUserId(id);
+        List<CompanyDeptUserInfo> searchUserInfo = statisticManageMapper.getSearchUserInfo(id);
         List result = Lists.newArrayList();
         List<Date> dates = generateDateList(param.getStartTime(), param.getEndTime());
-        for (Date date : dates){
+        for (Date date : dates) {
             param.setStartTime(date);
             param.setEndTime(date);
-            param.setDeptId(userInfoByUserId.getDeptId());
-            param.setUserId(id);
-            ComprehensiveStatisticsDTO statisticDataByDeptId = statisticManageMapper.getStatisticDataByDeptId(param);
-            if(ObjectUtils.isEmpty(statisticDataByDeptId)){
-                statisticDataByDeptId = new ComprehensiveStatisticsDTO();
-                statisticDataByDeptId.setAnswerNum(BigDecimal.ZERO.intValue());
-                statisticDataByDeptId.setRedPacketNum(BigDecimal.ZERO.intValue());
-                statisticDataByDeptId.setSendCount(BigDecimal.ZERO.intValue());
-                statisticDataByDeptId.setRedPacketAmount(BigDecimal.ZERO);
+            for (CompanyDeptUserInfo companyDeptUserInfo : searchUserInfo) {
+                Long parentId = companyDeptUserInfo.getParentId();
+                Long deptId = companyDeptUserInfo.getDeptId();
+                if (parentId != BigDecimal.ZERO.longValue()) {
+                    Optional<CompanyDeptUserInfo> first = searchUserInfo.stream().filter(dept -> dept.getDeptId().equals(parentId)).findFirst();
+                    if (first.isPresent()){
+                        CompanyDeptUserInfo findParentDept = first.get();
+                        String parentDeptName = findParentDept.getDeptName();
+                        String deptName = companyDeptUserInfo.getDeptName();
+                        companyDeptUserInfo.setDeptName(parentDeptName + "/" + deptName);
+                    }
+                }
+                String companyName = companyDeptUserInfo.getCompanyName();
+                Long companyId = companyDeptUserInfo.getCompanyId();
+                param.setUserId(companyDeptUserInfo.getUserId());
+                ComprehensiveStatisticsDTO statisticDataByDeptId =
+                        statisticManageMapper.getStatisticDataByDeptId(param);
+                if(ObjectUtils.isEmpty(statisticDataByDeptId)){
+                    statisticDataByDeptId = new ComprehensiveStatisticsDTO();
+                    statisticDataByDeptId.setAnswerNum(BigDecimal.ZERO.intValue());
+                    statisticDataByDeptId.setRedPacketNum(BigDecimal.ZERO.intValue());
+                    statisticDataByDeptId.setSendCount(BigDecimal.ZERO.intValue());
+                    statisticDataByDeptId.setRedPacketAmount(BigDecimal.ZERO);
+                    statisticDataByDeptId.setCompleteNum(BigDecimal.ZERO.intValue());
+                }
+                statisticDataByDeptId.setCompanyUserName(companyDeptUserInfo.getNickName());
+                statisticDataByDeptId.setCompanyUserId(companyDeptUserInfo.getUserId());
+                statisticDataByDeptId.setDeptName(companyDeptUserInfo.getDeptName());
+                statisticDataByDeptId.setDeptId(deptId.intValue());
+                statisticDataByDeptId.setCompanyName(companyName);
+                statisticDataByDeptId.setCompanyId(companyId);
+                statisticDataByDeptId.setStatisticsTime(date);
+                result.add(statisticDataByDeptId);
             }
-            statisticDataByDeptId.setDeptName(userInfoByUserId.getDeptName());
-            statisticDataByDeptId.setCompanyUserName(userInfoByUserId.getNickName());
-            statisticDataByDeptId.setCompanyUserId(userInfoByUserId.getUserId());
-            statisticDataByDeptId.setDeptName(userInfoByUserId.getDeptName());
-            statisticDataByDeptId.setDeptId(userInfoByUserId.getDeptId().intValue());
-            statisticDataByDeptId.setCompanyName(userInfoByUserId.getCompanyName());
-            statisticDataByDeptId.setCompanyId(userInfoByUserId.getCompanyId());
-            statisticDataByDeptId.setStatisticsTime(date);
-            result.add(statisticDataByDeptId);
         }
         return result;
     }
 
     /**
-     * 根据公司获取统计数据
+     * 根据公司获取统部门的计数据
      */
     public List getStatisticNumByCompanyId(ComprehensiveStatisticsParam param){
         //先从公司拿到公司和部门的信息
-        List<CompanyDeptUserInfo> searchDeptInfo = getSearchDeptInfo(param.getId());
+        List<CompanyDeptUserInfo> searchDeptInfo = statisticManageMapper.getSearchDeptInfo(param.getId());;
         List result = Lists.newArrayList();
-
         List<Date> dates = generateDateList(param.getStartTime(), param.getEndTime());
         for (Date date : dates){
             param.setStartTime(date);
             param.setEndTime(date);
             for (CompanyDeptUserInfo companyDeptUserInfo : searchDeptInfo) {
-                String deptName = companyDeptUserInfo.getDeptName();
+                //判断当前部门的父级id不是0就组装名称
+                Long parentId = companyDeptUserInfo.getParentId();
+                if(parentId != BigDecimal.ZERO.longValue()){
+                    String parentDeptName = searchDeptInfo.stream().filter(dept -> dept.getDeptId().equals(parentId)).findFirst().get().getDeptName();
+                    String deptName = companyDeptUserInfo.getDeptName();
+                    companyDeptUserInfo.setDeptName(parentDeptName + "/" + deptName);
+                }
                 String companyName = companyDeptUserInfo.getCompanyName();
                 Long deptId = companyDeptUserInfo.getDeptId();
                 Long companyId = companyDeptUserInfo.getCompanyId();
@@ -160,8 +175,9 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
                     statisticDataByDeptId.setRedPacketNum(BigDecimal.ZERO.intValue());
                     statisticDataByDeptId.setSendCount(BigDecimal.ZERO.intValue());
                     statisticDataByDeptId.setRedPacketAmount(BigDecimal.ZERO);
+                    statisticDataByDeptId.setCompleteNum(BigDecimal.ZERO.intValue());
                 }
-                statisticDataByDeptId.setDeptName(deptName);
+                statisticDataByDeptId.setDeptName(companyDeptUserInfo.getDeptName());
                 statisticDataByDeptId.setDeptId(deptId.intValue());
                 statisticDataByDeptId.setCompanyName(companyName);
                 statisticDataByDeptId.setCompanyId(companyId);
@@ -229,12 +245,51 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
     }
 
     /**
-     * 根据公司id获取部门下拉搜索信息
-     * @param id
+     * 根据公司id获取部门下拉搜索信息(递归获取完整部门树)
+     * @param id 公司ID
      */
     @Override
     public List<CompanyDeptUserInfo> getSearchDeptInfo(Long id){
-        return statisticManageMapper.getSearchDeptInfo(id);
+        List<CompanyDeptUserInfo> allDepts = statisticManageMapper.getSearchDeptInfo(id);
+        if(CollectionUtils.isEmpty(allDepts)){
+            return allDepts;
+        }
+
+        // 构建ID到部门的映射,方便查找
+        Map<Long, CompanyDeptUserInfo> deptMap = allDepts.stream()
+                .collect(Collectors.toMap(CompanyDeptUserInfo::getDeptId, Function.identity()));
+
+        // 找到所有根部门(父级ID为0或null的部门)
+        List<CompanyDeptUserInfo> rootDepts = allDepts.stream()
+                .filter(dept -> dept.getParentId() == null ||
+                        dept.getParentId().equals(BigDecimal.ZERO.longValue()))
+                .collect(Collectors.toList());
+
+        // 递归构建部门树
+        buildDeptTree(rootDepts, deptMap);
+
+        return rootDepts;
+    }
+
+    /**
+     * 递归构建部门树结构
+     * @param parentDepts 父部门列表
+     * @param deptMap 部门映射表
+     */
+    private void buildDeptTree(List<CompanyDeptUserInfo> parentDepts, Map<Long, CompanyDeptUserInfo> deptMap) {
+        for (CompanyDeptUserInfo parentDept : parentDepts) {
+            // 查找当前部门的所有直接子部门
+            List<CompanyDeptUserInfo> children = deptMap.values().stream()
+                    .filter(dept -> dept.getParentId() != null &&
+                            dept.getParentId().equals(parentDept.getDeptId()))
+                    .collect(Collectors.toList());
+
+            if (!children.isEmpty()) {
+                // 递归构建子部门的子树
+                buildDeptTree(children, deptMap);
+                parentDept.setChildren(children);
+            }
+        }
     }
 
     /**
@@ -264,7 +319,7 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
         companyDeptdUserList.forEach(companyDeptUserInfo -> {
             Long userId = companyDeptUserInfo.getUserId();
             courseWatchLogStatisticsMapper.deleteByStatisticsTimeAndDeptId(userId, companyDeptUserInfo.getDeptId(), finalDateTime);
-            //第一步通过人去查询【课程,项目,视频id】,【发送,看视频状态】
+            //第一步通过人去查询【课程,项目,视频id】,【发送,看视频状态】userId空就按照部门删除,但是要先将userId为空的先删除
             List<CourseWatchLogData> courseWatchLogData = statisticManageMapper.getCourseWatchLogData(userId, finalDateTime);
             CourseWatchLogStatistics courseWatchLogStatistics = component(companyDeptUserInfo, finalDateTime);
             if(CollectionUtils.isEmpty(courseWatchLogData)){
@@ -343,7 +398,7 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
         //上面代码其实都是差不多,为了方便之后优化代码,保持整个逻辑清晰,先将整个数据保存后,再反查数据将答题数量、红包领取数量,以及金额统计更新
         updateCourseRedPacket(finalDateTime);
         stopWatch.stop();
-        log.info("gs-执行数据统计任务完成,耗时:{}", stopWatch.getTotalTimeSeconds());
+        log.info("gs-执行数据统计任务完成,耗时:{}", stopWatch.prettyPrint());
     }
 
     /**

+ 2 - 0
fs-service/src/main/java/com/fs/statis/dto/ComprehensiveStatisticsDTO.java

@@ -29,6 +29,8 @@ public class ComprehensiveStatisticsDTO {
 
     private Integer logType;
 
+    private Integer completeNum; //完成数
+
     private Integer answerNum;
 
     private Integer redPacketNum;

+ 2 - 0
fs-service/src/main/java/com/fs/statis/param/ComprehensiveStatisticsParam.java

@@ -47,6 +47,8 @@ public class ComprehensiveStatisticsParam {
 
     private Long projectId;
 
+    private Long companyId;
+
     private String companyName;
 
     private String deptName;

+ 1 - 1
fs-service/src/main/resources/mapper/company/CourseWatchLogStatisticsMapper.xml

@@ -43,7 +43,7 @@
             #{companyUserId},
             #{companyId},
             #{deptId},
-            #{statisticsTime},
+            DATE_FORMAT(#{statisticsTime}, '%Y-%m-%d' ),
             #{courseId},
             #{videoId},
             #{projectId},

+ 23 - 11
fs-service/src/main/resources/mapper/company/StatisticManageMapper.xml

@@ -246,6 +246,7 @@
 
     <select id="getSearchUserInfo" resultMap="CompanyDeptUserListMap">
         SELECT
+            cd.parent_id,
             ci.company_id,
             ci.company_name,
             cd.dept_id,
@@ -256,22 +257,26 @@
         FROM company AS ci
                  LEFT JOIN company_dept AS cd ON ci.company_id = cd.company_id AND cd.STATUS = 0
                  LEFT JOIN company_user AS cu ON cu.dept_id = cd.dept_id AND cu.del_flag = 0
-        WHERE ci.is_del = 0 AND cd.del_flag = 0 and cu.dept_id = #{id}
+        WHERE ci.is_del = 0
+          AND cd.del_flag = 0
+          and (cd.dept_id = #{id} OR FIND_IN_SET(#{id}, cd.ancestors) > 0)
+        order by cd.dept_id
     </select>
 
     <select id="getCourseWatchLogData" resultType="com.fs.company.domain.CourseWatchLogData">
         SELECT
-            company_user_id,
-            video_id,
-            course_id,
-            project,
-            log_type,
-            create_time
+            fcwl.company_user_id,
+            fcwl.video_id,
+            fcwl.course_id,
+            fuc.project,
+            fcwl.log_type,
+            fcwl.create_time
         FROM
-            fs_course_watch_log
+            fs_course_watch_log as fcwl
+            left join fs_user_course as fuc on fcwl.course_id = fuc.course_id and fuc.is_del = 0
         WHERE
-            company_user_id = #{companyUserId}
-          and DATE_FORMAT(create_time, '%Y-%m-%d') = DATE_FORMAT(#{dateTime}, '%Y-%m-%d')
+            fcwl.company_user_id = #{companyUserId}
+          and DATE_FORMAT(fcwl.create_time, '%Y-%m-%d') = DATE_FORMAT(#{dateTime}, '%Y-%m-%d')
     </select>
 
     <select id="getAnswerAndRedPacketData" resultType="com.fs.company.domain.CourseWatchLogData">
@@ -302,6 +307,7 @@
 
     <select id="getStatisticDataByDeptId" resultType="com.fs.statis.dto.ComprehensiveStatisticsDTO">
         SELECT
+            SUM(CASE WHEN fs.log_type != 2 THEN 0 ELSE fs.send_count END) AS completeNum,
             SUM(IFNULL(fs.send_count, 0)) as sendCount,
             SUM(IFNULL(fs.log_type, 0)) as logType,
             SUM(IFNULL(fs.answer_num, 0)) as answerNum,
@@ -313,6 +319,9 @@
         WHERE
           DATE_FORMAT(fs.statistics_time, '%Y-%m-%d' ) >= DATE_FORMAT(#{startTime}, '%Y-%m-%d' )
           ANd DATE_FORMAT(fs.statistics_time, '%Y-%m-%d' ) &lt;= DATE_FORMAT(#{endTime}, '%Y-%m-%d' )
+        <if test="companyId != null">
+          and company_id = #{companyId}
+        </if>
         <if test="deptId != null">
              and fs.dept_id = #{deptId}
         </if>
@@ -335,9 +344,12 @@
         order by fs.statistics_time
         <if test="dimension != null">
             <if test="dimension == 2">
-                , fs.dept_id
+                , fs.company_id
             </if>
             <if test="dimension == 3">
+                , fs.dept_id
+            </if>
+            <if test="dimension == 1">
                 , fs.company_user_id
             </if>
         </if>