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);
|
});
|
}
|
}
|