//package com.huimv.admin.timer;
//
//import cn.hutool.core.util.ObjectUtil;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.huimv.admin.entity.*;
//import com.huimv.admin.service.*;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.util.LinkedMultiValueMap;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.client.RestTemplate;
//
//import java.math.BigDecimal;
//import java.text.ParseException;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.List;
//
//@Configuration
//@EnableScheduling
//public class NewEnvTimer {
//    @Autowired
//    private RestTemplate restTemplate;
//
//    @Autowired
//    private IEnvDeviceService envDeviceService;
//
//    @Autowired
//    private IEnvDataService envDataService;
//
//    @Autowired
//    private IEnvWarningThresholdService envWarningThresholdService;
//
//    @Autowired
//    private IEnvWarningInfoService envWarningInfoService;
//
//    @Autowired
//    private IEnvNh3nService nh3nService;
//
//
//    @Scheduled(cron = "0 0/59 * * * ?")  //每10分钟执行一次
////    @Scheduled(cron = "0 * * * * ?")  //每10分钟执行一次
////    @Scheduled(cron = "*/5 * * * * ?")  //每10秒钟执行一次
//    public void getDeviceFlowData() throws ParseException {
//        String userId = "-1618620464";
//        String farmCode = "26";
//
//        List<EnvDevice> farm_id = envDeviceService.list(new QueryWrapper<EnvDevice>().eq("farm_id", farmCode));
//        EnvWarningThreshold envWarningThreshold = envWarningThresholdService.getOne(new QueryWrapper<EnvWarningThreshold>().eq("farm_id", farmCode));
//        System.out.println("开始");
//        Date date = new Date();
//        for (EnvDevice envDevice : farm_id) {
//            String deviceCode = envDevice.getDeviceCode();
//            if (StringUtils.isNotBlank(deviceCode)) {
//                JSONObject resultJo = JSONObject.parseObject(getDeviceFlowFromRemoteURL(userId, deviceCode));
//                System.out.println("resultJo >>>>>>>>>>>> =" + resultJo);
//                if (resultJo.getInteger("code") != 1000) {
//                    System.out.println("牧场[" + farmCode + "]设备组编号(" + deviceCode + ")调用远程接口返回结果出错.");
//                } else {
//                    JSONArray dataJa = resultJo.getJSONArray("data");
//                    JSONObject dataJo = dataJa.getJSONObject(0);
//                    JSONArray realTimeDataJa = dataJo.getJSONArray("realTimeData");
//                    JSONObject tempJo = realTimeDataJa.getJSONObject(0);
//                    String temp = tempJo.getString("dataValue");
//                    JSONObject humiJo = realTimeDataJa.getJSONObject(1);
//                    String humi = humiJo.getString("dataValue");
//                    if (StringUtils.isBlank(humi)) {
//                        humi = "0";
//                    }
//                    if (StringUtils.isBlank(temp)) {
//                        temp = "0";
//                    }
//                    EnvData envData = new EnvData();
//                    envData.setUnitId(envDevice.getUnitId());
//                    envData.setEnvTemp(temp);
//                    envData.setEnvHum(humi);
//                    envData.setFarmId(envDevice.getFarmId());
//                    envData.setCreateTime(date);
//                    envData.setDeviceId(envDevice.getDeviceCode());
//                    saveTemWarning(temp, envWarningThreshold, envDevice);
//                    saveHumWarning(humi, envWarningThreshold, envDevice);
//                    envDataService.save(envData);
//                    String remark = envDevice.getRemark();
//                    if (StringUtils.isNotBlank(remark)) {
//                        QueryWrapper<EnvNh3n> queryWrapper = new QueryWrapper<>();
//                        queryWrapper.eq("farm_id", farmCode).eq("device_code", remark);
//                        Calendar calendar = Calendar.getInstance();
//                        calendar.setTime(new Date());
//                        calendar.add(Calendar.MINUTE, -40);//
//                        queryWrapper.between("create_date", calendar.getTime(), new Date());
//                        List<EnvNh3n> list = nh3nService.list(queryWrapper);
//                        if (list.size() == 0 && humi.equals("0") && temp.equals("0")) {
//                            envDevice.setDeviceStatus(0);
//                        } else {
//                            envDevice.setDeviceStatus(1);
//                        }
//                        envDeviceService.updateById(envDevice);
//                    }
//                }
//            }
//        }
//
//    }
//
//
//    //获取远程数据
//    private String getDeviceFlowFromRemoteURL(String userId, String groupId) {
//        //请求头
//        HttpHeaders headers = new HttpHeaders();
//        headers.add("userId", userId);
//        //Body参数
//        MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
//        HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
//        System.out.println("URL_ENVIRON_DEVICE_SERVICE=" + "http://www.0531yun.com/app/GetDeviceData");
//        ResponseEntity<String> result = restTemplate.exchange("http://www.0531yun.com/app/GetDeviceData?groupId=" + groupId, HttpMethod.GET, objectHttpEntity, String.class);
//        System.out.println("result.getBody()=" + result.getBody());
//        return result.getBody();
//    }
//
//    private void saveTemWarning(String val, EnvWarningThreshold envWarningThreshold, EnvDevice envDevice) {
//        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(val)) {
//            String maxTem;
//            String minTem;
//            if (ObjectUtil.isEmpty(envWarningThreshold) || com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(envWarningThreshold.getMaxTem())) {
//                maxTem = "40";
//            } else {
//                maxTem = envWarningThreshold.getMaxTem();
//            }
//            if (ObjectUtil.isEmpty(envWarningThreshold) || com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(envWarningThreshold.getMinTem())) {
//                minTem = "0";
//            } else {
//                minTem = envWarningThreshold.getMinTem();
//            }
//            System.out.println("maxTem:" + Double.parseDouble(maxTem) + "  minTem:" + Double.parseDouble(minTem) + "   val:" + Double.parseDouble(val));
//            if (Double.parseDouble(maxTem) < Double.parseDouble(val)) {
//                EnvWarningInfo envWarningInfo = new EnvWarningInfo();
//                envWarningInfo.setBuildLocation(envDevice.getUnitName());
//                envWarningInfo.setDate(new Date());
//                envWarningInfo.setDeviceId(envDevice.getDeviceCode());
//                envWarningInfo.setFarmId(envDevice.getFarmId());
//                envWarningInfo.setUnitId(envDevice.getUnitId());
//                envWarningInfo.setUnitName(envDevice.getUnitName());
//                envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
//                envWarningInfo.setWarningContent("当前温度为" + val + "°超过阈值,请及时检查");
//                envWarningInfo.setWarningType(1);
//                envWarningInfoService.save(envWarningInfo);
//            }
//            if (Double.parseDouble(minTem) > Double.parseDouble(val)) {
//                EnvWarningInfo envWarningInfo = new EnvWarningInfo();
//                envWarningInfo.setBuildLocation(envDevice.getUnitName());
//                envWarningInfo.setDate(new Date());
//                envWarningInfo.setDeviceId(envDevice.getDeviceCode());
//                envWarningInfo.setFarmId(envDevice.getFarmId());
//                envWarningInfo.setUnitId(envDevice.getUnitId());
//                envWarningInfo.setUnitName(envDevice.getUnitName());
//                envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
//                envWarningInfo.setWarningContent("当前温度为" + val + "°低于阈值,请及时检查");
//                envWarningInfo.setWarningType(1);
//                envWarningInfoService.save(envWarningInfo);
//            }
//        }
//    }
//
//    public void saveHumWarning(String val, EnvWarningThreshold envWarningThreshold, EnvDevice envDevice) {
//        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(val)) {
//            String maxHum;
//            String minHum;
//            if (ObjectUtil.isEmpty(envWarningThreshold) || com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(envWarningThreshold.getMaxHum())) {
//                maxHum = "90";
//            } else {
//                maxHum = envWarningThreshold.getMaxHum();
//            }
//            if (ObjectUtil.isEmpty(envWarningThreshold) || com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(envWarningThreshold.getMinHum())) {
//                minHum = "0";
//            } else {
//                minHum = envWarningThreshold.getMinHum();
//            }
//            if (Double.parseDouble(maxHum) < Double.parseDouble(val)) {
//                EnvWarningInfo envWarningInfo = new EnvWarningInfo();
//                envWarningInfo.setBuildLocation(envDevice.getUnitName());
//                envWarningInfo.setDate(new Date());
//                envWarningInfo.setDeviceId(envDevice.getDeviceCode());
//                envWarningInfo.setFarmId(envDevice.getFarmId());
//                envWarningInfo.setUnitId(envDevice.getUnitId());
//                envWarningInfo.setUnitName(envDevice.getUnitName());
//                envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
//                envWarningInfo.setWarningContent("当前湿度为" + val + "%超过阈值,请及时检查");
//                envWarningInfo.setWarningType(2);
//                envWarningInfoService.save(envWarningInfo);
//            }
//            if (Double.parseDouble(minHum) > Double.parseDouble(val)) {
//                EnvWarningInfo envWarningInfo = new EnvWarningInfo();
//                envWarningInfo.setBuildLocation(envDevice.getUnitName());
//                envWarningInfo.setDate(new Date());
//                envWarningInfo.setDeviceId(envDevice.getDeviceCode());
//                envWarningInfo.setFarmId(envDevice.getFarmId());
//                envWarningInfo.setUnitId(envDevice.getUnitId());
//                envWarningInfo.setUnitName(envDevice.getUnitName());
//                envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
//                envWarningInfo.setWarningContent("当前湿度为" + val + "%低于阈值,请及时检查");
//                envWarningInfo.setWarningType(2);
//                envWarningInfoService.save(envWarningInfo);
//            }
//        }
//    }
//}
