郑永安
2023-06-19 2fcd97552d16718cc7997629fd637a73a5a4483f
src/main/java/com/gk/firework/Scheduls/WarningTask/StockWarnTask.java
对比新文件
@@ -0,0 +1,148 @@
package com.gk.firework.Scheduls.WarningTask;
import com.gk.firework.Domain.StockInfo;
import com.gk.firework.Domain.WarnContentInfo;
import com.gk.firework.Domain.WarningInfo;
import com.gk.firework.Service.SaleOrderService;
import com.gk.firework.Service.StockService;
import com.gk.firework.Service.WarnContentService;
import com.gk.firework.Service.WarningService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class StockWarnTask {
    private Logger logger = LogManager.getLogger(StockWarnTask.class);
    private static final String warnType = "库存超量";
    @Autowired
    WarningService warningService;
    @Autowired
    SaleOrderService saleOrderService;
    @Autowired
    WarnContentService warnContentService;
    @Autowired
    StockService stockService;
//    @Scheduled(cron = "0/20 * * * * ?") //每隔5秒执行一次
    @Scheduled(cron = "0 0 2 * * ?") //每天凌晨两点执行一次
    private void purchaseWarn() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            List<WarningInfo> warningInfos = warningService.selectByType(warnType);
            if (warningInfos.size() > 0){
                for (WarningInfo warningInfo : warningInfos){
                    insertWarnContent(warningInfo);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void insertWarnContent(WarningInfo warningInfo) {
        Integer min = warningInfo.getMinimum();
        Integer max = warningInfo.getMaximum();
        List<Integer> slices = new ArrayList<>();
        for (int i = 1; i < 21; i++){
            slices.add(i);
        }
        //超过预警值
        List<StockInfo> earlyWarnList = stockService.selectEarlyWarn(min,max,warningInfo.getEnterprisetype(),slices);
        if (earlyWarnList.size() > 0){
            List<WarnContentInfo> warnContentInfos = new ArrayList<>();
            for (StockInfo stockInfo : earlyWarnList) {
                String warncontent = "超出库存限制,当前库存总数为"+stockInfo.getNum();
                WarnContentInfo warnContentExist = warnContentService.selectByEnterpriseWarn(warnType,"预警",Long.parseLong(stockInfo.getOwner()),warncontent);
                if (warnContentExist == null) {
                    WarnContentInfo warnContentInfo = new WarnContentInfo();
                    warnContentInfo.setWarntype(warnType);
                    warnContentInfo.setWarnlevel("预警");
                    warnContentInfo.setEnterpriseid(Long.parseLong(stockInfo.getOwner()));
                    warnContentInfo.setWarncontent(warncontent);
                    warnContentInfo.setIsmend((byte) 0);
                    warnContentInfo.setIsneed((byte) 0);
                    warnContentInfo.setIssend((byte) 0);
                    warnContentInfo.setModifiedby("系统生成");
                    warnContentInfo.setModifieddate(new Date());
                    warnContentInfo.setCreateddate(new Date());
                    if (warnContentInfos.size() > 0){
                        boolean isfound = false;
                        for (WarnContentInfo warnContent : warnContentInfos){
                            if (warnContent.getEnterpriseid().equals(warnContentInfo.getEnterpriseid())
                                    && warnContent.getWarncontent().equals(warnContentInfo.getWarncontent())){
                                isfound = true;
                                break;
                            }
                        }
                        if (!isfound){
                            warnContentInfos.add(warnContentInfo);
                        }
                    }else {
                        warnContentInfos.add(warnContentInfo);
                    }
                }
            }
            warnContentService.saveBatch(warnContentInfos);
        }
        //超过报警值
        List<StockInfo> alarmList = stockService.selectAlarm(max,warningInfo.getEnterprisetype(),slices);
        if (alarmList.size() > 0){
            List<WarnContentInfo> warnContentInfos = new ArrayList<>();
            Byte issms = warningInfo.getIssms();
            for (StockInfo stockInfo : alarmList) {
                String warncontent = "超出库存限制,当前库存总数为"+stockInfo.getNum();
                WarnContentInfo warnContentExist = warnContentService.selectByEnterpriseWarn(warnType,"报警",Long.parseLong(stockInfo.getOwner()),warncontent);
                if (warnContentExist == null) {
                    WarnContentInfo warnContentInfo = new WarnContentInfo();
                    warnContentInfo.setWarntype(warnType);
                    warnContentInfo.setWarnlevel("报警");
                    warnContentInfo.setEnterpriseid(Long.parseLong(stockInfo.getOwner()));
                    warnContentInfo.setWarncontent(warncontent);
                    warnContentInfo.setIsmend((byte) 0);
                    warnContentInfo.setIsneed(issms);
                    warnContentInfo.setIssend((byte) 0);
                    warnContentInfo.setModifiedby("系统生成");
                    warnContentInfo.setModifieddate(new Date());
                    warnContentInfo.setCreateddate(new Date());
                    if (warnContentInfos.size() > 0){
                        boolean isfound = false;
                        for (WarnContentInfo warnContent : warnContentInfos){
                            if (warnContent.getEnterpriseid().equals(warnContentInfo.getEnterpriseid())
                                    && warnContent.getWarncontent().equals(warnContentInfo.getWarncontent())){
                                isfound = true;
                                break;
                            }
                        }
                        if (!isfound){
                            warnContentInfos.add(warnContentInfo);
                        }
                    }else {
                        warnContentInfos.add(warnContentInfo);
                    }
                }
            }
            warnContentService.saveBatch(warnContentInfos);
        }
    }
}