无关风月
1 天以前 581a4811fb62fcfef4e6a19cc67e095a167f139d
ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/TIndexController.java
@@ -6,6 +6,7 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
@@ -13,7 +14,9 @@
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.applet.dto.*;
@@ -27,6 +30,7 @@
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
@@ -459,16 +463,12 @@
            leaveList.add(leaveUserListVO);
        }
        List<TTask> taskList = taskAll.stream().filter(e -> e.getPatrolInspector().equals(userId + "") && e.getStatus() == 4).collect(Collectors.toList());
        List<String> taskIds = taskList.stream().map(TTask::getId).collect(Collectors.toList());
        // 驳回原因
        List<TTaskDetail> tTaskDetails = taskDetailService.list(new LambdaQueryWrapper<TTaskDetail>()
                .eq(TTaskDetail::getHandleType,3).groupBy(TTaskDetail::getTaskId).orderByDesc(TTaskDetail::getCreateTime));
        for (TTask tTask : taskList) {
            List<TTaskDetail> taskDetailsStatus1 = taskDetailService.lambdaQuery()
                    .eq(TTaskDetail::getAuditStatus, 1).in(TTaskDetail::getTaskId, taskIds)
                    .eq(TTaskDetail::getClearStatus, 2)
                    .groupBy(TTaskDetail::getTaskId)
                    .orderByDesc(TTaskDetail::getCreateTime).list();
            TTaskDetail taskDetailsStatus1 = taskDetailService.lambdaQuery()
                    .eq(TTaskDetail::getTaskId, tTask.getId())
                    .in(TTaskDetail::getHandleType, Arrays.asList(3,6))
                    .orderByDesc(TTaskDetail::getCreateTime).last("limit 1").one();
            TaskPendingVO taskTodayVO = new TaskPendingVO();
            BeanUtils.copyProperties(tTask, taskTodayVO);
            TLocation tLocation = locationList.stream().filter(e -> e.getId().equals(tTask.getLocationId())).findFirst().orElse(null);
@@ -479,12 +479,6 @@
                taskTodayVO.setLocationName(tLocation.getLocationName());
                if(StringUtils.hasLength(dto.getLon())){
                    taskTodayVO.setDistance(tTask.getDistance().divide(new BigDecimal(1000),2, RoundingMode.HALF_DOWN));
//                    Map<String, String> distance = amapApiClient.getDistance(dto.getLon() + "," + dto.getLat(), tLocation.getLocationLon() + "," + tLocation.getLocationLat(), 1);
//                    if (distance != null) {
//                        taskTodayVO.setDistance(new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_EVEN));
//                    } else {
//                        taskTodayVO.setDistance(new BigDecimal("0"));
//                    }
                }else {
                    taskTodayVO.setDistance(new BigDecimal("0"));
                }
@@ -494,9 +488,8 @@
                    taskTodayVO.setLocationTypeIcon(tLocationType.getLocationIcon());
                }
            }
            TTaskDetail tTaskDetail = tTaskDetails.stream().filter(e -> e.getTaskId().equals(tTask.getId())).findFirst().orElse(null);
            if (tTaskDetail!=null){
                taskTodayVO.setRemark(tTaskDetail.getAuditRemark());
            if (taskDetailsStatus1!=null){
                taskTodayVO.setRemark(taskDetailsStatus1.getAuditRemark());
            }
            // 任务记录ids
            List<String> collect = taskAll.stream().map(TTask::getId).collect(Collectors.toList());
@@ -515,14 +508,9 @@
                        taskTodayVOOne.setLocationLat(tLocationOne.getLocationLat());
                        taskTodayVOOne.setLocationName(tLocationOne.getLocationName());
                        if(StringUtils.hasLength(dto.getLon())){
                            Map<String, String> distance = amapApiClient.getDistance(dto.getLon() + "," + dto.getLat(), tLocationOne.getLocationLon() + "," + tLocationOne.getLocationLat(), 1);
                            if (distance != null) {
                                taskTodayVOOne.setDistance(new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_EVEN));
                            } else {
                                taskTodayVOOne.setDistance(new BigDecimal("0"));
                            }
                            taskTodayVO.setDistance(tTask.getDistance().divide(new BigDecimal(1000),2, RoundingMode.HALF_DOWN));
                        }else {
                            taskTodayVOOne.setDistance(new BigDecimal("0"));
                            taskTodayVO.setDistance(new BigDecimal("0"));
                        }
                        TLocationType tLocationType = locationTypeList.stream().filter(e -> e.getId().equals(tLocationOne.getLocationType())).findFirst().orElse(null);
                        if (tLocationType != null) {
@@ -548,11 +536,10 @@
            if(CollectionUtils.isEmpty(collect)){
                break;
            }
            List<TTaskDetail> taskDetailsStatus1 = taskDetailService.lambdaQuery()
                    .eq(TTaskDetail::getAuditStatus, 1).in(TTaskDetail::getTaskId, collect)
                    .eq(TTaskDetail::getClearStatus, 2)
                    .groupBy(TTaskDetail::getTaskId)
                    .orderByDesc(TTaskDetail::getCreateTime).list();
            TTaskDetail taskDetailsStatus1 = taskDetailService.lambdaQuery()
                    .eq(TTaskDetail::getTaskId, tTask.getId())
                    .in(TTaskDetail::getHandleType, Arrays.asList(3,6))
                    .orderByDesc(TTaskDetail::getCreateTime).last("limit 1").one();
            BeanUtils.copyProperties(tTask, taskTodayVO);
            TLocation tLocation = locationList.stream().filter(e -> e.getId().equals(tTask.getLocationId())).findFirst().orElse(null);
            if (tLocation != null) {
@@ -576,9 +563,8 @@
                    taskTodayVO.setLocationTypeIcon(tLocationType.getLocationIcon());
                }
            }
            TTaskDetail tTaskDetail = tTaskDetails.stream().filter(e -> e.getTaskId().equals(tTask.getId())).findFirst().orElse(null);
            if (tTaskDetail!=null){
                taskTodayVO.setRemark(tTaskDetail.getAuditRemark());
            if (taskDetailsStatus1!=null){
                taskTodayVO.setRemark(taskDetailsStatus1.getAuditRemark());
            }
            todayTask.add(taskTodayVO);
@@ -594,7 +580,52 @@
            tomorrowTask.add(taskTomorrowVO);
        }
        Collections.shuffle(tomorrowTask);
        res.setTomorrowTask(tomorrowTask);
        List<TTemplate> templates = templateService.list();
        TTemplate tTemplate = templates.stream().filter(e -> e.getId().equals(sysUser.getTemplateId())).findFirst()
                .orElse(null);
            if (tTemplate!=null){
                List<TTask> inspection = createInspection(tTemplate, userId);
                List<String> collect1 = inspection.stream().map(TTask::getId).collect(Collectors.toList());
                if (collect1.isEmpty()){
                    collect1.add("0");
                }
                List<TTask> temp =new ArrayList<>();
                if (!StringUtils.hasLength(dto.getLon())){
                    temp = inspection;
                }else{
                    temp = taskCleanerService.getTaskByIds(collect1,dto.getLon(),dto.getLat());
                }
                List<TaskTomorrowVO> taskTomorrowVOS = new ArrayList<>();
                for (TTask tTask : temp) {
                    TaskTomorrowVO taskTomorrowVO = new TaskTomorrowVO();
                    BeanUtils.copyProperties(tTask, taskTomorrowVO);
                    TLocation tLocation = locationList.stream().filter(e -> e.getId().equals(tTask.getLocationId())).findFirst().orElse(null);
                    if (tLocation!=null){
                        TLocationType tLocationType = locationTypeList.stream().filter(e -> e.getId().equals(tLocation.getLocationType())).findFirst().orElse(null);
                        if (tLocationType!=null){
                            taskTomorrowVO.setLocationId(tLocation.getId());
                            taskTomorrowVO.setLocationName(tLocation.getLocationName());
                            taskTomorrowVO.setLocationTypeName(tLocationType.getLocationName());
                            taskTomorrowVO.setLocationTypeIcon(tLocationType.getLocationIcon());
                            taskTomorrowVO.setLocationAddress(tLocation.getLocationAddress());
                            taskTomorrowVO.setLocationLon(tLocation.getLocationLon());
                            taskTomorrowVO.setLocationLat(tLocation.getLocationLat());
                            if (StringUtils.hasLength(dto.getLon())){
                                taskTomorrowVO.setDistance(tTask.getDistance().divide(new BigDecimal("1000"),2, RoundingMode.HALF_DOWN));
                            }else {
                                taskTomorrowVO.setDistance(new BigDecimal("0"));
                            }
                        }
                    }
                    taskTomorrowVOS.add(taskTomorrowVO);
                }
                res.setTomorrowTask(taskTomorrowVOS);
            }else{
                res.setTomorrowTask(new ArrayList<>());
            }
        res.setIsLeave(0);
        // 查询请假状态
        List<TLeave> list2 = leaveService.lambdaQuery().eq(TLeave::getLeavePerson, userId)
@@ -612,6 +643,290 @@
        }
        return R.ok(res);
    }
    @Resource
    private TTemplateService templateService;
    // 项目部权重标识
    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;
    @Autowired
    private TTemplateDetailService templateDetailService;
    public List<TTask> createInspection(TTemplate template,Long userId) {
        // 查询所有的模板详情
        List<TTemplateDetail> list = templateDetailService.list(Wrappers.lambdaQuery(TTemplateDetail.class)
                .eq(TTemplateDetail::getTemplateId, template.getId()));
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<TTask> taskAll = new ArrayList<>();
        // 通过模板id查询员工巡检员
        List<SysUser> sysUsers = sysUserService.selectUserByTempLateId(template.getId());
        sysUsers = sysUsers.stream().filter(e->e.getUserId().equals(userId)).collect(Collectors.toList());
        // 创建任务
        for (TTemplateDetail detail : list) {
            // 计算周期
            int cycle = detail.getCycle();
            switch (detail.getCycleType()){
                case 2:
                    cycle = cycle * 7;
                    break;
                case 3:
                    cycle = cycle * 30;
                    break;
                case 4:
                    cycle = cycle * 90;
                    break;
                case 5:
                    cycle = cycle * 365;
                    break;
            }
            // 拿到保洁抽查次数
            int num1 = detail.getNum1();
            // 拿到项目部数
            int num2 = detail.getNum2();
            // 拿到每日重复点位
            int num3 = detail.getNum3();
            // 获取点位类型的占比
            String num4 = detail.getNum4();
            // 未绑定员工
            if(CollectionUtils.isEmpty(sysUsers)){
                continue;
            }
            int taskCount = 0;
            for (SysUser sysUser : sysUsers) {
                if("1".equals(sysUser.getStatus())){
                    continue;
                }
                List<TProjectDept> projectDeptLists = new ArrayList<>();
                if(sysUser.getDeptType() == 1){
                    TProjectDept projectDept = projectDeptService.getById(sysUser.getDeptId());
                    if(projectDept.getStatus() == 1){
                        if("0".equals(projectDept.getParentId())){
                            projectDeptLists = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                                    .eq(TProjectDept::getParentId, projectDept.getId())
                                    .eq(TProjectDept::getStatus, 1));
                        }else {
                            projectDeptLists.add(projectDept);
                        }
                    }
                }else {
                    projectDeptLists = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                            .ne(TProjectDept::getParentId,0)
                            .eq(TProjectDept::getStatus, 1));
                }
                if(CollectionUtils.isEmpty(projectDeptLists)){
                    continue;
                }
                List<String> proDeptIds = projectDeptLists.stream().map(TProjectDept::getId).collect(Collectors.toList());
                // 获取项目部在该模板详情中的权重
                List<String> projectDeptIds = redisCache.getCacheList(detail.getId() + PROJECT_DEPT_WEIGHT);
                // 获取项目部列表
                List<TProjectDept> projectDeptList;
                if(CollectionUtils.isEmpty(projectDeptIds)){
                    projectDeptList = projectDeptLists;
                }else {
                    projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                            .ne(TProjectDept::getParentId,0)
                            .in(TProjectDept::getId, proDeptIds)
                            .notIn(TProjectDept::getId, projectDeptIds));
                    // 所过所有的项目部都被抽取了,则重新抽取,并且清空项目部权重
                    if(CollectionUtils.isEmpty(projectDeptList)){
                        projectDeptList = projectDeptLists;
                    }
                }
                // 如果可抽取的项目部数不足,先抽取余下项目部后,再清空权重,重新抽取
                List<TProjectDept> projectDepts = randomSelection(projectDeptList, num2);
                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)
                            .ne(TProjectDept::getParentId,0));
                    List<TProjectDept> projectDepts1 = randomSelection(projectDeptList1, num2 - projectDepts.size());
                    List<String> proIds1 = projectDepts1.stream().map(TProjectDept::getId).collect(Collectors.toList());
                    // 将已抽取的项目部id保存到redis中
                    projectDepts.addAll(projectDepts1);
                }else {
                    List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                    // 将已抽取的项目部id保存到redis中
                }
                List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                // 拿到抽取的项目部下的所有保洁员
//            List<TProjectDept> tProjectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
//                    .in(TProjectDept::getId, proIds));
                // 获取片区id
                List<String> areaIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                if(CollectionUtils.isEmpty(areaIds)){
                    return new ArrayList<>();
                }
                // 计算每天需要抽取多少个保洁员
                long count = cleanerService.count(Wrappers.lambdaQuery(TCleaner.class)
                        .in(TCleaner::getProjectId, areaIds));
                int cleanerSums = num1 * Integer.parseInt(count + "");
                // 获取每天需要抽取的保洁员数,向上取整
                if (cleanerSums < cycle){
                    cleanerSums = cycle;
                }
                int dayCleanerCount = cleanerSums / cycle;
                if((cycle == detail.getCurrentValue()) && cleanerSums % cycle != 0){
                    dayCleanerCount++;
                }
                // 获取保洁员权重
                List<String> cleanerIds = redisCache.getCacheList(detail.getId() + CLEANER_WEIGHT);
                // 获取保洁员列表
                List<TCleaner> cleaners;
                if(CollectionUtils.isEmpty(cleanerIds)){
                    cleaners = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                            .in(TCleaner::getProjectId, areaIds));
                }else {
                    cleaners = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                            .in(TCleaner::getProjectId, areaIds)
                            .notIn(TCleaner::getId, cleanerIds));
                    if(CollectionUtils.isEmpty(cleaners)){
                        cleaners = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                                .in(TCleaner::getProjectId, areaIds));
                    }
                }
                // 抽取保洁员
                List<TCleaner> tCleaners = randomSelection(cleaners, dayCleanerCount);
                if(CollectionUtils.isEmpty(tCleaners)){
                    continue;
                }
                if(tCleaners.size() < dayCleanerCount){
                    List<String> cleanIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                    List<TCleaner> cleaners1 = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                            .in(TCleaner::getProjectId, areaIds)
                            .notIn(TCleaner::getId, cleanIds));
                    List<TCleaner> tCleaners1 = randomSelection(cleaners1, dayCleanerCount - tCleaners.size());
                    List<String> cleanIds1 = tCleaners1.stream().map(TCleaner::getId).collect(Collectors.toList());
                    // 将已抽取的保洁员id保存到redis中
                    cleanIds1.addAll(cleanIds);
                    tCleaners.addAll(tCleaners1);
                }else {
                    List<String> cleanIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                }
                // 通过保洁员id查询点位
                List<String> cleanersIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                List<TLocation> locationList = locationService.list(Wrappers.lambdaQuery(TLocation.class)
                        .in(TLocation::getLocationCleaner, cleanersIds));
                // 查询点位类型
                JSONArray jsonArray = JSONObject.parseArray(num4);
                List<TLocation> tLocationList = new ArrayList<>();
                for (Object o : jsonArray) {
                    JSONObject jsonObject = JSONObject.parseObject(o.toString());
                    String id = jsonObject.getString("id");
                    BigDecimal value = jsonObject.getBigDecimal("value");
                    List<TLocation> locations = locationList.stream().filter(tLocation -> tLocation.getLocationType().equals(id)).collect(Collectors.toList());
                    if(!CollectionUtils.isEmpty(locations)){
                        BigDecimal bigDecimal = new BigDecimal(locations.size()).multiply(value.divide(new BigDecimal(100), 2, BigDecimal.ROUND_DOWN)).setScale(0, BigDecimal.ROUND_UP);
                        int locationCount = bigDecimal.intValue();
                        if(locationCount > 0){
                            List<TLocation> tLocations = randomSelection(locations, locationCount);
                            tLocationList.addAll(tLocations);
                        }
                    }
                }
                tLocationList = tLocationList.stream().distinct().collect(Collectors.toList());
                // 抽取重复点位
                Integer currentValue = detail.getCurrentValue();
                if(currentValue != cycle){
                    // 周期天数加一
                    detail.setCurrentValue(currentValue + 1);
                    // 获取重复点位
                    Set<TLocation> repeatLocation = redisCache.getCacheSet(detail.getId() + ":" + sysUser.getUserId() + REPEAT_LOCATION);
                    if(!CollectionUtils.isEmpty(repeatLocation)){
                        List<TLocation> locations = randomSelection(new ArrayList<>(tLocationList), num3);
                        tLocationList.addAll(locations);
                    }
                    Set<TLocation> locationSet = new HashSet<>(tLocationList);
                }else {
                    // 设置当前周期为0
                    detail.setCurrentValue(0);
                }
                // 查询请假记录
                TLeave leave = leaveService.getOne(Wrappers.lambdaQuery(TLeave.class)
                        .eq(TLeave::getLeavePerson, sysUser.getUserId())
                        .eq(TLeave::getAuditStatus, 2)
                        .orderByDesc(TLeave::getCreateTime)
                        .last("LIMIT 1"));
                // 创建任务
                List<TTask> tasks = new ArrayList<>();
                for (TLocation tLocation : tLocationList) {
                    TTask task = new TTask();
                    // 获取保洁员
                    tCleaners.stream().filter(tCleaner -> tCleaner.getId().equals(tLocation.getLocationCleaner())).findFirst().ifPresent(tCleaner -> {
                        task.setProjectId(tCleaner.getProjectId());
                        task.setCleanerId(tCleaner.getId());
                    });
                    // 获取巡检员
                    task.setPatrolInspector(sysUser.getUserId().toString());
                    task.setPatrolInspectorDept(sysUser.getDeptId());
                    task.setUserId(sysUser.getUserId());
                    task.setStatus(1);
                    task.setLocationId(tLocation.getId());
                    // 判断今天是否在请假时间内
                    if(Objects.nonNull(leave) && ((leave.getStartTime().toLocalDate().isBefore(LocalDate.now().plusDays(1)) && leave.getEndTime().toLocalDate().isAfter(LocalDate.now().plusDays(1)))
                            || (leave.getStartTime().toLocalDate().isEqual(LocalDate.now().plusDays(1)) || leave.getEndTime().toLocalDate().isEqual(LocalDate.now().plusDays(1))))){
                        task.setImplementTime(leave.getEndTime().plusDays(1));
                    }else {
                        task.setImplementTime(LocalDateTime.now().plusDays(1));
                    }
                    task.setTaskType(1);
                    task.setTemplateId(detail.getTemplateId());
                    String nameAndCode = CodeGenerateUtils.generateVolumeSn();
                    task.setTaskName(nameAndCode);
                    task.setTaskCode(nameAndCode);
                    task.setDisabled(true);
                    tasks.add(task);
                }
                // 添加应生成任务数量
                TTemplateCount templateCount = new TTemplateCount();
                templateCount.setTemplateId(detail.getTemplateId());
                // 查询所有的保洁员下面的点位
                List<String> cleanIds = cleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                List<TLocation> tLocations = locationService.list(Wrappers.lambdaQuery(TLocation.class)
                        .in(TLocation::getLocationCleaner, cleanIds));
                templateCount.setTaskCount(tLocations.size());
                templateCount.setUserId(sysUser.getUserId());
//                templateCountService.save(templateCount);
                taskAll.addAll(tasks);
                taskCleanerService.saveBatch(tasks);
                taskCount = taskCount + tasks.size();
            }
            template.setTaskCount(taskCount);
//            templateService.updateById(template);
//            templateDetailService.updateById(detail);
        }
        return taskAll;
    }
    @Autowired
    private TTemplateCountService templateCountService;
    @Autowired
    private TCleanerService cleanerService;
    public static void sortTodayTasks(List<TaskTodayVO> taskList) {
        Map<Integer, Integer> statusOrder = new HashMap<>();
        statusOrder.put(1, 0);
@@ -623,6 +938,16 @@
        taskList.sort(Comparator.comparingInt((TaskTodayVO o) -> statusOrder.getOrDefault(o.getStatus(), Integer.MAX_VALUE)).thenComparing(TaskTodayVO::getDistance));
    }
    private static <T> List<T> randomSelection(List<T> list, int size) {
        List<T> selected = new ArrayList<>();
        int length = list.size();
        for (int i = 0; i < size; i++) {
            Collections.shuffle(list);
            // 如果需求的大小超过了集合的长度,则只能取到集合的全部元素
            int subListSize = Math.min(size - selected.size(), length);
            selected.addAll(list.subList(0, subListSize));
        }
        return selected;
    }
}