yaozc2021
2025-04-25 c19eda1fb09764cfa1ae134bda39efd9507784aa
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/attendance/service/impl/AttendanceLeaveServiceImpl.java
@@ -17,6 +17,11 @@
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.approval.domain.ApproveEvent;
import me.zhengjie.modules.approval.repository.ApproveEventRepository;
import me.zhengjie.modules.approval.service.TemplateControlFieldService;
import me.zhengjie.modules.approval.service.TemplateService;
import me.zhengjie.modules.approval.service.mapstruct.ApproveEventMapper;
import me.zhengjie.modules.attendance.domain.*;
import me.zhengjie.modules.attendance.repository.*;
import me.zhengjie.modules.attendance.service.AttendanceLeaveService;
@@ -25,7 +30,6 @@
import me.zhengjie.modules.attendance.service.dto.AttendanceLeaveQueryCriteria;
import me.zhengjie.modules.attendance.service.mapstruct.AttendanceLeaveMapper;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.quartz.service.OpaiDingTalkService;
import me.zhengjie.modules.quartz.service.impl.WeComServiceImpl;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.repository.DictDetailRepository;
@@ -49,11 +53,12 @@
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * @author wcd
* @website https://eladmin.vip
* @description 服务实现
* @author wcd
* @date 2022-12-16
**/
@Service
@@ -66,13 +71,16 @@
    private final AttendanceService attendanceService;
    private final SysUserLevelOtherRepository sysUserLevelOtherRepository;
    private final UserRepository userRepository;
    private final OpaiDingTalkService opaiDingTalkService;
    private final SysConfigHolidayRepository sysConfigHolidayRepository;
    private final AttendanceRepository attendanceRepository;
    private final AttendanceGoOutRepository attendanceGoOutRepository;
    private final AttendanceBusinessTravelRepository attendanceBusinessTravelRepository;
    private final AttendanceOvertimeRepository attendanceOvertimeRepository;
    private final ApproveEventRepository approveEventRepository;
    private final ApproveEventMapper approveEventMapper;
    private final WeComServiceImpl weComService;
    private final TemplateService templateService;
    private final TemplateControlFieldService templateControlFieldService;
    @Override
    public Map<String,Object> queryAll(AttendanceLeaveQueryCriteria criteria, Pageable pageable){
@@ -90,6 +98,13 @@
                //查询审批用户名
                User appUser = userRepository.findById(attendanceLeaveDto.getApprovalUserId()).orElseGet(User::new);
                attendanceLeaveDto.setAppUserName(appUser.getNickName());
                // 查询企微最新审批变化记录
                if (ObjectUtils.isNotEmpty(attendanceLeave.getWecomEventId())) {
                    ApproveEvent event = approveEventRepository.findById(attendanceLeave.getWecomEventId()).orElseGet(ApproveEvent::new);
                    attendanceLeaveDto.setEvent(approveEventMapper.toDto(event));
                }
                return attendanceLeaveDto;
            }
        };
@@ -154,6 +169,7 @@
        }else {
            // 获取当前登陆人信息,设置为请假发起人
            User user = userRepository.getOne(resources.getUserId());
            User approvalUser = userRepository.getOne(resources.getApprovalUserId());
            List<String> holiday = new ArrayList<>(), isWork = new ArrayList<>();
            // 查询节假日
            List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll();
@@ -173,23 +189,44 @@
                Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
                // 不是周末并且节假日表中不存在在创建
                if((!isWeekend && !holiday.contains(date)) || (isWeekend && isWork.contains(date))){
                AttendanceLeave attendanceLeave = getAttendanceLeave(resources,date,user,i,LeaStartTime,LeaEndTime,null);
                i++;
                attendanceLeaveRepository.save(attendanceLeave);
                    AttendanceLeave attendanceLeave = getAttendanceLeave(resources,date,user,i,LeaStartTime,LeaEndTime,null);
                    // 判断是否提交企微审批
                    List<DictDetail> siteTypes = dictDetailRepository.findByDictName(Constant.APPROVAL_SITE_TYPE).stream().filter(siteType -> siteType.getValue().toLowerCase().equals("wecom")).collect(Collectors.toList());
                    if (siteTypes.size() > 0) {
                        // 添加企微申请参数
                        if (resources.getLeaveDurationUnit().equals(Constant.LEAVE_TIME_TYPE_HOUR)) {
                            attendanceLeave.setWecomDateType(ElAdminConstant.Constant.TEMPLATE_DATE_TYPE_HOUR);
                            attendanceLeave.setWecomDuration(String.valueOf(new BigDecimal(attendanceLeave.getLeaveDuration()).multiply(new BigDecimal(3600)).longValue()));
                        } else {
                            attendanceLeave.setWecomDateType(ElAdminConstant.Constant.TEMPLATE_DATE_TYPE_HALFDAY);
                            attendanceLeave.setWecomDuration(String.valueOf(new BigDecimal(attendanceLeave.getLeaveDuration()).multiply(new BigDecimal(86400)).longValue()));
                        }
                        // 添加企微申请参数
                        Map<String, String> leaveTypeMap = dictDetailRepository.findByDictName(Constant.LEAVE_TYPE).stream().collect(Collectors.toMap(DictDetail::getValue, DictDetail::getLabel));
                        attendanceLeave.setWecomOptionKey(templateService.getVacationKey(leaveTypeMap.get(attendanceLeave.getLeaveType())));
                        attendanceLeave.setWecomApproveWecomId(approvalUser.getWeComId());
                        // 提交申请
                        Map<String, String> approvalResultMap = templateControlFieldService.createWecomApproval(attendanceLeave, user.getWeComId());
                        if (approvalResultMap.containsKey("spNo")) {
                            attendanceLeave.setWecomSpNo(approvalResultMap.get("spNo"));
                        } else {
                            throw new BadRequestException(approvalResultMap.containsKey("error") ? approvalResultMap.get("error") : "企微申请提交失败");
                        }
                    }
                    i++;
                    attendanceLeaveRepository.save(attendanceLeave);
                }
            }
            //发送钉钉给审批人
            List<String> sendList = new ArrayList<>();
            //查询审批者信息
            User appUser = userRepository.getOne(resources.getApprovalUserId());
            sendList.add(appUser.getDingding());
            opaiDingTalkService.sendNotice(sendList, "审批提醒", "### 审批提示 \n  >" + user.getNickName() + " 提交了请假审批,请登录PMS系统处理", "card");
            return attendanceLeaveMapper.toDto(resources);
        }
    }
    /**
     * 添加多条记录
     *
     * @param resources
     * @param date
     * @param user
@@ -329,6 +366,7 @@
    /**
     * 验证其他状态的表是否有重复
     *
     * @param startTime
     * @param endTime
     * @param userId
@@ -336,7 +374,7 @@
    public void repeatValidation(String startTime, String endTime, Long userId){
        // 查询外出表
        List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.checkCreateTime(startTime, endTime, userId);
        if(attendanceGoOutList.size() > 0){
        if(attendanceGoOutList.size() > 0) {
            AttendanceGoOut errorSysAskForLeave = attendanceGoOutList.get(0);
            SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日");
            throw new BadRequestException("外出时间冲突," + errorFormat.format(errorSysAskForLeave.getOutStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getOutEndTime())
@@ -355,6 +393,7 @@
    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     * @param endTime
     * @return
@@ -370,7 +409,7 @@
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
            while (startDate.getTime()<=endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
@@ -388,6 +427,7 @@
    /**
     * 计算请假时长
     *
     * @param resources /
     * @return
     * @throws Exception
@@ -451,26 +491,86 @@
    }
    /**
     * 获取用户年假剩余时长
     */
    @Override
    public Double getYearTimeLength(Long userId, String year) {
        //获取年假剩余(新)
        Double surplusYearTime = new Double(0.00);
        // 根据用户查询
        List<SysUserLevelOther> sysUserLevelOtherList = sysUserLevelOtherRepository.findByUserId(userId);
        if (sysUserLevelOtherList.size() > 0) {
            for (SysUserLevelOther sysUserLevelOther : sysUserLevelOtherList) {
                if (!sysUserLevelOther.getYearMonth().isEmpty() && sysUserLevelOther.getYearMonth().substring(0,4).equals(String.valueOf(year))) {
                    //年假总时长
                    Double yearTime = Double.valueOf(sysUserLevelOther.getAnnualLeaveDays() == null ? 0 : sysUserLevelOther.getAnnualLeaveDays() * 8);
                    String startTime = year + "-01-01 00:00:00";
                    String endTime = year + "-12-31 23:59:59";
                    Double usedYearTime = attendanceLeaveRepository.getSurplusYearTime(sysUserLevelOther.getUserId(), startTime, endTime);
                    surplusYearTime = yearTime - (usedYearTime == null ? 0.0 : usedYearTime);
                }
            }
        }
        return surplusYearTime;
    }
    /**
     * 获取用户调休假剩余时长
     */
    @Override
    public Double getOverTimeLength(Long userId) {
        //获取调休假剩余时长
        Double surplusYearTime = new Double(0.00);
        LocalDate localDateStart = LocalDate.now().withDayOfMonth(1).minusMonths(3);
        LocalDate localDateEnd = LocalDate.now().plusDays(1);
        List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay());
        if (!attendanceOvertimeList.isEmpty()) {
            surplusYearTime = attendanceOvertimeList.stream()
                    .filter(attendanceOvertime -> !attendanceOvertime.getDateType().equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS))
                    .map(attendanceOvertime -> attendanceOvertime.getOvertimeDuration()).reduce(0.0, (o1, o2) -> o1 + o2);
            Double usedTime = attendanceLeaveRepository.findByUserIdAndDateAndLeaveType(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay(), Constant.LEAVE_TYPE_REPLACE).stream().mapToDouble(AttendanceLeave::getLeaveDuration).sum();
            if (usedTime > 0) {
                surplusYearTime = surplusYearTime - usedTime;
            }
        LocalDate localDateStart = LocalDate.of(2024, 1, 1);
        LocalDate localDateMiddle = LocalDate.now().withDayOfMonth(1).minusMonths(3);
        Double overTimeLast = attendanceOvertimeRepository.findByUserIdAndYearMonth(userId, localDateStart.atStartOfDay(), localDateMiddle.atStartOfDay()); // TODO
        if (overTimeLast == null) {
            overTimeLast = 0.0;
        }
        LocalDate localDateEnd = LocalDate.now().plusMonths(1).withDayOfMonth(1);
        Double surplusYearTime = attendanceOvertimeRepository.findByUserIdAndYearMonth(userId, localDateMiddle.atStartOfDay(), localDateEnd.atStartOfDay());
        if (surplusYearTime == null) {
            surplusYearTime = 0.0;
        }
        Double usedTime = attendanceLeaveRepository.findByUserIdAndDateAndLeaveType(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay(), Constant.LEAVE_TYPE_REPLACE).stream().mapToDouble(AttendanceLeave::getLeaveDuration).sum();
        if (usedTime == null) {
            usedTime = 0.0;
        }
        if (overTimeLast > usedTime) {
            return surplusYearTime;
        }
        surplusYearTime = surplusYearTime + overTimeLast - usedTime;
        if (surplusYearTime <= 0.0) {
            return 0.0;
        }
        return BigDecimal.valueOf(surplusYearTime).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    @Override
    public Double getOverTimeLength(Long userId, String yearMonth) {
        LocalDate localDateStart = LocalDate.of(2024, 1, 1);
        LocalDate localDateMiddle = LocalDate.parse(yearMonth + "-01").minusMonths(3);
        Double overTimeLast = attendanceOvertimeRepository.findByUserIdAndYearMonth(userId, localDateStart.atStartOfDay(), localDateMiddle.atStartOfDay()); // TODO
        if (overTimeLast == null) {
            overTimeLast = 0.0;
        }
        LocalDate localDateEnd = LocalDate.parse(yearMonth + "-01").plusMonths(1).withDayOfMonth(1);
        Double surplusYearTime = attendanceOvertimeRepository.findByUserIdAndYearMonth(userId, localDateMiddle.atStartOfDay(), localDateEnd.atStartOfDay());
        if (surplusYearTime == null) {
            surplusYearTime = 0.0;
        }
        Double usedTime = attendanceLeaveRepository.findByUserIdAndDateAndLeaveType(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay(), Constant.LEAVE_TYPE_REPLACE).stream().mapToDouble(AttendanceLeave::getLeaveDuration).sum();
        if (usedTime == null) {
            usedTime = 0.0;
        }
        if (overTimeLast > usedTime) {
            return surplusYearTime;
        }
        surplusYearTime = surplusYearTime + overTimeLast - usedTime;
        if (surplusYearTime <= 0.0) {
            return 0.0;
        }
@@ -482,31 +582,47 @@
     */
    @Override
    public Double getOverTimeCloseLength(Long userId) {
        //获取年假剩余(新)
        Double surplusYearTime = new Double(0.00);
        LocalDate localDateStart = LocalDate.now().withDayOfMonth(1).minusMonths(4);
        LocalDate localDateEnd = LocalDate.now().plusDays(1);
        List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(userId, localDateStart.atStartOfDay(), localDateStart.plusMonths(1).withDayOfMonth(1).atStartOfDay());
        if (!attendanceOvertimeList.isEmpty()) {
            surplusYearTime = attendanceOvertimeList.stream()
                    .filter(attendanceOvertime -> !attendanceOvertime.getDateType().equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS))
                    .map(attendanceOvertime -> attendanceOvertime.getOvertimeDuration()).reduce(0.0, (o1, o2) -> o1 + o2);
            Double usedTime = attendanceLeaveRepository.findByUserIdAndDateAndLeaveType(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay(), Constant.LEAVE_TYPE_REPLACE).stream().mapToDouble(AttendanceLeave::getLeaveDuration).sum();
            if (usedTime > 0) {
                surplusYearTime = surplusYearTime - usedTime;
            }
        LocalDate localDateStart = LocalDate.of(2024, 1, 1);
        LocalDate localDateMiddle = LocalDate.now().withDayOfMonth(1).minusMonths(3);
        Double surplusYearTime = attendanceOvertimeRepository.findByUserIdAndYearMonth(userId, localDateStart.atStartOfDay(), localDateMiddle.atStartOfDay()); // TODO
        if (surplusYearTime == null) {
            surplusYearTime = 0.0;
        }
        if (surplusYearTime <= 0.0) {
        LocalDate localDateEnd = LocalDate.now().plusMonths(1).withDayOfMonth(1);
        Double usedTime = attendanceLeaveRepository.findByUserIdAndDateAndLeaveType(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay(), Constant.LEAVE_TYPE_REPLACE).stream().mapToDouble(AttendanceLeave::getLeaveDuration).sum();
        if (usedTime == null) {
            usedTime = 0.0;
        }
        if (surplusYearTime <= usedTime) {
            return 0.0;
        }
        surplusYearTime = surplusYearTime - usedTime;
        return BigDecimal.valueOf(surplusYearTime).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    @Override
    public Double getOverTimeCloseLength(Long userId, String yearMonth) {
        LocalDate localDateStart = LocalDate.of(2024, 1, 1);
        LocalDate localDateMiddle = LocalDate.parse(yearMonth + "-01").minusMonths(3);
        Double surplusYearTime = attendanceOvertimeRepository.findByUserIdAndYearMonth(userId, localDateStart.atStartOfDay(), localDateMiddle.atStartOfDay()); // TODO
        if (surplusYearTime == null) {
            surplusYearTime = 0.0;
        }
        LocalDate localDateEnd = LocalDate.parse(yearMonth + "-01").plusMonths(1).withDayOfMonth(1);
        Double usedTime = attendanceLeaveRepository.findByUserIdAndDateAndLeaveType(userId, localDateStart.atStartOfDay(), localDateEnd.atStartOfDay(), Constant.LEAVE_TYPE_REPLACE).stream().mapToDouble(AttendanceLeave::getLeaveDuration).sum();
        if (usedTime == null) {
            usedTime = 0.0;
        }
        if (surplusYearTime <= usedTime) {
            return 0.0;
        }
        surplusYearTime = surplusYearTime - usedTime;
        return BigDecimal.valueOf(surplusYearTime).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     * 审核功能
     *
     * @param resources /
     */
    @Override
@@ -555,6 +671,7 @@
    /**
     * 批量审核
     *
     * @param request
     */
    @Override