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

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


    @Autowired
    private UploadCloudDao uploadCloudDao;


    // 表面位移
    public void bmwyUpload() {
        Date date = new Date();
        List<TabSensor> list = uploadCloudDao.findbmwySensorDataByKind("M0021G");
        Map<String, Object> map = new LinkedHashMap<>();
        List<BmwyModel> 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) {
                System.out.println("bmwy=====" + BmwyData.getDisx() + "========" + BmwyData.getDisy() + "========" + BmwyData.getDisz());
                BmwyModel model = new BmwyModel();
                model.setBTBMWYCGQBH(BmwyData.getSensorname());
                model.setBTBMWYCGQWZ(sensor.getJdwd());
                model.setBTBMWYCJSJ(TimeUtils.timeValue(""));
                model.setBTBMSPSZ(Double.valueOf(dfFour.format(BmwyData.getDisx())));
                model.setBTBMSXSZ(Double.valueOf(dfFour.format(BmwyData.getDisy())));
                model.setBTBMQCFXSZ(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);
            }
        }

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

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


}
