From 98435cefa29bddac143ea3c2c6762595ef31518f Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期三, 09 七月 2025 09:17:09 +0800
Subject: [PATCH] bug修改

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java |   99 +++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 79 insertions(+), 20 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java
index 72f8850..adf3fd3 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java
@@ -6,10 +6,12 @@
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.CodeGenerateUtils;
 import com.ruoyi.system.mapper.TCleanerMapper;
 import com.ruoyi.system.model.*;
 import com.ruoyi.system.service.*;
 import javafx.concurrent.Task;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.formula.functions.T;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -17,6 +19,7 @@
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -25,6 +28,7 @@
  * @author xiaochen
  * @date 2025/6/11 18:39
  */
+@Slf4j
 @Component
 public class TaskUtil {
 
@@ -53,9 +57,13 @@
     private ISysUserService sysUserService;
     @Autowired
     private TTaskCleanService taskCleanService;
+    @Autowired
+    private TTemplateCountService templateCountService;
 
 
-    @Scheduled(cron = "0 0 0 * * ?")
+    // 每一个小时执行一次
+//    @Scheduled(cron = "0 0 0 * * ?")
+    @Scheduled(fixedRate = 1500000000)
     public void dayOfCreateInspection() {
         try {
 
@@ -84,7 +92,7 @@
         // 通过模板id查询员工巡检员
         List<SysUser> sysUsers = sysUserService.selectUserByTempLateId(template.getId());
         // 创建任务
-        list.forEach(detail -> {
+        for (TTemplateDetail detail : list) {
             // 计算周期
             int cycle = detail.getCycle();
             switch (detail.getCycleType()){
@@ -111,18 +119,43 @@
             // 获取点位类型的占比
             String num4 = detail.getNum4();
 
+            // 未绑定员工
+            if(CollectionUtils.isEmpty(sysUsers)){
+                continue;
+            }
+
+
+
+            SysUser sysUser = sysUsers.get(0);
+            List<TProjectDept> projectDeptLists = new ArrayList<>();
+            if(sysUser.getDeptType() == 1){
+                TProjectDept projectDept = projectDeptService.getById(sysUser.getDeptId());
+                if("0".equals(projectDept.getParentId())){
+                    projectDeptLists = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
+                            .eq(TProjectDept::getParentId, projectDept.getId()));
+                }else {
+                    projectDeptLists.add(projectDept);
+                }
+            }else {
+                projectDeptLists = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
+                        .ne(TProjectDept::getParentId,0));
+            }
+            List<String> proDeptIds = projectDeptLists.stream().map(TProjectDept::getId).collect(Collectors.toList());
+
             // 获取项目部在该模板详情中的权重
             List<String> projectDeptIds = redisCache.getCacheList(detail.getId() + PROJECT_DEPT_WEIGHT);
             // 获取项目部列表
             List<TProjectDept> projectDeptList;
             if(CollectionUtils.isEmpty(projectDeptIds)){
-                projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class));
+                projectDeptList = projectDeptLists;
             }else {
                 projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
-                        .in(TProjectDept::getId, projectDeptIds));
+                        .ne(TProjectDept::getParentId,0)
+                        .in(TProjectDept::getId, proDeptIds)
+                        .notIn(TProjectDept::getId, projectDeptIds));
                 // 所过所有的项目部都被抽取了,则重新抽取,并且清空项目部权重
                 if(CollectionUtils.isEmpty(projectDeptList)){
-                    projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class));
+                    projectDeptList = projectDeptLists;
                     redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT);
                 }
             }
@@ -131,7 +164,8 @@
             if(projectDepts.size() < num2){
                 List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                 List<TProjectDept> projectDeptList1 = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
-                        .notIn(TProjectDept::getId, proIds));
+                        .notIn(TProjectDept::getId, proIds)
+                        .ne(TProjectDept::getParentId,0));
                 redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT);
                 List<TProjectDept> projectDepts1 = randomSelection(projectDeptList1, num2 - projectDepts.size());
                 List<String> proIds1 = projectDepts1.stream().map(TProjectDept::getId).collect(Collectors.toList());
@@ -146,17 +180,27 @@
             List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
 
             // 拿到抽取的项目部下的所有保洁员
-            List<TProjectDept> tProjectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
-                    .in(TProjectDept::getParentId, proIds));
+//            List<TProjectDept> tProjectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
+//                    .in(TProjectDept::getId, proIds));
             // 获取片区id
-            List<String> areaIds = tProjectDeptList.stream().map(TProjectDept::getId).collect(Collectors.toList());
+            List<String> areaIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
+            if(CollectionUtils.isEmpty(areaIds)){
+                return;
+            }
 
             // 计算每天需要抽取多少个保洁员
             long count = cleanerService.count(Wrappers.lambdaQuery(TCleaner.class)
                     .in(TCleaner::getProjectId, areaIds));
             int cleanerSums = num1 * Integer.parseInt(count + "");
             // 获取每天需要抽取的保洁员数,向上取整
+            if (cleanerSums < cycle){
+                cleanerSums = cycle;
+            }
             int dayCleanerCount = cleanerSums / cycle;
+
+            if((cycle == detail.getCurrentValue()) && cleanerSums % cycle != 0){
+                dayCleanerCount++;
+            }
 
             // 获取保洁员权重
             List<String> cleanerIds = redisCache.getCacheList(detail.getId() + CLEANER_WEIGHT);
@@ -178,6 +222,10 @@
 
             // 抽取保洁员
             List<TCleaner> tCleaners = randomSelection(cleaners, dayCleanerCount);
+            if(CollectionUtils.isEmpty(tCleaners)){
+                log.error("没有可抽取的保洁员");
+                continue;
+            }
             if(tCleaners.size() < dayCleanerCount){
                 List<String> cleanIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                 List<TCleaner> cleaners1 = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
@@ -186,7 +234,8 @@
                 redisCache.deleteObject(detail.getId() + CLEANER_WEIGHT);
                 List<TCleaner> tCleaners1 = randomSelection(cleaners1, dayCleanerCount - tCleaners.size());
                 List<String> cleanIds1 = tCleaners1.stream().map(TCleaner::getId).collect(Collectors.toList());
-                // 将已抽取的项目部id保存到redis中
+                // 将已抽取的保洁员id保存到redis中
+                cleanIds1.addAll(cleanIds);
                 redisCache.setCacheList(detail.getId() + CLEANER_WEIGHT, cleanIds1);
                 tCleaners.addAll(tCleaners1);
             }else {
@@ -199,8 +248,6 @@
             List<TLocation> locationList = locationService.list(Wrappers.lambdaQuery(TLocation.class)
                     .in(TLocation::getLocationCleaner, cleanersIds));
             // 查询点位类型
-//            List<TLocationType> locationTypes = locationTypeService.list(Wrappers.lambdaQuery(TLocationType.class)
-//                    .orderByDesc(TLocationType::getCreateTime));
             JSONArray jsonArray = JSONObject.parseArray(num4);
             List<TLocation> tLocationList = new ArrayList<>();
             for (Object o : jsonArray) {
@@ -237,6 +284,10 @@
                 detail.setCurrentValue(0);
                 // 将重复点位置空
                 redisCache.deleteObject(detail.getId() + REPEAT_LOCATION);
+                // 将项目部权重置空
+                redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT);
+                // 将保洁员权重置空
+                redisCache.deleteObject(detail.getId() + CLEANER_WEIGHT);
             }
 
             // 创建任务
@@ -249,24 +300,32 @@
                     task.setCleanerId(tCleaner.getId());
                 });
                 // 获取巡检员
-                if(!CollectionUtils.isEmpty(sysUsers)){
-                    SysUser sysUser = sysUsers.get(0);
-                    task.setPatrolInspector(sysUser.getUserId().toString());
-                    task.setPatrolInspectorDept(sysUser.getDeptId());
-                }
+                task.setPatrolInspector(sysUser.getUserId().toString());
+                task.setPatrolInspectorDept(sysUser.getDeptId());
+                task.setUserId(sysUser.getUserId());
                 task.setStatus(1);
                 task.setLocationId(tLocation.getId());
                 task.setImplementTime(LocalDateTime.now().plusDays(1));
                 task.setTaskType(1);
-                task.setTemplateId(detail.getId());
+                task.setTemplateId(detail.getTemplateId());
+                String nameAndCode = CodeGenerateUtils.generateVolumeSn();
+                task.setTaskName(nameAndCode);
+                task.setTaskCode(nameAndCode);
                 tasks.add(task);
             }
+
+            // 添加应生成任务数量
+            TTemplateCount templateCount = new TTemplateCount();
+            templateCount.setTemplateId(detail.getId());
+            templateCount.setTaskCount(tLocationList.size()* cycle);
+            templateCount.setUserId(sysUser.getUserId());
+            templateCountService.save(templateCount);
+
             taskCleanService.saveBatch(tasks);
             template.setTaskCount(tasks.size());
             templateService.updateById(template);
             templateDetailService.updateById(detail);
-        });
-
+        }
     }
 
     /**

--
Gitblit v1.7.1