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

import cn.hutool.core.date.DateUtil;
import com.mortals.xhx.common.code.GoWorkResultEnum;
import com.mortals.xhx.common.code.TypeEnum;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.service.AttendanceClassService;
import com.mortals.xhx.module.attendance.service.AttendanceGroupFixedworkService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 固定班
 * @author:
 * @date: 2023/7/31 15:15
 */
@Slf4j
public class FixWorkAttendance extends AttendanceWorkAbstract {


    private AttendanceGroupFixedworkService attendanceGroupFixedworkService;

    private AttendanceClassService attendanceClassService;


    public FixWorkAttendance(int type) {
        super(type);
        this.attendanceGroupFixedworkService = SpringUtils.getBean(AttendanceGroupFixedworkService.class);
        this.attendanceClassService = SpringUtils.getBean(AttendanceClassService.class);

    }

    @Override
    int getType() {
        return TypeEnum.固定班制.getValue();
    }

    @Override
    void initData() {

    }

    @Override
    void doHandleWork(CommonData commonData) {


        AttendanceGroupFixedworkEntity attendanceGroupFixedworkEntity = attendanceGroupFixedworkService.selectOne(new AttendanceGroupFixedworkQuery()
                .groupId(commonData.getAttendanceGroupEntity().getId()));
        //判断当前考勤日期为周几

        String week = super.getWeek(commonData.getAttendanceDate());
        //log.info("week:{}", week);
        //通过考勤日期的星期拿到班次id.
        if (ObjectUtils.isEmpty(attendanceGroupFixedworkEntity)) {
            log.info("attendanceGroupFixedworkEntity is null ! groupId:{}", commonData.getAttendanceGroupEntity().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(commonData.getDetailEntityList())) {
            //遍历班次 构建详细考勤数据
            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);
                commonData.getDetailEntityList().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(commonData.getAttendanceDate())).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(commonData.getAttendanceDate()));
                continue;
            }
            if (goInTime) {
                //如果是上班打卡区间范围,判断是否迟到
                boolean inTime = DateUtil.isIn(curDateTime, goWorkDateBefore, classDetail.getGoWorkDate());
                if (inTime) {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : commonData.getDetailEntityList()) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.正常.getValue());
                            attendanceRecordDetailEntity.setGoWorkDate(commonData.getAttendanceDate());
                        }
                    }
                } else {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : commonData.getDetailEntityList()) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setGoWorkResult(GoWorkResultEnum.迟到.getValue());
                            attendanceRecordDetailEntity.setGoWorkDate(commonData.getAttendanceDate());
                        }
                    }
                }
            }
            if (offInTime) {
                boolean inTime = DateUtil.isIn(curDateTime, classDetail.getOffWorkDate(), offWorkDateAfter);
                if (inTime) {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : commonData.getDetailEntityList()) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.正常.getValue());
                            attendanceRecordDetailEntity.setOffWorkDate(commonData.getAttendanceDate());
                        }
                    }
                } else {
                    for (AttendanceRecordDetailEntity attendanceRecordDetailEntity : commonData.getDetailEntityList()) {
                        if (attendanceRecordDetailEntity.getShiftsId() == classDetail.getId()) {
                            attendanceRecordDetailEntity.setOffWorkResult(GoWorkResultEnum.早退.getValue());
                            attendanceRecordDetailEntity.setOffWorkDate(commonData.getAttendanceDate());
                        }
                    }
                }
            }
        }
    }


}
