package com.gemho.meizhou.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.gemho.meizhou.Util.DateUtil;
import com.gemho.meizhou.Util.TimeUtils;
import com.gemho.meizhou.dao.UploadCloudDao;
import com.gemho.meizhou.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

@Service
public class GtService {

    private DecimalFormat dfTwo = new DecimalFormat("0.00");

    @Autowired
    private UploadCloudDao uploadCloudDao;

    // 干滩数据推送
    public void gantanUpload() {
        Date date = new Date();
        // 从sensor表中取出干滩
        List<TabSensor> list = uploadCloudDao.findSensorDataByKind("干滩");

        Map<String, Object> pdstatusmap = new LinkedHashMap<>();
        List<StatusModel> pdstatuslist = new ArrayList();

        Map<String, Object> cdstatusmap = new LinkedHashMap<>();
        List<StatusModel> cdstatuslist = new ArrayList();

        Map<String, Object> gcstatusmap = new LinkedHashMap<>();
        List<StatusModel> gcstatuslist = new ArrayList();

        for (TabSensor sensor : list) {
            System.out.println(sensor.getMark() + "=======干滩编号");
            TabGantanweiyi tabGantanweiyi = uploadCloudDao.findGtData(sensor.getName(),
                    DateUtil.splitToDay(date),
                    DateUtil.splitToLastXDay(date, -1));
            if ("干滩".equals(sensor.getMark())) {
                String[] str = sensor.getImgurl().split("-");
                if (tabGantanweiyi != null) {
                    Map<String, Object> pdmap = new LinkedHashMap<>();
                    List<GtpoduModel> pdlist = new ArrayList();
                    GtpoduModel podu = new GtpoduModel();
                    podu.setGTPDCGQBH(str[1]);
                    podu.setGTPDCJSJ(TimeUtils.timeValue(""));
                    podu.setGTPDCGQWZ(sensor.getJdwd());
                    podu.setGTPDSZ(Double.valueOf(dfTwo.format(Double.valueOf(tabGantanweiyi.getPodu()))));
                    pdlist.add(podu);
                    pdmap.put("datastreams", pdlist);

                    Map<String, Object> cdmap = new LinkedHashMap<>();
                    List<GtchangduModel> cdlist = new ArrayList();
                    GtchangduModel changdu = new GtchangduModel();
                    changdu.setGTCDCGQBH(str[2]);
                    changdu.setGTCDCGQWZ(sensor.getJdwd());
                    changdu.setGTCDCJSJ(TimeUtils.timeValue(""));
                    changdu.setGTCDSZ(Double.valueOf(dfTwo.format(Double.valueOf(tabGantanweiyi.getGantan()))));
                    cdlist.add(changdu);
                    cdmap.put("datastreams", cdlist);

                    Map<String, Object> gcmap = new LinkedHashMap<>();
                    List<GtgaochengModel> gclist = new ArrayList();
                    GtgaochengModel gaocheng = new GtgaochengModel();
                    gaocheng.setTDGCCGQBH(str[0]);
                    gaocheng.setTDGCCGQWZ(sensor.getJdwd());
                    gaocheng.setTDGCCJSJ(TimeUtils.timeValue(""));
                    gaocheng.setTDGCSZ(Double.valueOf(dfTwo.format(Double.valueOf(tabGantanweiyi.getBagao()))));
                    gclist.add(gaocheng);
                    gcmap.put("datastreams", gclist);


                    System.out.println("干滩坡度======" + JSONUtil.parseObj(pdmap));
                    System.out.println("干滩长度======" + JSONUtil.parseObj(cdmap));
                    System.out.println("干滩高程======" + JSONUtil.parseObj(gcmap));
                    // 坡度
                    String result1 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=009&dataType=1")
                            .header("api-key", "94dd4579cbf74a56ad5a")
                            .body(JSONUtil.toJsonStr(pdmap))
                            .execute().body();
                    System.out.println("干滩坡度result:" + result1);

                    // 长度
                    String result2 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=005&dataType=1")
                            .header("api-key", "94dd4579cbf74a56ad5a")
                            .body(JSONUtil.toJsonStr(cdmap))
                            .execute().body();
                    System.out.println("干滩长度result:" + result2);

                    // 高程
                    String result3 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=004&dataType=1")
                            .header("api-key", "94dd4579cbf74a56ad5a")
                            .body(JSONUtil.toJsonStr(gcmap))
                            .execute().body();
                    System.out.println("干滩高程result:" + result3);

                    if ("0".equals(tabGantanweiyi.getIfbj())) {
                        StatusModel model1 = new StatusModel();
                        model1.setCGQBH(str[1]);
                        model1.setCJSJ(TimeUtils.timeValue(""));
                        model1.setSTATUS(1);
                        StatusModel model2 = new StatusModel();
                        model2.setCGQBH(str[2]);
                        model2.setCJSJ(TimeUtils.timeValue(""));
                        model2.setSTATUS(1);
                        StatusModel model3 = new StatusModel();
                        model3.setCGQBH(str[0]);
                        model3.setCJSJ(TimeUtils.timeValue(""));
                        model3.setSTATUS(1);
                        pdstatuslist.add(model1);
                        cdstatuslist.add(model2);
                        gcstatuslist.add(model3);

                    }else {
                        StatusModel model1 = new StatusModel();
                        model1.setCGQBH(str[1]);
                        model1.setCJSJ(TimeUtils.timeValue(""));
                        model1.setSTATUS(2);
                        StatusModel model2 = new StatusModel();
                        model2.setCGQBH(str[2]);
                        model2.setCJSJ(TimeUtils.timeValue(""));
                        model2.setSTATUS(2);
                        StatusModel model3 = new StatusModel();
                        model3.setCGQBH(str[0]);
                        model3.setCJSJ(TimeUtils.timeValue(""));
                        model3.setSTATUS(2);
                        pdstatuslist.add(model1);
                        cdstatuslist.add(model2);
                        gcstatuslist.add(model3);

                    }
                }else {
                    StatusModel model1 = new StatusModel();
                    model1.setCGQBH(str[1]);
                    model1.setCJSJ(TimeUtils.timeValue(""));
                    model1.setSTATUS(2);
                    StatusModel model2 = new StatusModel();
                    model2.setCGQBH(str[2]);
                    model2.setCJSJ(TimeUtils.timeValue(""));
                    model2.setSTATUS(2);
                    StatusModel model3 = new StatusModel();
                    model3.setCGQBH(str[0]);
                    model3.setCJSJ(TimeUtils.timeValue(""));
                    model3.setSTATUS(2);
                    pdstatuslist.add(model1);
                    cdstatuslist.add(model2);
                    gcstatuslist.add(model3);

                }
            }
            pdstatusmap.put("datastreams", pdstatuslist);
            cdstatusmap.put("datastreams", cdstatuslist);
            gcstatusmap.put("datastreams", gcstatuslist);

            System.out.println("干滩坡度状态json数据======" + JSONUtil.parseObj(pdstatusmap));
            System.out.println("干滩长度状态json数据======" + JSONUtil.parseObj(cdstatusmap));
            System.out.println("干滩高程状态json数据======" + JSONUtil.parseObj(gcstatusmap));

            // 坡度
            String result1 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=009&dataType=2")
                    .header("api-key", "94dd4579cbf74a56ad5a")
                    .body(JSONUtil.toJsonStr(pdstatusmap))
                    .execute().body();
            System.out.println("干滩坡度-status-result:" + result1);

            // 长度
            String result2 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=005&dataType=2")
                    .header("api-key", "94dd4579cbf74a56ad5a")
                    .body(JSONUtil.toJsonStr(cdstatusmap))
                    .execute().body();
            System.out.println("干滩长度-status-result:" + result2);

            // 高程
            String result3 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=004&dataType=2")
                    .header("api-key", "94dd4579cbf74a56ad5a")
                    .body(JSONUtil.toJsonStr(gcstatusmap))
                    .execute().body();
            System.out.println("干滩高程-status-result:" + result3);
        }
    }
}
