//package com.huimv.admin.timer;
//
//import cn.hutool.core.util.ObjectUtil;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.huimv.admin.entity.*;
//import com.huimv.admin.mapper.*;
//import com.huimv.admin.service.IDisplayTypeService;
//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.time.LocalDateTime;
//import java.util.List;
//
//@Configuration
//@EnableScheduling
//public class ProdWarningTimer {
//    @Autowired
//    private BaseFarmMapper baseFarmMapper;
//    @Autowired
//    private ProdWarningInfoMapper prodWarningInfoMapper;
//    @Autowired
//    private ProdProductionMapper prodProductionMapper;
//    @Autowired
//    private ProdProductionThresholdMapper prodProductionThresholdMapper;
//    @Autowired
//    private ProdStockMapper prodStockMapper;
//    @Autowired
//    private ProdStockThresholdMapper prodStockThresholdMapper;
//    @Autowired
//    private IDisplayTypeService displayTypeService;
//
////    @Scheduled(cron = "*/5 * * * * ?")
//    @Scheduled(cron = "0 0 0 * * ?")
//    private void action(){
//        Integer type1 = 0;
//        List<BaseFarm> baseFarms = baseFarmMapper.selectList(new QueryWrapper<BaseFarm>().lambda().ne(BaseFarm::getId, 25));
//        for (BaseFarm baseFarm : baseFarms) {
//            Integer farmId = baseFarm.getId();
//            DisplayType displayType1 = displayTypeService.getOne(new QueryWrapper<DisplayType>().lambda().eq(DisplayType::getFarmId, farmId).eq(DisplayType::getEventType, 3));
//            if (ObjectUtil.isNotEmpty(displayType1)){
//                type1 = displayType1.getDisplayType();
//            }
//            ProdStock prodStock = prodStockMapper.selectOne(new QueryWrapper<ProdStock>().lambda()
//                    .eq(ProdStock::getDisplayType, type1)
//                    .eq(ProdStock::getFarmId, farmId)
//                    .orderByDesc(ProdStock::getCreatTime).last("LIMIT 1"));
//            if (ObjectUtil.isNotEmpty(prodStock)){
//                ProdStockThreshold threshold = prodStockThresholdMapper.selectOne(new QueryWrapper<ProdStockThreshold>().lambda().eq(ProdStockThreshold::getFarmId, farmId));
//                if (ObjectUtil.isNotEmpty(threshold)){
//                    Integer minBrzcl = threshold.getMinBrzcl();
//                    Integer minByzcl = threshold.getMinByzcl();
//                    Integer minGzcl = threshold.getMinGzcl();
//                    Integer minHbzcl = threshold.getMinHbzcl();
//                    Integer minMzcl = threshold.getMinMzcl();
//                    Integer minYfzcl = threshold.getMinYfzcl();
//                    if (ObjectUtil.isNotEmpty(minBrzcl)){
//                        if (ObjectUtil.isNotEmpty(prodStock.getBrzcl())){
//                            if (prodStock.getBrzcl().compareTo(minBrzcl) < 0){
//                                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                                prodWarningInfo.setDate(LocalDateTime.now());
//                                prodWarningInfo.setFarmId(farmId);
//                                prodWarningInfo.setWarningContent("哺乳猪存栏小于阈值!");
//                                prodWarningInfo.setWarningType(0);
//                                prodWarningInfoMapper.insert(prodWarningInfo);
//                            }
//                        }
//                    }
//                    if (ObjectUtil.isNotEmpty(minByzcl)){
//                        if (ObjectUtil.isNotEmpty(prodStock.getByzcl())){
//                            if (prodStock.getByzcl().compareTo(minByzcl) < 0){
//                                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                                prodWarningInfo.setDate(LocalDateTime.now());
//                                prodWarningInfo.setFarmId(farmId);
//                                prodWarningInfo.setWarningContent("保育猪存栏小于阈值!");
//                                prodWarningInfo.setWarningType(0);
//                                prodWarningInfoMapper.insert(prodWarningInfo);
//                            }
//                        }
//                    }
//                    if (ObjectUtil.isNotEmpty(minGzcl)){
//                        if (ObjectUtil.isNotEmpty(prodStock.getGzcl())){
//                            if (prodStock.getGzcl().compareTo(minGzcl) < 0){
//                                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                                prodWarningInfo.setDate(LocalDateTime.now());
//                                prodWarningInfo.setFarmId(farmId);
//                                prodWarningInfo.setWarningContent("公猪存栏小于阈值!");
//                                prodWarningInfo.setWarningType(0);
//                                prodWarningInfoMapper.insert(prodWarningInfo);
//                            }
//                        }
//                    }
//                    if (ObjectUtil.isNotEmpty(minHbzcl)){
//                        if (ObjectUtil.isNotEmpty(prodStock.getHbzcl())){
//                            if (prodStock.getHbzcl().compareTo(minHbzcl) < 0){
//                                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                                prodWarningInfo.setDate(LocalDateTime.now());
//                                prodWarningInfo.setFarmId(farmId);
//                                prodWarningInfo.setWarningContent("后备猪存栏小于阈值!");
//                                prodWarningInfo.setWarningType(0);
//                                prodWarningInfoMapper.insert(prodWarningInfo);
//                            }
//                        }
//                    }
//                    if (ObjectUtil.isNotEmpty(minMzcl)){
//                        if (ObjectUtil.isNotEmpty(prodStock.getMzcl())){
//                            if (prodStock.getMzcl().compareTo(minMzcl) < 0){
//                                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                                prodWarningInfo.setDate(LocalDateTime.now());
//                                prodWarningInfo.setFarmId(farmId);
//                                prodWarningInfo.setWarningContent("母猪存栏小于阈值!");
//                                prodWarningInfo.setWarningType(0);
//                                prodWarningInfoMapper.insert(prodWarningInfo);
//                            }
//                        }
//                    }
//                    if (ObjectUtil.isNotEmpty(minYfzcl)){
//                        if (ObjectUtil.isNotEmpty(prodStock.getYfzcl())){
//                            if (prodStock.getYfzcl().compareTo(minYfzcl) < 0){
//                                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                                prodWarningInfo.setDate(LocalDateTime.now());
//                                prodWarningInfo.setFarmId(farmId);
//                                prodWarningInfo.setWarningContent("育肥猪存栏小于阈值!");
//                                prodWarningInfo.setWarningType(0);
//                                prodWarningInfoMapper.insert(prodWarningInfo);
//                            }
//                        }
//                    }
//                }
//            }
//            ProdProduction prodProduction = prodProductionMapper.selectOne(new QueryWrapper<ProdProduction>().lambda()
//                    .eq(ProdProduction::getFarmId, farmId).orderByDesc(ProdProduction::getCreatTime).last("LIMIT 1"));
//            if (ObjectUtil.isNotEmpty(prodProduction)){
//                ProdProductionThreshold prodProductionThreshold = prodProductionThresholdMapper.selectOne(new QueryWrapper<ProdProductionThreshold>().lambda().eq(ProdProductionThreshold::getFarmId,farmId));
//                if (ObjectUtil.isNotEmpty(prodProductionThreshold)){
//                    checkAndWarn(prodProductionThreshold.getMinBychl(), prodProduction.getBychl(),farmId, 1, "保育成活率小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinCfchl(), prodProduction.getCfchl(),farmId, 1, "产房成活率小于阈值!");
//                    checkAndWarn2(prodProductionThreshold.getMinFccts(), prodProduction.getFccts(),farmId, 1, "非生产天数大于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinFzchl(), prodProduction.getFzchl(),farmId, 1, "肥猪成活率小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinMrwll(), prodProduction.getMrwll(),farmId, 1, "每日喂料量小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinPjwchzs(), prodProduction.getPjwchzs(),farmId, 1, "平均窝产活仔数小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinPjwczczs(), prodProduction.getPjwczczs(),farmId, 1, "平均窝产正常仔数小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinPjwdnz(), prodProduction.getPjwdnz(),farmId, 1, "平均窝断奶窝重小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinPsy(), prodProduction.getPsy(),farmId, 1, "PSY小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinPzfml(), prodProduction.getPzfml(),farmId, 1, "配种分娩率小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinQrdpl(), prodProduction.getQrdpl(),farmId, 1, "7日断配率小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinRzz(), prodProduction.getRzz(),farmId, 1, "日增重小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinScws(), prodProduction.getScws(),farmId, 1, "年产窝数小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinWdnzzs(), prodProduction.getWdnzzs(),farmId, 1, "窝断奶仔猪数小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinYfchl(), prodProduction.getYfchl(),farmId, 1, "育肥成活率小于阈值!");
//                    checkAndWarn(prodProductionThreshold.getMinPjwczs(), prodProduction.getPjwczs(),farmId, 1, "平均窝产仔数小于阈值!");
//                }
//            }
//        }
//    }
//
//    private void checkAndWarn(String threshold, String actualValue,Integer farmId, int warningType, String warningContent) {
//        if (ObjectUtil.isNotEmpty(threshold) && ObjectUtil.isNotEmpty(actualValue)) {
//            if (Double.valueOf(actualValue).compareTo(Double.valueOf(threshold)) < 0) {
//                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                prodWarningInfo.setDate(LocalDateTime.now());
//                prodWarningInfo.setFarmId(farmId);
//                prodWarningInfo.setWarningContent(warningContent);
//                prodWarningInfo.setWarningType(warningType);
//                prodWarningInfoMapper.insert(prodWarningInfo);
//            }
//        }
//    }
//
//    private void checkAndWarn2(String threshold, String actualValue,Integer farmId, int warningType, String warningContent) {
//        if (ObjectUtil.isNotEmpty(threshold) && ObjectUtil.isNotEmpty(actualValue)) {
//            if (Double.valueOf(actualValue).compareTo(Double.valueOf(threshold)) > 0) {
//                ProdWarningInfo prodWarningInfo = new ProdWarningInfo();
//                prodWarningInfo.setDate(LocalDateTime.now());
//                prodWarningInfo.setFarmId(farmId);
//                prodWarningInfo.setWarningContent(warningContent);
//                prodWarningInfo.setWarningType(warningType);
//                prodWarningInfoMapper.insert(prodWarningInfo);
//            }
//        }
//    }
//
//}
