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

import cn.hutool.core.date.DateUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.common.code.AppealResultEnum;
import com.mortals.xhx.module.attendance.dao.AttendanceRecordErrorDao;
import com.mortals.xhx.module.attendance.model.*;
import com.mortals.xhx.module.attendance.model.vo.ErrorSummaryVo;
import com.mortals.xhx.module.attendance.service.AttendanceClassDetailService;
import com.mortals.xhx.module.attendance.service.AttendanceLeaveRecordService;
import com.mortals.xhx.module.attendance.service.AttendanceRecordErrorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 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 AttendanceLeaveRecordService attendanceLeaveRecordService;


    @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("考勤异常数据不存在");
        }
    }



    @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 List<ErrorSummaryVo> getMonthTop10(AttendanceRecordErrorQuery query) {
        return dao.getMonthTop10(query);
    }

}