瀏覽代碼

优化日报查询性能

seyason 1 年之前
父節點
當前提交
1518a37ebe

+ 2 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/mapper/ReportMapper.java

@@ -92,12 +92,12 @@ public interface ReportMapper extends BaseMapper<Report> {
     List<Map<String, Object>> getReportNameByDateAndDept(@Param("date") String date,
                                                          @Param("deptIds") List<Integer> deptIds,
                                                          @Param("userId") String userId,
-                                                         @Param("companyId") Integer companyId, String leaderId, Integer pageStart, Integer pageSize);
+                                                         @Param("companyId") Integer companyId, String viewUserId, Integer pageStart, Integer pageSize);
 
     Integer getReportNameByDateAndDeptCount(@Param("date") String date,
                                                          @Param("deptIds") List<Integer> deptIds,
                                                          @Param("userId") String userId,
-                                                         @Param("companyId") Integer companyId, String leaderId);
+                                                         @Param("companyId") Integer companyId, String viewUserId);
     List<Map<String, Object>> getDetailByStateInMyProfession(@Param("state") Integer state,
                                                @Param("companyId") Integer companyId,
                                                @Param("leaderId") String leaderId);

+ 63 - 233
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ReportServiceImpl.java

@@ -237,209 +237,39 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
             //首先根据日期获取当天所有提交过日志的人
             String userId = request.getHeader("Token");
             User user = userMapper.selectById(userId);
-            TimeType timeType = timeTypeMapper.selectById(user.getCompanyId());
+            String viewUserId = user.getId();
+            Integer companyId = user.getCompanyId();
+            TimeType timeType = timeTypeMapper.selectById(companyId);
             List<Map<String, Object>> nameList = new ArrayList<>();
             List<SysRichFunction> functionList = sysFunctionMapper.getRoleFunctions(user.getRoleId(), "查看全公司工时");
             Integer totalMembCount = 0;
             if (functionList.size() == 0) {
-                String leaderId = user.getId();
-                //不是项目经理,只看自己的报告
                 List<Map<String, Object>> list = new ArrayList<>();
-                //没有指定员工或者指定的就是自己
-                if (targetUid == null || targetUid.equals(user.getId())) {
-                    //查看自己的日报
-                    if (pageIndex != null && pageIndex == 0) {
-                        //仅第一页显示自己的
-                        Map<String, Object> map = new HashMap<>();
-                        map.put("id", user.getId());
-                        map.put("name", user.getName());
-                        List<String> uids = new ArrayList<>();
-                        uids.add(user.getId());
-                        list = reportMapper.getUserReportByDateOrId(date, uids, null);
-                        if (list.size() > 0) {
-                            //个人日报
-                            nameList.add(map);
-                            map.put("data", list);
-                            double reportTime = 0;
-                            BigDecimal total = new BigDecimal(0);
-                            int state = (int)list.get(0).get("state");
-                            boolean hasDeny = false;
-                            boolean hasWaiting = false;
-                            for (Map<String, Object> m : list) {
-                                double t = (double) m.get("time");
-                                reportTime += t;
-                                total = total.add((BigDecimal)m.get("cost"));
-                                int curState = (int)m.get("state");
-                                if (curState == 2) {
-                                    hasDeny = true;
-                                }
-                                if (curState == 0) {
-                                    hasWaiting = true;
-                                }
-                            }
-                            if(hasDeny) {
-                                state = 2;
-                            } else if (hasWaiting) {
-                                state = 0;
-                            }
-                            DecimalFormat df = new DecimalFormat("0.00");
-                            map.put("reportTime", df.format(reportTime));
-                            map.put("cost", total);
-                            map.put("state", state);
-                        }
-                    }
-                }
-
-                String reportLeaderId = null;
-                if(deptId==null&&targetUid==null){
-                    //没有指定部门和人员,按照自己所管辖范围来查看
-                    reportLeaderId = leaderId;
-                }
-                List<Integer> allVisibleDeptIdList = getAllVisibleDeptIdList(user, null);
-                //需要看可见部门(部门主要负责人和其他负责人以及查看本部门工时权限)所有人员的日报
-                if (nameList.size() > 0) {
-                    //自己填写的日报
-                    List<Map<String, Object>> deptNameList = reportMapper.getReportNameByDateAndDept(date,
-                            allVisibleDeptIdList.size() > 0?allVisibleDeptIdList:null, targetUid, null, reportLeaderId, pageStart, pageSize);
-                    totalMembCount = reportMapper.getReportNameByDateAndDeptCount(date, allVisibleDeptIdList.size() > 0?allVisibleDeptIdList:null, targetUid, null, reportLeaderId);
-                    for (Map<String, Object> deptNameItem : deptNameList) {
-                        if (!deptNameItem.get("id").equals(user.getId())) {
-                            nameList.add(deptNameItem);
-                        }
+                List<Integer> allVisibleDeptIdList = null;
+                if (deptId != null) {
+                    //指定了部门进行查看
+                    allVisibleDeptIdList = new ArrayList<>();
+                    allVisibleDeptIdList.add(deptId);
+                    //找到该部门的所有子部门
+                    List<Department> allDepts = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
+                    Optional<Department> first = allDepts.stream().filter(d -> d.getDepartmentId().equals(deptId)).findFirst();
+                    if (first.isPresent()) {
+                        Department department = first.get();
+                        //递归获取全部子部门
+                        List<Department> deptList = getSubDepts(department, allDepts);
+                        deptList.add(department);
+                        List<Integer> ids = deptList.stream().map(Department::getDepartmentId).collect(Collectors.toList());
+                        allVisibleDeptIdList.addAll(ids);
                     }
                 } else {
-                    nameList = reportMapper.getReportNameByDateAndDept(date,
-                            allVisibleDeptIdList.size() > 0?allVisibleDeptIdList:null, targetUid, null, reportLeaderId, pageStart, pageSize);
-                    totalMembCount = reportMapper.getReportNameByDateAndDeptCount(date, allVisibleDeptIdList.size() > 0?allVisibleDeptIdList:null, targetUid, null, reportLeaderId);
+                    allVisibleDeptIdList = getAllVisibleDeptIdList(user, null);
                 }
-                if (nameList.size() > 0) {
-                    List<String> userIds = new ArrayList<>();
-                    nameList.forEach(n->{
-                        String id = (String) n.get("id");
-                        userIds.add(id);
-                    });
-                    List<Map<String, Object>> reportList = reportMapper.getUserReportByDateOrId(date, userIds, null);
-                    for (Map<String, Object> memb : nameList) {
-                        //再根据人分别获取当天的报告
-                        List<Map<String, Object>> rList = new ArrayList<Map<String, Object>>();
-                        BigDecimal total = new BigDecimal(0);
-                        for (Map<String, Object> report : reportList) {
-                            if (((String)report.get("creatorId")).equals((String)memb.get("id"))) {
-                                rList.add(report);
-                                total = total.add((BigDecimal) report.get("cost"));
-                            }
-                        }
-                        memb.put("data", rList);
-                        memb.put("cost", total);
-                        double reportTime = 0;
-                        if (rList.size() > 0) {
-                            int state = 1;
-                            for (Map<String, Object> m : rList) {
-                                double t = (double) m.get("time");
-                                reportTime += t;
-
-                                //取最低的状态
-                                if (state == 2) {
-                                    continue;
-                                }
-                                if (state == 0) {
-                                    if ((int)m.get("state") == 2) {
-                                        state = 2;
-                                    } else {
-                                        continue;
-                                    }
-                                } else {
-                                    state = (int)m.get("state");
-                                }
-                            }
-                            memb.put("state", state);
-                        }
-                        DecimalFormat df = new DecimalFormat("0.00");
-                        memb.put("reportTime", df.format(reportTime));
-                    }
-                }
-//                if(deptId==null&&targetUid==null){
-//                    //担任项目经理或者日报审核人,查找相关的人员的日报
-//                    List<Map<String, Object>> puserNames = reportMapper.getReportNameByDate(date, user.getCompanyId(), leaderId);
-//                    List<Map<String, Object>> inchargeReportList= reportMapper.getInchargeReportByDate(date, leaderId, null);
-//
-//                    if ((timeType.getReportAuditType() == 5 || timeType.getReportAuditType() == 6)&& timeType.getReportCc() == 1) {
-//                        //存在抄送人的设置,需要把抄送人的项目相关日报也加上
-//                        List<Map<String, Object>> viewUserNames = reportMapper.getCcReportNameByDate(date, user.getCompanyId(),leaderId);
-//                        for (Map<String, Object> viewItem : viewUserNames) {
-//                            boolean exists = false;
-//                            for (Map<String, Object> u : puserNames) {
-//                                if (u.get("id").equals(viewItem.get("id"))) {
-//                                    exists = true;
-//                                    break;
-//                                }
-//                            }
-//                            if (!exists) {
-//                                puserNames.add(viewItem);
-//                            }
-//                        }
-//
-//                        List<Map<String, Object>> viewReportList = reportMapper.getCcReportByDate(date, leaderId, null);
-//                        for (Map<String, Object> viewItem : viewReportList) {
-//                            boolean exists = false;
-//                            for (Map<String, Object> u : inchargeReportList) {
-//                                if (u.get("id").equals(viewItem.get("id"))) {
-//                                    exists = true;
-//                                    break;
-//                                }
-//                            }
-//                            if (!exists) {
-//                                inchargeReportList.add(viewItem);
-//                            }
-//                        }
-//                    }
-//                    for (Map<String, Object> map2 : puserNames) {
-//
-//                        if (nameList.size() > 0) {
-//                            String myUserId = (String)nameList.get(0).get("id");
-//                            if (myUserId.equals(map2.get("id"))) {
-//                                //自己的报告,之前已经添加过了,排重
-//                                continue;
-//                            }
-//                        }
-//                        List<Object> collect = nameList.stream().map(nl -> nl.get("")).collect(Collectors.toList());
-//                        if(!collect.contains(map2.get("id"))){
-//                            nameList.add(map2);
-//                        }
-//                        //再根据人分别获取当天的报告
-//                        List<Map<String, Object>> list2 =
-//                                inchargeReportList.stream().filter(i->i.get("creatorId").equals(map2.get("id"))).collect(Collectors.toList());
-//                        map2.put("data", list2);
-//
-//                        double reportTime = 0;
-//                        BigDecimal total = new BigDecimal(0);
-//                        int state = 1;
-//                        for (Map<String, Object> m : list2) {
-//                            double t = (double) m.get("time");
-//                            reportTime += t;
-//                            total = total.add((BigDecimal)m.get("cost"));
-//                            //取最低的状态
-//                            if (state == 2) {
-//                                continue;
-//                            }
-//                            if (state == 0) {
-//                                if ((int)m.get("state") == 2) {
-//                                    state = 2;
-//                                } else {
-//                                    continue;
-//                                }
-//                            } else {
-//                                state = (int)m.get("state");
-//                            }
-//                        }
-//                        DecimalFormat df = new DecimalFormat("0.00");
-//                        map2.put("reportTime", df.format(reportTime));
-//                        map2.put("cost", total);
-//                        map2.put("state", state);
-//                    }
-//                }
+                //需要看可见部门(部门主要负责人和其他负责人以及查看本部门工时权限)所有人员的日报
+                nameList = reportMapper.getReportNameByDateAndDept(date,
+                        allVisibleDeptIdList.size() > 0?allVisibleDeptIdList:null, targetUid, companyId, (deptId == null?viewUserId:null), pageStart, pageSize);
+                totalMembCount = reportMapper.getReportNameByDateAndDeptCount(date, allVisibleDeptIdList.size() > 0?allVisibleDeptIdList:null, targetUid, companyId, (deptId == null?viewUserId:null));
             } else {
-                Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
+                //查看全公司的数据
                 List<Integer> ids = null;
                 if (deptId != null) {
                     if (deptId == 0) {
@@ -462,51 +292,51 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
                 nameList = reportMapper.getReportNameByDateAndDept(date,
                         ids, targetUid, companyId, null, pageStart, pageSize);
                 totalMembCount = reportMapper.getReportNameByDateAndDeptCount(date, ids, targetUid, companyId, null);
-                if (nameList.size() > 0) {
-                    List<String> userIds = new ArrayList<>();
-                    nameList.forEach(n->{
-                        String id = (String) n.get("id");
-                        userIds.add(id);
-                    });
-                    List<Map<String, Object>> reportList = reportMapper.getUserReportByDateOrId(date, userIds, null);
-                    for (Map<String, Object> map : nameList) {
-                        //再根据人分别获取当天的报告
-                        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
-                        BigDecimal total = new BigDecimal(0);
-                        for (Map<String, Object> report : reportList) {
-                            if (((String)report.get("creatorId")).equals((String)map.get("id"))) {
-                                list.add(report);
-                                total = total.add((BigDecimal) report.get("cost"));
-                            }
-
+            }
+            if (nameList.size() > 0) {
+                List<String> userIds = new ArrayList<>();
+                nameList.forEach(n->{
+                    String id = (String) n.get("id");
+                    userIds.add(id);
+                });
+                List<Map<String, Object>> reportList = reportMapper.getUserReportByDateOrId(date, userIds, null);
+                for (Map<String, Object> memb : nameList) {
+                    //再根据人分别获取当天的报告
+                    List<Map<String, Object>> rList = new ArrayList<Map<String, Object>>();
+                    BigDecimal total = new BigDecimal(0);
+                    for (Map<String, Object> report : reportList) {
+                        if (((String)report.get("creatorId")).equals((String)memb.get("id"))) {
+                            rList.add(report);
+                            total = total.add((BigDecimal) report.get("cost"));
                         }
-                        map.put("data", list);
-                        map.put("cost", total);
-                        double reportTime = 0;
-                        if (list.size() > 0) {
-                            int state = 1;
-                            for (Map<String, Object> m : list) {
-                                double t = (double) m.get("time");
-                                reportTime += t;
-                                //取最低的状态
-                                if (state == 2) {
-                                    continue;
-                                }
-                                if (state == 0) {
-                                    if ((int)m.get("state") == 2) {
-                                        state = 2;
-                                    } else {
-                                        continue;
-                                    }
+                    }
+                    memb.put("data", rList);
+                    memb.put("cost", total);
+                    double reportTime = 0;
+                    if (rList.size() > 0) {
+                        int state = 1;
+                        for (Map<String, Object> m : rList) {
+                            double t = (double) m.get("time");
+                            reportTime += t;
+
+                            //取最低的状态
+                            if (state == 2) {
+                                continue;
+                            }
+                            if (state == 0) {
+                                if ((int)m.get("state") == 2) {
+                                    state = 2;
                                 } else {
-                                    state = (int)m.get("state");
+                                    continue;
                                 }
+                            } else {
+                                state = (int)m.get("state");
                             }
-                            map.put("state", state);
                         }
-                        DecimalFormat df = new DecimalFormat("0.00");
-                        map.put("reportTime", df.format(reportTime));
+                        memb.put("state", state);
                     }
+                    DecimalFormat df = new DecimalFormat("0.00");
+                    memb.put("reportTime", df.format(reportTime));
                 }
             }
 

+ 58 - 37
fhKeeper/formulahousekeeper/management-platform/src/main/resources/mapper/ReportMapper.xml

@@ -677,30 +677,33 @@
             AND a.create_date=#{date}
         </if>
         <choose>
-            <when test="deptIds != null and leaderId == null">
-                AND b.department_id in
-                <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
-                    #{item, jdbcType=INTEGER}
-                </foreach>
+            <when test="userId != null">
+                AND b.id=#{userId}
             </when>
-            <!--项目负责人,日报审核人,抄送人 -->
-           <when test="deptIds != null and leaderId != null">
-               AND (b.department_id in
-               <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
-                   #{item, jdbcType=INTEGER}
-               </foreach>
-                    OR (a.project_auditor_id = #{leaderId} or project.incharger_id = #{leaderId}) or project_ccuser.`user_id` = #{leaderId})
-           </when>
-          <when test="deptIds == null and leaderId != null">
-              and (a.project_auditor_id = #{leaderId} or project.incharger_id = #{leaderId} or project_ccuser.`user_id` = #{leaderId})
-          </when>
+            <otherwise>
+                <!--自己,担任项目经理,日报审核人,抄送人,或者自己部门的人填的日报 -->
+                <if test="viewUserId != null">
+                    and (a.creator_id = #{viewUserId} or a.project_auditor_id = #{viewUserId} or project.incharger_id = #{viewUserId}
+                    or project_ccuser.`user_id` = #{viewUserId}
+                    <if test="deptIds != null">
+                        or b.department_id in
+                        <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
+                            #{item, jdbcType=INTEGER}
+                        </foreach>
+                    </if>
+                    )
+                </if>
+                <if test="viewUserId == null">
+                    <if test="deptIds != null">
+                        and b.department_id in
+                        <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
+                            #{item, jdbcType=INTEGER}
+                        </foreach>
+                    </if>
+                </if>
+            </otherwise>
         </choose>
-        <if test="companyId != null">
-            AND b.company_id = #{companyId}
-        </if>
-        <if test="userId != null">
-            AND b.id=#{userId}
-        </if>
+        AND b.company_id = #{companyId}
         <if test="pageStart != null">
             limit #{pageStart}, #{pageSize}
         </if>
@@ -710,22 +713,40 @@
         SELECT count(DISTINCT b.id) as count
         FROM report AS a
         JOIN user AS b ON a.creator_id=b.id
+        left join project on project.id = a.project_id
+        LEFT JOIN project_ccuser ON a.project_id = project_ccuser.project_id
         WHERE 1=1
         <if test="date != null and date != ''">
             AND a.create_date=#{date}
         </if>
-        <if test="deptIds != null">
-            AND b.department_id in
-            <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
-                #{item, jdbcType=INTEGER}
-            </foreach>
-        </if>
-        <if test="companyId != null">
-            AND b.company_id = #{companyId}
-        </if>
-        <if test="userId != null">
-            AND b.id=#{userId}
-        </if>
+        <choose>
+            <when test="userId != null">
+                AND b.id=#{userId}
+            </when>
+            <otherwise>
+                <!--自己,担任项目经理,日报审核人,抄送人,或者自己部门的人填的日报 -->
+                <if test="viewUserId != null">
+                    and (a.creator_id = #{viewUserId} or a.project_auditor_id = #{viewUserId} or project.incharger_id = #{viewUserId}
+                    or project_ccuser.`user_id` = #{viewUserId}
+                    <if test="deptIds != null">
+                        or b.department_id in
+                        <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
+                            #{item, jdbcType=INTEGER}
+                        </foreach>
+                    </if>
+                    )
+                </if>
+                <if test="viewUserId == null">
+                    <if test="deptIds != null">
+                        and b.department_id in
+                        <foreach item="item" collection="deptIds" separator="," open="(" close=")" index="">
+                            #{item, jdbcType=INTEGER}
+                        </foreach>
+                    </if>
+                </if>
+            </otherwise>
+        </choose>
+        AND b.company_id = #{companyId}
     </select>
 
     <!--专业待审核的报告列表-->
@@ -1205,7 +1226,7 @@
         WHERE b.state = 1
         AND b.company_id =#{companyId}
         <if test="startDate!=null and endDate!=null">
-        AND b.create_date BETWEEN #{startDate} AND #{endDate}
+            AND b.create_date BETWEEN #{startDate} AND #{endDate}
         </if>
         GROUP BY b.project_id,b.`create_date`
         HAVING IFNULL(SUM(b.custom_data),0) > 0
@@ -1215,8 +1236,8 @@
         SELECT p.id as projectId, SUM(IFNULL(r.working_time, 0)) AS total_working_time
         FROM report r LEFT JOIN project p ON r.project_id = p.id
         WHERE p.company_id = #{companyId}
-              AND p.status in(1,2)
-              AND (p.finish_date IS NULL OR p.finish_date >= '2023-11-08')
+        AND p.status in(1,2)
+        AND (p.finish_date IS NULL OR p.finish_date >= '2023-11-08')
         <if test="projectId!=null">
             and p.id=#{projectId}
         </if>