package com.inspect.job.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inspect.access.base.feign.AccessFeign;
import com.inspect.base.core.utils.HttpClientUtils;
import com.inspect.base.redis.service.RedisService;
import com.inspect.ivs.base.feign.IvsFeign;
import com.inspect.job.client.MontQueryClient;
import com.inspect.job.domain.task.EqpBookChannel;
import com.inspect.job.mapper.PatrolDeviceStateMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class PatrolEqpbookChannelTask {
    Logger logger = LoggerFactory.getLogger(PatrolEqpbookChannelTask.class);

    @Value("${liveSIPB.url}")
    private String liveSIPB_URL;
    @Autowired
    MontQueryClient basedataClient;
    @Autowired
    PatrolDeviceStateMapper patrolDeviceStateMapper;


    /**
     * 同步ivs名称及在线状态
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void synPatrolEqpOnlineStatus() {
        String url = liveSIPB_URL + "/api/v1/device/channellistJson";
        String result;
        try {
            //获取库里面的eqpbookchannel
            List<EqpBookChannel> eqpBookChannels = patrolDeviceStateMapper.selectEqpBookChannel();
            result = HttpClientUtils.get(url, StringUtils.EMPTY);
            logger.info("同步巡检设备在线状态");
//            logger.info("同步巡检设备在线状态：{}", result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (null != jsonObject) {
                JSONObject cameraBriefInfos = null;
                if (jsonObject.getJSONObject("cameraBriefInfos") != null) {
                    cameraBriefInfos = jsonObject.getJSONObject("cameraBriefInfos").getJSONObject("cameraBriefInfoList");
                }
                if (cameraBriefInfos == null) {
                    if (jsonObject.getJSONObject("cameraBriefInfosV2") != null) {
                        cameraBriefInfos = jsonObject.getJSONObject("cameraBriefInfosV2").getJSONObject("cameraBriefInfoList");
                    }
                }
                if (null != cameraBriefInfos) {
                    JSONArray jsonObjectItems = cameraBriefInfos.getJSONArray("cameraBriefInfo");
                    if (null != jsonObjectItems) {
                        for (EqpBookChannel eqpBookChannel : eqpBookChannels) {
                            for (int i = 0; i < jsonObjectItems.size(); i++) {
                                JSONObject cameraInfo = jsonObjectItems.getJSONObject(i);
                                //cameracode相等
                                String code = cameraInfo.getString("code");
                                String domainCode = cameraInfo.getString("domainCode");
                                String channelCode = eqpBookChannel.getChannelCode();
                                if (channelCode.equals(code) || channelCode.equals(code + "#" + domainCode)) {
                                    //状态发生改变 修改
                                    if (!eqpBookChannel.getOnlineState().equals(cameraInfo.getString("status")) ||
                                            !eqpBookChannel.getChannelName().equals(cameraInfo.getString("name"))) {
                                        eqpBookChannel.setOnlineState(cameraInfo.getString("status"));
                                        eqpBookChannel.setChannelName(cameraInfo.getString("name"));
                                        patrolDeviceStateMapper.updateEqpBookChannel(eqpBookChannel);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("error", e);
        }
    }
}
