package com.mortals.xhx.daemon.task;

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.DateUtils;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.common.code.YesNoEnum;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * 同步获取下午海康门禁事件
 */
@Slf4j
@Service("SyncDoorsEventAfterTask")
public class SyncDoorsEventAfterTaskImpl implements ITaskExcuteService {

    @Autowired
    private AttendanceRecordHikService recordHikService;
    @Autowired
    private AttendanceStatService attendanceStatService;

    @Value("${hik.host:}")
    protected String hikhost;


    @Override
    public void excuteTask(ITask task) throws AppException {
        // syncDoorEvents();
        //判断时间段 不在时间段的 不做计算
        //早上9点 子晚上8点 计算
        Date beginDateTime = DateUtil.parseDateTime(DateUtil.today() + " 07:00:00");
        Date endDateTime = DateUtil.parseDateTime(DateUtil.today() + " 23:30:00");

        boolean in = DateUtil.isIn(new Date(), beginDateTime, endDateTime);
        if (in) {
            calculateAttendByDay();
        }
        //统计当日
        log.info("统计当日考勤");
        attendanceStatService.homeStat(null);
        log.info("统计当日考勤结束");
    }


    private void calculateAttendByDay() {
        Context context = new Context();
        UserEntity userEntity = new UserEntity();
        userEntity.setCreateUserId(1L);
        userEntity.setId(1L);
        userEntity.setCreateUserName("admin");
        userEntity.setCreateTime(new Date());
        context.setUser(userEntity);
        AttendanceRecordHikQuery recordHikQuery = new AttendanceRecordHikQuery();
        // Date todayStart = DateUtil.offsetHour(new Date(), -5).toJdkDate();
        //  recordHikQuery.setAttendanceDateStart(DateUtils.getCurrStrDate());
        recordHikQuery.setAttendanceDateStart(DateUtil.today());
        recordHikQuery.setAttendanceDateEnd(DateUtil.today());
        try {
            recordHikService.addAttendanceRecordByQuery(recordHikQuery, context);
        } catch (Exception e) {
            log.error("计算考勤异常", e);
        }
    }

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

    }
}
