package com.zhgd.xmgl.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhgd.xmgl.modules.basicdata.entity.MonitorDev;
import com.zhgd.xmgl.modules.basicdata.service.IMonitorDevService;
import com.zhgd.xmgl.modules.basicdata.api.camera.MonitorApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 视频监控定时任务
 **/
@Slf4j
@Component
public class MonitorTask {

    @Autowired
    private IMonitorDevService projectMonitorService;

    /**
     * ，每一个小时修正监控点的状态
     */
//    @Scheduled(cron = "0 0 * * * ?")
    private void monitorOnline() {
        List<MonitorDev> list = projectMonitorService.list();
        JSONObject monitorOnline = MonitorApi.getMonitorOnline(list.stream().map(p -> p.getCode()).collect(Collectors.toList()));
        if (monitorOnline == null) {
            log.error("=========定时修正监控点的状态任务出现错误========");
            return;
        }
        JSONArray resultList = monitorOnline.getJSONObject("data").getJSONArray("list");
        for (int i = 0; i < list.size(); i++) {
            String online = resultList.getJSONObject(i).getString("online");
            if (StringUtils.isNotBlank(online) && online.equals(1)) {
                list.get(i).setState(1);
            } else {
                list.get(i).setState(0);
            }
        }
        projectMonitorService.updateBatchById(list);
        log.error("=========定时修正监控点的状态任务执行成功========");
    }
}
