package com.mortals.xhx.common.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.module.attendance.model.AttendanceRecordHikEntity;
import com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery;
import com.mortals.xhx.module.attendance.service.AttendanceRecordHikService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StopWatch;

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


@AllArgsConstructor
@Slf4j
public class AddAttendThread implements Runnable {


    private AttendanceRecordHikService hikService;

    private AttendanceRecordHikEntity hikEntity;

    private Context context;

    private ICacheService cacheService;


    public AddAttendThread(AttendanceRecordHikService hikService, AttendanceRecordHikEntity hikEntity, Context context) {
        this.hikService = hikService;
        this.hikEntity = hikEntity;
        this.context = context;
        this.cacheService = SpringUtils.getBean(ICacheService.class);
    }

    public AddAttendThread() {
        this.cacheService = SpringUtils.getBean(ICacheService.class);
    }

    @Override
    public void run() {
        log.info("考勤计算开始！");
        try {
            //天数区间分段计算
            DateTime attendStart = DateUtil.parseDate(hikEntity.getAttendanceDateStart());
            DateTime attendEnd = DateUtil.parseDate(hikEntity.getAttendanceDateEnd());
            Long compare = DateUtil.between(attendEnd, attendStart, DateUnit.DAY);
            StopWatch stopWatch = new StopWatch("stopwatch attend");
            log.info("考勤计算天数区间：{}", compare);
            for (int i = 0; i <= compare.intValue(); i++) {
                DateTime curDate = DateUtil.offsetDay(attendStart, i);
                log.info("考勤计算日期：{}", curDate.toDateStr());
                stopWatch.start("执行本地方法");
                hikEntity.setAttendanceDateStart(curDate.toDateStr());
                hikEntity.setAttendanceDateEnd(curDate.toDateStr());
                hikService.addAttendanceRecordByQuery(hikEntity, context);
                stopWatch.stop();
                log.info("考勤计算日期：{} 完成,耗时：{}ms", curDate.toDateStr(), stopWatch.getLastTaskTimeMillis());
            }
            //生成汇总信息
            log.info("生成考勤汇总消息开始。。。");
            AttendanceSummaryQuery query = new AttendanceSummaryQuery();
            if(!ObjectUtils.isEmpty(hikEntity.getStaffId())){
                query.setStaffId(hikEntity.getStaffId());
            }
            query.setSummaryTimeStart(attendStart.toDateStr());
            query.setSummaryTimeEnd(attendEnd.toDateStr());
            cacheService.lpush(KEY_ATTENDANCE_STAT_CACHE, query);
        } catch (Exception e) {
            log.error("考勤计算失败！", e);
        }
        log.info("考勤计算结束！");
    }

}
