From 1b978faa6df37feb92ea84f08fc63dbf0d120efb Mon Sep 17 00:00:00 2001
From: churf <123456>
Date: Thu, 07 Sep 2023 01:27:46 +0000
Subject: [PATCH] 首页日报查看进度逻辑修改

---
 eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/task/service/impl/TaskUserServiceImpl.java |   96 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 85 insertions(+), 11 deletions(-)

diff --git a/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/task/service/impl/TaskUserServiceImpl.java b/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/task/service/impl/TaskUserServiceImpl.java
index c672b96..e5ab8fa 100644
--- a/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/task/service/impl/TaskUserServiceImpl.java
+++ b/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/task/service/impl/TaskUserServiceImpl.java
@@ -17,6 +17,7 @@
 
 import java.io.IOException;
 import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
 import java.time.DayOfWeek;
 import java.time.Instant;
 import java.time.LocalDateTime;
@@ -24,6 +25,7 @@
 import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -35,7 +37,6 @@
 import org.bson.types.ObjectId;
 import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.cache.annotation.CacheEvict;
-import org.springframework.cache.annotation.Cacheable;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.aggregation.Aggregation;
@@ -119,7 +120,7 @@
         taskUserMongos.forEach(item -> {
             if (item.getTaskEndDate() != null && item.getTaskStartDate() != null) {
                 //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-                Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId());
+                Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId()).get(0);
                 TaskUserMongoDto dto = new TaskUserMongoDto();
                 if (item.getPerformanceType() == null) {
                     TaskMongo byId = mongoTemplate.findById(item.getTask().getId(), TaskMongo.class);
@@ -185,7 +186,7 @@
         List<TaskUserMongoDto> taskUserMongoDtoList = new ArrayList<>();
         taskUserMongoList.forEach(item -> {
             //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-            Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId());
+            Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId()).get(0);
             TaskUserMongoDto dto = new TaskUserMongoDto();
             if (item.getPerformanceType() == null) {
                 TaskMongo byId = mongoTemplate.findById(item.getTask().getId(), TaskMongo.class);
@@ -201,6 +202,8 @@
             } else {
                 item.setPerformanceHours(item.getTaskHours());
             }
+
+            mongoTemplate.save(item);
 
             dto.setId(item.getId());
             dto.setName(item.getName());
@@ -245,7 +248,7 @@
 
         taskUserMongoList.forEach(item -> {
             //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-            Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId());
+            Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId()).get(0);
             TaskUserMongoDto dto = new TaskUserMongoDto();
             if (item.getPerformanceType() == null) {
                 TaskMongo byId = mongoTemplate.findById(item.getTask().getId(), TaskMongo.class);
@@ -300,20 +303,19 @@
     }
 
     @Override
-    @Cacheable(key = "'allTaskUser:'+#p0")
-    public List<TaskUserMongoDto> getAllTaskUser(Long userId) {
+    public List<TaskUserMongoDto> getAllTaskUser(Long userId,String taskUserMongoId) {
         List<TaskUserMongo> taskUserMongoList = mongoTemplate.find(Query.query(new Criteria("userId").is(SecurityUtils.getCurrentUserId())), TaskUserMongo.class);
         List<TaskUserMongoDto> taskUserMongoDtoList = new ArrayList<>();
 
         taskUserMongoList.forEach(item -> {
             //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-            Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId());
+            Double coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(SecurityUtils.getCurrentUserId()).get(0);
             TaskUserMongoDto dto = new TaskUserMongoDto();
             if (item.getPerformanceType() == null) {
                 TaskMongo byId = mongoTemplate.findById(item.getTask().getId(), TaskMongo.class);
                 item.setPerformanceType(byId.getPerformanceType());
                 mongoTemplate.save(item);
-                getAllTaskUser(SecurityUtils.getCurrentUserId());
+                getAllTaskUser(SecurityUtils.getCurrentUserId(),taskUserMongoId);
             }
             if (item.getPerformanceType().equals("1")) {
                 //绩效任务
@@ -344,11 +346,17 @@
             daliyMongoList.forEach(daliy -> consumptionHours.updateAndGet(v -> v + daliy.getConsumedHours()));
             if (daliyMongoList.size() == 0) {
                 //如果没有日志,统一赋值为0
+                //List<TaskDaliyMongo> alreadyDaily = mongoTemplate.find(Query.query(new Criteria("taskUserMongo._id").is(taskUserMongoId)), TaskDaliyMongo.class);
+                // System.out.println(alreadyDaily.get(alreadyDaily.size() - 1));
                 dto.setMaxTaskProgress(0D);
                 dto.setAlreadyConsumedHours(consumptionHours.get());
                 taskUserMongoDtoList.add(dto);
             } else {
                 //如果有日志
+                //看看其他人写没写过日报
+                List<TaskDaliyMongo> alreadyDaily = mongoTemplate.find(Query.query(new Criteria("taskUserMongo._id").is(taskUserMongoId).and("userId").ne(SecurityUtils.getCurrentUserId())), TaskDaliyMongo.class);
+                //将其他人填写的工时合并过来
+                alreadyDaily.forEach(daily -> consumptionHours.updateAndGet(v -> v + daily.getConsumedHours()));
                 daliyMongoList.sort(((o1, o2) -> (int) (o2.getTaskProgress() - o1.getTaskProgress())));
                 dto.setMaxTaskProgress(daliyMongoList.get(0).getTaskProgress());
                 dto.setAlreadyConsumedHours(consumptionHours.get());
@@ -366,6 +374,63 @@
             mongoTemplateById.setTaskStatus("task_status_completed");
             mongoTemplate.save(mongoTemplateById);
         });
+    }
+
+    @Override
+    public List<TaskUserDetail> getTaskUserDetail(String taskUserMongoId) {
+        List<TaskUserDetail> returnList = new ArrayList<>();
+        List<String> colorList = new ArrayList<>();
+        Collections.addAll(colorList,"#0bbd87","#67C23A","#909399","#E6A23C","#F56C6C");
+        Criteria criteria = new Criteria();
+        criteria.and("taskUserMongo._id").is(taskUserMongoId);
+        Query query = new Query(criteria);
+        // 获取到这个分配任务下所有的日报信息
+        List<TaskDaliyMongo> daliyMongoList = mongoTemplate.find(query, TaskDaliyMongo.class);
+        // 按照人名进行分组
+        Map<Long, List<TaskDaliyMongo>> collect = daliyMongoList.stream().collect(Collectors.groupingBy(TaskDaliyMongo::getUserId));
+        Set<Map.Entry<Long, List<TaskDaliyMongo>>> entries = collect.entrySet();
+        // 分别染色
+        for (Map.Entry<Long, List<TaskDaliyMongo>> entry : entries) {
+            String color = colorList.get(new Random().nextInt(colorList.size()));
+            colorList.remove(color);
+            for (TaskDaliyMongo taskDaliyMongo : entry.getValue()) {
+                TaskUserDetail taskUserDetail = new TaskUserDetail();
+                if(taskDaliyMongo.getRemarks() != null) {
+                    taskUserDetail.setFlag(true);
+                    taskUserDetail.setRemarks(taskDaliyMongo.getRemarks());
+                }
+                taskUserDetail.setTaskUserName(userRepository.findById(taskDaliyMongo.getUserId()).get().getNickName());
+                taskUserDetail.setTitle(userRepository.findById(taskDaliyMongo.getUserId()).get().getNickName()+"提交了任务日报,消耗了"+taskDaliyMongo.getConsumedHours()+"个小时");
+                taskUserDetail.setFillDate(new SimpleDateFormat("yyyy-MM-dd").format(taskDaliyMongo.getFillDate()));
+                taskUserDetail.setColor(color);
+                taskUserDetail.setAlreadyConsumeHours(entry.getValue().stream().collect(Collectors.summingDouble(TaskDaliyMongo::getConsumedHours)));
+                returnList.add(taskUserDetail);
+            }
+        }
+        return returnList;
+    }
+
+    @Override
+    public List<TaskDetail> getTaskDetail(String taskMongoId) {
+        TaskMongo byId = mongoTemplate.findById(taskMongoId, TaskMongo.class);
+        List<TaskDetail> resultList = new ArrayList<>();
+        assert byId != null;
+        List<Long> containUserList = byId.getContainUserList();
+        for (Long userId : containUserList) {
+            TaskDetail taskDetail = new TaskDetail();
+            AtomicReference<Double> assignedWork = new AtomicReference<>(0D);
+            List<TaskUserMongo> taskUserMongoList = mongoTemplate.find(Query.query(new Criteria("userId").is(userId).and("task._id").is(taskMongoId).and("taskStatus").ne("task_status_completed")), TaskUserMongo.class);
+            taskDetail.setNickName(userRepository.findById(userId).get().getNickName());
+            taskUserMongoList.forEach(item -> {
+                if(item.getPerformanceHours() != null) {
+                    assignedWork.updateAndGet(v -> v + item.getPerformanceHours());
+                }
+            });
+            taskDetail.setAssignedWork(assignedWork.get());
+
+            resultList.add(taskDetail);
+        }
+        return resultList;
     }
 
     @Override
@@ -462,7 +527,7 @@
                 coefficientByUserId = 1D;
             } else {
                 //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-                coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(taskUserMongo.getUserId());
+                coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(taskUserMongo.getUserId()).get(0);
             }
 
             if (taskUserMongo.getPerformanceType() == null) {
@@ -476,9 +541,12 @@
                 DecimalFormat decimalFormat = new DecimalFormat("#0");
                 String format = decimalFormat.format(taskUserMongo.getTaskHours() / coefficientByUserId);
                 taskUserMongoDto.setPerformanceHours(Double.valueOf(format));
+                taskUserMongo.setPerformanceHours(Double.valueOf(format));
             } else {
                 taskUserMongoDto.setPerformanceHours(taskUserMongo.getTaskHours());
             }
+
+            mongoTemplate.save(taskUserMongo);
 
             taskUserMongoDtoList.add(taskUserMongoDto);
         }
@@ -533,7 +601,7 @@
                 coefficientByUserId = 1D;
             } else {
                 //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-                coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(taskUserMongo.getUserId());
+                coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(taskUserMongo.getUserId()).get(0);
             }
 
             if (taskUserMongo.getPerformanceType() == null) {
@@ -547,6 +615,8 @@
                 DecimalFormat decimalFormat = new DecimalFormat("#0");
                 String format = decimalFormat.format(taskUserMongo.getTaskHours() / coefficientByUserId);
                 taskUserMongoDto.setPerformanceHours(Double.valueOf(format));
+                taskUserMongo.setPerformanceHours(Double.valueOf(format));
+                mongoTemplate.save(taskUserMongo);
                 resultList.add(taskUserMongoDto);
             } else {
                 taskUserMongoDto.setPerformanceHours(taskUserMongo.getTaskHours());
@@ -687,7 +757,7 @@
             mongoTemplate.save(taskUserMongo);
         } else {
             //如果有指定的任务负责人就获取这个任务负责人的绩效绩点
-            coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(resources.getUserId());
+            coefficientByUserId = userLevelWageRepository.findPerformanceCoefficientByUserId(resources.getUserId()).get(0);
             if (taskUserMongo.getPerformanceType().equals("1")) {
                 //绩效任务
                 DecimalFormat decimalFormat = new DecimalFormat("#0");
@@ -720,6 +790,10 @@
     @Override
     public void deleteAll(String[] ids) { // TODO
         for (String id : ids) {
+            List<TaskDaliyMongo> daliyMongoList = mongoTemplate.find(Query.query(new Criteria("taskUserMongo._id").is(id)), TaskDaliyMongo.class);
+            if(daliyMongoList.size() > 0) {
+                throw new BadRequestException("当前分配任务已经有用户填写了日报!");
+            }
             mongoTemplate.remove(Query.query(Criteria.where("id").is(new ObjectId(id))), TaskUserMongo.class);
         }
 //    	mongoTemplate.remove(Query.query(Criteria.where("id").in(ids)), getClass());

--
Gitblit v1.9.3