郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.gk.firework.Scheduls.WarningTask;
 
import com.gk.firework.Domain.Enterprise;
import com.gk.firework.Domain.StockInfo;
import com.gk.firework.Domain.Vo.NoEntryVo;
import com.gk.firework.Domain.WarnContentInfo;
import com.gk.firework.Service.EnterpriseService;
import com.gk.firework.Service.StockService;
import com.gk.firework.Service.WarnContentService;
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.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * @author : jingjy
 * @date : 2021/6/16 15:55
 */
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class NoEntryWarnTask {
 
    private static final String WARN_TYPE = "超期未入库";
    private static final String WARN_LEVEL = "预警";
    private static final Integer WARN_PERIOD = 30;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private WarnContentService warnContentService;
    @Autowired
    private StockService stockService;
 
    @Scheduled(cron = "0 0 2 * * ?") //每天凌晨两点执行一次
//    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
    private void noEntryWarn(){
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        List<NoEntryVo> noEntryVos = stockService.selectNoEntryCount(calendar.getTime(),WARN_PERIOD);
        for (NoEntryVo noEntryVo : noEntryVos){
            insertWarnContent(noEntryVo,date);
        }
    }
 
    private void insertWarnContent(NoEntryVo noEntryVo, Date date) {
        Enterprise enterprise = enterpriseService.selectEnterpriseByName(noEntryVo.getName());
        WarnContentInfo warnContentInfo = new WarnContentInfo();
        warnContentInfo.setWarntype(WARN_TYPE);
        warnContentInfo.setPeriod(WARN_PERIOD);
        warnContentInfo.setEnterpriseid(enterprise.getId());
        warnContentInfo.setWarnlevel(WARN_LEVEL);
        warnContentInfo.setCreateddate(date);
        warnContentInfo.setModifieddate(date);
        warnContentInfo.setModifiedby("系统生成");
        String content = noEntryVo.getName()+"有 "+noEntryVo.getNumber()+" 个产品出库超过30天没有进行入库操作!";
        warnContentInfo.setWarncontent(content);
 
        WarnContentInfo warnContentInfoExist = warnContentService.selectByWarn(WARN_TYPE,WARN_LEVEL,null,content);
        if (warnContentInfoExist == null){
            warnContentService.save(warnContentInfo);
        }else {
            warnContentInfoExist.setModifieddate(new Date());
            warnContentService.updateById(warnContentInfoExist);
        }
    }
}