赵士淞
2023-01-13 ce7d3cebdf50f2ed89b98c70dce646b7682403f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package me.zhengjie.modules.quartz.task;
 
import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.List;
 
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.project.domain.Project;
import me.zhengjie.modules.project.domain.ProjectProgress;
import me.zhengjie.modules.project.repository.ProjectProgressRepository;
import me.zhengjie.modules.project.repository.ProjectRepository;
import me.zhengjie.modules.task.domain.Task;
import me.zhengjie.modules.task.domain.TaskDaliyCollectInterface;
import me.zhengjie.modules.task.repository.TaskDaliyRepository;
import me.zhengjie.modules.task.repository.TaskRepository;
 
/**
 * 钉钉定时任务(取考勤记录)
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Async
public class WeekReportMethod {
 
    private final ProjectRepository projectRepository;
 
    private final ProjectProgressRepository projectProgressRepository;
 
    private final TaskRepository taskRepository;
 
    private final TaskDaliyRepository taskDaliyRepository;
 
    /**
     * 生成周报
     * @throws Exception
     */
    public void createWeekReport() throws Exception {
//        String date = null;
//        LocalDate localDate = null;
//        if (date == null) {
//            localDate = LocalDate.now().with(wfs.dayOfWeek(), 1L);
//        } else {
//            localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        }
 
        WeekFields wfs = WeekFields.of(DayOfWeek.MONDAY, 4);
        LocalDate localDate = LocalDate.now().with(wfs.dayOfWeek(), 1L);
 
        int year = localDate.minusDays(7).get(wfs.weekBasedYear());
        int weeks = localDate.minusDays(7).get(wfs.weekOfWeekBasedYear());
 
        // 周初查询进行中的任务 TODO
//        List<Project> projectList = projectRepository.findByProjectStatus(Constant.PROJECT_STATUS_UNDER_WAY);
        List<Project> projectList = projectRepository.findAll();
 
        projectList.forEach(project -> {
            // 存在则更新,不存在则插入
            ProjectProgress projectProgress = projectProgressRepository.findByBelongYearAndBelongWeekAndProjectId(year, weeks, project.getId()).orElseGet(ProjectProgress::new);
            if (projectProgress.getId() == null) {
                projectProgress.setBelongYear(year);
                projectProgress.setBelongWeek(weeks);
                projectProgress.setBelongDate(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
 
                projectProgress.setProject(project);
                projectProgress.setApproveStatus(Constant.APPROVE_STATUS_UNRECOGNIZED);
            }
 
            // 总工时
            Double timeTotalInit = 0.0;
            // 总消化工时
            Double timeTotalCost = 0.0;
            // 总工作量
            Double workTotalInit = 0.0;
            // 总消耗工作量
            Double workTotalCost = 0.0;
            // 真实时长
            Double realLastCost = 0.0;
            List<Task> taskList = taskRepository.findByProjectIdUntilDate(project.getId(), projectProgress.getBelongDate());
            for (Task task : taskList) {
                timeTotalInit = timeTotalInit + task.getPlanHours();
                workTotalInit = workTotalInit + (task.getPlanWorkload() == null ? 0.0 : task.getPlanWorkload());
                TaskDaliyCollectInterface taskDaliyCollection = taskDaliyRepository.collectTaskDaliyUntilDate(task.getId(), projectProgress.getBelongDate());
                boolean flg = true;
                if (flg) { // TODO 任务状态:未结束  task.getStatus()
                    timeTotalCost += BigDecimal.valueOf(task.getPlanHours()).multiply(BigDecimal.valueOf(taskDaliyCollection.getTaskProgress())).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_CEILING).doubleValue();
                } else {
                    timeTotalCost += task.getPlanHours();
                }
                workTotalCost += taskDaliyCollection.getConsumedWorkload();
                realLastCost += taskDaliyCollection.getConsumedHours();
            }
            // 真实时长
            projectProgress.setRealLastCost(realLastCost);
            // 总消化工时
            projectProgress.setTimeTotalCost(timeTotalCost);
            // 总工时
            projectProgress.setTimeTotalInit(timeTotalInit);
            // 总消耗工作量
            projectProgress.setWorkTotalCost(workTotalCost);
            // 总工作量
            projectProgress.setWorkTotalInit(workTotalInit);
 
            // 存在则更新,不存在则插入
            projectProgressRepository.save(projectProgress);
        });
    }
}