| | |
| | | */ |
| | | package me.zhengjie.modules.project.service.impl; |
| | | |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.DayOfWeek; |
| | | import java.time.LocalDate; |
| | | import java.time.temporal.WeekFields; |
| | | import java.util.ArrayList; |
| | | import java.util.Comparator; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.Map.Entry; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.exception.BadRequestException; |
| | | 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.domain.ProjectProgressDetail;
|
| | | import me.zhengjie.modules.project.repository.ProjectProgressRepository; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | import me.zhengjie.modules.project.service.ProjectCostService; |
| | |
| | | import me.zhengjie.modules.project.service.dto.ProjectProgressQueryCriteria; |
| | | import me.zhengjie.modules.project.service.mapstruct.ProjectProgressMapper; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.task.domain.Task; |
| | | import me.zhengjie.modules.task.domain.TaskDailyInterface; |
| | | import me.zhengjie.modules.task.domain.TaskDaliy; |
| | | import me.zhengjie.modules.task.domain.TaskInterface; |
| | | import me.zhengjie.modules.task.domain.TaskMongo;
|
| | | import me.zhengjie.modules.task.domain.TaskUserMongo;
|
| | | import me.zhengjie.modules.task.repository.TaskDaliyRepository; |
| | | import me.zhengjie.modules.task.repository.TaskRepository; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import org.springframework.data.domain.Page;
|
| | | import org.springframework.data.domain.Pageable;
|
| | | import org.springframework.data.mongodb.core.MongoTemplate;
|
| | | import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
| | | import org.springframework.data.mongodb.core.aggregation.AggregationResults;
|
| | | import org.springframework.data.mongodb.core.query.Criteria;
|
| | | import org.springframework.data.mongodb.core.query.Query;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import java.io.IOException;
|
| | | import java.math.BigDecimal;
|
| | | import java.math.RoundingMode;
|
| | | import java.sql.Timestamp;
|
| | | import java.time.DayOfWeek;
|
| | | import java.time.LocalDate;
|
| | | import java.time.temporal.WeekFields;
|
| | | import java.util.*;
|
| | | import java.util.function.Function;
|
| | | import java.util.stream.Collectors;
|
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | private final TaskRepository taskRepository; |
| | | private final TaskDaliyRepository taskDaliyRepository; |
| | | private final ProjectCostService projectCostService; |
| | | private final MongoTemplate mongoTemplate;
|
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(ProjectProgressQueryCriteria criteria, Pageable pageable){ |
| | |
| | | |
| | | private ProjectProgressDto generalProjectProgress(ProjectProgress resources) { |
| | | |
| | | // 总消化工时 |
| | | // Double timeTotalCost = 0.0; |
| | | // 总消耗工作量 |
| | | Double workTotalCost = 0.0; |
| | | // 真实时长 |
| | | Double realLastCost = 0.0; |
| | | List<TaskDailyInterface> taskDaliyWithTaskList = taskDaliyRepository.collectByProjectIdAndFillDateLessThanGroupByTaskIdJoinTask(resources.getProject().getId(), resources.getBelongDate()); // TODO error |
| | | Query query = new Query(Criteria.where("projectId").is(resources.getProject().getId()));
|
| | | List<TaskMongo> taskMongoList = mongoTemplate.find(query, TaskMongo.class);
|
| | | |
| | | for (TaskDailyInterface taskDaily : taskDaliyWithTaskList) { |
| | | // 总消化工时 |
| | | // timeTotalCost += BigDecimal.valueOf(taskDaily.getPlanHours()).multiply(BigDecimal.valueOf(taskDaily.getTaskProgress())).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_CEILING).doubleValue(); |
| | | // 总消耗工作量 |
| | | workTotalCost += taskDaily.getConsumedWorkload(); |
| | | // 真实时长 |
| | | realLastCost += taskDaily.getConsumedHours(); |
| | | } |
| | | // 总消化工时 |
| | | // resources.setTimeTotalCost(timeTotalCost); |
| | | // 总消耗工作量 |
| | | resources.setWorkTotalCost(workTotalCost); |
| | | // 真实时长 |
| | | resources.setRealLastCost(realLastCost); |
| | | Aggregation customerAgg = Aggregation.newAggregation(
|
| | | Aggregation.match(Criteria.where("task").in(taskMongoList)),
|
| | | Aggregation.project("userId", "taskHours", "costHours"),
|
| | | Aggregation.group("userId").sum("taskHours").as("taskHours").sum("costHours").as("costHours")
|
| | | );
|
| | | |
| | | if (resources.getId() == null) { |
| | | // 根据项目ID取得任务信息 |
| | | TaskInterface taskTotal = taskRepository.collectByProjectId(resources.getProject().getId()); |
| | | // 总工时 |
| | | resources.setTimeTotalInit(taskTotal.getPlanHours()); |
| | | // 总工作量 |
| | | resources.setWorkTotalInit(taskTotal.getPlanWorkload()); |
| | | AggregationResults<Map> aggregate = mongoTemplate.aggregate(customerAgg, TaskUserMongo.class, Map.class);
|
| | | List<ProjectProgressDetail> projectProgressDetailList = aggregate.getMappedResults().stream().filter(data -> data.get("_id") != null).map(data -> {
|
| | | ProjectProgressDetail projectProgressDetail = new ProjectProgressDetail();
|
| | | projectProgressDetail.setUserId(Long.parseLong(String.valueOf(data.get("_id"))));
|
| | | BigDecimal taskHours = new BigDecimal(String.valueOf(data.get("taskHours")));
|
| | | projectProgressDetail.setTimeInitCost(taskHours.setScale(2, RoundingMode.HALF_UP).doubleValue());
|
| | | BigDecimal costHours = new BigDecimal(String.valueOf(data.get("costHours")));
|
| | | projectProgressDetail.setTimeTotalCost(costHours.setScale(2, RoundingMode.HALF_UP).doubleValue());
|
| | |
|
| | | BigDecimal moneyTotalCost = projectCostService.calcUserCostByHours(projectProgressDetail.getUserId(), projectProgressDetail.getTimeTotalCost());
|
| | | projectProgressDetail.setMoneyTotalCost(moneyTotalCost);
|
| | |
|
| | | return projectProgressDetail;
|
| | | }).collect(Collectors.toList());
|
| | |
|
| | | // 任务总时间
|
| | | resources.setTimeTotalInit(projectProgressDetailList.stream().mapToDouble(ProjectProgressDetail::getTimeInitCost).sum());
|
| | | // 消耗总时间
|
| | | resources.setTimeTotalCost(projectProgressDetailList.stream().mapToDouble(ProjectProgressDetail::getTimeTotalCost).sum());
|
| | |
|
| | | // 计划总成本 |
| | | resources.setMoneyTotalInit(resources.getProject().getPerformancePrice().multiply(BigDecimal.valueOf(taskTotal.getPlanHours())).divide(BigDecimal.valueOf(160))); |
| | | } |
| | | |
| | | // 根据日期和项目ID取得日报信息 |
| | | List<TaskDailyInterface> taskDaliyList = taskDaliyRepository.collectByProjectIdAndFillDateLessThanGroupByUserIdAndFillDate(resources.getProject().getId(), resources.getBelongDate()); |
| | | Map<Long, List<TaskDailyInterface>> collectMap = taskDaliyList.stream().collect(Collectors.groupingBy(TaskDailyInterface::getUserId)); |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | resources.setMoneyTotalInit(resources.getProject().getProjectPrice().multiply(BigDecimal.valueOf(10000)));
|
| | | // 已分配总成本
|
| | | resources.setMoneyTotalCost(BigDecimal.valueOf(resources.getTimeTotalCost()).multiply(BigDecimal.valueOf(11250)).divide(BigDecimal.valueOf(160), 2, RoundingMode.HALF_UP));
|
| | | // 总消耗成本 |
| | | BigDecimal totalCost = BigDecimal.valueOf(0.0); |
| | | for (Entry<Long, List<TaskDailyInterface>> entry : collectMap.entrySet()) { |
| | | List<TaskDailyInterface> taskDaliyListSub = entry.getValue(); |
| | | // 最小日期 |
| | | TaskDailyInterface taskDaliyFirst = taskDaliyListSub.stream().min(Comparator.comparing(taskDaliy -> taskDaliy.getFillDate())).get(); |
| | | // 最大日期 |
| | | TaskDailyInterface taskDaliyLast = taskDaliyListSub.stream().max(Comparator.comparing(taskDaliy -> taskDaliy.getFillDate())).get(); |
| | | Map<String, Double> costHourMap = new HashMap<String, Double>(); |
| | | taskDaliyListSub.forEach(taskDaliy -> { |
| | | costHourMap.put(sdf.format(taskDaliy.getFillDate()), taskDaliy.getConsumedHours()); |
| | | }); |
| | | // 根据员工成本单金算出当前工作的成本 |
| | | totalCost = totalCost.add(projectCostService.calcUserCostByDate(entry.getKey(), new Timestamp(taskDaliyFirst.getFillDate().getTime()), new Timestamp(taskDaliyLast.getFillDate().getTime()), costHourMap)); |
| | | } |
| | | resources.setMoneyRealCost(projectProgressDetailList.stream().map(ProjectProgressDetail::getMoneyTotalCost).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_UP));
|
| | | |
| | | // 总消耗成本 |
| | | resources.setMoneyTotalCost(totalCost); |
| | | ProjectProgressDto projectProgressDto = projectProgressMapper.toDto(projectProgressRepository.save(resources));
|
| | | |
| | | return projectProgressMapper.toDto(projectProgressRepository.save(resources)); |
| | | return projectProgressDto;
|
| | | } |
| | | |
| | | @Override |