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 KswService {

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

    @Autowired
    private UploadCloudDao uploadCloudDao;

    // 库水位
    public void kushuiweiUpload() {
        Date date = new Date();
        List<TabSensor> list = uploadCloudDao.findSensorDataByKind("库水位");
        Map<String, Object> map = new LinkedHashMap<>();
        List<KswModel> kswList = new ArrayList();
        Map<String, Object> kswstatusMap = new LinkedHashMap<>();
        List<StatusModel> kswStatusList = new ArrayList<>();

        for (TabSensor sensor : list) {
            TabKushuiwei kswData = uploadCloudDao.findKswData(sensor.getName(),
                    DateUtil.splitToDay(date),
                    DateUtil.splitToLastXDay(date, -1));
            if (kswData != null) {
                KswModel model = new KswModel();
                model.setKSWCGQBH(kswData.getSensorname());
                model.setKSWCJSJ(TimeUtils.timeValue(""));
                model.setKSWCGQWZ(sensor.getJdwd());
                model.setKSWSZ(Double.valueOf(dfTwo.format(kswData.getValue2())));
                kswList.add(model);
                if ("0".equals(kswData.getIfbj())) {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getName());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(1);
                    kswStatusList.add(statusModel);
                }else {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getName());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(2);
                    kswStatusList.add(statusModel);
                }
            }else {
                StatusModel statusModel = new StatusModel();
                statusModel.setCGQBH(sensor.getName());
                statusModel.setCJSJ(TimeUtils.timeValue(""));
                statusModel.setSTATUS(2);
                kswStatusList.add(statusModel);
            }
        }

        map.put("datastreams", kswList);
        System.out.println("库水位json数据======" + JSONUtil.parseObj(map));
        String result = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=006&dataType=1")
                .header("api-key", "94dd4579cbf74a56ad5a")
                .body(JSONUtil.toJsonStr(map))
                .execute().body();
        System.out.println("库水位result:" + result);


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