| | |
| | | */ |
| | | package me.zhengjie.modules.task.service.impl; |
| | | |
| | | import com.sun.jna.platform.win32.Sspi; |
| | | 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.repository.ProjectProgressRepository; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | 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.task.domain.Task; |
| | | import me.zhengjie.modules.task.domain.TaskDailyInterface; |
| | | import me.zhengjie.modules.task.domain.TaskDaliy; |
| | | import me.zhengjie.modules.task.domain.TaskUser; |
| | | import me.zhengjie.modules.task.repository.TaskRepository; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import me.zhengjie.modules.task.repository.TaskUserRepository; |
| | | import me.zhengjie.utils.*; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.task.repository.TaskDaliyRepository; |
| | | import me.zhengjie.modules.task.service.TaskDaliyService; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import java.sql.Timestamp; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | private final ProjectRepository projectRepository; |
| | | private final TaskRepository taskRepository; |
| | | private final UserRepository userRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | private final TaskUserRepository taskUserRepository; |
| | | private final ProjectProgressRepository projectProgressRepository; |
| | | private final RoleService roleService; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(TaskDaliyQueryCriteria criteria, Pageable pageable){ |
| | | // 获取登录用户角色等级 |
| | | 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 PageUtil.toPage(Collections.emptyList(), 0); |
| | | } |
| | | criteria.setProjectIdInList(totalProjectIdList); |
| | | } |
| | | Page<TaskDaliy> page = taskDaliyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | // 将实体类转换为DTO(重写) |
| | | Function<TaskDaliy, TaskDaliyDto> func = new Function<TaskDaliy, TaskDaliyDto>(){ |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public TaskDaliyDto create(TaskDaliy resources) { |
| | | // 新增修改删除验证 |
| | | checkAddOrUpdateOrDelete(resources); |
| | | // 新增修改验证 |
| | | checkAddOrUpdate(resources); |
| | | return taskDaliyMapper.toDto(taskDaliyRepository.save(resources)); |
| | | } |
| | | |
| | |
| | | public void update(TaskDaliy resources) { |
| | | TaskDaliy taskDaliy = taskDaliyRepository.findById(resources.getId()).orElseGet(TaskDaliy::new); |
| | | ValidationUtil.isNull( taskDaliy.getId(),"TaskDaliy","id",resources.getId()); |
| | | // 新增修改删除验证 |
| | | checkAddOrUpdateOrDelete(taskDaliy); |
| | | // 修改删除验证 |
| | | checkUpdateOrDelete(taskDaliy); |
| | | // 新增修改验证 |
| | | checkAddOrUpdate(resources); |
| | | taskDaliy.copy(resources); |
| | | taskDaliyRepository.save(taskDaliy); |
| | | } |
| | | |
| | | /** |
| | | * 新增修改验证 |
| | | * @param resources |
| | | */ |
| | | private void checkAddOrUpdate(TaskDaliy resources) { |
| | | // 任务日报检索类 |
| | | TaskDaliyQueryCriteria criteria = new TaskDaliyQueryCriteria(); |
| | | criteria.setProjectId(resources.getProjectId()); |
| | | criteria.setTaskId(resources.getTaskId()); |
| | | criteria.setUserId(resources.getUserId()); |
| | | criteria.setFillDate(resources.getFillDate()); |
| | | // 根据项目ID、任务ID、用户ID、填报日期查询 |
| | | List<TaskDaliy> taskDaliyList = taskDaliyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)); |
| | | // 判断是否已有日报 |
| | | if (taskDaliyList.size() > 1 || (taskDaliyList.size() == 1 && taskDaliyList.get(0).getId() != resources.getId())) { |
| | | throw new BadRequestException("每个任务每天只能填写一次日报!"); |
| | | } |
| | | // 根据项目ID、任务ID、用户ID查询填报日期前最近的任务日报信息 |
| | | TaskDaliy previousTaskDaliy = taskDaliyRepository.findPreviousByProjectIdAndTaskIdAndAndUserId(resources.getProjectId(), resources.getTaskId(), resources.getUserId(), resources.getFillDate()); |
| | | // 根据项目ID、任务ID、用户ID查询填报日期后最近的任务日报信息 |
| | | TaskDaliy nextTaskDaliy = taskDaliyRepository.findNextByProjectIdAndTaskIdAndAndUserId(resources.getProjectId(), resources.getTaskId(), resources.getUserId(), resources.getFillDate()); |
| | | // 判断任务进度是否比前一次多,比后一次少 |
| | | if ((previousTaskDaliy != null && resources.getTaskProgress() <= previousTaskDaliy.getTaskProgress()) || (nextTaskDaliy != null && resources.getTaskProgress() >= nextTaskDaliy.getTaskProgress())) { |
| | | throw new BadRequestException("任务进度必须比前一次多且比后一次少!"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | TaskDaliy taskDaliy = taskDaliyRepository.findById(id).orElseGet(TaskDaliy::new); |
| | | ValidationUtil.isNull( taskDaliy.getId(),"TaskDaliy","id",id); |
| | | // 新增修改删除验证 |
| | | checkAddOrUpdateOrDelete(taskDaliy); |
| | | // 修改删除验证 |
| | | checkUpdateOrDelete(taskDaliy); |
| | | taskDaliyRepository.deleteById(id); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修改删除验证 |
| | | * @param resources |
| | | */ |
| | | private void checkUpdateOrDelete(TaskDaliy resources) { |
| | | // 判断旧记录结算状态 |
| | | if (Constant.SETTLEMENT_STATUS_SETTLED.equals(resources.getSettlementStatus())) { |
| | | throw new BadRequestException("已结算的任务日报无法修改/删除!"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 新增修改删除验证 |
| | | * @param resources |
| | | */ |
| | | private void checkAddOrUpdateOrDelete(TaskDaliy resources) { |
| | | // 获取登录用户角色等级 |
| | | 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) { |
| | | // 判断是否是自己的日报 |
| | | if (SecurityUtils.getCurrentUserId().longValue() != resources.getUserId().longValue()) { |
| | | throw new BadRequestException("只能新增修改自己的任务日报!"); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | String projectName = taskDaliy.getProjectId() != null ? projectRepository.findById(taskDaliy.getProjectId()).orElseGet(Project::new).getName() : ""; |
| | | String taskName = taskDaliy.getTaskId() != null ? taskRepository.findById(taskDaliy.getTaskId()).orElseGet(Task::new).getName() : ""; |
| | | String userNickName = taskDaliy.getUserId() != null ? userRepository.findById(taskDaliy.getUserId()).orElseGet(User::new).getNickName() : ""; |
| | | String settlementStatus = taskDaliy.getSettlementStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.SETTLEMENT_STATUS, taskDaliy.getSettlementStatus()).getLabel() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("填写日期", taskDaliy.getFillDate()); |
| | | map.put("项目名称", projectName); |
| | |
| | | map.put("员工姓名", userNickName); |
| | | map.put("消耗工时", taskDaliy.getConsumedHours()); |
| | | map.put("消耗工作量", taskDaliy.getConsumedWorkload()); |
| | | map.put("任务进度", taskDaliy.getTaskProgress()); |
| | | map.put("结算状态", settlementStatus); |
| | | map.put("备注", taskDaliy.getRemarks()); |
| | | map.put("创建者", taskDaliy.getCreateBy()); |
| | | map.put("创建时间", taskDaliy.getCreateTime()); |
| | |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | @Override |
| | | public Double queryCompletedWork(TaskDaliyQueryCriteria criteria){ |
| | | // 已完成工时 |
| | | Double completedWork = 0D; |
| | | // 国际时间转中国时间 |
| | | String targetYearMonth = DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth()); |
| | | // 任务日报检索类 |
| | | criteria.setFillDateLike(targetYearMonth.substring(0,7)); |
| | | // 获取本月任务日报列表 |
| | | List<TaskDaliy> taskDaliyMonthList = taskDaliyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)); |
| | | // 判断是否有本月任务日报 |
| | | if (taskDaliyMonthList.size() != 0) { |
| | | // 获取本月任务日报中所有任务ID |
| | | List<Long> taskIds = taskDaliyMonthList.stream().map(item -> item.getTaskId()).distinct().collect(Collectors.toList()); |
| | | // 获取本月任务分配列表 |
| | | List<TaskUser> taskUserMonthList = taskUserRepository.findByUserIdAndTaskIdIn(criteria.getUserId(), taskIds); |
| | | // 日历类 |
| | | Calendar cal = setUpCalendar(targetYearMonth); |
| | | // 获取本月最大天数 |
| | | int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | // 循环本月最大天数 |
| | | for (int i = 1; i <= lastDay; i ++) { |
| | | // 设置天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, i); |
| | | // 获取本日任务日报列表 |
| | | List<TaskDaliy> taskDaliyDayList = taskDaliyMonthList.stream().filter(item -> item.getFillDate().getTime() == cal.getTime().getTime()).collect(Collectors.toList()); |
| | | // 循环本日任务日报列表 |
| | | for (int j = 0; j < taskDaliyDayList.size(); j ++) { |
| | | // 本日任务日报 |
| | | TaskDaliy taskDaliyDay = taskDaliyDayList.get(j); |
| | | // 本日任务分配列表 |
| | | List<TaskUser> taskUserDayList = taskUserMonthList.stream().filter(item -> item.getTaskId() == taskDaliyDay.getTaskId()).collect(Collectors.toList()); |
| | | // 判断本日任务分配列表数量 |
| | | if (taskUserDayList.size() > 1) { |
| | | throw new BadRequestException("任务存在多次分配同一人情况!"); |
| | | } else if (taskUserDayList.size() == 0) { |
| | | throw new BadRequestException("未找到任务分配记录!"); |
| | | } |
| | | // 本日任务分配 |
| | | TaskUser taskUserDay = taskUserDayList.get(0); |
| | | // 根据项目ID、任务ID、用户ID查询填报日期前最近的任务日报信息 |
| | | TaskDaliy taskDaliyHistory = taskDaliyRepository.findPreviousByProjectIdAndTaskIdAndAndUserId(taskDaliyDay.getProjectId(), taskDaliyDay.getTaskId(), taskDaliyDay.getUserId(), taskDaliyDay.getFillDate()); |
| | | // 本日任务日报进度 |
| | | Double taskDaliyDayProgress = taskDaliyDay.getTaskProgress(); |
| | | // 判断最近的任务日报是否为空 |
| | | if (taskDaliyHistory != null) { |
| | | // 本日任务日报相关历史进度 |
| | | Double taskDaliyHistoryProgress = taskDaliyHistory.getTaskProgress(); |
| | | // 判断进度是否正常 |
| | | if (taskDaliyDayProgress - taskDaliyHistoryProgress <= 0) { |
| | | throw new BadRequestException("任务日报进度计算存在异常!"); |
| | | } |
| | | // 本日任务日报进度 |
| | | taskDaliyDayProgress = taskDaliyDayProgress - taskDaliyHistoryProgress; |
| | | } |
| | | // 已完成工时 |
| | | completedWork += taskUserDay.getTaskHours() * (taskDaliyDayProgress / 100); |
| | | // 本日任务日报 |
| | | taskDaliyDay.setSettlementStatus(Constant.SETTLEMENT_STATUS_SETTLED); |
| | | // 保存本日任务日报 |
| | | taskDaliyRepository.save(taskDaliyDay); |
| | | } |
| | | } |
| | | } |
| | | // 已完成工时 |
| | | return completedWork; |
| | | } |
| | | |
| | | /** |
| | | * 设置日历类 |
| | | * @return |
| | | */ |
| | | private Calendar setUpCalendar(String targetYearMonth) { |
| | | // 日历类 |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, Integer.valueOf(targetYearMonth.substring(0,4))); |
| | | // 设置月份 |
| | | cal.set(Calendar.MONTH, Integer.valueOf(targetYearMonth.substring(5,7)) - 1); |
| | | // 设置时数 |
| | | cal.set(Calendar.HOUR_OF_DAY, 0); |
| | | // 设置分数 |
| | | cal.set(Calendar.MINUTE, 0); |
| | | // 设置秒数 |
| | | cal.set(Calendar.SECOND, 0); |
| | | // 设置毫秒数 |
| | | cal.set(Calendar.MILLISECOND, 0); |
| | | // 日历类 |
| | | return cal; |
| | | } |
| | | |
| | | @Override |
| | | public Double queryTaskTotalConsumedHours(TaskDaliyQueryCriteria criteria) { |
| | | // 根据项目ID、任务ID、用户ID查询总消耗工时、总消耗工作量、最大任务进度 |
| | | TaskDailyInterface taskDailyInterface = taskDaliyRepository.findSumConsumedHoursAndSumConsumedWorkloadAndMaxTaskProgressByProjectIdAndTaskIdAndUserId(criteria.getProjectId(), criteria.getTaskId(), criteria.getUserId()); |
| | | // 返回 |
| | | return taskDailyInterface.getConsumedHours(); |
| | | } |
| | | } |