Administrator
2023-06-19 49588f5a462ae7425e7eb030438a35fd80c246fa
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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);
        }
    }
 
 
 
}