weicd
2023-11-07 d38392f1bc5b437feaec39da315926e1f5bdccf2
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java
@@ -42,6 +42,7 @@
import me.zhengjie.modules.task.domain.TaskDaliy;
import me.zhengjie.modules.task.domain.TaskDaliyMongo;
import me.zhengjie.modules.task.domain.TaskMongo;
import me.zhengjie.modules.task.domain.TaskUserMongo;
import me.zhengjie.modules.task.repository.TaskDaliyRepository;
import me.zhengjie.modules.task.service.TaskDaliyService;
import me.zhengjie.modules.task.service.dto.TaskDaliyQueryCriteria;
@@ -51,7 +52,9 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
@@ -63,7 +66,9 @@
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
@@ -150,6 +155,7 @@
        String calendarStr = sdf.format(calendar.getTime());
        TaskMongo taskMongo = mongoTemplate.findById(taskId, TaskMongo.class);
        List<Long> containUserList = taskMongo.getContainUserList();
        List<User> userList = new ArrayList<>();
        if (containUserList.size() != 0) {
@@ -173,6 +179,60 @@
            }
        }
        return userDto;
    }
    @Override
    public List<UserDto> getAllUserByTaskId(String taskId) {
        // 日历类
        Calendar calendar = Calendar.getInstance();
        // 日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 日历字符串
        String calendarStr = sdf.format(calendar.getTime());
        TaskMongo taskMongo = mongoTemplate.findById(taskId, TaskMongo.class);
        List<Long> containUserList = taskMongo.getContainUserList();
        //递归添加
        addSubTaskUserList(taskMongo, containUserList);
        containUserList = containUserList.stream().distinct().collect(Collectors.toList());
        List<User> userList = new ArrayList<>();
        if (containUserList.size() != 0) {
            for (Long userId : containUserList) {
                userList.add(userRepository.findById(userId).get());
            }
        }
        List<UserDto> userDto = userMapper.toDto(userList);
        for (int i = 0; i < userDto.size(); i++) {
            // 根据用户ID、指定年月查询用户级别工资
            SysUserLevelWage sysUserLevelWage = sysUserLevelWageRepository.findTargetYearMonth(userDto.get(i).getId(), calendarStr);
            // 判断用户级别工资是否为空
            if (sysUserLevelWage != null) {
                // 根据ID查询用户级别
                SysLevel sysLevel = sysLevelRepository.findById(sysUserLevelWage.getLevelId()).orElseGet(SysLevel::new);
                // 用户
                userDto.get(i).setLevelName(sysLevel.getName());
            } else {
                // 用户
                userDto.get(i).setLevelName(null);
            }
        }
        return userDto;
    }
    /**
     * 任务下的所有子级
     * @param taskMongo
     * @param containUserList
     */
    public void addSubTaskUserList(TaskMongo taskMongo, List<Long> containUserList) {
        List<TaskMongo> subTasks = mongoTemplate.find(Query.query(Criteria.where("pid").is(taskMongo.getOld_id())), TaskMongo.class);
        for (TaskMongo subTask : subTasks) {
            containUserList.addAll(subTask.getContainUserList());
            addSubTaskUserList(subTask, containUserList);
        }
    }
    @Override
@@ -292,6 +352,107 @@
        list.add(0, deptId_leaf);
        list = getPidNode(list, deptId_leaf);
        return list.toArray(new Long[list.size()]);
    }
    @Override
    public List<Map> getPerformanceDetail(Long nextMonth) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
//        //本月第一天
//        Calendar c = Calendar.getInstance();
//        c.add(Calendar.MONTH, 0);
//        c.set(Calendar.DAY_OF_MONTH, 1);
//        String todayMonth = df.format(c.getTime());
        String nextMonth_ = df.format(new Date(nextMonth));
        Calendar cal = Calendar.getInstance();
        cal.setTime(df.parse(nextMonth_));
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String nextMonthFirstDay = df.format(cal.getTime());
        List<TaskDaliyMongo> daliyMongoList;
        try {
//            daliyMongoList = mongoTemplate.find(Query.query(new Criteria("userId").is(SecurityUtils.getCurrentUserId()).and("fillDate").gte(new SimpleDateFormat("yyyy-MM-dd").parse(todayMonth)).lte(new SimpleDateFormat("yyyy-MM-dd").parse(nextMonth_))), TaskDaliyMongo.class);
            daliyMongoList = mongoTemplate.find(Query.query(new Criteria("userId").is(SecurityUtils.getCurrentUserId()).and("fillDate").gte(new SimpleDateFormat("yyyy-MM-dd").parse(nextMonth_)).lt(new SimpleDateFormat("yyyy-MM-dd").parse(nextMonthFirstDay))), TaskDaliyMongo.class);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        List<Map> resultList = new ArrayList<>();
        for (TaskDaliyMongo taskDaliyMongo : daliyMongoList) {
            String taskName = taskDaliyMongo.getTaskUserMongo().getTask().getName();
            if (resultList.isEmpty()) {
                Map map = new HashMap();
                map.put("name", taskName);
                if (taskDaliyMongo.getTaskUserMongo().getPerformanceType().equals("1")){
                    map.put("value", taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours());
                }else {
                    map.put("value", taskDaliyMongo.getConsumedHours());
                }
                map.put(taskDaliyMongo.getTaskUserMongo().getName(),taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours());
                resultList.add(map);
                continue;
            }
            findList(resultList, taskDaliyMongo);
        }
        for (Map map : resultList) {
            ArrayList<Map> list = new ArrayList<>();
            for (Object key : map.keySet()) {
                if (!key.equals("name") && !key.equals("value")){
                    Map innerMap = new HashMap();
                    innerMap.put(key,map.get(key));
                    list.add(innerMap);
                }
            }
            map.put("list",list);
        }
        return resultList;
    }
    @Override
    public List<User> getUserInfose() {
        List<User> userList = new ArrayList<>();
        //获取当前登入人信息
        User user1 = userRepository.getOne(SecurityUtils.getCurrentUserId());
        Role role = user1.getRoles().iterator().next();
        //超管、总经理,痛苦哦经理查看全部
        if(Long.valueOf(role.getLevel()) == Constant.ROLE_LEVEL_SUPER || Long.valueOf(role.getLevel()) == Constant.ROLE_LEVEL_GENERAL){
            userList = userRepository.findAllByEnabled(true);
        } else {
            userList = userRepository.findByUserId(user1.getId());
        }
        return userList;
    }
    /*
    方法作用:
    遍历resultList 集合将集合中的元素逐个遍历,如果有元素与当前任务的任务名称相等
    如果任务名称相等,取出当前元素并且将当前任务的消耗工时添加进元素中
     */
    private static void findList(List<Map> resultList, TaskDaliyMongo taskDaliyMongo) {
        boolean flag = false;
        for (Map map : resultList) {
            if (map.get("name").equals(taskDaliyMongo.getTaskUserMongo().getTask().getName())) {
                // 有
                Double value = (Double) map.get("value");
                value += taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours();
                map.put("value", value);
                map.put(taskDaliyMongo.getTaskUserMongo().getName(),taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours());
                flag = true;
            }
        }
        // 无
        if (!flag) {
            Map newMap = new HashMap();
            if (taskDaliyMongo.getTaskUserMongo().getPerformanceType().equals("1")){
                newMap.put("name", taskDaliyMongo.getTaskUserMongo().getTask().getName());
                newMap.put("value", taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours());
                newMap.put(taskDaliyMongo.getTaskUserMongo().getName(),taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours());
            }else {
                newMap.put("name", taskDaliyMongo.getTaskUserMongo().getTask().getName());
                newMap.put("value", taskDaliyMongo.getConsumedHours());
                newMap.put(taskDaliyMongo.getTaskUserMongo().getName(),taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours() ? taskDaliyMongo.getTaskUserMongo().getPerformanceHours() : taskDaliyMongo.getConsumedHours());
            }
            resultList.add(newMap);
        }
    }
    private ArrayList<Long> getPidNode(ArrayList<Long> list, Long deptId_leaf) {
@@ -478,15 +639,15 @@
        //查询日志表
        List<TaskDaliyMongo> taskDaliyList = mongoTemplate.find(Query.query(new Criteria("userId").is(userId).and("fillDate").gte(startDate).lte(endDate)), TaskDaliyMongo.class);
        Map<String,Double> map = new HashMap<>();
        Map<String, Double> map = new HashMap<>();
        for (TaskDaliyMongo taskDaliy : taskDaliyList) {
            if (map.get(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0").format(taskDaliy.getFillDate())) != null) {
                // 如果已经有了
                Double todayHours = map.get(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0").format(taskDaliy.getFillDate()));
                todayHours += taskDaliy.getConsumedHours();
                map.put(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0").format(taskDaliy.getFillDate()),todayHours);
                map.put(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0").format(taskDaliy.getFillDate()), todayHours);
            } else {
                map.put(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0").format(taskDaliy.getFillDate()),taskDaliy.getConsumedHours());
                map.put(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0").format(taskDaliy.getFillDate()), taskDaliy.getConsumedHours());
            }
        }
        for (Map.Entry<String, Double> entry : map.entrySet()) {
@@ -494,48 +655,48 @@
            if (entry.getValue() >= 8) {
                calendarVo.setDate(entry.getKey());
                calendarVo.setIsOk(true);
                calendarVo.setTitle("工时:"+entry.getValue());
                calendarVo.setTitle("工时:" + entry.getValue());
                calendarVo.setType(2);
            } else {
                calendarVo.setDate(entry.getKey());
                calendarVo.setIsOk(false);
                calendarVo.setTitle("已填工时:"+entry.getValue()+" 不足8小时,请补充日报");
                calendarVo.setTitle("已填工时:" + entry.getValue() + " 不足8小时,请补充日报");
                calendarVo.setType(2);
            }
            calendarInfo.add(calendarVo);
        }
            for (String date : dateList) {
                String type1 = "";
                String type2 = "";
                System.out.println(date);
                CalendarVo dailyVo = new CalendarVo();
                if (map.get(date) != null) {
                    // 如果在map中找到了它的值,说明用户填写过日报
                    continue;
                }
                dailyVo.setDate(date);
                for (Attendance attendanceRecord : attendanceList) {
                    type1 = attendanceRecord.getStartClockResults();
                    type2 = attendanceRecord.getEndClockResults();
                    date = date.substring(0, 10);
                    if (date.equals(df.format(attendanceRecord.getAttendanceDate())) && type1.equals(Constant.ATTENDANCE_STATE_LEAVE) && type2.equals(Constant.ATTENDANCE_STATE_LEAVE)) {
                        dailyVo.setTitle("请假");
                        dailyVo.setIsOk(true);
                    } else if (date.equals(df.format(attendanceRecord.getAttendanceDate())) && type1.equals(Constant.ATTENDANCE_STATE_BUSINESS) && type2.equals(Constant.ATTENDANCE_STATE_BUSINESS)) {
                        dailyVo.setTitle("出差");
                        dailyVo.setIsOk(true);
                    } else if (date.equals(df.format(attendanceRecord.getAttendanceDate())) && type1.equals(Constant.ATTENDANCE_STATE_REST) && type2.equals(Constant.ATTENDANCE_STATE_REST)) {
                        dailyVo.setTitle("休息");
                        dailyVo.setIsOk(true);
                    } else if (date.equals(df.format(attendanceRecord.getAttendanceDate()))) {
                        dailyVo.setTitle("日报未填写,请补充日报");
                        dailyVo.setIsOk(false);
                    }
                }
                dailyVo.setType(2);
                calendarInfo.add(dailyVo);
        for (String date : dateList) {
            String type1 = "";
            String type2 = "";
            System.out.println(date);
            CalendarVo dailyVo = new CalendarVo();
            if (map.get(date) != null) {
                // 如果在map中找到了它的值,说明用户填写过日报
                continue;
            }
            dailyVo.setDate(date);
            for (Attendance attendanceRecord : attendanceList) {
                type1 = attendanceRecord.getStartClockResults();
                type2 = attendanceRecord.getEndClockResults();
                date = date.substring(0, 10);
                if (date.equals(df.format(attendanceRecord.getAttendanceDate())) && type1.equals(Constant.ATTENDANCE_STATE_LEAVE) && type2.equals(Constant.ATTENDANCE_STATE_LEAVE)) {
                    dailyVo.setTitle("请假");
                    dailyVo.setIsOk(true);
                } else if (date.equals(df.format(attendanceRecord.getAttendanceDate())) && type1.equals(Constant.ATTENDANCE_STATE_BUSINESS) && type2.equals(Constant.ATTENDANCE_STATE_BUSINESS)) {
                    dailyVo.setTitle("出差");
                    dailyVo.setIsOk(true);
                } else if (date.equals(df.format(attendanceRecord.getAttendanceDate())) && type1.equals(Constant.ATTENDANCE_STATE_REST) && type2.equals(Constant.ATTENDANCE_STATE_REST)) {
                    dailyVo.setTitle("休息");
                    dailyVo.setIsOk(true);
                } else if (date.equals(df.format(attendanceRecord.getAttendanceDate()))) {
                    dailyVo.setTitle("日报未填写,请补充日报");
                    dailyVo.setIsOk(false);
                }
            }
            dailyVo.setType(2);
            calendarInfo.add(dailyVo);
        }
        return calendarInfo;
    }
@@ -720,8 +881,64 @@
        taskDaliyQueryCriteria.setUserId(userId);
        taskDaliyQueryCriteria.setTargetYearMonth(sdfDate.substring(0, 7));
        taskDaliyQueryCriteria.setTargetYearMonth(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yearMonth));
        // 查询某月已完成工时
        Double completedWork = taskDaliyService.queryCompletedWork(taskDaliyQueryCriteria, true);
        Double completedWork = 0D;
//        List<TaskDaliyMongo> daliyMongoList = mongoTemplate.find(Query.query(new Criteria("userId").is(SecurityUtils.getCurrentUserId()).and("fillDate").gte(new SimpleDateFormat("yyyy-MM-dd").parse(startTime)).lte(new SimpleDateFormat("yyyy-MM-dd").parse(endTime))), TaskDaliyMongo.class);
//        for (TaskDaliyMongo taskDaliyMongo : daliyMongoList) {
//            if (taskDaliyMongo.getTaskUserMongo().getPerformanceType().equals("1")) {
//                // 绩效任务
//                if (taskDaliyMongo.getConsumedHours() > taskDaliyMongo.getTaskUserMongo().getPerformanceHours()) {
//                    // 如果当前的绩效任务中,用户填写的日报超出了绩效时长就以绩效任务中绩效时长为准
//                    completedWork += taskDaliyMongo.getTaskUserMongo().getPerformanceHours();
//                } else {
//                    completedWork += taskDaliyMongo.getConsumedHours();
//                }
//                //判断集合中填写的日报是否一个任务填写次数超过1次
//
//            } else {
//                // 非绩效任务
//                completedWork += taskDaliyMongo.getConsumedHours();
//            }
//        }
        DecimalFormat decimalFormat = new DecimalFormat("#");
        decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
        List<TaskDaliyMongo> daliyMongoList = mongoTemplate.find(Query.query(new Criteria("userId").is(SecurityUtils.getCurrentUserId()).and("fillDate").gte(new SimpleDateFormat("yyyy-MM-dd").parse(startTime)).lte(new SimpleDateFormat("yyyy-MM-dd").parse(endTime))), TaskDaliyMongo.class);
        //根据日报中项目分配字段的id去重,并保留日报进度最高的
        Map<Object, TaskDaliyMongo> uniqueMap = daliyMongoList.stream()
                .collect(Collectors.toMap(
                        taskDaliyMongo -> taskDaliyMongo.getTaskUserMongo().getId(),
                        taskDaliyMongo -> taskDaliyMongo,
                        (existing, replacement) -> existing.getTaskProgress() > replacement.getTaskProgress() ? existing : replacement));
        List<TaskDaliyMongo> taskDaliyMongoList = new ArrayList<>(uniqueMap.values());
        for (TaskDaliyMongo taskDaliyMongo : taskDaliyMongoList) {
            //绩效
            if (taskDaliyMongo.getTaskUserMongo().getPerformanceType().equals("1")) {
                // 查询日报表是否存在小于当前月填写的当前任务的日报
                List<TaskDaliyMongo> taskDaliyMongoSm = mongoTemplate.find(Query.query(new Criteria("userId").is(SecurityUtils.getCurrentUserId()).and("old_task_id").is(taskDaliyMongo.getOld_task_id()).and("fillDate").lte(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime))).with(Sort.by(Sort.Direction.DESC, "fillDate")), TaskDaliyMongo.class);
                //跨月
                if(taskDaliyMongoSm.size() > 0) {
                    Double horseValue1 = Double.valueOf(decimalFormat.format((taskDaliyMongoSm.get(0).getTaskProgress() / 100) * taskDaliyMongo.getTaskUserMongo().getPerformanceHours()));
                    horseValue1 = taskDaliyMongo.getTaskUserMongo().getPerformanceHours() - horseValue1;
                    completedWork += horseValue1;
                }else {
                    completedWork += taskDaliyMongo.getTaskUserMongo().getPerformanceHours();
                }
            }
        }
        // 非绩效
        for(TaskDaliyMongo taskDaliyMongo : daliyMongoList) {
            //非绩效
            if (!taskDaliyMongo.getTaskUserMongo().getPerformanceType().equals("1")) {
                completedWork += taskDaliyMongo.getConsumedHours();
            }
        }
        // 月初到今天绩效时长
        returnData.put("achievementsTime", completedWork);