New file |
| | |
| | | package com.ycl.task; |
| | | |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; |
| | | import com.ycl.common.base.Result; |
| | | import com.ycl.common.enums.business.CodingRulerIntervalTypeEnum; |
| | | import com.ycl.common.enums.business.ProjectProcessTypeEnum; |
| | | import com.ycl.domain.entity.*; |
| | | import com.ycl.domain.vo.CodingRulerVO; |
| | | import com.ycl.domain.vo.ProcessCodingVO; |
| | | import com.ycl.domain.vo.ProcessOvertimeTimesVO; |
| | | import com.ycl.factory.FlowServiceFactory; |
| | | import com.ycl.mapper.*; |
| | | import com.ycl.service.CodingRulerService; |
| | | import com.ycl.service.ProjectOvertimeTimesService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | @Component("codingTask") |
| | | public class CodingTask extends FlowServiceFactory { |
| | | |
| | | private final ProjectOvertimeTimesMapper projectOvertimeTimesMapper; |
| | | |
| | | private final ProjectOvertimeTimesService projectOvertimeTimesService; |
| | | |
| | | private final ProjectProcessMapper projectProcessMapper; |
| | | |
| | | private final ProjectInfoMapper projectInfoMapper; |
| | | |
| | | private final CodingRulerService codingRulerService; |
| | | |
| | | private final ProcessCodingMapper processCodingMapper; |
| | | |
| | | //计数项目超时任务数并更具赋码规则进行赋码 |
| | | public void codingTask(){ |
| | | log.info("开始计算项目并且赋码"); |
| | | //获得启用的赋码规则 |
| | | Result result = codingRulerService.getStartRuler(); |
| | | CodingRulerVO yellowRuler = (CodingRulerVO) result.get("yellowRuler"); |
| | | CodingRulerVO redRuler = (CodingRulerVO) result.get("redRuler"); |
| | | |
| | | //没启用直接结束 |
| | | if (Objects.isNull(yellowRuler) && Objects.isNull(redRuler)) { |
| | | log.error("未启用赋码规则"); |
| | | return; |
| | | } |
| | | |
| | | //获得所有 超时任务数与流程实例id |
| | | List<ProcessOvertimeTimesVO> processOvertimeTimesVOS = processCodingMapper.countProjectOvertimeTimes(); |
| | | if (!CollectionUtils.isEmpty(processOvertimeTimesVOS)){ |
| | | //将流程实例id转换为list |
| | | List<String> processIds = processOvertimeTimesVOS.stream().map(ProcessOvertimeTimesVO::getProcessInsId).toList(); |
| | | //获得所有 更具流程定义id,流程相关项目id, key为流程id |
| | | Map<String, ProjectProcess> projectMap = new LambdaQueryChainWrapper<>(projectProcessMapper) |
| | | .in(ProjectProcess::getProcessInsId, processIds) |
| | | .eq(ProjectProcess::getProjectType, ProjectProcessTypeEnum.PROJECT) |
| | | .list() |
| | | .stream() |
| | | .collect(Collectors.toMap(ProjectProcess::getProcessInsId, Function.identity())); |
| | | |
| | | //修改为黄码的项目 id |
| | | List<String> yellowProjectCodingIds = new ArrayList<>(); |
| | | //修改为红码的项目 id |
| | | List<String> redProjectCodingIds = new ArrayList<>(); |
| | | |
| | | |
| | | //新增的记录集合 |
| | | List<ProjectOvertimeTimes> addList = new ArrayList<>(); |
| | | |
| | | for (ProcessOvertimeTimesVO processOvertimeTimesVO : processOvertimeTimesVOS) { |
| | | //判断 项目类型为 ProjectProcessTypeEnum.PROJECT 并且超时任务数大于0 |
| | | if (processOvertimeTimesVO.getTaskOvertimeNum() != 0 && projectMap.containsKey(processOvertimeTimesVO.getProcessInsId())) { |
| | | ProjectOvertimeTimes projectOvertimeTimes = new ProjectOvertimeTimes(); |
| | | projectOvertimeTimes.setProjectType(ProjectProcessTypeEnum.PROJECT.getValue()); |
| | | projectOvertimeTimes.setProjectId(projectMap.get(processOvertimeTimesVO.getProcessInsId()).getProjectId()); |
| | | projectOvertimeTimes.setProcessInsId(processOvertimeTimesVO.getProcessInsId()); |
| | | projectOvertimeTimes.setTaskOvertimeNum(processOvertimeTimesVO.getTaskOvertimeNum()); |
| | | //添加到新增集合内 |
| | | addList.add(projectOvertimeTimes); |
| | | //检验赋码规则 |
| | | checkCodingType(projectOvertimeTimes.getTaskOvertimeNum(), |
| | | projectOvertimeTimes.getProjectId(), |
| | | yellowProjectCodingIds,redProjectCodingIds, |
| | | yellowRuler,redRuler); |
| | | } |
| | | } |
| | | |
| | | if(!CollectionUtils.isEmpty(addList)){ |
| | | projectOvertimeTimesMapper.delAll(); |
| | | projectOvertimeTimesService.saveBatch(addList); |
| | | } |
| | | //修改项目赋码 |
| | | updateProjectCoding(redProjectCodingIds,"red"); |
| | | log.info("打印赋值为红码的项目id"); |
| | | for (String s : redProjectCodingIds){ |
| | | System.out.println(s); |
| | | } |
| | | log.info("打印赋值为红码的项目id完毕"); |
| | | updateProjectCoding(yellowProjectCodingIds,"yellow"); |
| | | log.info("打印赋值为黄码的项目id"); |
| | | for (String s : yellowProjectCodingIds){ |
| | | System.out.println(s); |
| | | } |
| | | log.info("打印赋值为黄码的项目id完毕"); |
| | | } |
| | | |
| | | log.info("结束计算项目并且赋码"); |
| | | } |
| | | |
| | | /** |
| | | * 赋码 |
| | | * |
| | | * @param projectIds 流程实例ID列表 |
| | | * @param coding 赋码值 |
| | | */ |
| | | private void updateProjectCoding(List<String> projectIds, String coding) { |
| | | if (!CollectionUtils.isEmpty(projectIds)) { |
| | | //将id 类型转换为Long |
| | | List<Long> longList = projectIds.stream() |
| | | .map(Long::parseLong) |
| | | .collect(Collectors.toList()); |
| | | new LambdaUpdateChainWrapper<>(projectInfoMapper) |
| | | .in(ProjectInfo::getId, longList) |
| | | .set(ProjectInfo::getCoding, coding) |
| | | .update(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * |
| | | * @param overTimeNum 超时任务数 |
| | | * @param projectId 项目id |
| | | * @param yellowProjectCodingIds 修改项目赋码为黄码的id集合 |
| | | * @param redProjectCodingIds 修改项目赋码为红码的id集合 |
| | | * @param yellowRuler 黄码规则 |
| | | * @param redRuler 红码规则 |
| | | */ |
| | | private void checkCodingType(Long overTimeNum,String projectId, |
| | | List<String> yellowProjectCodingIds, List<String> redProjectCodingIds, |
| | | CodingRulerVO yellowRuler,CodingRulerVO redRuler){ |
| | | //先判断红码,规则如果满足,则不执行黄码规则 |
| | | if (judgeState(overTimeNum, projectId, redProjectCodingIds, redRuler)) return; |
| | | |
| | | //判断是否满足黄码规则 |
| | | judgeState(overTimeNum, projectId, yellowProjectCodingIds, yellowRuler); |
| | | |
| | | |
| | | } |
| | | |
| | | private boolean judgeState(Long overTimeNum, String projectId, List<String> projectCodingIds, CodingRulerVO ruler) { |
| | | if (ruler != null){ |
| | | if (CodingRulerIntervalTypeEnum.Interval.getValue().equals(ruler.getIntervalType())){ |
| | | //区间情况 |
| | | boolean result = false; |
| | | //判断满足左区间 |
| | | if (ruler.getLeftSymbol().equals("0")){ |
| | | result = (overTimeNum > ruler.getLeftValue()); |
| | | }else if (ruler.getLeftSymbol().equals("1")){ |
| | | result = (overTimeNum >= ruler.getLeftValue()); |
| | | }else if (ruler.getLeftSymbol().equals("2")){ |
| | | result = (Objects.equals(overTimeNum, ruler.getLeftValue())); |
| | | } |
| | | //满足则判断满足右区间 |
| | | if (result){ |
| | | if (ruler.getRightSymbol().equals("2")){ |
| | | result = (Objects.equals(overTimeNum, ruler.getRightValue())); |
| | | }else if (ruler.getRightSymbol().equals("3")){ |
| | | result = (overTimeNum < ruler.getRightValue()); |
| | | }else if (ruler.getRightSymbol().equals("4")){ |
| | | result = (overTimeNum <= ruler.getRightValue()); |
| | | } |
| | | } |
| | | if (result){ |
| | | //符合该赋码条件 |
| | | projectCodingIds.add(projectId); |
| | | return true; |
| | | } |
| | | } |
| | | else{ |
| | | //单区间情况 |
| | | boolean result = false; |
| | | if (ruler.getLeftSymbol().equals("0")){ |
| | | result = (overTimeNum > ruler.getLeftValue()); |
| | | }else if (ruler.getLeftSymbol().equals("1")){ |
| | | result = (overTimeNum >= ruler.getLeftValue()); |
| | | }else if (ruler.getLeftSymbol().equals("2")){ |
| | | result = (Objects.equals(overTimeNum, ruler.getLeftValue())); |
| | | }else if (ruler.getLeftSymbol().equals("3")){ |
| | | result = (overTimeNum < ruler.getLeftValue()); |
| | | }else if (ruler.getLeftSymbol().equals("4")){ |
| | | result = (overTimeNum <= ruler.getLeftValue()); |
| | | } |
| | | if (result){ |
| | | //符合该赋码条件 |
| | | projectCodingIds.add(projectId); |
| | | return true; |
| | | } |
| | | |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | } |