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.JrxModel;
import com.gemho.meizhou.model.StatusModel;
import com.gemho.meizhou.model.TabJinrunxian;
import com.gemho.meizhou.model.TabSensor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class JrxService {

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

    @Autowired
    private UploadCloudDao uploadCloudDao;

    // 浸润线
    public void jinrunxianUpload() {
        Date date = new Date();
        List<TabSensor> list = uploadCloudDao.findSensorDataByKind("浸润线");
        Map<String, Object> map = new LinkedHashMap<>();
        List<JrxModel> jrxlist = new ArrayList();

        Map<String, Object> statusMap = new LinkedHashMap<>();
        List<StatusModel> jrxStatusList = new ArrayList<>();

        for (TabSensor sensor : list) {
            TabJinrunxian tabJinrunxian = uploadCloudDao.findJrxData(sensor.getName(),
                    DateUtil.splitToDay(date),
                    DateUtil.splitToLastXDay(date, -1));
            if (tabJinrunxian != null) {
                JrxModel model = new JrxModel();
                model.setJRXCGQBH(tabJinrunxian.getSensorname());
                model.setJRXCJSJ(TimeUtils.timeValue(""));
                model.setJRXMSSZ(Double.valueOf(dfTwo.format(tabJinrunxian.getValue3())));
                model.setJRXCGQWZ(sensor.getJdwd());
                model.setJRXKKGCSZ(Double.valueOf(sensor.getAddress2().substring(sensor.getAddress2().indexOf("|") + 1)));
                model.setJRXKSSZ(Double.valueOf(sensor.getJiazhichushi()));
                jrxlist.add(model);
                if ("0".equals(tabJinrunxian.getIfbj())) {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getName());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(1);
                    jrxStatusList.add(statusModel);
                }else {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getName());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(2);
                    jrxStatusList.add(statusModel);
                }
            }else {
                StatusModel statusModel = new StatusModel();
                statusModel.setCGQBH(sensor.getName());
                statusModel.setCJSJ(TimeUtils.timeValue(""));
                statusModel.setSTATUS(2);
                jrxStatusList.add(statusModel);
            }
        }
        map.put("datastreams", jrxlist);
        System.out.println("浸润线json数据======" + JSONUtil.parseObj(map));
        String result = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=001&dataType=1")
                .header("api-key", "94dd4579cbf74a56ad5a")
                .form("deviceType", "001")
                .form("dataType", 1)
                .body(JSONUtil.toJsonStr(map))
                .execute().body();
        System.out.println("浸润线result:" + result);


        statusMap.put("datastreams", jrxStatusList);
        System.out.println("浸润线状态json数据======" + JSONUtil.parseObj(statusMap));
        String result1 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=001&dataType=2")
                .header("api-key", "94dd4579cbf74a56ad5a")
                .body(JSONUtil.toJsonStr(statusMap))
                .execute().body();
        System.out.println("浸润线状态result:" + result1);
    }
}
