package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.module.attendance.service.AttendanceRecordHikService;
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.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private AttendanceRecordHikService recordHikService;

    @Autowired
    private StaffService service;

    @Autowired
    private IHikDoorService hikDoorService;


    @Override
    public void excuteTask(ITask task) throws AppException {
        DoorEventReq doorEventReq = new DoorEventReq();
        List<Integer> eventTypes = new ArrayList<>();
        eventTypes.add(196885);
        eventTypes.add(196887);
        doorEventReq.setEventTypes(eventTypes);
        // 获取当天的开始时间
        Date todayStart = DateUtil.beginOfDay(new Date());
        // 获取当天的结束时间
        Date todayEnd = DateUtil.endOfDay(new Date());
        doorEventReq.setStartTime(todayStart);
        doorEventReq.setEndTime(todayEnd);
        doorEventReq.setPageNo(1);
        doorEventReq.setPageSize(1000);

        Rest<DoorEventDataInfo> doorEventsRest = hikDoorService.getDoorEvents(doorEventReq);

        log.info(JSON.toJSONString(doorEventsRest));


    }


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

    }

    public static void main(String[] args) {
        // 获取当前时间的小时数
        int hour = DateUtil.hour(new Date(), true);
        // 获取当前时间所在小时的开始时间和结束时间
        Date startTime = DateUtil.beginOfHour(new Date());
        Date endTime = DateUtil.endOfHour(new Date());

        // 格式化时间为指定格式
        String startTimeStr = DateUtil.format(startTime, "yyyy-MM-dd HH:00:00");
        String endTimeStr = DateUtil.format(endTime, "yyyy-MM-dd HH:59:59");

        // 输出结果
        System.out.println("当前时间所在小时的开始时间：" + startTimeStr);
        System.out.println("当前时间所在小时的结束时间：" + endTimeStr);
    }
}
