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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.service.*;
import com.mortals.xhx.module.attendance.service.work.AttendanceWorkAbstract;
import com.mortals.xhx.module.attendance.service.work.CommonData;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.attendance.dao.AttendanceRecordHikDao;
import com.mortals.framework.model.Context;
import org.springframework.util.ObjectUtils;


import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * AttendanceRecordHikService
 * 海康考勤打卡记录汇总信息 service实现
 *
 * @author zxfei
 * @date 2023-04-08
 */
@Service("attendanceRecordHikService")
@Slf4j
public class AttendanceRecordHikServiceImpl extends AbstractCRUDServiceImpl<AttendanceRecordHikDao, AttendanceRecordHikEntity, Long> implements AttendanceRecordHikService {

    @Autowired
    private DeptService deptService;
    @Autowired
    private AttendanceRecordService attendanceRecordService;
    @Autowired
    private AttendanceRecordHikService attendanceRecordHikService;
    @Autowired
    private AttendanceGroupStaffService attendanceGroupStaffService;
    @Autowired
    private AttendanceGroupService attendanceGroupService;
    @Autowired
    private AttendanceGroupFixedworkService attendanceGroupFixedworkService;
    @Autowired
    private AttendanceClassService attendanceClassService;
    @Autowired
    private AttendanceClassDetailService attendanceClassDetailService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private AttendanceRecordErrorService errorService;


    @Override
    public void addAttendanceRecord(AttendanceRecordHikEntity hikEntity, Context context) throws Exception {
        List<AttendanceRecordHikEntity> hikEntityList = new ArrayList<>();
        addAttendanceOtherRecordList(hikEntityList, context);
    }

    @Override
    public void addAttendanceRecordByQuery(AttendanceRecordHikEntity attendanceRecordHikQuery, Context context) throws Exception {
        List<AttendanceRecordHikEntity> hikEntityList = attendanceRecordHikService.find(attendanceRecordHikQuery);
        addAttendanceOtherRecordList(hikEntityList, context);
    }


    public void addAttendanceOtherRecordList(List<AttendanceRecordHikEntity> hikEntityList, Context context) throws Exception {
        List<AttendanceRecordHikEntity> collect = hikEntityList.stream().sorted(Comparator.comparing(AttendanceRecordHikEntity::getAttendanceDate)).collect(Collectors.toList());
        //另一种实现
        Map<Long, List<AttendanceRecordHikEntity>> listMap = collect.stream().collect(Collectors.groupingBy(AttendanceRecordHikEntity::getStaffId));
        for (Map.Entry<Long, List<AttendanceRecordHikEntity>> itemEntry : listMap.entrySet()) {
            //按人员
            Long staffId = itemEntry.getKey();
            List<AttendanceRecordHikEntity> recordHikEntityList = itemEntry.getValue();
            Map<String, List<AttendanceRecordHikEntity>> dateRecordMap = recordHikEntityList.stream().collect(Collectors.groupingBy(hikEntity -> DateUtil.formatDate(hikEntity.getAttendanceDate())));
            for (Map.Entry<String, List<AttendanceRecordHikEntity>> dateEntry : dateRecordMap.entrySet()) {
                //按每日
                String dateStr = dateEntry.getKey();
                List<AttendanceRecordHikEntity> recordHikEntities = dateEntry.getValue();
                if (ObjectUtils.isEmpty(recordHikEntities)) continue;
                AttendanceRecordHikEntity item = recordHikEntities.get(0);
                AttendanceRecordEntity attendanceRecordEntity = new AttendanceRecordEntity();
                attendanceRecordEntity.initAttrValue();

                attendanceRecordEntity.setStaffId(item.getStaffId());
                attendanceRecordEntity.setStaffName(item.getStaffName());
                attendanceRecordEntity.setDeptId(item.getDeptId());
                attendanceRecordEntity.setDeptName(item.getDeptName());
                attendanceRecordEntity.setPositionId(item.getPositionId());
                attendanceRecordEntity.setPositionName(item.getPositionName());
                attendanceRecordEntity.setWorkNum(item.getWorkNum());
                attendanceRecordEntity.setAttendanceDate(item.getAttendanceDate());
                attendanceRecordEntity.setCreateTime(item.getAttendanceDate());
                attendanceRecordEntity.setCreateUserId(this.getContextUserId(context));

                StaffEntity staffEntity = staffService.getExtCache(staffId.toString());
                if (ObjectUtils.isEmpty(staffEntity)) {
                    log.info("staffEntity is null ,staffId:{}", staffId);
                    continue;
                }
                //通过员工id查询考勤组人员信息
                List<AttendanceGroupStaffEntity> attendanceGroupStaffEntities = attendanceGroupStaffService.find(new AttendanceGroupStaffQuery().staffId(staffId));
                List<AttendanceRecordDetailEntity> detailEntityList = new ArrayList<>();
                if (!ObjectUtils.isEmpty(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size() == 1) {
                    //checkAttendGroupByOne(attendanceRecordEntity, detailEntityList, attendanceDate, attendanceGroupStaffEntities.get(0), context);
                    //查询考勤组
                    AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.get(attendanceGroupStaffEntities.get(0).getGroupId(), context);
                    attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity == null ? 0L : attendanceGroupEntity.getId());
                    attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity == null ? "" : attendanceGroupEntity.getGroupName());
                    //查询考勤组详细信息获取班次 todo 默认固定班次 还有自由 与排班制
                    AttendanceWorkAbstract workAbstract = AttendanceWorkAbstract.newType(attendanceGroupEntity.getType());
                    CommonData commonData = new CommonData();
                    commonData.setContext(context);
                    commonData.setAttendanceGroupEntity(attendanceGroupEntity);
                    commonData.setAttendanceRecordEntity(attendanceRecordEntity);
                    commonData.setAttendanceDate(attendanceRecordEntity.getAttendanceDate());
                    commonData.setDetailEntityList(detailEntityList);
                    commonData.setStaffId(attendanceRecordEntity.getStaffId());
                    commonData.setDateStr(dateStr);
                    workAbstract.doHandleWork(commonData);
                }

                //如果考勤人员信息list长度大于1 则该考勤人员有多个考勤组.
                if (StringUtils.isNotNull(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size() > 1) {
                    for (AttendanceGroupStaffEntity groupStaffEntity : attendanceGroupStaffEntities) {
                        //多考勤组 todo 暂时和单一覆盖
                        //checkAttendGroupByOne(attendanceRecordEntity, detailEntityList, attendanceDate, groupStaffEntity, context);
                        //查询考勤组
                        AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.get(groupStaffEntity.getGroupId(), context);
                        attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity == null ? 0L : attendanceGroupEntity.getId());
                        attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity == null ? "" : attendanceGroupEntity.getGroupName());
                        //查询考勤组详细信息获取班次 todo 默认固定班次 还有自由 与排班制
                        AttendanceWorkAbstract workAbstract = AttendanceWorkAbstract.newType(attendanceGroupEntity.getType());
                        CommonData commonData = new CommonData();
                        commonData.setContext(context);
                        commonData.setAttendanceGroupEntity(attendanceGroupEntity);
                        commonData.setAttendanceRecordEntity(attendanceRecordEntity);
                        commonData.setDetailEntityList(detailEntityList);
                        commonData.setAttendanceDate(attendanceRecordEntity.getAttendanceDate());
                        commonData.setStaffId(attendanceRecordEntity.getStaffId());
                        commonData.setDateStr(dateStr);
                        workAbstract.doHandleWork(commonData);
                    }
                }

                if (!ObjectUtils.isEmpty(detailEntityList)) {
                    attendanceRecordEntity.setAttendanceRecordDetailList(detailEntityList);
                    //判断签到 只要存在一次正常打卡详细记录 就判断签到
                    attendanceRecordEntity.setSignInResult(YesNoEnum.YES.getValue());
                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (GoWorkResultEnum.缺卡.getValue() == detailEntity.getGoWorkResult() || GoWorkResultEnum.迟到.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setSignInResult(YesNoEnum.NO.getValue());
                            break;
                        }
                    }
                    attendanceRecordEntity.setSignOutResult(YesNoEnum.YES.getValue());
                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (OffWorkResultEnum.缺卡.getValue() == detailEntity.getOffWorkResult() || OffWorkResultEnum.早退.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setSignOutResult(YesNoEnum.NO.getValue());
                            break;
                        }
                    }

                    if (YesNoEnum.YES.getValue() == attendanceRecordEntity.getSignInResult() && YesNoEnum.YES.getValue() == attendanceRecordEntity.getSignOutResult()) {
                        attendanceRecordEntity.setPunchResult(YesNoEnum.YES.getValue());
                    } else {
                        attendanceRecordEntity.setPunchResult(YesNoEnum.NO.getValue());
                    }

                    attendanceRecordEntity.setAttendType("现场打卡");
                    //判断是否存在记录 如果存在 则更新  维度   staffId  attendanceDate shiftsId attendanceGroupId
                    AttendanceRecordQuery query = new AttendanceRecordQuery();
                    query.setStaffId(attendanceRecordEntity.getStaffId());
                   // query.setClassId(attendanceRecordEntity.getClassId());
                   // query.setAttendanceGroupId(attendanceRecordEntity.getAttendanceGroupId());
                    query.setAttendanceDate(attendanceRecordEntity.getAttendanceDate());

                    AttendanceRecordEntity recordEntity = attendanceRecordService.selectOne(query);
                    if (!ObjectUtils.isEmpty(recordEntity)) {
                        attendanceRecordEntity.setId(recordEntity.getId());
                        attendanceRecordEntity.setUpdateTime(new Date());
                        attendanceRecordEntity.setUpdateUserId(this.getContextUserId(context));
                        attendanceRecordService.update(attendanceRecordEntity, context);
                    } else {
                        attendanceRecordService.save(attendanceRecordEntity, context);
                    }
                }

            }

        }


    }


    public void addAttendanceRecordList(List<AttendanceRecordHikEntity> hikEntityList, Context context) throws Exception {
        //对考勤数据根据考勤时间进行排序
        List<AttendanceRecordHikEntity> collect = hikEntityList.stream().sorted(Comparator.comparing(AttendanceRecordHikEntity::getAttendanceDate)).collect(Collectors.toList());
        //根据工号对考勤数据进行分组
        Map<String, List<AttendanceRecordHikEntity>> listMap = collect.stream().collect(Collectors.groupingBy(AttendanceRecordHikEntity::getWorkNum));
        for (Map.Entry<String, List<AttendanceRecordHikEntity>> itemEntry : listMap.entrySet()) {
            String workNum = itemEntry.getKey();
            List<AttendanceRecordHikEntity> recordHikEntityList = itemEntry.getValue();
            //对按照工号分组了的数据recordHikEntityList进行时间分组
            // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Map<String, List<AttendanceRecordHikEntity>> dateRecordMap = recordHikEntityList.stream().collect(Collectors.groupingBy(hikEntity -> DateUtil.formatDate(hikEntity.getAttendanceDate())));
            for (Map.Entry<String, List<AttendanceRecordHikEntity>> dateEntry : dateRecordMap.entrySet()) {
                String dateStr = dateEntry.getKey();
                List<AttendanceRecordHikEntity> attendanceRecordHikEntities = dateEntry.getValue();
                AttendanceRecordEntity attendanceRecordEntity = new AttendanceRecordEntity();
                attendanceRecordEntity.initAttrValue();
                List<AttendanceRecordDetailEntity> detailEntityList = new ArrayList<>();
                //分日期 遍历考勤记录 考勤记录一天只生成一条 详细多条
                for (AttendanceRecordHikEntity item : attendanceRecordHikEntities) {
                    //对主表的对象进行赋值
                    attendanceRecordEntity.setStaffId(item.getStaffId());
                    attendanceRecordEntity.setStaffName(item.getStaffName());
                    attendanceRecordEntity.setDeptId(item.getDeptId());
                    attendanceRecordEntity.setDeptName(item.getDeptName());
                    attendanceRecordEntity.setPositionId(item.getPositionId());
                    attendanceRecordEntity.setPositionName(item.getPositionName());
                    attendanceRecordEntity.setWorkNum(item.getWorkNum());
                    attendanceRecordEntity.setAttendanceDate(item.getAttendanceDate());
                    attendanceRecordEntity.setCreateTime(item.getAttendanceDate());
                    attendanceRecordEntity.setCreateUserId(this.getContextUserId(context));
                    //获取考勤打卡时间
                    Date attendanceDate = item.getAttendanceDate();
                    //通过工号查询员工信息
                    StaffEntity staffEntity = staffService.getExtCache(StrUtil.padPre(item.getWorkNum(), 8, "0"));
                    if (ObjectUtils.isEmpty(staffEntity)) {
                        log.info("staffEntity is null ,workNum:{}", StrUtil.padPre(item.getWorkNum(), 8, "0"));
                        continue;
                    }
                    //通过员工id查询考勤组人员信息
                    List<AttendanceGroupStaffEntity> attendanceGroupStaffEntities = attendanceGroupStaffService.find(new AttendanceGroupStaffQuery().staffId(staffEntity.getId()));
                    //查询该考勤人员,如果查出来的考勤人员信息list长度为1 则该考勤人员只有一个考勤组
                    if (!ObjectUtils.isEmpty(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size() == 1) {
                        //checkAttendGroupByOne(attendanceRecordEntity, detailEntityList, attendanceDate, attendanceGroupStaffEntities.get(0), context);
                        //查询考勤组
                        AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.get(attendanceGroupStaffEntities.get(0).getGroupId(), context);
                        attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity == null ? 0L : attendanceGroupEntity.getId());
                        attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity == null ? "" : attendanceGroupEntity.getGroupName());
                        //查询考勤组详细信息获取班次 todo 默认固定班次 还有自由 与排班制
                        AttendanceWorkAbstract workAbstract = AttendanceWorkAbstract.newType(attendanceGroupEntity.getType());
                        CommonData commonData = new CommonData();
                        commonData.setAttendanceGroupEntity(attendanceGroupEntity);
                        commonData.setAttendanceDate(attendanceDate);
                        commonData.setDetailEntityList(detailEntityList);
                        commonData.setStaffId(attendanceRecordEntity.getStaffId());
                        workAbstract.doHandleWork(commonData);
                    }

                    //如果考勤人员信息list长度大于1 则该考勤人员有多个考勤组.
                    if (StringUtils.isNotNull(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size() > 1) {
                        for (AttendanceGroupStaffEntity groupStaffEntity : attendanceGroupStaffEntities) {
                            //多考勤组 todo 暂时和单一覆盖
                            //checkAttendGroupByOne(attendanceRecordEntity, detailEntityList, attendanceDate, groupStaffEntity, context);
                            //查询考勤组
                            AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.get(groupStaffEntity.getGroupId(), context);
                            attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity == null ? 0L : attendanceGroupEntity.getId());
                            attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity == null ? "" : attendanceGroupEntity.getGroupName());
                            //查询考勤组详细信息获取班次 todo 默认固定班次 还有自由 与排班制
                            AttendanceWorkAbstract workAbstract = AttendanceWorkAbstract.newType(attendanceGroupEntity.getType());
                            CommonData commonData = new CommonData();
                            commonData.setAttendanceGroupEntity(attendanceGroupEntity);
                            commonData.setAttendanceDate(attendanceDate);
                            commonData.setDetailEntityList(detailEntityList);
                            commonData.setStaffId(attendanceRecordEntity.getStaffId());
                            workAbstract.doHandleWork(commonData);

                        }
                    }
                }
                if (!ObjectUtils.isEmpty(detailEntityList)) {
                    AttendanceClassDetailEntity attendanceClassDetailEntity = attendanceClassDetailService.get(detailEntityList.get(0).getShiftsId(), context);
                    attendanceRecordEntity.setClassId(attendanceClassDetailEntity == null ? 0L : attendanceClassDetailEntity.getClassId());
                    attendanceRecordEntity.setClassName(attendanceClassDetailEntity == null ? "" : attendanceClassDetailEntity.getClassName());
                }
                //生成异常记录
                genErrorAttendRecord(context, dateStr, attendanceRecordEntity, detailEntityList);
                if (!ObjectUtils.isEmpty(detailEntityList)) {
                    attendanceRecordEntity.setAttendanceRecordDetailList(detailEntityList);
                    //判断签到 只要存在一次正常打卡详细记录 就判断签到
                    attendanceRecordEntity.setSignInResult(YesNoEnum.YES.getValue());
                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (GoWorkResultEnum.缺卡.getValue() == detailEntity.getGoWorkResult() || GoWorkResultEnum.迟到.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setSignInResult(YesNoEnum.NO.getValue());
                            break;
                        }
                    }
                    attendanceRecordEntity.setSignOutResult(YesNoEnum.YES.getValue());
                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (OffWorkResultEnum.缺卡.getValue() == detailEntity.getOffWorkResult() || OffWorkResultEnum.早退.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setSignOutResult(YesNoEnum.NO.getValue());
                            break;
                        }
                    }

     /*               for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (OffWorkResultEnum.缺卡.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setPunchResult(YesNoEnum.NO.getValue());
                            break;
                        }

                        if (OffWorkResultEnum.缺卡.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setPunchResult(YesNoEnum.NO.getValue());
                            break;
                        }
                    }*/

                    if (YesNoEnum.YES.getValue() == attendanceRecordEntity.getSignInResult() && YesNoEnum.YES.getValue() == attendanceRecordEntity.getSignOutResult()) {
                        attendanceRecordEntity.setPunchResult(YesNoEnum.YES.getValue());
                    } else {
                        attendanceRecordEntity.setPunchResult(YesNoEnum.NO.getValue());
                    }

                    attendanceRecordEntity.setAttendType("现场打卡");
                    //判断是否存在记录 如果存在 则更新  维度   staffId  attendanceDate shiftsId attendanceGroupId
                    AttendanceRecordQuery query = new AttendanceRecordQuery();
                    query.setStaffId(attendanceRecordEntity.getStaffId());
                    query.setClassId(attendanceRecordEntity.getClassId());
                    query.setAttendanceGroupId(attendanceRecordEntity.getAttendanceGroupId());
                    query.setAttendanceDate(attendanceRecordEntity.getAttendanceDate());

                    AttendanceRecordEntity recordEntity = attendanceRecordService.selectOne(query);
                    if (!ObjectUtils.isEmpty(recordEntity)) {
                        attendanceRecordEntity.setId(recordEntity.getId());
                        attendanceRecordEntity.setUpdateTime(new Date());
                        attendanceRecordEntity.setUpdateUserId(this.getContextUserId(context));
                        attendanceRecordService.update(attendanceRecordEntity, context);
                    } else {
                        attendanceRecordService.save(attendanceRecordEntity, context);
                    }
                }
            }
        }
    }

    private void genErrorAttendRecord(Context context, String dateStr, AttendanceRecordEntity attendanceRecordEntity, List<AttendanceRecordDetailEntity> detailEntityList) {
        //处理异常打卡，所以异常打卡的 都新增一条记录
        List<AttendanceRecordErrorEntity> errorEntityList = new ArrayList<>();
        for (AttendanceRecordDetailEntity recordDetailEntity : detailEntityList) {
            Integer goWorkResult = recordDetailEntity.getGoWorkResult();
            Integer offWorkResult = recordDetailEntity.getOffWorkResult();
            //判断上班
            AttendanceClassDetailEntity attendanceClassDetailEntity = attendanceClassDetailService.get(recordDetailEntity.getShiftsId());
            if (ObjectUtils.isEmpty(attendanceClassDetailEntity)) continue;
            AttendanceRecordErrorEntity errorEntity = new AttendanceRecordErrorEntity();
            errorEntity.initAttrValue();
            BeanUtils.copyProperties(attendanceRecordEntity, errorEntity, new String[]{"id"});
            errorEntity.setGoOffDateTime(errorEntity.getErrorDateTime());
            errorEntity.setProcessStatus(YesNoEnum.NO.getValue());
            errorEntity.setCreateTime(new Date());
            errorEntity.setCreateUserId(this.getContextUserId(context));

            //判断上班
            if (GoWorkResultEnum.正常.getValue() != goWorkResult) {
                Date goWorkDate = attendanceClassDetailEntity.getGoWorkDate();
                errorEntity.setGoOffDateTime(goWorkDate);
                if (!ObjectUtils.isEmpty(recordDetailEntity.getGoWorkDate())) {
                    errorEntity.setErrorDateTime(recordDetailEntity.getGoWorkDate());
                    errorEntity.setActualAttendanceDateTime(recordDetailEntity.getGoWorkDate());
                } else {
                    errorEntity.setErrorDateTime(DateUtil.parseDateTime(dateStr + " " + DateUtil.formatTime(goWorkDate)).toJdkDate());
                }

                if (GoWorkResultEnum.早退.getValue() == recordDetailEntity.getGoWorkResult()) {
                    errorEntity.setErrorStatus(ErrorStatusEnum.早退.getValue());
                }
                if (GoWorkResultEnum.迟到.getValue() == recordDetailEntity.getGoWorkResult()) {
                    errorEntity.setErrorStatus(ErrorStatusEnum.迟到.getValue());
                }
                if (GoWorkResultEnum.缺卡.getValue() == recordDetailEntity.getGoWorkResult()) {
                    errorEntity.setErrorStatus(ErrorStatusEnum.缺卡.getValue());
                }

                errorEntityList.add(errorEntity);
            }
            //判断下班
            if (OffWorkResultEnum.正常.getValue() != offWorkResult) {
                Date offWorkDate = attendanceClassDetailEntity.getOffWorkDate();
                errorEntity.setGoOffDateTime(offWorkDate);
                if (!ObjectUtils.isEmpty(recordDetailEntity.getGoWorkDate())) {
                    errorEntity.setErrorDateTime(recordDetailEntity.getGoWorkDate());
                    errorEntity.setActualAttendanceDateTime(recordDetailEntity.getGoWorkDate());
                } else {
                    errorEntity.setErrorDateTime(DateUtil.parseDateTime(dateStr + " " + DateUtil.formatTime(offWorkDate)).toJdkDate());
                }
                if (OffWorkResultEnum.缺卡.getValue() == recordDetailEntity.getOffWorkResult()) {
                    errorEntity.setErrorStatus(ErrorStatusEnum.缺卡.getValue());
                }
                if (OffWorkResultEnum.早退.getValue() == recordDetailEntity.getOffWorkResult()) {
                    errorEntity.setErrorStatus(ErrorStatusEnum.早退.getValue());
                }
                if (OffWorkResultEnum.迟到.getValue() == recordDetailEntity.getOffWorkResult()) {
                    errorEntity.setErrorStatus(ErrorStatusEnum.迟到.getValue());
                }
                errorEntityList.add(errorEntity);
            }

        }

        if (!ObjectUtils.isEmpty(errorEntityList)) {
            errorService.save(errorEntityList, context);
        }
    }

    private void checkAttendGroupByOne(AttendanceRecordEntity attendanceRecordEntity, List<AttendanceRecordDetailEntity> detailEntityList, Date attendanceDate, AttendanceGroupStaffEntity attendanceGroupStaffEntity, Context context) {
        //查询考勤组
        AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.get(attendanceGroupStaffEntity.getGroupId(), context);
        attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity == null ? 0L : attendanceGroupEntity.getId());
        attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity == null ? "" : attendanceGroupEntity.getGroupName());
        //查询考勤组详细信息获取班次 todo 默认固定班次 还有自由 与排班制
        AttendanceWorkAbstract workAbstract = AttendanceWorkAbstract.newType(attendanceGroupEntity.getType());
        CommonData commonData = new CommonData();
        commonData.setAttendanceGroupEntity(attendanceGroupEntity);
        commonData.setAttendanceDate(attendanceDate);
        commonData.setDetailEntityList(detailEntityList);
        commonData.setStaffId(attendanceRecordEntity.getStaffId());
        workAbstract.doHandleWork(commonData);

/*
        AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(new AttendanceGroupFixedworkQuery()
                .groupId(attendanceGroupEntity.getId()));
        //判断当前考勤日期为周几
        String week = this.getWeek(attendanceDate);
        //log.info("week:{}", week);
        //通过考勤日期的星期拿到班次id.
        if (ObjectUtils.isEmpty(attendanceGroupFixedworkEntity)) {
            log.info("attendanceGroupFixedworkEntity is null ! groupId:{}", attendanceGroupEntity.getId());
            return;
        }
        //获取当前日期的详细班次
        Long weekClassId = this.getWeekClassId(attendanceGroupFixedworkEntity, week);
        //log.info("weekClassId:{}", weekClassId);
        //weekClassId为-1 则不在考勤
        if (weekClassId == -1L) {
            //跳过本次循环
            return;
        }
        //获取班次信息
        AttendanceClassEntity attendanceClassEntity = attendanceClassService.get(weekClassId);
        if (ObjectUtils.isEmpty(attendanceClassEntity)) {
            log.info("attendanceClassEntity is null ,weekClassId:{}", weekClassId);
            return;
        }

        //List<AttendanceClassDetailEntity> attendanceClassDetailEntities = attendanceClassDetailService.find(new AttendanceClassDetailQuery().classId(attendanceClassEntity.getId()));
        //获取当前班的多个班次，一个班次为一个上下班时间段
        List<AttendanceClassDetailEntity> attendanceClassDetailEntities = attendanceClassEntity.getAttendanceClassDetailList();
        Integer orderNum = 0;
        //构造数据
        if (ObjectUtils.isEmpty(detailEntityList)) {
            //遍历班次 构建详细考勤数据
            for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntities) {
                orderNum++;
                AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                recordDetailEntity.setShiftsId(classDetailEntity.getId());
                recordDetailEntity.setShiftsName(classDetailEntity.getClassName());
                recordDetailEntity.setGoWorkResult(GoWorkResultEnum.缺卡.getValue());
                recordDetailEntity.setOffWorkResult(GoWorkResultEnum.缺卡.getValue());
                recordDetailEntity.setOrderNum(orderNum);
                detailEntityList.add(recordDetailEntity);
            }
        }
        for (AttendanceClassDetailEntity classDetail : attendanceClassDetailEntities) {
            //计算区间范围
            //上班打卡前时间
            Date goWorkDateBefore = DateUtil.offsetMinute(classDetail.getGoWorkDate(), -classDetail.getGoWorkDateBefore());
            //上班打卡后时间
            Date goWorkDateAfter = DateUtil.offsetMinute(classDetail.getGoWorkDate(), classDetail.getGoWorkDateAfter());
            // 下班打卡前时间
            Date offWorkDateBefore = DateUtil.offsetMinute(classDetail.getOffWorkDate(), -classDetail.getOffWorkDateBefore());
            //下班打卡后时间
            Date offWorkDateAfter = DateUtil.offsetMinute(classDetail.getOffWorkDate(), classDetail.getOffWorkDateAfter());
            //判断考勤时间是否在上班打卡区间内  true 在 false 不在  格式化时间
            //String currDate = DateUtil.formatTime(attendanceDate);
            Date curDateTime = DateUtil.parseTime(DateUtil.formatTime(attendanceDate)).toJdkDate();
            boolean goInTime = DateUtil.isIn(curDateTime, goWorkDateBefore, goWorkDateAfter);
            //判断考勤时间是否在下班打卡区间内
            boolean offInTime = DateUtil.isIn(curDateTime, offWorkDateBefore, offWorkDateAfter);
            if (goInTime == false && offInTime == false) {
                log.info("考勤时间不在上班区间与下班区间中，不做处理！attendDate:{}", DateUtil.formatDateTime(attendanceDate));
                continue;
            }
            if (goInTime) {
                //如果是上班打卡区间范围,判断是否迟到
                boolean inTime = DateUtil.isIn(curDateTime, goWorkDateBefore, classDetail.getGoWorkDate());
                if (inTime) {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.正常.getValue());
                            attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                        }
                    }
                } else {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.迟到.getValue());
                            attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                        }
                    }
                }
            }
            if (offInTime) {
                boolean inTime = DateUtil.isIn(curDateTime, classDetail.getOffWorkDate(), offWorkDateAfter);
                if (inTime) {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.正常.getValue());
                            attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                        }
                    }
                } else {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.早退.getValue());
                            attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                        }
                    }
                }
            }
        }*/
    }


    public String getWeek(Date date) {
        // Get the day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday)
        int dayOfWeek = DateUtil.dayOfWeek(date);
        // Map the numeric day of the week to its corresponding Chinese name
        String[] dayOfWeekNames = new String[]{
                "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
        };
        // Get the Chinese name of the day of the week
        String dayOfWeekName = dayOfWeekNames[dayOfWeek - 1];
        return dayOfWeekName;
    }

    /**
     * 获取通过星期获取班次id  如果返参为-1 则不在考勤
     *
     * @return
     */
    public Long getWeekClassId(AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity, String week) {
        Long weekClassId = 0L;
        switch (week) {
            case "星期一":
                if (attendanceGroupFixedworkEntity.getMonday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getMondayClassId();
                break;
            case "星期二":
                if (attendanceGroupFixedworkEntity.getTuesday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getTuesdayClassId();
                break;
            case "星期三":
                if (attendanceGroupFixedworkEntity.getWednesday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getWednesdayClassId();
                break;
            case "星期四":
                if (attendanceGroupFixedworkEntity.getThursday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getThursdayClassId();
                break;
            case "星期五":
                if (attendanceGroupFixedworkEntity.getFriday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getFridayClassId();
                break;
            case "星期六":
                if (attendanceGroupFixedworkEntity.getSaturday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getSaturdayClassId();
                break;
            case "星期日":
                if (attendanceGroupFixedworkEntity.getSunday() == 0) {
                    weekClassId = -1L;
                    return weekClassId;
                }
                weekClassId = attendanceGroupFixedworkEntity.getSundayClassId();
                break;
        }
        return weekClassId;
    }

}
   /*  //map遍历.先把所有键取出来
        Set<String> workNumSet = listMap.keySet();
        //根据键取出value
        for (String workNum : workNumSet) {
            List<AttendanceRecordHikEntity> attendanceRecordHikEntities1 = listMap.get(workNum);
            //对按照工号分组了的数据attendanceRecordHikEntities进行时间分组
            // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // Map<String, List<AttendanceRecordHikEntity>> map = attendanceRecordHikEntities1.stream().collect(Collectors.groupingBy(hikEntity -> hikEntity.getAttendanceDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().format(formatter)));
            Map<String, List<AttendanceRecordHikEntity>> map = attendanceRecordHikEntities1.stream().collect(Collectors.groupingBy(hikEntity -> DateUtil.formatDate(hikEntity.getAttendanceDate())));
            Set<String> dateSet = map.keySet();
            for (String s : dateSet) {
                List<AttendanceRecordHikEntity> attendanceRecordHikEntities = map.get(s);
                //对相同工号的人员进行遍历判断考勤规则.
                AttendanceRecordEntity attendanceRecordEntity = new AttendanceRecordEntity();
                attendanceRecordEntity.initAttrValue();
                List<AttendanceRecordDetailEntity> detailEntityList = new ArrayList<>();
                for (AttendanceRecordHikEntity item : attendanceRecordHikEntities) {
                    //对主表的对象进行赋值
                    if (StringUtils.isNull(attendanceRecordEntity) || StringUtils.isEmpty(attendanceRecordEntity.getStaffName())) {
                        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        attendanceRecordEntity.setStaffId(item.getStaffId());
                        attendanceRecordEntity.setStaffName(item.getStaffName());
                        attendanceRecordEntity.setDeptId(item.getDeptId());
                        attendanceRecordEntity.setDeptName(item.getDeptName());
                        attendanceRecordEntity.setPositionId(item.getPositionId());
                        attendanceRecordEntity.setPositionName(item.getPositionName());
                        attendanceRecordEntity.setWorkNum(item.getWorkNum());
                        //attendanceRecordEntity.setAttendanceDate(sdf.parse(sdf.format(item.getAttendanceDate())));
                        attendanceRecordEntity.setAttendanceDate(item.getAttendanceDate());
                        attendanceRecordEntity.setCreateTime(new Date());
                        attendanceRecordEntity.setCreateUserId(this.getContextUserId(context));
                    }
                    //获取考勤打卡时间
                    Date attendanceDate = item.getAttendanceDate();
                    //通过工号查询员工信息
                    //StaffEntity staffEntity = staffService.selectOne(new StaffQuery().workNum(item.getWorkNum()));
                    StaffEntity staffEntity = staffService.getExtCache(StrUtil.padPre(item.getWorkNum(), 8, "0"));
                    if (ObjectUtils.isEmpty(staffEntity)) {
                        log.info("staffEntity is null ,workNum:{}", StrUtil.padPre(item.getWorkNum(), 8, "0"));
                        continue;
                    }
                    //通过员工id查询考勤人员信息
                    List<AttendanceGroupStaffEntity> attendanceGroupStaffEntities = attendanceGroupStaffService.find(new AttendanceGroupStaffQuery().staffId(staffEntity.getId()));
                    //查询该考勤人员,如果查出来的考勤人员信息list长度为1 则该考勤人员只有一个考勤组
                    if (StringUtils.isNotNull(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size() == 1) {
                        //查询考勤组
                        AttendanceGroupQuery attendanceGroupQuery = new AttendanceGroupQuery();
                        AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.selectOne(attendanceGroupQuery.id(attendanceGroupStaffEntities.get(0).getGroupId()));
                        attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity.getId());
                        attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity.getGroupName());
                        //log.info("attendanceGroupEntity.getId()=={}", attendanceGroupEntity.getId());
                        //查询考勤组详细信息获取班次
                        AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(new AttendanceGroupFixedworkQuery()
                                .groupId(attendanceGroupEntity.getId()));
                        //判断当前考勤日期为周几
                        Date attendanceDate1 = attendanceDate;
                        String week = this.getWeek(attendanceDate1);
                        //log.info("week:{}", week);
                        //通过考勤日期的星期拿到班次id.
                        if (ObjectUtils.isEmpty(attendanceGroupFixedworkEntity)) {
                            log.info("attendanceGroupFixedworkEntity is null ! groupId:{}", attendanceGroupEntity.getId());
                            continue;
                        }
                        Long weekClassId = this.getWeekClassId(attendanceGroupFixedworkEntity, week);
                        //log.info("weekClassId:{}", weekClassId);
                        //weekClassId为-1 则不在考勤
                        if (weekClassId == -1L) {
                            //跳过本次循环
                            continue;
                        }
                        //获取班次信息
                        //AttendanceClassEntity attendanceClassEntity = attendanceClassService.selectOne(new AttendanceClassQuery().id(weekClassId));
                        AttendanceClassEntity attendanceClassEntity = attendanceClassService.get(weekClassId);
                        if (ObjectUtils.isEmpty(attendanceClassEntity)) {
                            log.info("attendanceClassEntity is null ,weekClassId:{}", weekClassId);
                            continue;
                        }

                        //获取班次详细信息
                        AttendanceClassDetailQuery attendanceClassDetailQuery = new AttendanceClassDetailQuery();
                        List<AttendanceClassDetailEntity> attendanceClassDetailEntities = attendanceClassDetailService.find(attendanceClassDetailQuery.classId(attendanceClassEntity.getId()));
                        Integer orderNum = 0;
                        if (ObjectUtils.isEmpty(detailEntityList)) {
                            for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntities) {
                                orderNum++;
                                AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                                recordDetailEntity.setShiftsId(classDetailEntity.getId());
                                recordDetailEntity.setShiftsName(classDetailEntity.getClassName());
                                recordDetailEntity.setGoWorkResult(GoWorkResultEnum.缺卡.getValue());
                                recordDetailEntity.setOffWorkResult(GoWorkResultEnum.缺卡.getValue());
                                recordDetailEntity.setOrderNum(orderNum);
                                detailEntityList.add(recordDetailEntity);
                            }
                        }
                        for (AttendanceClassDetailEntity classDetail : attendanceClassDetailEntities) {
                            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                            //上班打卡前时间
                            Date goWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), -classDetail.getGoWorkDateBefore())));
                            //上班打卡后时间
                            Date goWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), classDetail.getGoWorkDateAfter())));
                            //下班打卡前时间
                            Date offWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), -classDetail.getOffWorkDateBefore())));
                            //下班打卡后时间
                            Date offWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), classDetail.getOffWorkDateAfter())));
                            //判断考勤时间是否在上班打卡区间内  true 在 false 不在
                            boolean goInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore, goWorkDateAfter);
                            //判断考勤时间是否在下班打卡区间内
                            boolean offInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), offWorkDateBefore, offWorkDateAfter);
                            if (goInTime == false && offInTime == false) {
                                continue;
                            }
                            if (goInTime) {
                                //如果是上班打卡,判断是否迟到
                                boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore, sdf.parse(sdf.format(classDetail.getGoWorkDate())));
                                if (!inTime) {
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                        if (attendanceRecordDetailEntity.getShiftsId().longValue() == classDetail.getId().longValue()) {
                                            attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.迟到.getValue());
                                            attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                        }
                                    }
                                }
                                if (inTime) {
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                        if (attendanceRecordDetailEntity.getShiftsId().longValue() == classDetail.getId().longValue()) {
                                            attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.正常.getValue());
                                            attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                        }
                                    }
                                }
                            }
                            if (offInTime) {
                                boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)), sdf.parse(sdf.format(classDetail.getOffWorkDate())), offWorkDateAfter);
                                if (!inTime) {
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                        if (attendanceRecordDetailEntity.getShiftsId().longValue() == classDetail.getId().longValue()) {
                                            attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.早退.getValue());
                                            attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                        }
                                    }
                                }
                                if (inTime) {
                                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                        if (attendanceRecordDetailEntity.getShiftsId().longValue() == classDetail.getId().longValue()) {
                                            attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.正常.getValue());
                                            attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //如果考勤人员信息list长度大于1 则该考勤人员有多个考勤组.
                    if (StringUtils.isNotNull(attendanceGroupStaffEntities) && attendanceGroupStaffEntities.size() > 1) {
                        for (AttendanceGroupStaffEntity groupStaffEntity : attendanceGroupStaffEntities) {
                            {
                                //查询考勤组
                                AttendanceGroupQuery attendanceGroupQuery = new AttendanceGroupQuery();
                                AttendanceGroupEntity attendanceGroupEntity = attendanceGroupService.selectOne(attendanceGroupQuery.id(groupStaffEntity.getGroupId()));
                                //查询考勤组详细信息获取班次
                                AttendanceGroupFixedworkQuery attendanceGroupFixedworkQuery = new AttendanceGroupFixedworkQuery();
                                AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(attendanceGroupFixedworkQuery.groupId(attendanceGroupEntity.getId()));
                                //判断当前考勤日期为周几
                                Date attendanceDate1 = attendanceDate;
                                String week = this.getWeek(attendanceDate1);
                                //通过考勤日期的星期拿到班次id.
                                Long weekClassId = this.getWeekClassId(attendanceGroupFixedworkEntity, week);
                                //weekClassId为-1 则不在考勤
                                if (weekClassId == -1L) {
                                    //跳过本次循环
                                    continue;
                                }
                                attendanceRecordEntity.setAttendanceGroupId(attendanceGroupEntity.getId());
                                attendanceRecordEntity.setAttendanceGroupName(attendanceGroupEntity.getGroupName());
                                //获取班次信息
                                AttendanceClassQuery attendanceClassQuery = new AttendanceClassQuery();
                                AttendanceClassEntity attendanceClassEntity = attendanceClassService.selectOne(attendanceClassQuery.id(weekClassId));
                                //获取班次详细信息
                                AttendanceClassDetailQuery attendanceClassDetailQuery = new AttendanceClassDetailQuery();
                                List<AttendanceClassDetailEntity> attendanceClassDetailEntities = attendanceClassDetailService.find(attendanceClassDetailQuery.classId(attendanceClassEntity.getId()));
                                Integer orderNum = 0;
                                if (detailEntityList != null || detailEntityList.size() == 0) {
                                    for (AttendanceClassDetailEntity classDetailEntity : attendanceClassDetailEntities) {
                                        orderNum++;
                                        AttendanceRecordDetailEntity recordDetailEntity = new AttendanceRecordDetailEntity();
                                        recordDetailEntity.setShiftsId(classDetailEntity.getId());
                                        recordDetailEntity.setShiftsName(classDetailEntity.getClassName());
                                        recordDetailEntity.setGoWorkResult(GoWorkResultEnum.缺卡.getValue());
                                        recordDetailEntity.setOffWorkResult(GoWorkResultEnum.缺卡.getValue());
                                        recordDetailEntity.setOrderNum(orderNum);
                                        detailEntityList.add(recordDetailEntity);
                                    }
                                }
                                for (AttendanceClassDetailEntity classDetail : attendanceClassDetailEntities) {
                                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                                    //上班打卡前时间
                                    Date goWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), -classDetail.getGoWorkDateBefore())));
                                    //上班打卡后时间
                                    Date goWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getGoWorkDate(), classDetail.getGoWorkDateAfter())));
                                    //下班打卡前时间
                                    Date offWorkDateBefore = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), -classDetail.getOffWorkDateBefore())));
                                    //下班打卡后时间
                                    Date offWorkDateAfter = sdf.parse(sdf.format(DateUtils.addMinutes(classDetail.getOffWorkDate(), classDetail.getOffWorkDateAfter())));
                                    //判断考勤时间是否在上班打卡区间内  true 在 false 不在
                                    boolean goInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore, goWorkDateAfter);
                                    //判断考勤时间是否在下班打卡区间内
                                    boolean offInTime = isInTime(sdf.parse(sdf.format(attendanceDate)), offWorkDateBefore, offWorkDateAfter);
                                    if (goInTime == false && offInTime == false) {
                                        continue;
                                    }
                                    if (goInTime) {
                                        //如果是上班打卡,判断是否迟到
                                        boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)), goWorkDateBefore, sdf.parse(sdf.format(classDetail.getGoWorkDate())));
                                        if (!inTime) {
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getId())) {
                                                    attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.迟到.getValue());
                                                    attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                        if (inTime) {
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getId())) {
                                                    attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.正常.getValue());
                                                    attendanceRecordDetailEntity.setGoWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                    }
                                    if (offInTime) {
                                        boolean inTime = isInTime(sdf.parse(sdf.format(attendanceDate)), sdf.parse(sdf.format(classDetail.getOffWorkDate())), offWorkDateAfter);
                                        if (!inTime) {
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getId())) {
                                                    attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.早退.getValue());
                                                    attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                        if (inTime) {
                                            for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : detailEntityList) {
                                                if (attendanceRecordDetailEntity.getShiftsId().equals(classDetail.getId())) {
                                                    attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.正常.getValue());
                                                    attendanceRecordDetailEntity.setOffWorkDate(attendanceDate);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                List<AttendanceRecordErrorEntity> errorEntityList = new ArrayList<>();
                for (AttendanceRecordDetailEntity recordDetailEntity : detailEntityList) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    SimpleDateFormat sdft = new SimpleDateFormat("HH:mm:ss");
                    Integer goWorkResult = recordDetailEntity.getGoWorkResult();
                    Integer offWorkResult = recordDetailEntity.getOffWorkResult();

                    if (GoWorkResultEnum.正常.getValue() != goWorkResult) {
                        AttendanceRecordErrorEntity errorEntity = new AttendanceRecordErrorEntity();
                        errorEntity.setStaffId(attendanceRecordEntity.getStaffId());
                        errorEntity.setStaffName(attendanceRecordEntity.getStaffName());
                        errorEntity.setWorkNum(attendanceRecordEntity.getWorkNum());
                        errorEntity.setDeptId(attendanceRecordEntity.getDeptId());
                        errorEntity.setDeptName(attendanceRecordEntity.getDeptName());
                        errorEntity.setPositionId(attendanceRecordEntity.getPositionId());
                        errorEntity.setPositionName(attendanceRecordEntity.getPositionName());
                        errorEntity.setShiftsId(recordDetailEntity.getShiftsId());
                        errorEntity.setShiftsName(recordDetailEntity.getShiftsName());
                        Date goWorkDate = attendanceClassDetailService.selectOne(new AttendanceClassDetailQuery().id(recordDetailEntity.getShiftsId())).getGoWorkDate();
                        errorEntity.setGoOffDateTime(goWorkDate);
                        if (recordDetailEntity.getGoWorkDate() != null && !recordDetailEntity.getGoWorkDate().equals("")) {
                            errorEntity.setErrorDateTime(recordDetailEntity.getGoWorkDate());
                            errorEntity.setActualAttendanceDateTime(recordDetailEntity.getGoWorkDate());
                        } else {
                            String s1 = s + " " + sdft.format(goWorkDate);
                            errorEntity.setErrorDateTime(sdf.parse(s1));
                            errorEntity.setActualAttendanceDateTime(sdf.parse(s1));
                        }
                        if (GoWorkResultEnum.缺卡.getValue() == recordDetailEntity.getGoWorkResult()) {

                            errorEntity.setErrorStatus(ErrorStatusEnum.缺卡.getValue());
                        }
                        if (GoWorkResultEnum.迟到.getValue() == recordDetailEntity.getGoWorkResult()) {
                            errorEntity.setErrorStatus(ErrorStatusEnum.迟到.getValue());
                        }
                        errorEntity.setProcessStatus(0);
                        errorEntity.setCreateTime(new Date());
                        errorEntity.setCreateUserId(this.getContextUserId(context));
                        errorEntityList.add(errorEntity);
                    }
                    if (offWorkResult != 1) {
                        AttendanceRecordErrorEntity errorEntity = new AttendanceRecordErrorEntity();
                        errorEntity.setStaffId(attendanceRecordEntity.getStaffId());
                        errorEntity.setStaffName(attendanceRecordEntity.getStaffName());
                        errorEntity.setWorkNum(attendanceRecordEntity.getWorkNum());
                        errorEntity.setDeptId(attendanceRecordEntity.getDeptId());
                        errorEntity.setDeptName(attendanceRecordEntity.getDeptName());
                        errorEntity.setPositionId(attendanceRecordEntity.getPositionId());
                        errorEntity.setPositionName(attendanceRecordEntity.getPositionName());
                        errorEntity.setShiftsId(recordDetailEntity.getShiftsId());
                        errorEntity.setShiftsName(recordDetailEntity.getShiftsName());
                        Date offWorkDate = attendanceClassDetailService.selectOne(new AttendanceClassDetailQuery().id(recordDetailEntity.getShiftsId())).getOffWorkDate();
                        errorEntity.setGoOffDateTime(offWorkDate);
                        if (recordDetailEntity.getOffWorkDate() != null && !recordDetailEntity.getOffWorkDate().equals("")) {
                            errorEntity.setErrorDateTime(recordDetailEntity.getOffWorkDate());
                            errorEntity.setActualAttendanceDateTime(recordDetailEntity.getOffWorkDate());
                        } else {
                            errorEntity.setErrorDateTime(sdf.parse(s + " " + sdft.format(offWorkDate)));
                            errorEntity.setActualAttendanceDateTime(sdf.parse(s + " " + sdft.format(offWorkDate)));
                        }
                        if (recordDetailEntity.getOffWorkResult() == 3) {
                            errorEntity.setErrorStatus(0);
                        }
                        if (recordDetailEntity.getOffWorkResult() == 5) {
                            errorEntity.setErrorStatus(1);
                        }
                        errorEntity.setProcessStatus(0);
                        errorEntity.setCreateTime(new Date());
                        errorEntity.setCreateUserId(this.getContextUserId(context));
                        errorEntityList.add(errorEntity);
                    }

                }
                if (detailEntityList != null && detailEntityList.size() > 0) {
                    if (errorEntityList != null && errorEntityList.size() > 0) {
                        errorService.save(errorEntityList, context);
                    }
                    attendanceRecordEntity.setShiftsName(detailEntityList.get(0).getShiftsName());
                    attendanceRecordEntity.setShiftsId(attendanceClassDetailService.selectOne(new AttendanceClassDetailQuery().id(detailEntityList.get(0).getShiftsId())).getClassId());
                    attendanceRecordEntity.setAttendanceRecordDetailList(detailEntityList);

                    //判断签到 只要存在一次正常打卡详细记录 就判断签到
                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (GoWorkResultEnum.正常.getValue() == detailEntity.getGoWorkResult() || GoWorkResultEnum.迟到.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setSignInResult(YesNoEnum.YES.getValue());
                            break;
                        }
                    }

                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (OffWorkResultEnum.正常.getValue() == detailEntity.getOffWorkResult() || OffWorkResultEnum.早退.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setSignOutResult(YesNoEnum.YES.getValue());
                            break;
                        }
                    }

                    for (AttendanceRecordDetailEntity detailEntity : detailEntityList) {
                        if (OffWorkResultEnum.缺卡.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setPunchResult(YesNoEnum.NO.getValue());
                            break;
                        }

                        if (OffWorkResultEnum.缺卡.getValue() == detailEntity.getOffWorkResult()) {
                            attendanceRecordEntity.setPunchResult(YesNoEnum.NO.getValue());
                            break;
                        }
                    }

                    if (YesNoEnum.YES.getValue() == attendanceRecordEntity.getSignInResult() && YesNoEnum.YES.getValue() == attendanceRecordEntity.getSignOutResult()) {
                        attendanceRecordEntity.setPunchResult(YesNoEnum.YES.getValue());
                    }

                    attendanceRecordEntity.setAttendType("现场打卡");
                    //判断是否存在记录 如果存在 则更新  维度   staffId  attendanceDate shiftsId attendanceGroupId

                    AttendanceRecordQuery query = new AttendanceRecordQuery();
                    query.setStaffId(attendanceRecordEntity.getStaffId());
                    query.setShiftsId(attendanceRecordEntity.getShiftsId());
                    query.setAttendanceGroupId(attendanceRecordEntity.getAttendanceGroupId());
                    query.setAttendanceDate(attendanceRecordEntity.getAttendanceDate());

                    AttendanceRecordEntity recordEntity = attendanceRecordService.selectOne(query);
                    if (!ObjectUtils.isEmpty(recordEntity)) {
                        attendanceRecordEntity.setId(recordEntity.getId());
                        attendanceRecordEntity.setUpdateTime(new Date());
                        attendanceRecordEntity.setUpdateUserId(this.getContextUserId(context));
                        attendanceRecordService.update(attendanceRecordEntity, context);
                    } else {
                        attendanceRecordService.save(attendanceRecordEntity, context);
                    }
                }
            }
        }*/