Просмотр исходного кода

Merge branch 'master' of http://47.100.37.243:10191/quyueting/manHourHousekeeper

QuYueTing 3 недель назад
Родитель
Сommit
3afad2e824

+ 50 - 39
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ReportServiceImpl.java

@@ -16132,29 +16132,28 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
         }
 
         List<Map<String, Object>> deptReportList = reportMapper.selectMaps(new QueryWrapper<Report>()
-                .select("dept_id as deptId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
+                .select("dept_id as deptId", "creator_id as userId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
                 .eq("company_id", companyId)
                 .eq("state", 1)
                 .apply("date_format(create_date, '%Y-%m') = {0}", month)
                 .in("project_id", projectIds)
-                .groupBy("dept_id")
+                .groupBy("dept_id", "creator_id")
                 .orderByDesc("sum(working_time)"));
         Map<Integer, Department> departmentMap = getDepartmentMap(companyId);
         List<HashMap> resultList = buildDeptReportResult(rollupDeptReportRows(deptReportList, departmentMap), departmentMap, false);
         msg.data = resultList;
         return msg;
     }
-
     @Override
     public HttpRespMsg getProjectReportGroupByDept(Integer companyId, String ymonth) {
         HttpRespMsg msg = new HttpRespMsg();
         String month = normalizeYearMonth(ymonth);
         List<Map<String, Object>> deptReportList = reportMapper.selectMaps(new QueryWrapper<Report>()
-                .select("dept_id as deptId", "sum(working_time) as workingTime", "count(distinct creator_id) as memberCount", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
+                .select("dept_id as deptId", "creator_id as userId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
                 .eq("company_id", companyId)
                 .eq("state", 1)
                 .apply("date_format(create_date, '%Y-%m') = {0}", month)
-                .groupBy("dept_id")
+                .groupBy("dept_id", "creator_id")
                 .orderByDesc("sum(working_time)"));
         Map<Integer, Department> departmentMap = getDepartmentMap(companyId);
         List<HashMap> resultList = buildDeptReportResult(rollupDeptReportRows(deptReportList, departmentMap), departmentMap, true);
@@ -16198,13 +16197,13 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
         Map<String, Object> result = new HashMap<>();
         result.put("projectSituation", getProjectSituation(companyId, month));
         result.put("projectOvertimeTop5", getProjectRank(companyId, month, "sum(ifnull(overtime_hours, 0))", 5, true));
-        result.put("top3OvertimeProjectDept", getTopProjectDeptReport(companyId, month, "sum(ifnull(overtime_hours, 0))", true));
+        result.put("top3OvertimeProjectDept", getTopProjectDeptReport(companyId, month, "sum(working_time)", false));
         List<Map<String, Object>> deptOvertimeSummaryList = reportMapper.selectMaps(new QueryWrapper<Report>()
-                .select("dept_id as deptId", "sum(working_time) as workingTime", "count(distinct creator_id) as memberCount", "sum(ifnull(overtime_hours, 0)) as overtimeHours", "max(ifnull(overtime_hours, 0)) as maxOvertimeHours")
+                .select("dept_id as deptId", "creator_id as userId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours", "max(ifnull(overtime_hours, 0)) as maxOvertimeHours")
                 .eq("company_id", companyId)
                 .eq("state", 1)
                 .apply("date_format(create_date, '%Y-%m') = {0}", month)
-                .groupBy("dept_id")
+                .groupBy("dept_id", "creator_id")
                 .orderByDesc("sum(ifnull(overtime_hours, 0))"));
         result.put("deptOvertimeSummary", buildDeptReportResult(rollupDeptReportRows(deptOvertimeSummaryList, departmentMap), departmentMap, true));
         result.put("deptProjectTop3", getDeptProjectTopN(companyId, month, departmentMap, false));
@@ -16271,19 +16270,28 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
         if (!departmentMap.containsKey(deptId)) {
             return deptId;
         }
+        List<Integer> path = new ArrayList<>();
         Set<Integer> visited = new HashSet<>();
         Integer currentDeptId = deptId;
         while (currentDeptId != null && departmentMap.containsKey(currentDeptId) && visited.add(currentDeptId)) {
+            path.add(currentDeptId);
             Department current = departmentMap.get(currentDeptId);
             Integer superiorId = current.getSuperiorId();
             if (superiorId == null || superiorId == 0 || !departmentMap.containsKey(superiorId)) {
-                return currentDeptId;
+                break;
             }
             currentDeptId = superiorId;
         }
-        return deptId;
+        if (path.isEmpty()) {
+            return deptId;
+        }
+        Integer rootDeptId = path.get(path.size() - 1);
+        Department rootDepartment = departmentMap.get(rootDeptId);
+        if (rootDepartment != null && Integer.valueOf(1).equals(rootDepartment.getCorpwxDeptid()) && path.size() > 1) {
+            return path.get(path.size() - 2);
+        }
+        return rootDeptId;
     }
-
     private void fillDepartmentInfo(HashMap<String, Object> map, Integer deptId, Map<Integer, Department> departmentMap) {
         Department department = departmentMap.get(deptId);
         map.put("departmentId", deptId);
@@ -16307,20 +16315,31 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
                 map.put("projectCount", 0D);
                 map.put("overtimeProjectCount", 0D);
                 map.put("maxOvertimeHours", 0D);
+                map.put("memberIds", new HashSet<String>());
                 return map;
             });
             target.put("workingTime", toDouble(target.get("workingTime")) + toDouble(row.get("workingTime")));
             target.put("overtimeHours", toDouble(target.get("overtimeHours")) + toDouble(row.get("overtimeHours")));
-            target.put("memberCount", toDouble(target.get("memberCount")) + toDouble(row.get("memberCount")));
+            String userId = row.get("userId") == null ? null : String.valueOf(row.get("userId"));
+            if (!StringUtils.isEmpty(userId)) {
+                ((Set<String>) target.get("memberIds")).add(userId);
+            } else {
+                target.put("memberCount", toDouble(target.get("memberCount")) + toDouble(row.get("memberCount")));
+            }
             target.put("projectCount", toDouble(target.get("projectCount")) + toDouble(row.get("projectCount")));
             target.put("overtimeProjectCount", toDouble(target.get("overtimeProjectCount")) + toDouble(row.get("overtimeProjectCount")));
             if (row.containsKey("maxOvertimeHours")) {
                 target.put("maxOvertimeHours", Math.max(toDouble(target.get("maxOvertimeHours")), toDouble(row.get("maxOvertimeHours"))));
             }
         });
+        grouped.values().forEach(row -> {
+            Set<String> memberIds = (Set<String>) row.remove("memberIds");
+            if (memberIds != null && !memberIds.isEmpty()) {
+                row.put("memberCount", memberIds.size());
+            }
+        });
         return new ArrayList<>(grouped.values());
     }
-
     private List<Map<String, Object>> rollupDeptProjectCountRows(List<Map<String, Object>> rows, Map<Integer, Department> departmentMap) {
         Map<Integer, Set<Integer>> projectMap = new LinkedHashMap<>();
         Map<Integer, Set<Integer>> overtimeProjectMap = new LinkedHashMap<>();
@@ -16408,17 +16427,16 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
             return new ArrayList<>();
         }
         List<Map<String, Object>> deptReportList = reportMapper.selectMaps(new QueryWrapper<Report>()
-                .select("dept_id as deptId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
+                .select("dept_id as deptId", "creator_id as userId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
                 .eq("company_id", companyId)
                 .eq("state", 1)
                 .apply("date_format(create_date, '%Y-%m') = {0}", month)
                 .in("project_id", projectIds)
-                .groupBy("dept_id")
+                .groupBy("dept_id", "creator_id")
                 .orderByDesc(orderByOvertime ? "sum(ifnull(overtime_hours, 0))" : "sum(working_time)"));
         Map<Integer, Department> departmentMap = getDepartmentMap(companyId);
         return buildDeptReportResult(rollupDeptReportRows(deptReportList, departmentMap), departmentMap, false);
     }
-
     private List<HashMap> getDeptProjectTopN(Integer companyId, String month, Map<Integer, Department> departmentMap, boolean overtimeOnly) {
         List<Map<String, Object>> reportList = reportMapper.selectMaps(new QueryWrapper<Report>()
                 .select("dept_id as deptId", "project_id as projectId", "sum(working_time) as workingTime", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
@@ -16515,46 +16533,39 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
 
     private List<HashMap> getUserOvertimeTopN(Integer companyId, String month, Map<Integer, Department> departmentMap) {
         List<Map<String, Object>> reportList = reportMapper.selectMaps(new QueryWrapper<Report>()
-                .select("creator_id as userId", "dept_id as deptId", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
+                .select("creator_id as userId", "sum(ifnull(overtime_hours, 0)) as overtimeHours")
                 .eq("company_id", companyId)
                 .eq("state", 1)
                 .apply("date_format(create_date, '%Y-%m') = {0}", month)
-                .groupBy("creator_id", "dept_id")
+                .groupBy("creator_id")
                 .having("sum(ifnull(overtime_hours, 0)) > 0"));
         List<String> userIds = reportList.stream()
                 .map(row -> String.valueOf(row.get("userId")))
                 .filter(id -> !StringUtils.isEmpty(id))
                 .collect(Collectors.toList());
         Map<String, User> userMap = userIds.isEmpty() ? new HashMap<>() : userMapper.selectList(new QueryWrapper<User>()
-                .select("id, name, corpwx_userid")
+                .select("id, name, corpwx_userid, department_id")
                 .in("id", userIds))
                 .stream()
                 .collect(Collectors.toMap(User::getId, Function.identity(), (left, right) -> left));
-        Map<String, HashMap<String, Object>> grouped = new LinkedHashMap<>();
-        reportList.forEach(row -> {
-            Integer deptId = toInteger(row.get("deptId"));
-            Integer topDeptId = getTopDepartmentId(deptId, departmentMap);
-            String userId = String.valueOf(row.get("userId"));
-            String key = userId + "_" + (topDeptId == null ? -1 : topDeptId);
-            HashMap<String, Object> map = grouped.computeIfAbsent(key, groupKey -> {
-                User user = userMap.get(userId);
-                HashMap<String, Object> item = new HashMap<>();
-                fillDepartmentInfo(item, topDeptId, departmentMap);
-                item.put("userId", userId);
-                item.put("userName", user == null ? userId : user.getName());
-                item.put("corpwxUserid", user == null ? null : user.getCorpwxUserid());
-                item.put("overtimeHours", 0D);
-                return item;
-            });
-            map.put("overtimeHours", roundHours(toDouble(map.get("overtimeHours")) + toDouble(row.get("overtimeHours"))));
-        });
-        return grouped.values().stream()
+        return reportList.stream()
+                .map(row -> {
+                    String userId = String.valueOf(row.get("userId"));
+                    User user = userMap.get(userId);
+                    Integer topDeptId = user == null ? null : getTopDepartmentId(user.getDepartmentId(), departmentMap);
+                    HashMap<String, Object> item = new HashMap<>();
+                    fillDepartmentInfo(item, topDeptId, departmentMap);
+                    item.put("userId", userId);
+                    item.put("userName", user == null ? userId : user.getName());
+                    item.put("corpwxUserid", user == null ? null : user.getCorpwxUserid());
+                    item.put("overtimeHours", roundHours(row.get("overtimeHours")));
+                    return item;
+                })
                 .sorted((left, right) -> Double.compare(toDouble(right.get("overtimeHours")), toDouble(left.get("overtimeHours"))))
                 .limit(10)
                 .map(item -> (HashMap) item)
                 .collect(Collectors.toList());
     }
-
     private List<HashMap> fillProjectInfo(Integer companyId, List<Map<String, Object>> reportList) {
         if (reportList.isEmpty()) {
             return new ArrayList<>();

+ 1 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/resources/mapper/DepartmentMapper.xml

@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8"?>
+<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.management.platform.mapper.DepartmentMapper">