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

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class JylService {


    @Autowired
    private UploadCloudDao uploadCloudDao;

    // 推送降雨量设备采集数据
    public void jiangyuliang() {
        // 判断时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 查询雨量计传感器
        List<TabSensor> list = uploadCloudDao.findSensorDataByKind("雨量计");
        Map<String, Object> statusmap = new LinkedHashMap<>();
        List<StatusModel> statusList = new ArrayList<>();
        // 查询雨量计的值
        for (TabSensor sensor : list) {
            // 根据传感器名称查询
            TabJiangyuliang jyl = uploadCloudDao.findJylData(sensor.getName(),
                    DateUtil.splitToDay(date),
                    DateUtil.splitToLastXDay(date, -1));
            if (jyl != null) {
                Map<String, Object> map = new LinkedHashMap<>();
                List<JylModel> jyllist = new ArrayList();
                JylModel model = new JylModel();
                model.setJSLCGQBH(jyl.getSensorname());
                model.setJSLCJSJ(TimeUtils.timeValue(""));
                model.setJSLSZ(jyl.getValue2());
                jyllist.add(model);
                map.put("datastreams", jyllist);
                System.out.println("雨量计json数据======" + JSONUtil.parseObj(map));
                String result = HttpRequest.post("")
                        .header("api-key", "94dd4579cbf74a56ad5a")
                        .form("deviceType", "007")
                        .form("dataType", 1)
                        .form("data", JSONUtil.parseObj(map))
                        .execute().body();
                System.out.println("降雨量result:" + result);
                if ("0".equals(jyl.getIfbj())) {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getMark());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(1);
                    statusList.add(statusModel);
                } else {
                    StatusModel statusModel = new StatusModel();
                    statusModel.setCGQBH(sensor.getMark());
                    statusModel.setCJSJ(TimeUtils.timeValue(""));
                    statusModel.setSTATUS(2);
                    statusList.add(statusModel);
                }
            } else {
                StatusModel model = new StatusModel();
                model.setCGQBH(sensor.getMark());
                model.setCJSJ(TimeUtils.timeValue(""));
                model.setSTATUS(2);
                statusList.add(model);
            }
        }
        statusmap.put("datastreams", statusList);
        String result = HttpRequest.post("http://a4vrhf.natappfree.cc/monitorData/ws/reportDeviceStatus")
                .header("api-key", "94dd4579cbf74a56ad5a")
                .form("deviceType", "007")
                .form("dataType", 2)
                .form("data", JSONUtil.parseObj(statusmap))
                .execute().body();
        System.out.println("降雨量-status-result:" + result);
    }

}
