| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.utils.CodeGenerateUtils; |
| | | import com.ruoyi.system.mapper.TCleanerMapper; |
| | | import com.ruoyi.system.model.*; |
| | | import com.ruoyi.system.service.*; |
| | |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private static final String PROJECT_DEPT_WEIGHT = ":PROJECT_DEPT_WEIGHT"; |
| | | // 保洁员权重标识 |
| | | private static final String CLEANER_WEIGHT = ":CLEANER_WEIGHT"; |
| | | // 重复点位标识 |
| | | private static final String REPEAT_LOCATION = ":REPEAT_LOCATION"; |
| | | |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | |
| | | private TTaskCleanService taskCleanService; |
| | | |
| | | |
| | | @Scheduled(cron = "0 0 0 * * ?") |
| | | // 每一个小时执行一次 |
| | | // @Scheduled(cron = "0 0 0 * * ?") |
| | | @Scheduled(fixedRate = 1500000000) |
| | | |
| | | public void dayOfCreateInspection() { |
| | | try { |
| | | |
| | |
| | | } |
| | | |
| | | public void createInspection(TTemplate template) { |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date date = new Date(); |
| | | // 查询所有的模板详情 |
| | | List<TTemplateDetail> list = templateDetailService.list(Wrappers.lambdaQuery(TTemplateDetail.class) |
| | | .eq(TTemplateDetail::getTemplateId, template.getId())); |
| | |
| | | // 创建任务 |
| | | list.forEach(detail -> { |
| | | // 计算周期 |
| | | long cycle = detail.getCycle(); |
| | | int cycle = detail.getCycle(); |
| | | switch (detail.getCycleType()){ |
| | | case 2: |
| | | cycle = cycle * 7; |
| | |
| | | // 获取项目部列表 |
| | | List<TProjectDept> projectDeptList; |
| | | if(CollectionUtils.isEmpty(projectDeptIds)){ |
| | | projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)); |
| | | projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class) |
| | | .ne(TProjectDept::getParentId,0)); |
| | | }else { |
| | | projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class) |
| | | .ne(TProjectDept::getParentId,0) |
| | | .in(TProjectDept::getId, projectDeptIds)); |
| | | // 所过所有的项目部都被抽取了,则重新抽取,并且清空项目部权重 |
| | | if(CollectionUtils.isEmpty(projectDeptList)){ |
| | |
| | | if(projectDepts.size() < num2){ |
| | | List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList()); |
| | | List<TProjectDept> projectDeptList1 = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class) |
| | | .notIn(TProjectDept::getId, proIds)); |
| | | .notIn(TProjectDept::getId, proIds) |
| | | .ne(TProjectDept::getParentId,0)); |
| | | redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT); |
| | | List<TProjectDept> projectDepts1 = randomSelection(projectDeptList1, num2 - projectDepts.size()); |
| | | List<String> proIds1 = projectDepts1.stream().map(TProjectDept::getId).collect(Collectors.toList()); |
| | |
| | | |
| | | // 拿到抽取的项目部下的所有保洁员 |
| | | List<TProjectDept> tProjectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class) |
| | | .in(TProjectDept::getParentId, proIds)); |
| | | .in(TProjectDept::getId, proIds)); |
| | | // 获取片区id |
| | | List<String> areaIds = tProjectDeptList.stream().map(TProjectDept::getId).collect(Collectors.toList()); |
| | | if(CollectionUtils.isEmpty(areaIds)){ |
| | | return; |
| | | } |
| | | |
| | | // 计算每天需要抽取多少个保洁员 |
| | | long count = cleanerService.count(Wrappers.lambdaQuery(TCleaner.class) |
| | | .in(TCleaner::getProjectId, areaIds)); |
| | | int cleanerSums = num1 * Integer.parseInt(count + ""); |
| | | // 获取每天需要抽取的保洁员数,向上取整 |
| | | int dayCleanerCount = cleanerSums / detail.getCycle(); |
| | | if (cleanerSums <cycle){ |
| | | cleanerSums = cycle; |
| | | } |
| | | int dayCleanerCount = cleanerSums / cycle; |
| | | |
| | | // 获取保洁员权重 |
| | | List<String> cleanerIds = redisCache.getCacheList(detail.getId() + CLEANER_WEIGHT); |
| | |
| | | } |
| | | } |
| | | |
| | | // TODO 抽取重复点位 |
| | | // 抽取重复点位 |
| | | Integer currentValue = detail.getCurrentValue(); |
| | | if(currentValue != cycle){ |
| | | // 周期天数加一 |
| | | detail.setCurrentValue(currentValue + 1); |
| | | // 获取重复点位 |
| | | Set<TLocation> repeatLocation = redisCache.getCacheSet(detail.getId() + REPEAT_LOCATION); |
| | | if(!CollectionUtils.isEmpty(repeatLocation)){ |
| | | List<TLocation> locations = randomSelection(new ArrayList<>(tLocationList), num3); |
| | | tLocationList.addAll(locations); |
| | | } |
| | | Set<TLocation> locationSet = new HashSet<>(tLocationList); |
| | | redisCache.setCacheSet(detail.getId() + REPEAT_LOCATION, locationSet); |
| | | |
| | | }else { |
| | | // 设置当前周期为0 |
| | | detail.setCurrentValue(0); |
| | | // 将重复点位置空 |
| | | redisCache.deleteObject(detail.getId() + REPEAT_LOCATION); |
| | | } |
| | | |
| | | // 创建任务 |
| | | List<TTask> tasks = new ArrayList<>(); |
| | |
| | | SysUser sysUser = sysUsers.get(0); |
| | | task.setPatrolInspector(sysUser.getUserId().toString()); |
| | | task.setPatrolInspectorDept(sysUser.getDeptId()); |
| | | task.setUserId(sysUser.getUserId()); |
| | | } |
| | | task.setStatus(1); |
| | | task.setLocationId(tLocation.getId()); |
| | | task.setImplementTime(LocalDateTime.now().plusDays(1)); |
| | | task.setTaskType(1); |
| | | task.setTemplateId(detail.getId()); |
| | | String nameAndCode = CodeGenerateUtils.generateVolumeSn(); |
| | | task.setTaskName(nameAndCode); |
| | | task.setTaskCode(nameAndCode); |
| | | tasks.add(task); |
| | | } |
| | | taskCleanService.saveBatch(tasks); |
| | | |
| | | template.setTaskCount(tasks.size()); |
| | | templateService.updateById(template); |
| | | templateDetailService.updateById(detail); |
| | | }); |
| | | |
| | | } |