郑永安
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
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
149
150
151
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);
        }
    }
 
}