package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.CheckTypeEnum;
import com.mortals.xhx.module.attendance.model.AttendanceRecordErrorQuery;
import com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery;
import com.mortals.xhx.module.attendance.service.AttendanceRecordErrorService;
import com.mortals.xhx.module.attendance.service.AttendanceStatService;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

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

/**
 * 考勤汇总
 */
@Slf4j
@Service("AttendanceSummaryTask")
public class AttendanceSummaryTaskImpl implements ITaskExcuteService {

    @Autowired
    private ICacheService cacheService;
    @Autowired
    private AttendanceRecordErrorService attendanceRecordErrorService;


    @Override
    public void excuteTask(ITask task) throws AppException {
        //考勤汇总前先自动处理前一天的异常考勤数据
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.add(Calendar.DAY_OF_MONTH, -1); //默认查前一天数据
//        attendanceRecordErrorService.doAutoProcess(now.getTime());

        AttendanceSummaryQuery query = new AttendanceSummaryQuery();
        query.setSummaryTimeEnd(DateUtils.getStrDate(now.getTime()));
        now.add(Calendar.DAY_OF_MONTH, -10);
        query.setSummaryTimeStart(DateUtils.getStrDate(now.getTime()));
        log.info("考勤汇总时间:" + DateUtils.getDateTime(now.getTime(), "yyyy-MM-dd HH:mm:ss"));
        //重新汇总最近10天的考勤数据
        cacheService.lpush(KEY_ATTENDANCE_STAT_CACHE, query);


        //统计添加前一天考勤中绩效异常记录
        log.info("统计添加前一天考勤中绩效异常记录开始");
        AttendanceRecordErrorQuery errorQuery = new AttendanceRecordErrorQuery();
        errorQuery.setErrorDateTimeStart(DateUtil.yesterday().toDateStr());
        errorQuery.setErrorDateTimeEnd(DateUtil.yesterday().toDateStr());
        attendanceRecordErrorService.doReprocess(errorQuery, null);
        log.info("统计添加前一天考勤中绩效异常记录结束");

        try {
            log.info("汇总最近两个月绩效分数开始");
            StaffCheckSummaryQuery summaryQuery = new StaffCheckSummaryQuery();
            Calendar now1 = Calendar.getInstance();
            now1.setTime(new Date());
            now1.add(Calendar.MONTH,-1);
            summaryQuery.setCheckTimeStart(DateUtils.getStrDate(now1.getTime()));
            summaryQuery.setCheckTimeEnd(DateUtils.getCurrStrDate());
            cacheService.lpush(KEY_CHECK_SUMMARY_CACHE, summaryQuery);
            summaryQuery.setCheckTimeStart(DateUtils.getCurrStrDate());
            summaryQuery.setCheckTimeEnd(DateUtils.getCurrStrDate());
            cacheService.lpush(KEY_CHECK_SUMMARY_CACHE, summaryQuery);
            log.info("汇总最近两个月绩效分数结束");
        } catch (Exception e) {
            log.error("汇总最近两个月绩效分数出错", e);
        }
    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }

}
