package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.utils.AddAttendThread;
import com.mortals.xhx.module.attendance.model.AttendanceRecordHikEntity;
import com.mortals.xhx.module.attendance.model.AttendanceRecordHikQuery;
import com.mortals.xhx.module.attendance.service.AttendanceRecordHikService;
import com.mortals.xhx.module.attendance.service.AttendanceStatService;
import com.mortals.xhx.module.hik.door.model.req.door.DoorEventReq;
import com.mortals.xhx.module.hik.door.model.rsp.door.DoorEventDataInfo;
import com.mortals.xhx.module.hik.door.service.IHikDoorService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 同步获取本周门禁事件与计算
 */
@Slf4j
@Service("SyncDoorsEventWeekTask")
public class SyncDoorsEventWeekTaskImpl implements ITaskExcuteService {

    @Autowired
    private AttendanceRecordHikService recordHikService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        //同步本周所以人的门禁事件
        log.info("同步本周所有的门禁事}");
        DateTime weekStart = DateUtil.offsetDay(new Date(), -7);
        AttendanceRecordHikQuery hikEntity = new AttendanceRecordHikQuery();
        hikEntity.setAttendanceDateStart(DateUtil.format(weekStart, "yyyy-MM-dd"));
        hikEntity.setAttendanceDateEnd(DateUtil.today());

        //天数区间分段计算
        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 attend1");
        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());
            recordHikService.buildSourceHikRecord(hikEntity, null);
            stopWatch.stop();
            log.info("考勤计算日期：{} 完成,耗时：{}ms", curDate.toDateStr(), stopWatch.getLastTaskTimeMillis());
        }


        AddAttendThread addAttendThread = new AddAttendThread(recordHikService, hikEntity, null);
        ThreadPool.getInstance().execute(addAttendThread);
    }



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

    }
}
