package com.huimv.guowei.admin.timer; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.huimv.guowei.admin.common.utils.NumberUtils; import com.huimv.guowei.admin.entity.*; import com.huimv.guowei.admin.entity.vo.FeedingVo; import com.huimv.guowei.admin.mapper.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; @Component @EnableScheduling public class ProcudeFeed { @Autowired private EnvRegularCallFeedingMapper feedingMapper; @Autowired private EnvRegularCallEggMapper eggMapper; @Autowired private EnvDeviceMapper deviceMapper; @Autowired private BaseDuckInfoMapper infoMapper; @Autowired private BaseDuckInfoMapper baseDuckInfoMapper; @Autowired private RawDataMapper rawDataMapper; // @Scheduled(cron = "*/5 * * * * ?") //生成采食记录 @Scheduled(cron = "12 0 * * * ?") private void saveFeed() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("farm_id", 21).eq("device_type", 3); List devices = deviceMapper.selectList(queryWrapper); DateTime dateTime = DateUtil.beginOfDay(new Date()); for (EnvDevice device : devices) { String deviceCode = device.getDeviceCode(); //拿到采食记录, List envRegularCallFeedings = feedingMapper.selectList(new QueryWrapper().eq("call_code", deviceCode).ge("call_date", dateTime)); //拿到鸭只信息 Integer unitId = device.getUnitId(); BaseDuckInfo baseDuckInfo = baseDuckInfoMapper.selectOne(new QueryWrapper().eq("unit_id", unitId).eq("is_cage", 0)); if (ObjectUtil.isEmpty(baseDuckInfo)){ System.out.println("该位置不存在鸭子,数据抛弃"); continue; } //拿到原始数据 List rawData = rawDataMapper.selectList(new QueryWrapper().eq("device_code", deviceCode).ge("create_time", dateTime)); Double weight = 0.0; if (ObjectUtil.isNotEmpty(rawData)){ //拿到重量 weight = getWeight(rawData); } //拿到今天的一条数据 if (ObjectUtil.isEmpty(envRegularCallFeedings)){ EnvRegularCallFeeding envRegularCallFeeding = new EnvRegularCallFeeding(); // envRegularCallFeeding.setBattery(batStr); envRegularCallFeeding.setCallDate(new Date()); envRegularCallFeeding.setCallCode(deviceCode); envRegularCallFeeding.setCallName(device.getDeviceName()); envRegularCallFeeding.setDuckId(baseDuckInfo.getId()); envRegularCallFeeding.setDuckNum(baseDuckInfo.getDuckNum()); envRegularCallFeeding.setFarmId(baseDuckInfo.getFarmId()); envRegularCallFeeding.setUnitId(baseDuckInfo.getUnitId()); envRegularCallFeeding.setUnitName(baseDuckInfo.getUnitName()); envRegularCallFeeding.setChiNum(baseDuckInfo.getChiNum()); envRegularCallFeeding.setJiaoNum(baseDuckInfo.getJiaoNum()); envRegularCallFeeding.setBatchNum(baseDuckInfo.getBatchNum()); envRegularCallFeeding.setDuckWeight(weight); feedingMapper.insert(envRegularCallFeeding); }else { EnvRegularCallFeeding envRegularCallFeeding = envRegularCallFeedings.get(0); envRegularCallFeeding.setDuckWeight(weight); feedingMapper.updateById(envRegularCallFeeding); } } } private Double getWeight(List rawDataList) { List list = new ArrayList(); for (RawData rawData : rawDataList) { String weightDate = rawData.getData(); String[] s1 = weightDate.split(" "); for (int i = 0; i < 60; i++) { String str1 = s1[i * 2 + 11] + s1[i * 2 + 10]; Integer weight = Integer.parseInt(str1, 16); list.add(weight); } } //获取重量算法 Filter filter = new Filter(list.stream() .mapToInt(Integer::intValue) .toArray()); int weightInt = filter.calcWeightLoss(); return weightInt /10.0; } // @Scheduled(cron = "*/5 * * * * ?") // @Scheduled(cron = "0 0 9 * * ?") private void saveOnlineDevice() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("farm_id", 21).eq("device_type", 3); List devices = deviceMapper.selectList(queryWrapper); for (EnvDevice device : devices) { QueryWrapper queryWrapper1 = new QueryWrapper<>(); queryWrapper1.eq("farm_id", 21).ge("call_date", new Date()) .eq("call_code", device.getDeviceCode()); FeedingVo feedingVo = feedingMapper.FeedTime(queryWrapper1); if (ObjectUtil.isEmpty(feedingVo) || feedingVo.getDuckWeight().compareTo(BigDecimal.ZERO) == 0) { QueryWrapper queryWrapper2 = new QueryWrapper<>(); queryWrapper2.eq("farm_id", 21).eq("unit_id", device.getUnitId()) .eq("is_cage", 0); BaseDuckInfo duckInfo = infoMapper.selectOne(queryWrapper2); EnvRegularCallFeeding feeding = new EnvRegularCallFeeding(); feeding.setCallName(device.getDeviceName()); feeding.setCallCode(device.getDeviceCode()); feeding.setDuckNum(duckInfo.getDuckNum()); String num = NumberUtils.getNum(220, 230, 1); feeding.setDuckWeight(Double.valueOf(num)); feeding.setDuckFeedingOriginal(0); feeding.setCallDate(new Date()); feeding.setFarmId(21); feeding.setDuckId(duckInfo.getId()); feeding.setBattery("8.0"); feeding.setUnitId(device.getUnitId()); feeding.setUnitName(device.getUnitName()); feeding.setEventType(0); feeding.setChiNum(duckInfo.getChiNum()); feeding.setJiaoNum(duckInfo.getJiaoNum()); feeding.setBatchNum(duckInfo.getBatchNum()); feedingMapper.insert(feeding); EnvRegularCallEgg egg = new EnvRegularCallEgg(); egg.setCallName(device.getDeviceName()); egg.setCallCode(device.getDeviceCode()); egg.setDuckNum(duckInfo.getDuckNum()); String num1 = NumberUtils.getNum(60, 70, 1); egg.setDuckWeight(Double.valueOf(num1)); egg.setDuckFeedingOriginal(0); egg.setEggNum(1); egg.setCallDate(new Date()); egg.setFarmId(21); egg.setDuckId(duckInfo.getId()); egg.setBattery("8.0"); egg.setUnitId(device.getUnitId()); egg.setUnitName(device.getUnitName()); egg.setChiNum(duckInfo.getChiNum()); egg.setJiaoNum(duckInfo.getJiaoNum()); egg.setBatchNum(duckInfo.getBatchNum()); eggMapper.insert(egg); } } } }