package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateTime;
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.ICacheService;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DataUtil;
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.common.key.RedisKey;
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.door.model.DoorEntity;
import com.mortals.xhx.module.door.model.DoorQuery;
import com.mortals.xhx.module.door.service.DoorService;
import com.mortals.xhx.module.hik.door.model.req.door.DoorEventReq;
import com.mortals.xhx.module.hik.door.model.req.door.HikDoorEventReq;
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.checkerframework.checker.units.qual.A;
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("SyncDoorsEventTask")
public class SyncDoorsEventTaskImpl implements ITaskExcuteService {

    @Autowired
    private AttendanceRecordHikService recordHikService;
    private Integer day;
    @Value("${hik.host:}")
    protected String hikhost;

    @Autowired
    private DoorService doorService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        //同步多久时间段的 偏移量为天 默认当天
        String excuteParam = task.getExcuteParam();
        if (!ObjectUtils.isEmpty(excuteParam)) {
            day = DataUtil.converStr2Int(excuteParam, 0);
        } else {
            day = 0;
        }
        Date beginDateTime = DateUtil.parseDateTime(DateUtil.today() + " 08:00:00");
        Date endDateTime = DateUtil.parseDateTime(DateUtil.today() + " 23:30:00");

        boolean in = DateUtil.isIn(new Date(), beginDateTime, endDateTime);
        if (in) {
            AttendanceRecordHikQuery hikQuery = new AttendanceRecordHikQuery();
            DateTime startTime = DateUtil.offsetDay(new Date(), day > 0 ? -day : 0);
            hikQuery.setAttendanceDateStart(startTime.toDateStr());
            // 获取当天的结束时间
            hikQuery.setAttendanceDateEnd(DateUtil.today());
            if (!ObjectUtils.isEmpty(hikhost)) {
                recordHikService.syncDoorEvents(hikQuery);
                recordHikService.deletFakeRecord(hikQuery, null);
            } else {
                log.info("同步设备上门禁事件！");
                HikDoorEventReq hikDoorEventReq = new HikDoorEventReq();
                hikDoorEventReq.setStartTime(startTime.toJdkDate());
                hikDoorEventReq.setEndTime(DateUtil.endOfDay(new Date()));
                List<DoorEntity> doorEntities = doorService.find(new DoorQuery());
                for (DoorEntity doorEntity : doorEntities) {
                    doorService.syncDoorDeviceEvents(doorEntity, hikDoorEventReq);
                }
                recordHikService.deletFakeRecord(hikQuery, null);
            }
        }
    }

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

    }
}
