| | |
| | | */ |
| | | package me.zhengjie.modules.task.service.impl; |
| | | |
| | | import com.sun.jna.platform.win32.Sspi; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Calendar; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | 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.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.TaskDaliyRepository; |
| | | import me.zhengjie.modules.task.repository.TaskRepository; |
| | | 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 me.zhengjie.modules.task.service.dto.TaskDaliyDto; |
| | | import me.zhengjie.modules.task.service.dto.TaskDaliyQueryCriteria; |
| | | import me.zhengjie.modules.task.service.mapstruct.TaskDaliyMapper; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | |
| | | 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; |
| | | import me.zhengjie.utils.DateUtil; |
| | | 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; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | 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 = taskUserRepository.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); |
| | | // 新增修改验证 |
| | |
| | | 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); |
| | |
| | | // 判断旧记录结算状态 |
| | | 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("只能新增修改自己的任务日报!"); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | // 判断是否有本月任务日报 |
| | | if (taskDaliyMonthList.size() != 0) { |
| | | // 获取本月任务日报中所有任务ID |
| | | List<Long> taskIds = taskDaliyMonthList.stream().map(item -> item.getTaskId()).collect(Collectors.toList()); |
| | | List<Long> taskIds = taskDaliyMonthList.stream().map(item -> item.getTaskId()).distinct().collect(Collectors.toList()); |
| | | // 获取本月任务分配列表 |
| | | List<TaskUser> taskUserMonthList = taskUserRepository.findByTaskIdIn(taskIds); |
| | | List<TaskUser> taskUserMonthList = taskUserRepository.findByUserIdAndTaskIdIn(criteria.getUserId(), taskIds); |
| | | // 日历类 |
| | | Calendar cal = setUpCalendar(targetYearMonth); |
| | | // 获取本月最大天数 |
| | |
| | | return completedWork; |
| | | } |
| | | |
| | | @Override |
| | | public Double queryComplianceWork(TaskDaliyQueryCriteria criteria){ |
| | | // 已达标工时 |
| | | Double complianceWork = 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()).collect(Collectors.toList()); |
| | | // 获取本月任务日报中所有项目ID |
| | | List<Long> projectIds = taskDaliyMonthList.stream().map(item -> item.getProjectId()).collect(Collectors.toList()); |
| | | // 获取本月任务分配列表 |
| | | List<TaskUser> taskUserMonthList = taskUserRepository.findByTaskIdIn(taskIds); |
| | | // 日历类 |
| | | Calendar cal = setUpCalendar(targetYearMonth); |
| | | // 获取本月最大天数 |
| | | int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | // 设置本月最大天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay); |
| | | // 获取本月最大周数 |
| | | int lastWeek = cal.get(Calendar.WEEK_OF_YEAR); |
| | | // 获取本月最大任务进度列表 |
| | | List<ProjectProgress> projectProgressLastList = projectProgressRepository.findByBelongYearAndBelongWeekAndProjectIdIn(Integer.valueOf(targetYearMonth.substring(0,4)), lastWeek, projectIds); |
| | | // 循环本月最大天数 |
| | | 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); |
| | | // 本日最大任务进度列表 |
| | | List<ProjectProgress> projectProgressLastDayList = projectProgressLastList.stream().filter(item -> item.getProject().getId() == taskDaliyDay.getProjectId()).collect(Collectors.toList()); |
| | | // 判断本日最大任务进度列表 |
| | | if (projectProgressLastDayList.size() > 1) { |
| | | throw new BadRequestException("任务进度存在多条同年同周情况!"); |
| | | } else if (projectProgressLastDayList.size() == 0) { |
| | | throw new BadRequestException("未找到任务进度记录!"); |
| | | } |
| | | // 本日最大任务进度 |
| | | ProjectProgress projectProgressLastDay = projectProgressLastDayList.get(0); |
| | | // 判断本日最大任务进度是否未承认 |
| | | if (Constant.APPROVE_STATUS_UNRECOGNIZED.equals(projectProgressLastDay.getApproveStatus())) { |
| | | throw new BadRequestException("任务进度未承认!"); |
| | | } |
| | | // 判断本日最大任务进度是否达标 |
| | | if (projectProgressLastDay.getProjectProgress() < projectProgressLastDay.getEstimatedProgress()) { |
| | | // 跳过当前循环 |
| | | continue; |
| | | } |
| | | // 根据项目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; |
| | | } |
| | | // 已达标工时 |
| | | complianceWork += taskUserDay.getTaskHours() * (taskDaliyDayProgress / 100); |
| | | } |
| | | } |
| | | } |
| | | // 已达标工时 |
| | | return complianceWork; |
| | | } |
| | | |
| | | /** |
| | | * 设置日历类 |
| | | * @return |
| | |
| | | // 日历类 |
| | | return cal; |
| | | } |
| | | |
| | | @Override |
| | | public Double queryTaskTotalConsumedHours(TaskDaliyQueryCriteria criteria) { |
| | | // 根据项目ID、任务ID、用户ID查询总消耗工时、总消耗工作量、最大任务进度 |
| | | TaskDailyInterface taskDailyInterface = taskDaliyRepository.findSumConsumedHoursAndSumConsumedWorkloadAndMaxTaskProgressByProjectIdAndTaskIdAndUserId(criteria.getProjectId(), criteria.getTaskId(), criteria.getUserId()); |
| | | // 返回 |
| | | return taskDailyInterface.getConsumedHours(); |
| | | } |
| | | } |