package com.ruoyi.web.controller.chenyanlogin.timer;




import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.web.controller.chenyanlogin.haikang.*;
import com.ruoyi.web.domain.entity.LotEnvironment;
import com.ruoyi.web.domain.entity.LotVideo;
import com.ruoyi.web.service.LotEnvironmentService;
import com.ruoyi.web.service.LotVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.alibaba.fastjson.JSON;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
@CrossOrigin
public class HaiKangTimer {

    @Autowired
    private LotVideoService lotVideoService;
    @Autowired
    private LotEnvironmentService lotEnvironmentService;

   private  String camerList =  "/api/resource/v1/camera/advance/cameraList";
    private  String  cameras =  "/api/resource/v1/cameras/indexCode";
//   private  String  =  "/api/resource/v1/camera/advance/cameraList";
    //获取传感器
//   private  String  search =  "/api/pems/v1/monitor/search";
    //查询传感器数据
   private  String  search =  "/api/pems/v1/sensor/search";
   private  String  indexCode =  "847110a50d54480cbdb64f6e0feb49e3";

    //获取所有监控
    @Transactional
    @Scheduled(cron = "0 0 0 * * ? ")
    public  void listCamerList() throws Exception {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 100);
        jsonBody.put("isCascade", 0);

        String body = jsonBody.toJSONString();
        String getCameraPreviewURL = GetCameraPreviewURL.GetCameraPreviewURL(camerList, body);
        System.out.println(getCameraPreviewURL);
        CameraResponse cameraResponse = JSON.parseObject(getCameraPreviewURL, CameraResponse.class);
        if (ObjectUtil.isNotEmpty(cameraResponse) && "0".equals(cameraResponse.getCode())){
            List<CameraInfo> list = cameraResponse.getData().getList();
            for (CameraInfo cameraInfo : list) {
                LotVideo video = lotVideoService.getOne(new QueryWrapper<LotVideo>().eq("camera_index_code", cameraInfo.getCameraIndexCode()));
                if (ObjectUtil.isEmpty(video)){
                     video = new LotVideo();
                    video.setCreateTime(cameraInfo.getCreateTime());
                    video.setName(cameraInfo.getCameraName());
                    video.setLocation(cameraInfo.getCameraName());
                    video.setUpdateTime(cameraInfo.getUpdateTime());
                    video.setRegionIndexCode(cameraInfo.getRegionIndexCode());
                    video.setCameraIndexCode(cameraInfo.getCameraIndexCode());
                    video.setCapabilitySetName(cameraInfo.getCapabilitySetName());
                    video.setEncodeDevIndexCode(cameraInfo.getEncodeDevIndexCode());
                    lotVideoService.save(video);
                }else {
                    video.setCreateTime(cameraInfo.getCreateTime());
                    video.setName(cameraInfo.getCameraName());
                    video.setLocation(cameraInfo.getCameraName());
                    video.setUpdateTime(cameraInfo.getUpdateTime());
                    video.setRegionIndexCode(cameraInfo.getRegionIndexCode());
                    video.setCameraIndexCode(cameraInfo.getCameraIndexCode());
                    video.setCapabilitySetName(cameraInfo.getCapabilitySetName());
                    video.setEncodeDevIndexCode(cameraInfo.getEncodeDevIndexCode());
                    lotVideoService.updateById(video);
                }


            }
        }

    }

    //获取状态
    @Scheduled(cron = "0 0 0 * * ? ")
//    @Scheduled(cron = "0 * * * * ? ")
    public  void statusCamerList() throws Exception {
        List<LotVideo> list1 = lotVideoService.list();
        for (LotVideo video : list1) {
            System.out.println("----->"+video.toString());
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("cameraIndexCode",  video.getCameraIndexCode());
            String body = jsonBody.toJSONString();
            String getCameraPreviewURL = GetCameraPreviewURL.GetCameraPreviewURL(cameras, body);
            CameraStatusResponse cameraResponse = JSON.parseObject(getCameraPreviewURL, CameraStatusResponse.class);
            if (ObjectUtil.isNotEmpty(cameraResponse) && "0".equals(cameraResponse.getCode())){
                CameraStatusData data = cameraResponse.getData();
                if (!(video.getStatus() + "").equals(data.getStatus())){
                    video.setStatus(Integer.parseInt(data.getStatus()));
                    lotVideoService.updateById(video);
                };
            }
        }
    }

    //获取传感器
//    @Scheduled(cron = "0 0 0 * * ? ")
    @Scheduled(cron = "0 0 * * * ? ")
    public  void search() throws Exception {
        System.out.println("水位开始");

        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo",  1);
        jsonBody.put("pageSize",  1000);
        jsonBody.put("indexCode",  indexCode);
        String body = jsonBody.toJSONString();
        String getCameraPreviewURL = GetCameraPreviewURL.GetCameraPreviewURL(search, body);

        ShuiWeiResponseData cameraResponse = JSON.parseObject(getCameraPreviewURL, ShuiWeiResponseData.class);
        if (0 ==cameraResponse.getCode()){
            ShuiWeiData data = cameraResponse.getData();
            List<ShuiWeiDataList> list = data.getList();
            LotEnvironment lotEnvironment = new LotEnvironment();
            for (ShuiWeiDataList shuiWeiDataList : list) {
                String latestValue = shuiWeiDataList.getLatestValue();
                if ("wl".equals(shuiWeiDataList.getSensorType())){
                    lotEnvironment.setWaterLevel(latestValue);
                }
                if ("precipitation".equals(shuiWeiDataList.getSensorType())){
                    lotEnvironment.setRainfall(latestValue);
                }
            }
            lotEnvironment.setDate(new Date());
            lotEnvironment.setDate(new Date());
            lotEnvironmentService.save(lotEnvironment);
        }

    }

    public static void main(String[] args) throws Exception {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo",  1);
        jsonBody.put("pageSize",  1000);
//        jsonBody.put("type",  "wl");
        jsonBody.put("indexCode",  "847110a50d54480cbdb64f6e0feb49e3");
//        jsonBody.put("parentIndexCode",  "7438a19b5dd3443ca9a9a67e45ad5c8a");
        String body = jsonBody.toJSONString();
        System.out.println("sssssss"+body);
//        String getCameraPreviewURL = GetCameraPreviewURL.GetCameraPreviewURL("/api/pems/v1/monitor/search", body);
        String getCameraPreviewURL = GetCameraPreviewURL.GetCameraPreviewURL("/api/pems/v1/sensor/search", body);

        ShuiWeiResponseData cameraResponse = JSON.parseObject(getCameraPreviewURL, ShuiWeiResponseData.class);
        System.out.println(cameraResponse);

    }


}
