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

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.busiz.h5.req.AttendSaveReq;
import com.mortals.xhx.busiz.h5.web.ApiWebPerformController;
import com.mortals.xhx.common.code.ErrorStatusEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.attendance.model.AttendanceClassDetailEntity;
import com.mortals.xhx.module.attendance.model.AttendanceClassEntity;
import com.mortals.xhx.module.attendance.service.AttendanceClassDetailService;
import com.mortals.xhx.module.attendance.service.AttendanceClassService;
import com.mortals.xhx.module.perform.service.PerformAttendRecordService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
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 com.mortals.xhx.module.attendance.model.AttendanceRecordErrorEntity;
import com.mortals.xhx.module.attendance.service.AttendanceRecordErrorService;
import org.springframework.util.ObjectUtils;

import java.util.List;

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

    @Autowired
    private AttendanceClassDetailService classDetailService;

    @Autowired
    private AttendanceClassService attendanceClassService;
    @Autowired
    private PerformAttendRecordService attendRecordService;

    @Autowired
    private ApiWebPerformController apiWebPerformController;
    @Autowired
    private StaffService staffService;


    @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 updateAfter(AttendanceRecordErrorEntity entity, Context context) throws AppException {
        if (YesNoEnum.YES.getValue() == entity.getProcessStatus()) {
            //判断后打绩效
            String ruleCode = "";
            if (ErrorStatusEnum.早退.getValue() == entity.getErrorStatus()) {
                //判断时间差定义
                Long 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 if (ErrorStatusEnum.迟到.getValue() == entity.getErrorStatus()) {

                Long 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 if (ErrorStatusEnum.缺卡.getValue() == entity.getErrorStatus()) {
                ruleCode = "ATTEND1007";
            }
            if (!ObjectUtils.isEmpty(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("考勤:" + ErrorStatusEnum.getByValue(entity.getErrorStatus()).getDesc());
                attendSaveReq.setHappenTime(entity.getErrorDateTime());
                attendSaveReq.setRuleCode(ruleCode);
                attendSaveReq.setStaffId(entity.getStaffId());
                attendRecordService.saveAttend(attendSaveReq,context);
            }
        }
    }
}