郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
src/main/java/com/gk/firework/Scheduls/WarningTask/PurchaseWarnTask.java
对比新文件
@@ -0,0 +1,152 @@
package com.gk.firework.Scheduls.WarningTask;
import com.gk.firework.Domain.SaleOrderInfo;
import com.gk.firework.Domain.WarnContentInfo;
import com.gk.firework.Domain.WarningInfo;
import com.gk.firework.Service.SaleOrderDetailService;
import com.gk.firework.Service.SaleOrderService;
import com.gk.firework.Service.WarnContentService;
import com.gk.firework.Service.WarningService;
import io.swagger.models.auth.In;
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 PurchaseWarnTask {
    private Logger logger = LogManager.getLogger(PurchaseWarnTask.class);
    private static final String warnType = "购买超量";
    @Autowired
    WarningService warningService;
    @Autowired
    SaleOrderService saleOrderService;
    @Autowired
    WarnContentService warnContentService;
//        @Scheduled(cron = "0/5 * * * * ?") //每隔30秒执行一次
    @Scheduled(cron = "0 0/1 * * * ?") //每隔1分钟执行一次
    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) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DATE, -warningInfo.getPeriod());
        Date starttime = calendar.getTime();
        String starttimeStr = sdf.format(starttime);
        String endttimeStr = sdf.format(now);
        Integer min = warningInfo.getMinimum();
        Integer max = warningInfo.getMaximum();
        //超过预警值
        List<SaleOrderInfo> earlyWarnList = saleOrderService.selectEarlyWarn(starttimeStr,endttimeStr,min,max);
        if (earlyWarnList.size() > 0){
            List<WarnContentInfo> warnContentInfos = new ArrayList<>();
            for (SaleOrderInfo earlyWarn : earlyWarnList){
                String warncontent = "超出购买限制,当前购买数量为"+earlyWarn.getBoxnum();
                WarnContentInfo warnContentExist = warnContentService.selectByWarn(warnType,"预警",earlyWarn.getCustomer(),warncontent);
                if (warnContentExist == null){
                    WarnContentInfo warnContentInfo = new WarnContentInfo();
                    warnContentInfo.setCustomid(earlyWarn.getCustomer());
                    warnContentInfo.setWarncontent(warncontent);
                    warnContentInfo.setWarntype(warnType);
                    warnContentInfo.setWarnlevel("预警");
                    warnContentInfo.setIsmend((byte) 0);
                    warnContentInfo.setIsneed((byte) 0);
                    warnContentInfo.setIssend((byte) 0);
                    warnContentInfo.setModifiedby("系统生成");
                    warnContentInfo.setModifieddate(new Date());
                    warnContentInfo.setPeriod(warningInfo.getPeriod());
                    warnContentInfo.setCreateddate(new Date());
                    if (warnContentInfos.size() > 0){
                        boolean isfound = false;
                        for (WarnContentInfo warnContent : warnContentInfos){
                            if (warnContent.getCustomid().equals(warnContentInfo.getCustomid())
                                    && warnContent.getWarncontent().equals(warnContentInfo.getWarncontent())){
                                isfound = true;
                                break;
                            }
                        }
                        if (!isfound){
                            warnContentInfos.add(warnContentInfo);
                        }
                    }else {
                        warnContentInfos.add(warnContentInfo);
                    }
                }
            }
            warnContentService.saveBatch(warnContentInfos);
        }
        //超过报警值
        List<SaleOrderInfo> alarmList = saleOrderService.selectAlarm(starttimeStr,endttimeStr,max);
        if (alarmList.size() > 0){
            Byte issms = warningInfo.getIssms();
            List<WarnContentInfo> warnContentInfos = new ArrayList<>();
            for (SaleOrderInfo alarm : alarmList){
                String warncontent = "超出购买限制,当前购买数量为"+alarm.getBoxnum();
                WarnContentInfo warnContentExist = warnContentService.selectByWarn(warnType,"报警",alarm.getCustomer(),warncontent);
                if (warnContentExist == null){
                    WarnContentInfo warnContentInfo = new WarnContentInfo();
                    warnContentInfo.setCustomid(alarm.getCustomer());
                    warnContentInfo.setWarncontent(warncontent);
                    warnContentInfo.setWarntype(warnType);
                    warnContentInfo.setWarnlevel("报警");
                    warnContentInfo.setIsmend((byte)0);
                    warnContentInfo.setIsneed(issms);
                    warnContentInfo.setIssend((byte)0);
                    warnContentInfo.setModifiedby("系统生成");
                    warnContentInfo.setModifieddate(new Date());
                    warnContentInfo.setPeriod(warningInfo.getPeriod());
                    warnContentInfo.setCreateddate(new Date());
                    if (warnContentInfos.size() > 0){
                        boolean isfound = false;
                        for (WarnContentInfo warnContent : warnContentInfos){
                            if (warnContent.getCustomid().equals(warnContentInfo.getCustomid())
                                    && warnContent.getWarncontent().equals(warnContentInfo.getWarncontent())){
                                isfound = true;
                                break;
                            }
                        }
                        if (!isfound){
                            warnContentInfos.add(warnContentInfo);
                        }
                    }else {
                        warnContentInfos.add(warnContentInfo);
                    }
                }
            }
            warnContentService.saveBatch(warnContentInfos);
        }
    }
}