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

    private DecimalFormat dfFour = new DecimalFormat("0.0000");


    @Autowired
    private UploadCloudDao uploadCloudDao;


    // 表面位移
    public void bmwyhpStatusUpload() {
        Date date = new Date();
        List<TabSensor> list = uploadCloudDao.findbmwySensorDataByKind("M0081M");
        Map<String, Object> map = new LinkedHashMap<>();
        List<BmwyhpModel> bmwylist = new ArrayList();
        Map<String, Object> BmwystatusMap = new LinkedHashMap<>();
        List<StatusModel> BmwyStatusList = new ArrayList<>();

        for (TabSensor sensor : list) {
            TabBatidibiaoweiyi BmwyData = uploadCloudDao.findBmwyData(sensor.getName(),
                    DateUtil.splitToDay(date),
                    DateUtil.splitToLastXDay(date, -1));
            if (BmwyData != null) {
                BmwyhpModel model = new BmwyhpModel();
                model.setKQDZHPTBMWYCGQBH(BmwyData.getSensorname());
                model.setKQDZHPTBMWYCGQWZ(sensor.getJdwd());
                model.setKQDZHPTBMWYCJSJ(TimeUtils.timeValue(""));
                model.setKQDZHPTBMSPWYSZ(Double.valueOf(dfFour.format(BmwyData.getDisx())));
                model.setKQDZHPTBMSXWYSZ(Double.valueOf(dfFour.format(BmwyData.getDisy())));
                model.setKQDZHPTBMQCFXSZ(Double.valueOf(dfFour.format(BmwyData.getDisz())));
                bmwylist.add(model);
                if ((BmwyData.getIfbj() == 0)) {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getName());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(1);
                    BmwyStatusList.add(statusModel);
                }else {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getName());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(2);
                    BmwyStatusList.add(statusModel);
                }
            }else {
                StatusModel statusModel = new StatusModel();
                statusModel.setCGQBH(sensor.getName());
                statusModel.setCJSJ(TimeUtils.timeValue(""));
                statusModel.setSTATUS(2);
                BmwyStatusList.add(statusModel);
            }
        }


        BmwystatusMap.put("datastreams", BmwyStatusList);
        System.out.println("表面位移状态hpjson数据======" + JSONUtil.parseObj(BmwystatusMap));
        String result1 = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=008&dataType=2")
                .header("api-key", "94dd4579cbf74a56ad5a")
                .body(JSONUtil.toJsonStr(BmwystatusMap))
                .execute().body();
        System.out.println("表面位移状态hpresult:" + result1);

    }



    public void oneHourPushBmwyhpData(){
        Date date = new Date();
        List<TabSensor> list = uploadCloudDao.findbmwySensorDataByKind("M0081M");
        Map<String, Object> map = new LinkedHashMap<>();
        List<BmwyhpModel> bmwylist = new ArrayList();
        for (TabSensor sensor : list) {
            TabBatidibiaoweiyi BmwyData = uploadCloudDao.findBmwyData(sensor.getName(),
                    DateUtil.splitToDay(date),
                    DateUtil.splitToLastXDay(date, -1));
            System.out.println("bmwyhp=====" + BmwyData.getDisx() + "========" + BmwyData.getDisy() + "========" + BmwyData.getDisz());
            if (BmwyData != null) {
                BmwyhpModel model = new BmwyhpModel();
                model.setKQDZHPTBMWYCGQBH(BmwyData.getSensorname());
                model.setKQDZHPTBMWYCGQWZ(sensor.getJdwd());
                model.setKQDZHPTBMWYCJSJ(TimeUtils.timeValue(String.valueOf(BmwyData.getJctime())));
                model.setKQDZHPTBMSPWYSZ(Double.valueOf(dfFour.format(BmwyData.getDisx())));
                model.setKQDZHPTBMSXWYSZ(Double.valueOf(dfFour.format(BmwyData.getDisy())));
                model.setKQDZHPTBMQCFXSZ(Double.valueOf(dfFour.format(BmwyData.getDisz())));
                bmwylist.add(model);
            }
        }
        map.put("datastreams", bmwylist);
        System.out.println("表面位移hpjson数据======" + JSONUtil.parseObj(map));
        String result = HttpRequest.post("http://111.61.77.35:8110/monitorData/ws/reportDeviceStatus?deviceType=008&dataType=1")
                .header("api-key", "94dd4579cbf74a56ad5a")
                .body(JSONUtil.toJsonStr(map))
                .execute().body();
        System.out.println("表面位移hpresult:" + result);
    }

}
