| | |
| | | */ |
| | | 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.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.ProjectTask; |
| | | import me.zhengjie.modules.project.domain.ProjectType; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | import me.zhengjie.modules.project.service.dto.ProjectDto; |
| | | import me.zhengjie.modules.project.service.dto.ProjectTaskDto; |
| | | import me.zhengjie.modules.system.domain.Job; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | 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.ProjectProgressService; |
| | | import me.zhengjie.modules.project.service.dto.ProjectProgressDto; |
| | | import me.zhengjie.modules.project.service.dto.ProjectProgressQueryCriteria; |
| | | import me.zhengjie.modules.project.service.mapstruct.ProjectProgressMapper; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | 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.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 java.util.List; |
| | | import java.util.Map; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.function.Function; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | private final ProjectRepository projectRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | |
| | | private final TaskRepository taskRepository; |
| | | private final TaskDaliyRepository taskDaliyRepository; |
| | | private final ProjectCostService projectCostService; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(ProjectProgressQueryCriteria criteria, Pageable pageable){ |
| | | Page<ProjectProgress> page = projectProgressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | Page<ProjectProgress> page = projectProgressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder), pageable); |
| | | |
| | | WeekFields wfs = WeekFields.of(DayOfWeek.MONDAY, 4); |
| | | |
| | | // 将实体类转换为DTO(重写) |
| | | Function<ProjectProgress, ProjectProgressDto> func = new Function<ProjectProgress, ProjectProgressDto>() { |
| | | @Override |
| | | public ProjectProgressDto apply(ProjectProgress projectProgress) { |
| | | // 将实体类转换成DTO |
| | | ProjectProgressDto projectProgressDto =projectProgressMapper.toDto(projectProgress); |
| | | ProjectProgressDto projectProgressDto = projectProgressMapper.toDto(projectProgress); |
| | | // 根据项目ID查询项目信息 |
| | | Project project = projectRepository.findById(projectProgressDto.getProjectId()).orElseGet(Project::new); |
| | | ValidationUtil.isNull(projectProgress.getId(),"Project","id",projectProgressDto.getProjectId()); |
| | | // 项目名称 |
| | | projectProgressDto.setProjectName(project.getName()); |
| | | LocalDate localDate = LocalDate.now() |
| | | .with(wfs.weekBasedYear(), projectProgress.getBelongYear()) |
| | | .with(wfs.weekOfWeekBasedYear(), projectProgress.getBelongWeek()) |
| | | .with(wfs.dayOfWeek(), 1L) |
| | | .minusDays(7); |
| | | |
| | | Optional<ProjectProgress> projectProgressOpl = projectProgressRepository.findByBelongYearAndBelongWeekAndProjectId(localDate.get(wfs.weekBasedYear()), localDate.get(wfs.weekOfWeekBasedYear()), projectProgressDto.getProject().getId()); |
| | | projectProgressDto.setLastProjectProcess(projectProgressOpl.orElseGet(ProjectProgress::new)); |
| | | if (!projectProgressOpl.isPresent()) { |
| | | projectProgressDto.setTimeLastCost(projectProgressDto.getTimeTotalCost()); |
| | | projectProgressDto.setWorkLastCost(projectProgressDto.getWorkTotalCost()); |
| | | projectProgressDto.setLastedProgress(0.0); |
| | | } else { |
| | | if (projectProgressDto.getTimeTotalCost() != null) { |
| | | if (projectProgressOpl.get().getTimeTotalCost() != null) { |
| | | projectProgressDto.setTimeLastCost(projectProgressDto.getTimeTotalCost() - projectProgressOpl.get().getTimeTotalCost()); |
| | | } else { |
| | | projectProgressDto.setTimeLastCost(projectProgressDto.getTimeTotalCost()); |
| | | } |
| | | } |
| | | if (projectProgressDto.getWorkTotalCost() != null) { |
| | | if (projectProgressOpl.get().getWorkTotalCost() != null) { |
| | | projectProgressDto.setWorkLastCost(projectProgressDto.getWorkTotalCost() - projectProgressOpl.get().getWorkTotalCost()); |
| | | } else { |
| | | projectProgressDto.setWorkLastCost(projectProgressDto.getWorkTotalCost()); |
| | | } |
| | | } |
| | | projectProgressDto.setLastedProgress(projectProgressOpl.get().getEstimatedProgress() != null ? projectProgressOpl.get().getEstimatedProgress() : 0.0); |
| | | } |
| | | if (projectProgressDto.getProjectProgress() == null) { |
| | | projectProgressDto.setProjectProgress(0.0); |
| | | } |
| | | if (projectProgressDto.getEstimatedProgress() == null) { |
| | | projectProgressDto.setEstimatedProgress(0.0); |
| | | } |
| | | return projectProgressDto; |
| | | } |
| | | }; |
| | | return PageUtil.toPage(page.map(func)); |
| | | } |
| | | |
| | | @Override |
| | | public void generateProjectProcess(Integer year, Integer weeks, Long[] ids) { |
| | | |
| | | List<Project> projectList = null; |
| | | if (ids == null || ids.length == 0) { |
| | | projectList = projectRepository.findByProjectStatus(Constant.PROJECT_STATUS_UNDER_WAY); |
| | | } else { |
| | | projectList = projectRepository.findByProjectStatusAndIdIn(Constant.PROJECT_STATUS_UNDER_WAY, ids); |
| | | } |
| | | |
| | | WeekFields wfs = WeekFields.of(DayOfWeek.MONDAY, 4); |
| | | |
| | | // 根据项目ID查询项目信息 |
| | | LocalDate localDate = LocalDate.now() |
| | | .with(wfs.weekBasedYear(), year) |
| | | .with(wfs.weekOfWeekBasedYear(), weeks) |
| | | .with(wfs.dayOfWeek(), 1L) |
| | | .minusDays(7); |
| | | |
| | | 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(Timestamp.valueOf(localDate.atStartOfDay())); |
| | | |
| | | 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()); |
| | | |
| | | List<TaskDaliy> taskDaliyList = taskDaliyRepository.findByProjectId(project.getId()); |
| | | Map<Long, List<TaskDaliy>> collectMap = taskDaliyList.stream().collect(Collectors.groupingBy(TaskDaliy::getUserId)); |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | BigDecimal totalCost = BigDecimal.valueOf(0.0); |
| | | for (Entry<Long, List<TaskDaliy>> entry : collectMap.entrySet()) { |
| | | List<TaskDaliy> taskDaliyListSub = entry.getValue(); |
| | | Map<String, Double> costHourMap = new HashMap<String, Double>(); |
| | | taskDaliyListSub.forEach(taskDaliy -> { |
| | | costHourMap.put(sdf.format(taskDaliy.getFillDate()), taskDaliy.getConsumedHours()); |
| | | }); |
| | | String startDate = sdf.format(taskDaliyListSub.get(0).getFillDate()); |
| | | String endDate = sdf.format(taskDaliyListSub.get(taskDaliyListSub.size() - 1).getFillDate()); |
| | | try { |
| | | totalCost = totalCost.add(projectCostService.calcUserCostByDate(entry.getKey(), startDate + " 00:00:00", endDate + " 00:00:00", costHourMap)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | for (Task task : taskList) { |
| | | timeTotalInit = timeTotalInit + task.getPlanHours(); |
| | | workTotalInit = workTotalInit + (task.getPlanWorkload() == null ? 0.0 : task.getPlanWorkload()); |
| | | TaskDailyInterface taskDailyInterface = taskDaliyRepository.collectTaskDaliyUntilDate(task.getId(), projectProgress.getBelongDate()); |
| | | if (Constant.TASK_STATUS_COMPLETED.equals(task.getTaskStatus())) { |
| | | timeTotalCost += BigDecimal.valueOf(task.getPlanHours()).multiply(BigDecimal.valueOf(taskDailyInterface.getTaskProgress())).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_CEILING).doubleValue(); |
| | | } else { |
| | | timeTotalCost += task.getPlanHours(); |
| | | } |
| | | workTotalCost += taskDailyInterface.getConsumedWorkload(); |
| | | realLastCost += taskDailyInterface.getConsumedHours(); |
| | | } |
| | | // 真实时长 |
| | | projectProgress.setRealLastCost(realLastCost); |
| | | // 总消化工时 |
| | | projectProgress.setTimeTotalCost(timeTotalCost); |
| | | // 总工时 |
| | | projectProgress.setTimeTotalInit(timeTotalInit); |
| | | // 总消耗工作量 |
| | | projectProgress.setWorkTotalCost(workTotalCost); |
| | | // 总工作量 |
| | | projectProgress.setWorkTotalInit(workTotalInit); |
| | | // 计划总成本 |
| | | projectProgress.setMoneyTotalInit(project.getPerformancePrice().multiply(BigDecimal.valueOf(timeTotalInit))); |
| | | // 总消耗成本 |
| | | projectProgress.setMoneyTotalCost(totalCost); |
| | | |
| | | // 存在则更新,不存在则插入 |
| | | projectProgressRepository.save(projectProgress); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public ProjectProgressDto create(ProjectProgress resources) { |
| | | checkProjectProgress(resources); |
| | | |
| | | WeekFields wfs = WeekFields.of(DayOfWeek.MONDAY, 4); |
| | | if (resources.getBelongDate() == null) { |
| | | LocalDate localDate = LocalDate.now() |
| | | .with(wfs.weekBasedYear(), resources.getBelongYear()) |
| | | .with(wfs.weekOfWeekBasedYear(), resources.getBelongWeek()) |
| | | .with(wfs.dayOfWeek(), 1L); |
| | | resources.setBelongDate(Timestamp.valueOf(localDate.atStartOfDay())); |
| | | } |
| | | return projectProgressMapper.toDto(projectProgressRepository.save(resources)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(ProjectProgress resources) { |
| | | checkProjectProgress(resources); |
| | | ProjectProgress projectProgress = projectProgressRepository.findById(resources.getId()).orElseGet(ProjectProgress::new); |
| | | ValidationUtil.isNull( projectProgress.getId(),"ProjectProgress","id",resources.getId()); |
| | | projectProgress.copy(resources); |
| | |
| | | public void download(List<ProjectProgressDto> all, HttpServletResponse response) throws IOException { |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (ProjectProgressDto projectProgress : all) { |
| | | String projectName = projectProgress.getProjectId() != null ? projectRepository.findById(projectProgress.getProjectId()).orElseGet(Project::new).getName() : ""; |
| | | String settlemtnStatus= projectProgress.getSettlementStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.SETTLEMENT_STATUS, projectProgress.getSettlementStatus()).getLabel() : ""; |
| | | String projectName = projectProgress.getProject().getId() != null ? projectRepository.findById(projectProgress.getProject().getId()).orElseGet(Project::new).getName() : ""; |
| | | String approveStatus= projectProgress.getApproveStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.APPROVAL_STATUS, projectProgress.getApproveStatus()).getLabel() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("项目", projectName); |
| | | map.put("项目进度", projectProgress.getProjectProgress()); |
| | | map.put("结算状态", settlemtnStatus); |
| | | map.put("批准状态", approveStatus); |
| | | |
| | | map.put("备注", projectProgress.getRemarks()); |
| | | map.put("创建者", projectProgress.getCreateBy()); |
| | | map.put("创建时间", projectProgress.getCreateTime()); |
| | | map.put("更新者", projectProgress.getUpdateBy()); |
| | | map.put("更新时间", projectProgress.getUpdateTime()); |
| | | |
| | | map.put("总绩效工时", projectProgress.getTimeTotalInit()); |
| | | map.put("总消耗绩效工时", projectProgress.getTimeTotalCost()); |
| | | map.put("本周消化绩效工时", projectProgress.getTimeLastCost()); |
| | | map.put("实际消化工时", projectProgress.getRealLastCost()); |
| | | |
| | | map.put("总工作量", projectProgress.getWorkTotalInit()); |
| | | map.put("总消耗工作量", projectProgress.getWorkTotalCost()); |
| | | map.put("本周消化工作量", projectProgress.getWorkLastCost()); |
| | | list.add(map); |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | public void checkProjectProgress(ProjectProgress resources){ |
| | | List<ProjectProgress> list = projectProgressRepository.findByProjectId(resources.getProjectId()); |
| | | if(list.size()>0){ |
| | | if(resources.getProjectProgress()<=list.get(0).getProjectProgress()){ |
| | | List<ProjectProgress> list = projectProgressRepository.findByProjectId(resources.getProject().getId()); |
| | | if (list.size() > 0 && list.get(0).getProjectProgress() != null) { |
| | | if (resources.getProjectProgress() <= list.get(0).getProjectProgress()) { |
| | | throw new BadRequestException("项目进度必须大于上一次的项目进度"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<Integer> queryYearSelection() { |
| | | return projectProgressRepository.findAllBelongYear(); |
| | | } |
| | | |
| | | @Override |
| | | public List<Integer> queryWeeksSelection(Integer year) { |
| | | return projectProgressRepository.findAllBelongWeekByBelongYear(year); |
| | | } |
| | | } |