| | |
| | | */ |
| | | 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.util.*; |
| | | import java.util.Map.Entry; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.contract.repository.ContractProjectRepository; |
| | | import me.zhengjie.modules.cost.repository.CostRepository; |
| | | import me.zhengjie.modules.project.repository.*; |
| | | import me.zhengjie.modules.project.service.ProjectCostService; |
| | | import me.zhengjie.modules.task.domain.TaskDailyInterface; |
| | | import me.zhengjie.modules.task.domain.TaskDaliy; |
| | | import me.zhengjie.modules.task.domain.TaskInterface; |
| | | 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.modules.constant.Constant; |
| | | import me.zhengjie.modules.project.domain.Project; |
| | | import me.zhengjie.modules.project.domain.ProjectCostOther; |
| | | import me.zhengjie.modules.project.domain.ProjectCostSummary; |
| | | import me.zhengjie.modules.project.domain.ProjectTask; |
| | | import me.zhengjie.modules.project.domain.ProjectTaskSummary; |
| | | import me.zhengjie.modules.project.domain.ProjectType; |
| | | import me.zhengjie.modules.project.repository.ProjectTaskRepository; |
| | | import me.zhengjie.modules.project.repository.ProjectTypeRepository; |
| | | import me.zhengjie.modules.system.domain.Job; |
| | | import me.zhengjie.modules.system.domain.SysLevel; |
| | | import me.zhengjie.modules.project.service.ProjectService; |
| | | import me.zhengjie.modules.project.service.dto.ProjectDto; |
| | | import me.zhengjie.modules.project.service.dto.ProjectQueryCriteria; |
| | | import me.zhengjie.modules.project.service.mapstruct.ProjectMapper; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.modules.system.service.RoleService; |
| | | import me.zhengjie.modules.system.service.dto.RoleSmallDto; |
| | | import me.zhengjie.modules.system.service.dto.SysLevelDto; |
| | | import me.zhengjie.modules.task.domain.Task; |
| | | import me.zhengjie.modules.task.repository.TaskDaliyRepository; |
| | | import me.zhengjie.modules.task.repository.TaskRepository; |
| | | import me.zhengjie.modules.task.repository.TaskUserRepository; |
| | | import me.zhengjie.modules.task.rest.TaskController; |
| | | import me.zhengjie.modules.task.service.TaskService; |
| | | import me.zhengjie.modules.task.service.impl.TaskServiceImpl; |
| | | import me.zhengjie.utils.*; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | import me.zhengjie.modules.project.service.ProjectService; |
| | | import me.zhengjie.modules.project.service.dto.ProjectDto; |
| | | import me.zhengjie.modules.project.service.dto.ProjectQueryCriteria; |
| | | import me.zhengjie.modules.project.service.mapstruct.ProjectMapper; |
| | | 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.utils.FileUtil; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | import me.zhengjie.utils.SecurityUtils; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.enums.DataScopeEnum; |
| | | |
| | | import java.sql.Timestamp; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | private final UserRepository userRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | private final ProjectTaskRepository projectTaskRepository; |
| | | private final TaskService taskService; |
| | | private final RoleService roleService; |
| | | private final TaskUserRepository taskUserRepository; |
| | | private final ProjectCostRepository projectCostRepository; |
| | | private final ProjectCostOtherRepository projectCostOtherRepository; |
| | | private final TaskRepository taskRepository; |
| | | private final TaskDaliyRepository taskDaliyRepository; |
| | | private final ProjectCostService projectCostService; |
| | | private final ProjectProgressRepository projectProgressRepository; |
| | | private final ProjectPerformanceRepository projectPerformanceRepository; |
| | | private final ProjectPerformanceDetailRepository projectPerformanceDetailRepository; |
| | | private final CostRepository costRepository; |
| | | private final ContractProjectRepository contractProjectRepository; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(ProjectQueryCriteria criteria, Pageable pageable){ |
| | | public Map<String,Object> queryAll(ProjectQueryCriteria criteria, Pageable pageable) { |
| | | |
| | | if (criteria.getLiableUserId() == null) { |
| | | // 获取当前用户ID |
| | | Long userId = SecurityUtils.getCurrentUserId(); |
| | | // 查询用户角色 |
| | | List<RoleSmallDto> roleSet = roleService.findByUsersId(userId); |
| | | if (roleSet.stream().anyMatch(role -> DataScopeEnum.find(role.getDataScope()) == DataScopeEnum.THIS_LEVEL)) { |
| | | // 根据用户ID查询负责的项目ID |
| | | List<Long> projectIdList = projectRepository.findIdByLiableUserId(userId); |
| | | // 根据用户ID查询参与的项目ID |
| | | List<Long> taskUserProjectIdList = projectCostRepository.findProjectIdByUserId(userId); |
| | | // 检索条件 |
| | | List<Long> totalProjectIdList = Stream.of(taskUserProjectIdList, projectIdList).flatMap(Collection::stream).distinct().collect(Collectors.toList()); |
| | | if (totalProjectIdList.size() == 0) { |
| | | return PageUtil.toPage(Collections.emptyList(), 0); |
| | | } |
| | | criteria.setLiableUserId(userId); |
| | | } |
| | | } |
| | | |
| | | Page<Project> page = projectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | // 将实体类转换为DTO(重写) |
| | | Function<Project, ProjectDto> func = new Function<Project, ProjectDto>(){ |
| | |
| | | // 项目类型名称 |
| | | projectDto.setProjectTypeName(projectType.getName()); |
| | | // 判断是否有负责人 |
| | | if(projectDto.getLiableUserId() != null){ |
| | | if(projectDto.getLiableUserId() != null) { |
| | | // 根据负责人ID查询用户信息 |
| | | User user = userRepository.findById(projectDto.getLiableUserId()).orElseGet(User::new); |
| | | ValidationUtil.isNull(project.getId(),"User","id",projectDto.getLiableUserId()); |
| | | // 负责人名称 |
| | | projectDto.setLiableUserName(user.getNickName()); |
| | | } |
| | | |
| | | ProjectTaskSummary projectTaskSummary = projectTaskRepository.findSummaryByProjectId(project.getId()); |
| | | projectDto.setTaskStartDate(projectTaskSummary.getTaskStartDate()); |
| | | projectDto.setTaskEndDate(projectTaskSummary.getTaskEndDate()); |
| | | projectDto.setTotalTaskTime(projectTaskSummary.getTotalTaskTime()); |
| | | |
| | | ProjectCostSummary projectCostSummary = projectCostRepository.findSummaryByProjectId(project.getId()); |
| | | projectDto.setTotalLaborCost(projectCostSummary.getTotalLaborCost()); |
| | | |
| | | // 其他项目成本 |
| | | List<ProjectCostOther> projectCostOtherList = projectCostOtherRepository.findByProjectId(project.getId()); |
| | | projectDto.setTotalOtherCost(projectCostOtherList.stream().map(ProjectCostOther::getProjectCost).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | |
| | | // 查询项目的任务数据 |
| | | TaskInterface taskInterface = taskRepository.collectByProjectId(project.getId()); |
| | | projectDto.setActualTaskTime(taskInterface.getPlanHours()); |
| | | |
| | | // 根据日期和项目ID取得日报信息 |
| | | List<TaskDailyInterface> taskDailyInterfaceList = taskDaliyRepository.collectByProjectIdGroupByUserIdAndFillDate(project.getId()); |
| | | Map<Long, List<TaskDailyInterface>> collectMap = taskDailyInterfaceList.stream().collect(Collectors.groupingBy(TaskDailyInterface::getUserId)); |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | // 实际消耗成本 |
| | | BigDecimal actualCost = 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()); |
| | | }); |
| | | // 根据员工成本单金算出当前工作的成本 |
| | | actualCost = actualCost.add(projectCostService.calcUserCostByDate(entry.getKey(), new Timestamp(taskDaliyFirst.getFillDate().getTime()), new Timestamp(taskDaliyLast.getFillDate().getTime()), costHourMap)); |
| | | } |
| | | // 实际消耗成本 |
| | | projectDto.setActualLaborCost(actualCost); |
| | | |
| | | // 返回处理好的Dto |
| | | return projectDto; |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<ProjectDto> queryAll(ProjectQueryCriteria criteria){ |
| | | public List<ProjectDto> queryAll(ProjectQueryCriteria criteria) { |
| | | if (criteria.getLiableUserId() == null) { |
| | | // 获取当前用户ID |
| | | Long userId = SecurityUtils.getCurrentUserId(); |
| | | // 查询用户角色 |
| | | List<RoleSmallDto> roleSet = roleService.findByUsersId(userId); |
| | | if (roleSet.stream().anyMatch(role -> DataScopeEnum.find(role.getDataScope()) == DataScopeEnum.THIS_LEVEL)) { |
| | | criteria.setLiableUserId(userId); |
| | | } |
| | | } |
| | | return projectMapper.toDto(projectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | } |
| | | |
| | | @Override |
| | | public List<ProjectDto> queryRelevant(ProjectQueryCriteria criteria){ |
| | | // 获取登录用户角色等级 |
| | | List<Integer> levels = roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList()); |
| | | // 登录用户最大角色等级 |
| | | int min = Collections.min(levels); |
| | | // 判断角色等级 |
| | | if (min == Constant.ROLE_LEVEL_MANAGER || min == Constant.ROLE_LEVEL_LEADER || min == Constant.ROLE_LEVEL_ORDINARY) { |
| | | // 根据用户ID查询参与的项目ID |
| | | List<Long> taskUserProjectIdList = taskDaliyRepository.findProjectIdByUserId(SecurityUtils.getCurrentUserId()); |
| | | // 根据用户ID查询负责的项目ID |
| | | List<Long> projectIdList = projectRepository.findIdByLiableUserId(SecurityUtils.getCurrentUserId()); |
| | | // 检索条件 |
| | | List<Long> totalProjectIdList = Stream.of(taskUserProjectIdList, projectIdList).flatMap(Collection::stream).distinct().collect(Collectors.toList()); |
| | | if (totalProjectIdList.size() == 0) { |
| | | return Collections.emptyList(); |
| | | } |
| | | criteria.setIdInList(totalProjectIdList); |
| | | } |
| | | return projectMapper.toDto(projectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | projectRepository.deleteById(id); |
| | | // 根据项目ID查询任务日报 |
| | | List<TaskDaliy> taskDaliyList = taskDaliyRepository.findByProjectId(id); |
| | | // 判断任务日报数量 |
| | | if (taskDaliyList.size() == 0) { |
| | | projectRepository.deleteById(id); |
| | | projectTaskRepository.deleteByProjectId(id); |
| | | projectCostRepository.deleteByProjectId(id); |
| | | projectProgressRepository.deleteByProjectId(id); |
| | | projectPerformanceRepository.deleteByProjectId(id); |
| | | projectPerformanceDetailRepository.deleteByProjectId(id); |
| | | costRepository.deleteByProjectId(id); |
| | | taskRepository.deleteByProjectId(id); |
| | | taskUserRepository.deleteByProjectId(id); |
| | | taskDaliyRepository.deleteByProjectId(id); |
| | | contractProjectRepository.deleteByProjectId(id); |
| | | } else { |
| | | throw new BadRequestException("存在任务日报的项目无法删除!"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateProjectAdmit(Long[] ids){ |
| | | for (Long id : ids) { |
| | | Project project = projectRepository.findByIdUpdate(id); |
| | |
| | | task.setRemarks(list.get(i).getRemarks()); |
| | | } |
| | | task.setPriority(Constant.PRIORITY_LOW); |
| | | taskService.create(task); |
| | | // 绩效区分 |
| | | task.setPerformanceType(list.get(i).getPerformanceType()); |
| | | // 未开始 |
| | | task.setTaskStatus(Constant.TASK_STATUS_NOT_STARTED); |
| | | task.setPlanStartTime(list.get(i).getPlanStartTime()); |
| | | task.setPlanEndTime(list.get(i).getPlanEndTime()); |
| | | taskRepository.save(task); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void download(List<ProjectDto> all, HttpServletResponse response) throws IOException { |
| | | public void download(List<ProjectDto> all, HttpServletResponse response) throws IOException { // TODO |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (ProjectDto project : all) { |
| | | String projectTypeName = project.getProjectTypeId() != null ? projectTypeRepository.findById(project.getProjectTypeId()).orElseGet(ProjectType::new).getName() : ""; |
| | |
| | | map.put("项目区分:", projectDistinguish); |
| | | map.put("需求说明", project.getRequirement()); |
| | | map.put("单价币种", unitCurrency); |
| | | map.put("单价金额", project.getUnitPrice()); |
| | | map.put("报价金额", project.getTotalPrice()); |
| | | // TODO |
| | | // map.put("单价金额", project.getUnitPrice()); |
| | | // map.put("报价金额", project.getTotalPrice()); |
| | | map.put("责任人", getLiableUser); |
| | | map.put("计划开始日期", project.getPlanStartDate()); |
| | | map.put("计划结束日期", project.getPlanEndDate()); |