xuhy
2 天以前 653cb0ed3e7be231059480cee266b11f1c9ba11e
ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/TIndexController.java
@@ -29,6 +29,7 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
@@ -141,7 +142,7 @@
            }
        } else {
            // 根据当前登录人查询部门
            Long userId = tokenService.getLoginUser().getUserId();
            Long userId = tokenService.getLoginUserApplet().getUserId();
            SysUser sysUser = sysUserService.selectUserById(userId);
            if (sysUser.getDeptType() == 1) {
                TProjectDept projectDept = projectDeptService.getById(sysUser.getDeptId());
@@ -150,8 +151,7 @@
                        tTaskLambdaQueryWrapper.eq(TTask::getProjectId, projectDept.getId());
                        userDeptIds.add(projectDept.getId());
                    } else {
                        TProjectDept parent = projectDeptService.getById(projectDept.getParentId());
                        List<TProjectDept> list = projectDeptService.lambdaQuery().eq(TProjectDept::getParentId, parent.getId()).list();
                        List<TProjectDept> list = projectDeptService.lambdaQuery().eq(TProjectDept::getParentId, projectDept.getId()).list();
                        List<String> deptIds = list.stream().map(TProjectDept::getId).collect(Collectors.toList());
                        if (!deptIds.isEmpty()) {
                            tTaskLambdaQueryWrapper.in(TTask::getProjectId, deptIds);
@@ -180,8 +180,19 @@
        List<TTask> tasks = taskCleanerService.list(tTaskLambdaQueryWrapper);
        HashMap<String, Integer> taskMap = new HashMap<>();
        HashMap<String, Integer> statusMap = new HashMap<>();
        statusMap.put("未执行", 0);
        statusMap.put("超时", 0);
        statusMap.put("待确认", 0);
        statusMap.put("已驳回", 0);
        statusMap.put("已完成", 0);
        HashMap<String, Integer> clearMap = new HashMap<>();
        clearMap.put("合格", 0);
        clearMap.put("不合格", 0);
        HashMap<String, Integer> unqualifiedMap = new HashMap<>();
        List<TDictData> dictData = dataList.stream().filter(e -> e.getDataType() == 2).collect(Collectors.toList());
        for (TDictData dictDatum : dictData) {
            unqualifiedMap.put(dictDatum.getDataContent(), 0);
        }
        for (TTask task : tasks) {
            TProjectDept tProjectDept = projectDepts.stream().filter(e -> e.getId().equals(task.getProjectId())).findFirst().orElse(null);
            if (tProjectDept == null) continue;
@@ -220,7 +231,7 @@
                        temp1.append("不合格");
                        TDictData tDictData = dataList.stream().filter(e -> e.getId().equals(tTaskDetail.getUnqualified())).findFirst().orElse(null);
                        if (tDictData != null){
                            unqualifiedMap.put(tTaskDetail.getUnqualifiedName(), unqualifiedMap.getOrDefault(tDictData.getDataContent(), 0) + 1);
                            unqualifiedMap.put(tDictData.getDataContent(), unqualifiedMap.getOrDefault(tDictData.getDataContent(), 0) + 1);
                        }
                        break;
                }
@@ -289,15 +300,21 @@
        }
        return R.ok(projectDeptListNoLimitVOS);
    }
@Resource
private TNoticeService noticeService;
    @ApiOperation(value = "首页")
    @PostMapping(value = "/index")
    public R<IndexVO> index(@RequestBody IndexDTO dto) throws IOException {
        if (!StringUtils.hasLength(dto.getLon()) || !StringUtils.hasLength(dto.getLat())) {
            return R.fail("请上传经纬度");
        }
//        if (!StringUtils.hasLength(dto.getLon()) || !StringUtils.hasLength(dto.getLat())) {
//            return R.fail("请上传经纬度");
//        }
        IndexVO res = new IndexVO();
        Long userId = tokenService.getLoginUser().getUserId();
        res.setIsNotice(0);
        Long userId = tokenService.getLoginUserApplet().getUserId();
        Long count = noticeService.lambdaQuery().eq(TNotice::getUserId, userId).eq(TNotice::getStatus, 1).count();
        if (count>0){
            res.setIsNotice(1);
        }
        List<TLocation> locationList = locationService.list();
        List<TLocationType> locationTypeList = locationTypeService.list();
        List<TTask> taskAll = taskCleanerService.lambdaQuery().eq(TTask::getPatrolInspector, userId).list();
@@ -318,7 +335,7 @@
                TProjectDept projectDept = projectDeptService.getById(sysUser.getDeptId());
                if (projectDept != null) {
                    if (!"0".equals(projectDept.getParentId())) {
                    if ("0".equals(projectDept.getParentId())) {
                        // 查询片区下的所有人员
                        users = sysUsers.stream().filter(e -> e.getDeptId()
                                .equals(projectDept.getId())
@@ -345,21 +362,22 @@
            } else {
                TDept dept = deptService.getById(sysUser.getDeptId());
                if (dept != null) {
                    if (!dept.getDeptName().equals("公司")) {
                        users = sysUsers.stream().filter(e -> e.getDeptId()
                    if (!dept.getDeptName().contains("公司")) {
                        users = sysUsers.stream().filter(e -> (e.getDeptId()
                                .equals(sysUser.getDeptId())
                                && e.getStatus().equals("0")
                                && e.getDeptType() == 1).collect(Collectors.toList());
                                && e.getStatus().equals("0"))
                                || e.getDeptType() == 1).collect(Collectors.toList());
                    }
                }
            }
        }
        List<TaskTodayVO> pendingTask = new ArrayList<>();
        List<TaskPendingVO> pendingTask = new ArrayList<>();
        res.setTotalUserCount(users.size());
        if (!users.isEmpty()) {
            List<TTask> tasks = taskCleanerService.lambdaQuery().in(TTask::getPatrolInspector, users).list();
            List<Long> userIds = users.stream().map(SysUser::getUserId).collect(Collectors.toList());
            List<TTask> tasks = taskCleanerService.lambdaQuery().in(TTask::getPatrolInspector, userIds).list();
            List<String> taskIds = tasks.stream().map(TTask::getId).collect(Collectors.toList());
            if (!tasks.isEmpty()) {
                List<TTask> status1 = tasks.stream().filter(e -> e.getStatus() == 5 || e.getStatus() == 6).collect(Collectors.toList());
@@ -377,9 +395,9 @@
                List<TTaskDetail> status3 = taskDetails.stream().filter(e -> e.getClearStatus() == 1).collect(Collectors.toList());
                List<TTaskDetail> status4 = taskDetails.stream().filter(e -> e.getClearStatus() == 2).collect(Collectors.toList());
                if (status3.size() + status4.size() != 0) {
                    BigDecimal divide = new BigDecimal(status3.size() + status4.size())
                            .divide(new BigDecimal(status3.size()), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
                if (!status3.isEmpty()) {
                    BigDecimal divide = new BigDecimal(status3.size() )
                            .divide(new BigDecimal(status3.size()+ status4.size()), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
                    res.setRate(divide);
                } else {
                    res.setRate(new BigDecimal(0));
@@ -410,6 +428,7 @@
            leaveUserListVO.setCreateTime1(tLeave.getCreateTime());
            leaveUserListVO.setStartTime1(tLeave.getStartTime());
            leaveUserListVO.setEndTime1(tLeave.getEndTime());
            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());
@@ -419,7 +438,7 @@
                    .eq(TTaskDetail::getClearStatus, 2)
                    .groupBy(TTaskDetail::getTaskId)
                    .orderByDesc(TTaskDetail::getCreateTime).list();
            TaskTodayVO taskTodayVO = new TaskTodayVO();
            TaskPendingVO taskTodayVO = new TaskPendingVO();
            BeanUtils.copyProperties(tTask, taskTodayVO);
            TLocation tLocation = locationList.stream().filter(e -> e.getId().equals(tTask.getLocationId())).findFirst().orElse(null);
            if (tLocation != null) {
@@ -427,11 +446,14 @@
                taskTodayVO.setLocationLon(tLocation.getLocationLon());
                taskTodayVO.setLocationLat(tLocation.getLocationLat());
                taskTodayVO.setLocationName(tLocation.getLocationName());
                // todo
                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 {
                if(StringUtils.hasLength(dto.getLon())){
                    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"));
                }
                TLocationType tLocationType = locationTypeList.stream().filter(e -> e.getId().equals(tLocation.getLocationType())).findFirst().orElse(null);
@@ -450,13 +472,19 @@
            }
            pendingTask.add(taskTodayVO);
        }
        // 将pendingTask按照距离 从小到大排序
        pendingTask.sort(Comparator.comparing(TaskPendingVO::getDistance));
        res.setPendingTask(pendingTask);
        res.setLeaveList(leaveList);
        List<TaskTodayVO> todayTask = new ArrayList<>();
        List<String> collect = taskAll.stream().map(TTask::getId).collect(Collectors.toList());
        for (TTask tTask : taskToday) {
            TaskTodayVO taskTodayVO = new TaskTodayVO();
            if(CollectionUtils.isEmpty(collect)){
                break;
            }
            List<TTaskDetail> taskDetailsStatus1 = taskDetailService.lambdaQuery()
                    .eq(TTaskDetail::getAuditStatus, 1).in(TTaskDetail::getTaskId, taskIds)
                    .eq(TTaskDetail::getAuditStatus, 1).in(TTaskDetail::getTaskId, collect)
                    .eq(TTaskDetail::getClearStatus, 2)
                    .groupBy(TTaskDetail::getTaskId)
                    .orderByDesc(TTaskDetail::getCreateTime).list();
@@ -467,10 +495,14 @@
                taskTodayVO.setLocationLon(tLocation.getLocationLon());
                taskTodayVO.setLocationLat(tLocation.getLocationLat());
                taskTodayVO.setLocationName(tLocation.getLocationName());
                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 {
                if(StringUtils.hasLength(dto.getLon())){
                    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"));
                }
                TLocationType tLocationType = locationTypeList.stream().filter(e -> e.getId().equals(tLocation.getLocationType())).findFirst().orElse(null);
@@ -489,11 +521,46 @@
            }
            todayTask.add(taskTodayVO);
        }
        // 将todayTask进行排序 优先根据status状态排序,优先展示未执行、超时、待整改、待确认、整改完成、已完成,然后状态一样再根据距离排序
        sortTodayTasks(todayTask);
        res.setTodayTask(todayTask);
        List<TaskTodayVO> tomorrowTask = new ArrayList<>(todayTask);
        List<TaskTomorrowVO> tomorrowTask = new ArrayList<>();
        for (TaskTodayVO taskTodayVO : todayTask) {
            TaskTomorrowVO taskTomorrowVO = new TaskTomorrowVO();
            BeanUtils.copyProperties(taskTodayVO, taskTomorrowVO);
            tomorrowTask.add(taskTomorrowVO);
        }
        Collections.shuffle(tomorrowTask);
        res.setTomorrowTask(tomorrowTask);
        res.setIsLeave(0);
        // 查询请假状态
        List<TLeave> list2 = leaveService.lambdaQuery().eq(TLeave::getLeavePerson, userId)
                .eq(TLeave::getAuditStatus, 2)
                .list();
        for (TLeave tLeave : list2) {
            LocalDate now = LocalDate.now();
            LocalDate startDate = tLeave.getStartTime().toLocalDate();
            LocalDate endDate = tLeave.getStartTime().toLocalDate();
            if (now.isAfter(startDate) && now.isBefore(endDate)) {
                res.setIsLeave(1);
            } else if (now.isEqual(startDate) || now.isEqual(endDate)) {
                res.setIsLeave(1);
            }
        }
        return R.ok(res);
    }
    public static void sortTodayTasks(List<TaskTodayVO> taskList) {
        Map<Integer, Integer> statusOrder = new HashMap<>();
        statusOrder.put(1, 0);
        statusOrder.put(2, 1);
        statusOrder.put(4, 2);
        statusOrder.put(3, 3);
        statusOrder.put(5, 4);
        statusOrder.put(6, 5);
        taskList.sort(Comparator.comparingInt((TaskTodayVO o) -> statusOrder.getOrDefault(o.getStatus(), Integer.MAX_VALUE)).thenComparing(TaskTodayVO::getDistance));
    }
}