package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.mortals.framework.common.Rest;
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.DataUtil;
import com.mortals.xhx.common.code.DwMinorEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.attendance.model.AttendanceRecordHikEntity;
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.HikDoorEventReq;
import com.mortals.xhx.module.hik.door.model.rsp.door.info.EventInfo;
import com.mortals.xhx.module.hik.door.model.rsp.door.info.StruTime;
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 java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 同步获取当天海康门禁事件
 */
@Slf4j
//@Service("SyncDoorsEventByDevicesTask")
public class SyncDoorsEventByDevicesTaskImpl implements ITaskExcuteService {

    @Autowired
    private AttendanceRecordHikService recordHikService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private IHikDoorService hikDoorService;

    private Integer day;
    @Autowired
    private ICacheService cacheService;

    @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;
        }

        HikDoorEventReq hikDoorEventReq = new HikDoorEventReq();
        //当前时间前一个小时
        // Date startTime = DateUtil.offsetHour(new Date(), -1).toJdkDate();
        Date startTime = DateUtil.offsetDay(new Date(), -1).toJdkDate();
        Date endTime = new Date();

        hikDoorEventReq.setStartTime(startTime);
        hikDoorEventReq.setEndTime(endTime);

        boolean in = true;
        if (in) {
            List<DoorEntity> doorEntities = doorService.find(new DoorQuery());
            for (DoorEntity doorEntity : doorEntities) {
                doorService.syncDoorDeviceEvents(doorEntity,hikDoorEventReq);
               // syncDoorEvents(doorEntity);
            }
        }
    }


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

    }
}
