//package com.huimv.admin.timer;
//
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.date.DateUtil;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.huimv.admin.common.utils.NumberUtils;
//import com.huimv.admin.entity.*;
//import com.huimv.admin.mapper.*;
//import org.apache.commons.lang.time.DateUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import java.text.DecimalFormat;
//import java.util.Date;
//import java.util.List;
//
//@Configuration
//@EnableScheduling
//public class EnergyTimer {
//
//    @Autowired
//    private EnergyEnvDeviceMapper deviceMapper;
//    @Autowired
//    private EnergyDataMapper dataMapper;
//    @Autowired
//    private BasePigpenMapper basePigpenMapper;
//    @Autowired
//    private EnergyWarningThresholdMapper warningThresholdMapper;
//    @Autowired
//    private EnergyWarningInfoMapper infoMapper;
//    @Autowired
//    private BaseFarmMapper baseFarmMapper;
//
//    //    能耗数据
//    @Scheduled(cron = "0 0 */1 * * ? ")
////    @Scheduled(cron = "0 */1 * * * ? ")
//    private void getShenChan() throws Exception {
//        List<EnergyEnvDevice> energyEnvDevices = deviceMapper.selectList(new QueryWrapper<EnergyEnvDevice>().ne("farm_id",26)
//                .ne("farm_id",29).ne("farm_id",21).ne("farm_id",28).ne("farm_id",27).
//                        ne("farm_id",35).ne("farm_id",33).ne("farm_id",22).ne("farm_id",23)
//                .ne("farm_id",34) .ne("farm_id",36) .ne("farm_id",37).ne("farm_id",38)
//                .ne("farm_id",40).ne("farm_id",41).ne("farm_id",39).ne("farm_id",42)
//                .ne("farm_id",24));
//
//        for (EnergyEnvDevice energyEnvDevice : energyEnvDevices) {
//            QueryWrapper<BasePigpen> basePigpenQueryWrapper = new QueryWrapper<>();
//            basePigpenQueryWrapper.eq("id", energyEnvDevice.getUnitId()).eq("farm_id", energyEnvDevice.getFarmId());
//            BasePigpen basePigpen1 = basePigpenMapper.selectOne(basePigpenQueryWrapper);
//            QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("id", basePigpen1.getParentId());
//            BasePigpen basePigpen = basePigpenMapper.selectOne(queryWrapper);
//
//            String feed = NumberUtils.getNumFloat2(6.00, 6.50);
//            String water = NumberUtils.getNumFloat2(0.08, 0.15);
////            String gas = NumberUtils.getNum(200, 280, 1);
//            String gas = "0";
//            String electricity = NumberUtils.getNumFloat2(1.0, 1.45);
//
//            EnergyData energyData = new EnergyData();
//            energyData.setCreateDate(new Date());
//            energyData.setFarmId(energyEnvDevice.getFarmId());
//            energyData.setUnitId(energyEnvDevice.getUnitId());
//            if (energyEnvDevice.getFarmId() == 23) {
//                energyData.setFeedValue("0");
//            } else {
//                energyData.setFeedValue(feed);
//            }
//
//            energyData.setWaterValue(water);
//            energyData.setGasValue(gas);
//            energyData.setElectricityValue(electricity);
//            if (energyEnvDevice.getFarmId() == 24) {
//                String feed1 = NumberUtils.getNumFloat2(1.40, 1.50);
//                String water1 = NumberUtils.getNumFloat2(20.80, 20.84);
//                String electricity1 = NumberUtils.getNumFloat2(333.30, 333.34);
//                energyData.setElectricityValue(electricity1);
//                energyData.setWaterValue(water1);
//                energyData.setFeedValue(feed1);
//            }
//            dataMapper.insert(energyData);
//
//            QueryWrapper<EnergyWarningThreshold> wrapper = new QueryWrapper<>();
//            wrapper.eq("other1", basePigpen.getStageCode()).eq("farm_id", energyEnvDevice.getFarmId());
//            EnergyWarningThreshold threshold = warningThresholdMapper.selectOne(wrapper);
//
//            QueryWrapper<EnergyData> dataQueryWrapper = new QueryWrapper<>();
//            dataQueryWrapper.eq("farm_id", energyEnvDevice.getFarmId()).eq("unit_id", energyEnvDevice.getUnitId());
//            DateTime dateTime = DateUtil.beginOfDay(new Date());
//            dataQueryWrapper.ge("create_date", dateTime);
//            EnergyData energyData1 = dataMapper.listDataCount(dataQueryWrapper);
//
//            if (Float.parseFloat(energyData1.getWaterValue()) + Float.parseFloat(water) > Float.parseFloat(threshold.getMaxWater())) {
//                float waterCount = Float.parseFloat(energyData1.getWaterValue()) + Float.parseFloat(water);
//                DecimalFormat df = new DecimalFormat("#.00");
//                String format = df.format(waterCount);
//                EnergyWarningInfo warningInfo = new EnergyWarningInfo();
//                warningInfo.setWarningType(1);
//                warningInfo.setWarningContent("今日累计用水量" + format + "t,超过阈值");
//                warningInfo.setUnitName(basePigpen1.getBuildName());
//                warningInfo.setUnitId(energyEnvDevice.getUnitId());
//                warningInfo.setDeviceId(energyEnvDevice.getId());
//                warningInfo.setDate(new Date());
//                warningInfo.setFarmId(energyEnvDevice.getFarmId());
//                warningInfo.setUserIds(threshold.getUserIds());
//                infoMapper.insert(warningInfo);
//            }
//            if (Float.parseFloat(energyData1.getElectricityValue()) + Float.parseFloat(electricity) > Float.parseFloat(threshold.getMaxElectricity())) {
//                float electricityCount = Float.parseFloat(energyData1.getElectricityValue()) + Float.parseFloat(electricity);
//                DecimalFormat df = new DecimalFormat("#.00");
//                String format = df.format(electricityCount);
//                EnergyWarningInfo warningInfo = new EnergyWarningInfo();
//                warningInfo.setWarningType(2);
//                warningInfo.setWarningContent("今日累计用电量" + format + "kw/h,超过阈值");
//                warningInfo.setUnitName(basePigpen1.getBuildName());
//                warningInfo.setUnitId(energyEnvDevice.getUnitId());
//                warningInfo.setDeviceId(energyEnvDevice.getId());
//                warningInfo.setDate(new Date());
//                warningInfo.setFarmId(energyEnvDevice.getFarmId());
//                warningInfo.setUserIds(threshold.getUserIds());
//                infoMapper.insert(warningInfo);
//            }
//            if (Float.parseFloat(energyData1.getFeedValue()) + Float.parseFloat(feed) > Float.parseFloat(threshold.getMaxFeed())) {
//                float feedCount = Float.parseFloat(energyData1.getFeedValue()) + Float.parseFloat(feed);
//                DecimalFormat df = new DecimalFormat("#.00");
//                String format = df.format(feedCount);
//                EnergyWarningInfo warningInfo = new EnergyWarningInfo();
//                warningInfo.setWarningType(3);
//                warningInfo.setWarningContent("今日累计用料量" + format + "kg,超过阈值");
//                warningInfo.setUnitName(basePigpen.getBuildName());
//                warningInfo.setUnitId(energyEnvDevice.getUnitId());
//                warningInfo.setDeviceId(energyEnvDevice.getId());
//                warningInfo.setDate(new Date());
//                warningInfo.setFarmId(energyEnvDevice.getFarmId());
//                warningInfo.setUserIds(threshold.getUserIds());
//                infoMapper.insert(warningInfo);
//            }
//            if (Float.parseFloat(energyData1.getGasValue()) + Float.parseFloat(gas) > Float.parseFloat(threshold.getMaxGas())) {
//                float gasCount = Float.parseFloat(energyData1.getGasValue()) + Float.parseFloat(gas);
//                DecimalFormat df = new DecimalFormat("#.00");
//                String format = df.format(gasCount);
//                EnergyWarningInfo warningInfo = new EnergyWarningInfo();
//                warningInfo.setWarningType(4);
//                warningInfo.setWarningContent("今日累计用气量" + format + "m³,超过阈值");
//                warningInfo.setUnitName(basePigpen.getBuildName());
//                warningInfo.setUnitId(energyEnvDevice.getUnitId());
//                warningInfo.setDeviceId(energyEnvDevice.getId());
//                warningInfo.setDate(new Date());
//                warningInfo.setFarmId(energyEnvDevice.getFarmId());
//                warningInfo.setUserIds(threshold.getUserIds());
//                infoMapper.insert(warningInfo);
//            }
//
//        }
//
//
//    }
//}
