package com.mortals.xhx.module.attendance.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.DataUtil;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.busiz.h5.req.AttendSaveReq;
import com.mortals.xhx.busiz.h5.web.ApiWebPerformController;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery;
import com.mortals.xhx.module.attendance.model.vo.ErrorSummaryVo;
import com.mortals.xhx.module.attendance.service.*;
import com.mortals.xhx.module.check.model.CheckAttendRecordEntity;
import com.mortals.xhx.module.check.model.CheckAttendRecordQuery;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery;
import com.mortals.xhx.module.check.service.CheckAttendRecordService;
import com.mortals.xhx.module.perform.model.PerformAttendRecordEntity;
import com.mortals.xhx.module.perform.model.PerformAttendRecordQuery;
import com.mortals.xhx.module.perform.service.PerformAttendRecordService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.attendance.dao.AttendanceRecordErrorDao;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.mortals.xhx.common.key.RedisKey.KEY_ATTENDANCE_STAT_CACHE;
import static com.mortals.xhx.common.key.RedisKey.KEY_CHECK_SUMMARY_CACHE;

/**
 * AttendanceRecordErrorService
 * 考勤打卡记录异常信息 service实现
 *
 * @author zxfei
 * @date 2023-04-08
 */
@Service("attendanceRecordErrorService")
@Slf4j
public class AttendanceRecordErrorServiceImpl extends AbstractCRUDServiceImpl<AttendanceRecordErrorDao, AttendanceRecordErrorEntity, Long> implements AttendanceRecordErrorService {

    @Autowired
    private AttendanceClassDetailService classDetailService;
    @Autowired
    private PerformAttendRecordService performAttendRecordService;
    @Autowired
    private ICacheService cacheService;
    @Autowired
    private AttendanceLeaveRecordService attendanceLeaveRecordService;
    @Autowired
    private CheckAttendRecordService checkAttendRecordService;


    @Override
    protected void findAfter(AttendanceRecordErrorEntity params, PageInfo pageInfo, Context context, List<AttendanceRecordErrorEntity> list) throws AppException {

        list.forEach(item -> {
            AttendanceClassDetailEntity attendanceClassDetailEntity = classDetailService.get(item.getShiftsId(), context);

            if (!ObjectUtils.isEmpty(attendanceClassDetailEntity)) {
                String str = DateUtil.formatTime(item.getGoOffDateTime());
                String goStr = DateUtil.formatTime(attendanceClassDetailEntity.getGoWorkDate());
                String offStr = DateUtil.formatTime(attendanceClassDetailEntity.getOffWorkDate());

                if (!ObjectUtils.isEmpty(str) && str.equals(goStr)) {
                    item.setGoOrOff("上班");
                }
                if (!ObjectUtils.isEmpty(str) && str.equals(offStr)) {
                    item.setGoOrOff("下班");
                }
                if (ObjectUtils.isEmpty(item.getGoOrOff())) {
                    item.setGoOrOff("");
                }
            } else {
                item.setGoOrOff("");
            }
        });
    }

    @Override
    protected void updateBefore(AttendanceRecordErrorEntity entity, Context context) throws AppException {
        AttendanceRecordErrorEntity temp = this.get(entity.getId());
        if (temp == null) {
            throw new AppException("考勤异常数据不存在");
        }
        if (temp.getErrorStatus().intValue() != DataUtil.converStr2Int(entity.getProcessResult(), 0)) {
            //异常处理结果与原来不符合时先删除已生成的考勤绩效
            PerformAttendRecordQuery query = new PerformAttendRecordQuery();
            query.setStaffId(temp.getStaffId());
            query.setErrorTime(temp.getErrorDateTime());
            List<PerformAttendRecordEntity> delAttendRecords = performAttendRecordService.find(query, context);
            if (!ObjectUtils.isEmpty(delAttendRecords)) {
                Long[] delIds = delAttendRecords.stream().map(i -> i.getId()).toArray(Long[]::new);
                performAttendRecordService.remove(delIds, context);
            }
        }
    }

    @Override
    protected void updateAfter(AttendanceRecordErrorEntity entity, Context context) throws AppException {
        if (YesNoEnum.YES.getValue() == entity.getProcessStatus()) {
            entity = this.get(entity.getId());
            //判断后打绩效

            String ruleCode = getRuleCode(entity);
            if (!ObjectUtils.isEmpty(ruleCode)) {
                saveAttendPerformRecord(context, entity, ruleCode);
            }
            //考勤异常处理为正常后修改考勤绩效
            if (entity.getProcessResult().equals(ErrorStatusEnum.正常.getValue().toString())||entity.getProcessResult().equals(ErrorStatusEnum.年假.getValue().toString())) {
                CheckAttendRecordEntity checkAttendRecordQuery = new CheckAttendRecordEntity();
                checkAttendRecordQuery.setStaffId(entity.getStaffId());
                checkAttendRecordQuery.setErrorTime(entity.getErrorDateTime());
                CheckAttendRecordEntity checkAttendRecord = checkAttendRecordService.selectOne(checkAttendRecordQuery);
                if (checkAttendRecord != null) {
                    CheckAttendRecordEntity checkAttendUpdate = new CheckAttendRecordEntity();
                    checkAttendUpdate.setId(checkAttendRecord.getId());
                    checkAttendUpdate.setCheckResult(CheckResultSubEnum.核查不扣分.getValue());
                    checkAttendUpdate.setAuditStatus(AuditStatusEnum.拒绝.getValue());
                    checkAttendUpdate.setUpdateTime(new Date());
                    checkAttendUpdate.setCheckStatus(CheckStatusEnum.已处理.getValue());
                    checkAttendRecordService.update(checkAttendUpdate, context);
                }
            }
            //异常处理后重新汇总考勤 context!=null时表示页面调用的修改，自动执行处理任务调用时context为null，无需立即汇总
            if (StringUtils.isNotEmpty(entity.getProcessResult()) && context != null) {
                AttendanceSummaryQuery query = new AttendanceSummaryQuery();
                query.setStaffId(entity.getStaffId());
                query.setSummaryTime(DateUtils.getStrDate(entity.getErrorDateTime()));
//                AttendSummaryThread summaryThread = new AttendSummaryThread(attendanceStatService, query);
//                ThreadPool.getInstance().execute(summaryThread);
                cacheService.lpush(KEY_ATTENDANCE_STAT_CACHE, query);
                //立即汇总考勤绩效分数
                StaffCheckSummaryQuery checkSummaryQuery = new StaffCheckSummaryQuery();
                checkSummaryQuery.setStaffId(entity.getStaffId());
                checkSummaryQuery.setCheckTimeStart(DateUtils.getStrDate(entity.getErrorDateTime()));
                checkSummaryQuery.setCheckTimeEnd(checkSummaryQuery.getCheckTimeStart());
                checkSummaryQuery.setCheckType(CheckTypeEnum.考勤绩效.getValue());
                cacheService.lpush(KEY_CHECK_SUMMARY_CACHE, checkSummaryQuery);
            }
        }
    }


    @Override
    public boolean errorConfirm(Long errorId) {
        boolean result = true;
        AttendanceRecordErrorEntity errorEntity = this.get(errorId);
        if (errorEntity != null) {
            AttendanceLeaveRecordQuery query = new AttendanceLeaveRecordQuery();
            query.setEndTimeStart(DateUtils.getStrDate(errorEntity.getErrorDateTime()));
            query.setLeavePersonId(errorEntity.getStaffId());
            query.setAuditResult(AppealResultEnum.通过.getValue());
            AttendanceLeaveRecordEntity leaveRecordEntity = attendanceLeaveRecordService.selectOne(query);
            if (leaveRecordEntity != null) {
                result = false;
            }
        }
        return result;
    }

    @Override
    public void doAutoProcess(Date date) {
        AttendanceRecordErrorQuery query = new AttendanceRecordErrorQuery();
        query.setErrorDateTimeStart(DateUtils.getStrDate(date));
        query.setErrorDateTimeEnd(DateUtils.getStrDate(date));
        List<AttendanceRecordErrorEntity> list = this.find(query);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(item -> {
                if (errorConfirm(item.getId())) {
                    AttendanceRecordErrorEntity updateEntity = new AttendanceRecordErrorEntity();
                    updateEntity.setId(item.getId());
                    updateEntity.setProcessStatus(1);
                    updateEntity.setProcessResult(String.valueOf(item.getErrorStatus()));
                    updateEntity.setStaffId(item.getStaffId());
                    updateEntity.setOpertor("系统自动处理");
                    updateEntity.setOperDateTime(new Date());
                    updateEntity.setRemark("系统自动处理");
                    updateEntity.setUpdateTime(new Date());
                    updateEntity.setUpdateUserId(1l);
                    this.update(updateEntity, null);
                }
            });
        }
    }

    @Override
    public void doReprocess(AttendanceRecordErrorQuery query, Context context) {
        log.info("开始执行任务:重新生成已处理的绩效考勤核查数据.....");
        List<String> dateList = getDatesBetween(query.getErrorDateTimeStart(), query.getErrorDateTimeEnd());
        for (String day : dateList) {
            query.setErrorDateTimeStart(day);
            query.setErrorDateTimeEnd(day);
            List<AttendanceRecordErrorEntity> list = this.find(query);
            if (CollectionUtils.isNotEmpty(list)) {
                log.info("重新生成[" + day + "]已处理的绩效考勤核查数据.....");
                for (AttendanceRecordErrorEntity entity : list) {
                    //   if (YesNoEnum.YES.getValue() == entity.getProcessStatus()) {
                    //判断后打绩效
                    String ruleCode = getRuleCode(entity);
                    if (!ObjectUtils.isEmpty(ruleCode)) {
                        saveAttendPerformRecord(context, entity, ruleCode);
                    }

                }
            }
        }
        log.info("从新生成已处理的绩效考勤核查数据,执行完成.....");
    }


    private List<String> getDatesBetween(String startDateStr, String endDateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.parse(endDateStr, formatter);
        List<String> dates = new ArrayList<>();
        //这里是判断开始日期是否在结束日期之后或者=结束日期
        while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
            String add = startDate.format(formatter);
            dates.add(add);
            startDate = startDate.plusDays(1);
        }
        return dates;
    }

    @Override
    public List<ErrorSummaryVo> getMonthTop10(AttendanceRecordErrorQuery query) {
        return dao.getMonthTop10(query);
    }

    @Override
    protected void removeBefore(Long[] ids, Context context) throws AppException {
        //todo 删除考勤绩效中记录信息
        AttendanceRecordErrorQuery errorQuery = new AttendanceRecordErrorQuery();
        errorQuery.setIdList(Arrays.asList(ids));
        List<AttendanceRecordErrorEntity> delErrorList = this.find(errorQuery, context);
        for (AttendanceRecordErrorEntity attendanceRecordErrorEntity : delErrorList) {
            PerformAttendRecordQuery query = new PerformAttendRecordQuery();
            query.setStaffId(attendanceRecordErrorEntity.getStaffId());
            query.setErrorTime(attendanceRecordErrorEntity.getErrorDateTime());
            query.setRuleIdNotList(Arrays.asList(9L));
            List<PerformAttendRecordEntity> delAttendRecords = performAttendRecordService.find(query, context);
            if (!ObjectUtils.isEmpty(delAttendRecords)) {
                Long[] delIds = delAttendRecords.stream().map(i -> i.getId()).toArray(Long[]::new);
                performAttendRecordService.remove(delIds, context);
                //log.info("删除考勤记录成功,size:{}", delIds.length);
            }
        }
    }


    @Override
    protected void saveAfter(AttendanceRecordErrorEntity entity, Context context) throws AppException {
        super.saveAfter(entity, context);
        //   if (YesNoEnum.YES.getValue() == entity.getProcessStatus()) {
        //判断后打绩效
        // log.info("生成绩效考勤核查数据.....");
        String ruleCode = getRuleCode(entity);
        if (!ObjectUtils.isEmpty(ruleCode)) {
            saveAttendPerformRecord(context, entity, ruleCode);
        }
    }

    @Override
    protected void saveAfter(List<AttendanceRecordErrorEntity> list, Context context) throws AppException {
        super.saveAfter(list, context);
        if (!ObjectUtils.isEmpty(list)) {
            // log.info("生成绩效考勤核查数据.....");
            for (AttendanceRecordErrorEntity entity : list) {
                //   if (YesNoEnum.YES.getValue() == entity.getProcessStatus()) {
                //判断后打绩效
                String ruleCode = getRuleCode(entity);
                if (!ObjectUtils.isEmpty(ruleCode)) {
                    saveAttendPerformRecord(context, entity, ruleCode);
                }

            }
        }

    }


    private void saveAttendPerformRecord(Context context, AttendanceRecordErrorEntity entity, String ruleCode) {
        AttendSaveReq attendSaveReq = new AttendSaveReq();
        attendSaveReq.setAttendanceGroupId(entity.getAttendanceGroupId());
        attendSaveReq.setAttendanceGroupName(entity.getAttendanceGroupName());
        attendSaveReq.setAttendanceDate(entity.getActualAttendanceDateTime());
        attendSaveReq.setErrorTime(entity.getErrorDateTime());
        attendSaveReq.setGoOffTimeStr(entity.getGoOrOff());
        attendSaveReq.setActualAttendTime(entity.getActualAttendanceDateTime());
        attendSaveReq.setErrorResult(entity.getRemark());
        attendSaveReq.setTitle("考勤");
        attendSaveReq.setHappenTime(entity.getErrorDateTime());
        attendSaveReq.setRuleCode(ruleCode);
        attendSaveReq.setStaffId(entity.getStaffId());
        performAttendRecordService.saveAttend(attendSaveReq, context);
    }


    private String getRuleCode(AttendanceRecordErrorEntity entity) {
        //请假时间段排除
        //判断当天是否请假 请假分为多个区间段与请假类型。
        boolean isLeave = false;
        AttendanceLeaveRecordQuery leaveRecordQuery = new AttendanceLeaveRecordQuery();
        leaveRecordQuery.setLeavePersonId(entity.getStaffId());
        leaveRecordQuery.setAuditResultList(Arrays.asList(1, 4));
        List<AttendanceLeaveRecordEntity> leaveRecordEntities = attendanceLeaveRecordService.find(leaveRecordQuery);

        List<AttendanceLeaveRecordEntity> leaveRecords = null;
        if (!ObjectUtils.isEmpty(leaveRecordEntities)) {
            //判断当日日期中 是否有请假记录
            leaveRecords = leaveRecordEntities.stream().filter(f -> {
                DateTime dateTime = DateUtil.parseDate(DateUtil.formatDate(entity.getErrorDateTime()));
                DateTime start = DateUtil.parseDate(DateUtil.formatDate(f.getStartTime()));
                DateTime end = DateUtil.parseDate(DateUtil.formatDate(f.getEndTime()));

                boolean in = DateUtil.isIn(dateTime, start, end);
                return in;
            }).collect(Collectors.toList());

            if (!ObjectUtils.isEmpty(leaveRecords)) {
                //当前日期范围内 存在请假。判断请假计入考勤。目前只有事件计入
                isLeave = true;
            }
        }

        Long shiftsId = entity.getShiftsId();
        AttendanceClassDetailEntity attendanceClassDetailEntity = classDetailService.get(shiftsId);
        String ruleCode = "";
        if (ErrorStatusEnum.早退.getValue() == DataUtil.converStr2Int(entity.getProcessResult(), 3)) {
            if (!ObjectUtils.isEmpty(entity.getErrorDateTime()) && !ObjectUtils.isEmpty(entity.getGoOffDateTime())) {
                Long earlyMin = 0L;
                //判断如果有请假记录记录。这计算早退时间与请假记录开始时间的差值
                if (isLeave) {
                    List<Long> earlyMinList = new ArrayList<>();
                    Date goOffDateTime = entity.getGoOffDateTime();
                    if (!ObjectUtils.isEmpty(attendanceClassDetailEntity) && attendanceClassDetailEntity.getOffWorkDateBefore() > 0) {
                        goOffDateTime = DateUtil.offsetMinute(goOffDateTime, -attendanceClassDetailEntity.getOffWorkDateBefore());
                    }
                    for (AttendanceLeaveRecordEntity leaveRecord : leaveRecords) {
                        long between = DateUtil.between(leaveRecord.getStartTime(), leaveRecord.getEndTime(), DateUnit.DAY);
                        if (between == 0) {
                            //当天
                            // 1.判断异常时间与请假结束时间那个大。取大那个与当前班次下班时间比较差值，去正向值 偏移后的时间计算
                            int compare = DateUtil.compare(entity.getErrorDateTime(), leaveRecord.getEndTime());
                            if (compare < 0) {
                                //异常时间小于请假结束时间，早退则计算假期结束时间与班次下班时间还有前置时间差值
                                long tempEarly = DateUtil.between(leaveRecord.getEndTime(), goOffDateTime, DateUnit.MINUTE, false);
                                if (tempEarly > 0L) {
                                    earlyMinList.add(tempEarly);
                                }
                            }
                            if (compare >= 0) {
                                //异常时间大于请假结束时间，早退则计算异常时间与班次下班时间差值
                                long tempEarly = DateUtil.between(entity.getErrorDateTime(), goOffDateTime, DateUnit.MINUTE, false);
                                if (tempEarly > 0L) {
                                    earlyMinList.add(tempEarly);
                                }
                            }
                        }
     /*                   if (between > 0) {
                            //跨天   2024-07-01---2024-07-03
                            // 判断当前异常日期是否在请假全天，
                            long betweened = DateUtil.between(DateUtil.parseDate(DateUtil.formatDate(entity.getErrorDateTime())), leaveRecord.getEndTime(), DateUnit.DAY);
                            if (betweened == 0) {
                                //异常日期在请假的最后一天，计算早退与请假的开始时间的差值 todo
                                int compare = DateUtil.compare(entity.getErrorDateTime(), leaveRecord.getEndTime());

                            }
                        }*/
                    }

                    if (!ObjectUtils.isEmpty(earlyMinList)) {
                        //去列表中最小值赋值给earlyMin
                        earlyMin = earlyMinList.stream().min(Comparator.comparing(x -> x)).orElse(0L);
                    } else {
                        //请假不满足条件
                        earlyMin = DateUtil.between(entity.getErrorDateTime(), goOffDateTime, DateUnit.MINUTE);
                    }

                } else {
                    //判断时间差定义
                    earlyMin = DateUtil.between(entity.getErrorDateTime(), entity.getGoOffDateTime(), DateUnit.MINUTE);

                }

                if (earlyMin >= 0L && earlyMin < 5L) {
                    //早退5分钟内
                    ruleCode = "ATTEND1002";
                }
                if (earlyMin >= 5L && earlyMin < 60L) {
                    //早退5分钟内
                    ruleCode = "ATTEND1004";
                }
                if (earlyMin >= 60L) {
                    //早退5分钟内
                    ruleCode = "ATTEND1006";
                }

            } else {
                ruleCode = "ATTEND1002";
            }
        } else if (ErrorStatusEnum.迟到.getValue() == DataUtil.converStr2Int(entity.getProcessResult(), 3)) {
            if (!ObjectUtils.isEmpty(entity.getErrorDateTime()) && !ObjectUtils.isEmpty(entity.getGoOffDateTime())) {
                //判断如果有请假记录记录。这计算早退时间与请假记录开始时间的差值
                List<Long> lateMinList = new ArrayList<>();
                Long lateMin = 0L;
                if (isLeave) {
                    Date goOffDateTime = entity.getGoOffDateTime();
                    if (!ObjectUtils.isEmpty(attendanceClassDetailEntity) && attendanceClassDetailEntity.getGoWorkDateAfter() > 0) {
                        goOffDateTime = DateUtil.offsetMinute(goOffDateTime, attendanceClassDetailEntity.getGoWorkDateAfter());
                    }

                    for (AttendanceLeaveRecordEntity leaveRecord : leaveRecords) {
                        long between = DateUtil.between(leaveRecord.getStartTime(), leaveRecord.getEndTime(), DateUnit.DAY);
                        if (between == 0) {
                            //当天
                            // 1.判断异常时间与请假开始时间那个大。
                            int compare = DateUtil.compare(entity.getErrorDateTime(), leaveRecord.getStartTime());
                            int compare1 = DateUtil.compare(entity.getErrorDateTime(), leaveRecord.getEndTime());

                            boolean in = DateUtil.isIn(goOffDateTime, leaveRecord.getStartTime(), leaveRecord.getEndTime());

                            if (in) {
                                //上班打卡时间在请假时间范围内，迟到分钟按照请假结束时间与异常时间做差值
                                long tempLate = DateUtil.between(leaveRecord.getEndTime(), entity.getErrorDateTime(), DateUnit.MINUTE, false);
                                if (tempLate > 0L) {
                                    lateMinList.add(tempLate);
                                }
                            } else {
                                //异常时间不在请假时间范围内
                                //1.异常时间在请假开始时间之前
                                if (compare < 0) {
                                    //异常时间小于请假k开始时间,则迟到计算异常时间与班次时间时间差值
                                    long tempLate = DateUtil.between(goOffDateTime, entity.getErrorDateTime(), DateUnit.MINUTE, false);
                                    if (tempLate > 0L) {
                                        lateMinList.add(tempLate);
                                    }
                                }
                                //1.异常时间在请假结束时间之后
                                if (compare1 > 0) {
                                  //  则迟到计算异常时间与班次时间时间差值
                                    //1.请假开始时间 大于上班时间时候
                                    int rightBig = DateUtil.compare(goOffDateTime, leaveRecord.getStartTime());
                                    long leftRange = 0L;
                                    if (rightBig <= 0) {
                                        //多段值  左段值
                                        leftRange = DateUtil.between(goOffDateTime, leaveRecord.getStartTime(), DateUnit.MINUTE, false);
                                    }
                                    //右段值
                                    long leftRange1 = 0;
                                    int rightBig1 = DateUtil.compare(leaveRecord.getEndTime(), entity.getErrorDateTime());
                                    if (rightBig1 < 0) {
                                        leftRange1 = DateUtil.between(leaveRecord.getEndTime(), entity.getErrorDateTime(), DateUnit.MINUTE, false);
                                    }
                                    leftRange = leftRange + leftRange1;
                                    if(leftRange>0){
                                        lateMinList.add(leftRange);
                                    }
                                }
                            }

/*
                            if (compare < 0) {
                                //异常时间小于请假结束时间,迟到则计算异常时间与班次班次班时间还有前置时间差值
                                long tempLate = DateUtil.between(goOffDateTime, entity.getErrorDateTime(), DateUnit.MINUTE, false);
                                if (tempLate > 0L) {
                                    lateMinList.add(tempLate);
                                }
                            }
                            if (compare >= 0) {
                                //异常时间小于请假结束时间,迟到则计算异常时间与班次班次班时间还有前置时间差值
                                long tempLate = DateUtil.between(goOffDateTime, leaveRecord.getEndTime(), DateUnit.MINUTE, false);
                                if (tempLate > 0L) {
                                    lateMinList.add(tempLate);
                                }
                            }*/

                        }

                        if (between > 0) {
                            //todo 跨天
                        }
                    }

                    if (!ObjectUtils.isEmpty(lateMinList)) {
                        lateMin = lateMinList.stream().min(Comparator.comparing(x -> x)).orElse(0L);
                    } else {
                        lateMin = DateUtil.between(entity.getErrorDateTime(), entity.getGoOffDateTime(), DateUnit.MINUTE);
                    }
                } else {
                    lateMin = DateUtil.between(entity.getErrorDateTime(), entity.getGoOffDateTime(), DateUnit.MINUTE);

                }
                if (lateMin >= 0L && lateMin < 5L) {
                    //迟到5分钟内
                    ruleCode = "ATTEND1001";
                }
                if (lateMin >= 5L && lateMin < 60L) {
                    //迟到5-60
                    ruleCode = "ATTEND1003";
                }
                if (lateMin >= 60L) {
                    //迟到60分钟以上
                    ruleCode = "ATTEND1005";
                }
            } else {
                //迟到5分钟内
                ruleCode = "ATTEND1001";
            }
        } else if (ErrorStatusEnum.缺卡.getValue() == DataUtil.converStr2Int(entity.getProcessResult(), 3)) {
            ruleCode = "ATTEND1007";
        } else if (ErrorStatusEnum.事假半天.getValue() == DataUtil.converStr2Int(entity.getProcessResult(), 4)) {
            ruleCode = "ATTEND1010";
        } else if (ErrorStatusEnum.事假全天.getValue() == DataUtil.converStr2Int(entity.getProcessResult(), 5)) {
            ruleCode = "ATTEND1011";
        }
        return ruleCode;
    }


    public static void main(String[] args) {
        long compare = DateUtil.compare(DateUtil.parseDateTime("2024-07-01 15:00:00"), DateUtil.parseDateTime("2024-07-01 16:00:00"));
        long between = DateUtil.between(DateUtil.parseDateTime("2024-07-01 15:00:00"), DateUtil.parseDateTime("2024-07-01 16:00:00"), DateUnit.MINUTE, false);

        System.out.println(compare);
        System.out.println(between);
    }
}