Commit f9f9203dc077353be70ac2f6e6394cf09c90bd56
1 parent
276f3e0c
定时检测超时任务
Showing
1 changed file
with
295 additions
and
5 deletions
src/main/java/com/order/erp/job/OrderOverTimeEventJob.java
1 | package com.order.erp.job; | 1 | package com.order.erp.job; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 4 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
4 | -import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | ||
5 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | 5 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
6 | import com.order.erp.common.constant.Constant; | 6 | import com.order.erp.common.constant.Constant; |
7 | -import com.order.erp.common.utils.DateUtils; | 7 | +import com.order.erp.common.constant.ServerResult; |
8 | +import com.order.erp.common.utils.EmailSendUtils; | ||
9 | +import com.order.erp.common.utils.StringUtils; | ||
8 | import com.order.erp.common.utils.TransactionHelper; | 10 | import com.order.erp.common.utils.TransactionHelper; |
11 | +import com.order.erp.domain.EmailTemplateEnum; | ||
12 | +import com.order.erp.domain.OrderEventEnum; | ||
9 | import com.order.erp.domain.OrderStatusEnum; | 13 | import com.order.erp.domain.OrderStatusEnum; |
10 | import com.order.erp.domain.dto.BaseDO; | 14 | import com.order.erp.domain.dto.BaseDO; |
11 | import com.order.erp.domain.dto.order.*; | 15 | import com.order.erp.domain.dto.order.*; |
12 | -import com.order.erp.domain.vo.order.OrderInfoResultVO; | 16 | +import com.order.erp.domain.email.OrderProductEmailVO; |
17 | +import com.order.erp.domain.vo.order.ReceiveEmailConfigItemVO; | ||
13 | import com.order.erp.service.order.*; | 18 | import com.order.erp.service.order.*; |
19 | +import freemarker.template.TemplateException; | ||
14 | import lombok.extern.slf4j.Slf4j; | 20 | import lombok.extern.slf4j.Slf4j; |
15 | -import org.joda.time.DateTime; | ||
16 | import org.springframework.scheduling.annotation.Scheduled; | 21 | import org.springframework.scheduling.annotation.Scheduled; |
17 | import org.springframework.stereotype.Component; | 22 | import org.springframework.stereotype.Component; |
18 | 23 | ||
19 | import javax.annotation.Resource; | 24 | import javax.annotation.Resource; |
25 | +import javax.mail.MessagingException; | ||
26 | +import java.io.File; | ||
27 | +import java.io.IOException; | ||
28 | +import java.time.LocalDate; | ||
29 | +import java.time.format.DateTimeFormatter; | ||
30 | +import java.time.temporal.ChronoUnit; | ||
31 | +import java.util.ArrayList; | ||
20 | import java.util.List; | 32 | import java.util.List; |
21 | -import java.util.Objects; | 33 | +import java.util.Map; |
22 | import java.util.Set; | 34 | import java.util.Set; |
35 | +import java.util.function.Function; | ||
23 | import java.util.stream.Collectors; | 36 | import java.util.stream.Collectors; |
24 | 37 | ||
38 | +import static com.order.erp.domain.EmailTemplateEnum.BIG_SAMPLE_TEMPLATE; | ||
39 | + | ||
40 | + | ||
25 | /** | 41 | /** |
26 | * @author: xms | 42 | * @author: xms |
27 | * @description: TODO | 43 | * @description: TODO |
@@ -50,13 +66,287 @@ public class OrderOverTimeEventJob { | @@ -50,13 +66,287 @@ public class OrderOverTimeEventJob { | ||
50 | @Resource | 66 | @Resource |
51 | private TransactionHelper transactionHelper; | 67 | private TransactionHelper transactionHelper; |
52 | 68 | ||
69 | + @Resource | ||
70 | + private EmailSendUtils emailSendUtils; | ||
71 | + | ||
72 | + @Resource | ||
73 | + private ReceiveEmailMappingService receiveEmailMappingService; | ||
74 | + | ||
53 | /** | 75 | /** |
54 | * 凌晨1点执行,一天一次 | 76 | * 凌晨1点执行,一天一次 |
55 | */ | 77 | */ |
56 | @Scheduled(cron = "0 0 1 * * ?") | 78 | @Scheduled(cron = "0 0 1 * * ?") |
57 | public void checkOverTimeExecute() { | 79 | public void checkOverTimeExecute() { |
80 | + /*首先判断字符串。如果没有ok并且次数达到了三次(统计fail是否存在三次),就提醒。 | ||
81 | + * 如果有ok,取出ok前的日期,得到日期,那这个日期与HOD日期进行判断*/ | ||
82 | + List<OrderBaseInfoDO> orderBaseInfoList = orderBaseInfoService.list(new LambdaQueryWrapper<OrderBaseInfoDO>() | ||
83 | + .eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN) | ||
84 | + .ne(OrderBaseInfoDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus())); | ||
85 | + Set<Long> orderList = orderBaseInfoList.stream().map(OrderBaseInfoDO::getId).collect(Collectors.toSet()); | ||
86 | + Map<Long, OrderBaseInfoDO> baseInfoDOMap = orderBaseInfoList.stream().collect(Collectors.toMap(OrderBaseInfoDO::getId, Function.identity(), (x, y) -> x)); | ||
87 | + List<OrderInspectionStageDO> orderInspectionStageList = inspectionStageService.listByIds(orderList); | ||
88 | + Map<Long, OrderInspectionStageDO> inspectionStageDOMap = orderInspectionStageList.stream().collect(Collectors.toMap(OrderInspectionStageDO::getOrderId, Function.identity(), (x, y) -> x)); | ||
89 | + List<OrderTrackStageDO> orderTrackStageList = trackStageService.listByIds(orderList); | ||
90 | + OrderProductEmailVO orderProductEmailVO = new OrderProductEmailVO(); | ||
91 | + System.out.println("1111111111111111111111111"); | ||
92 | + //只有跟单信息不为空时,才会有时间和状态。 | ||
93 | + if (CollectionUtils.isNotEmpty(orderTrackStageList)) { | ||
94 | + System.out.println("orderId"+baseInfoDOMap); | ||
95 | + System.out.println("order Base Info"+inspectionStageDOMap); | ||
96 | + orderTrackStageList.stream().forEach(x -> { | ||
97 | + System.out.println("orderId"+x.getOrderId()); | ||
98 | + System.out.println("order Base Info"+baseInfoDOMap.get(x.getOrderId())); | ||
99 | + if (StringUtils.isBlank(baseInfoDOMap.get(x.getOrderId()).getOrderHodTime())) { | ||
100 | +// return ServerResult.fail("HODTime不能为空"); | ||
101 | + } | ||
102 | + if (baseInfoDOMap.get(x.getOrderId()).getId() == x.getOrderId()) { | ||
103 | + orderProductEmailVO.setBaseInfo(baseInfoDOMap.get(x.getOrderId())); | ||
104 | + try { | ||
105 | + System.out.println("222222222222222222222"); | ||
106 | + test(EmailTemplateEnum.AITEX_SGS_TEST_TEMPLATE, x.getPpConfirmResult(), orderProductEmailVO); | ||
107 | + System.out.println("2333333333333333333333"); | ||
108 | + } catch (MessagingException e) { | ||
109 | + throw new RuntimeException(e); | ||
110 | + } catch (TemplateException e) { | ||
111 | + throw new RuntimeException(e); | ||
112 | + } catch (IOException e) { | ||
113 | + throw new RuntimeException(e); | ||
114 | + } | ||
115 | + try { | ||
116 | + test(EmailTemplateEnum.CONFIRM_SAMPLE_TEMPLATE, x.getShippmentSampleConfirmResult(), orderProductEmailVO); | ||
117 | + } catch (MessagingException e) { | ||
118 | + throw new RuntimeException(e); | ||
119 | + } catch (TemplateException e) { | ||
120 | + throw new RuntimeException(e); | ||
121 | + } catch (IOException e) { | ||
122 | + throw new RuntimeException(e); | ||
123 | + } | ||
124 | + try { | ||
125 | + test(EmailTemplateEnum.AITEX_SGS_TEST_TEMPLATE, x.getAitexTestFinishResult(), orderProductEmailVO); | ||
126 | + } catch (MessagingException e) { | ||
127 | + throw new RuntimeException(e); | ||
128 | + } catch (TemplateException e) { | ||
129 | + throw new RuntimeException(e); | ||
130 | + } catch (IOException e) { | ||
131 | + throw new RuntimeException(e); | ||
132 | + } | ||
133 | + try { | ||
134 | + test(EmailTemplateEnum.AITEX_SGS_TEST_TEMPLATE, x.getSgsTestFinishResult(), orderProductEmailVO); | ||
135 | + } catch (MessagingException e) { | ||
136 | + throw new RuntimeException(e); | ||
137 | + } catch (TemplateException e) { | ||
138 | + throw new RuntimeException(e); | ||
139 | + } catch (IOException e) { | ||
140 | + throw new RuntimeException(e); | ||
141 | + } | ||
142 | + try { | ||
143 | + test(EmailTemplateEnum.OTHER_SAMPLE_TEMPLATE, x.getEsoSampleSendTime(), orderProductEmailVO); | ||
144 | + } catch (MessagingException e) { | ||
145 | + throw new RuntimeException(e); | ||
146 | + } catch (TemplateException e) { | ||
147 | + throw new RuntimeException(e); | ||
148 | + } catch (IOException e) { | ||
149 | + throw new RuntimeException(e); | ||
150 | + } | ||
151 | + try { | ||
152 | + test(EmailTemplateEnum.BARCODE_ARRIVAL_TEMPLATE, x.getLatestArrivalTime(), orderProductEmailVO); | ||
153 | + } catch (MessagingException e) { | ||
154 | + throw new RuntimeException(e); | ||
155 | + } catch (TemplateException e) { | ||
156 | + throw new RuntimeException(e); | ||
157 | + } catch (IOException e) { | ||
158 | + throw new RuntimeException(e); | ||
159 | + } | ||
160 | + try { | ||
161 | + test(EmailTemplateEnum.LATEST_DC_TEMPLATE, x.getLatestBkTime(), orderProductEmailVO); | ||
162 | + } catch (MessagingException e) { | ||
163 | + throw new RuntimeException(e); | ||
164 | + } catch (TemplateException e) { | ||
165 | + throw new RuntimeException(e); | ||
166 | + } catch (IOException e) { | ||
167 | + throw new RuntimeException(e); | ||
168 | + } | ||
169 | + } | ||
170 | + if (baseInfoDOMap.get(x.getOrderId()).getId() == x.getOrderId() && | ||
171 | + baseInfoDOMap.get(x.getOrderId()).getId() == inspectionStageDOMap.get(x.getOrderId()).getOrderId()) { | ||
172 | + orderProductEmailVO.setInspectionStageInfo(inspectionStageDOMap.get(x.getOrderId())); | ||
173 | + orderProductEmailVO.setBaseInfo(baseInfoDOMap.get(x.getOrderId())); | ||
174 | + //尾期验货日期。尾期放这里写是因为尾期验货日期需要从inspectionStageDOMap中获取。 | ||
175 | + try { | ||
176 | + test(EmailTemplateEnum.END_CHECK_DATE_TEMPLATE, inspectionStageDOMap.get(x.getOrderId()).getEndCheckApplyTime(), orderProductEmailVO); | ||
177 | + } catch (MessagingException e) { | ||
178 | + throw new RuntimeException(e); | ||
179 | + } catch (TemplateException e) { | ||
180 | + throw new RuntimeException(e); | ||
181 | + } catch (IOException e) { | ||
182 | + throw new RuntimeException(e); | ||
183 | + } | ||
184 | + /*加上中期验货报告和尾期验货报告的发送,因为他需要发送inspectionStageDOMap实体类中的数据*/ | ||
185 | + //todo | ||
186 | + } | ||
187 | + | ||
188 | + } | ||
189 | + ); | ||
190 | + } | ||
58 | 191 | ||
59 | } | 192 | } |
60 | 193 | ||
194 | + // 写一下检测方法。 | ||
195 | + /*@param event 代表的含义,这个是对应的枚举类型。 | ||
196 | + *@param str 代表的含义,这个是状态日期。 | ||
197 | + *@param orderProductEmailVO 代表的含义,这个邮件要发送的内容。 | ||
198 | + * */ | ||
199 | + //todo 还需要判断其他时间,不能单独只判断po时间。 | ||
200 | + public void test(EmailTemplateEnum eventEnum, String str, OrderProductEmailVO orderProductEmailVO) throws MessagingException, TemplateException, IOException { | ||
201 | + //获取邮件收件人。 | ||
202 | + System.out.println("22222222222222222222--1111111111111"); | ||
203 | + List<List<String>> emails = getEmails(eventEnum, orderProductEmailVO); | ||
204 | + if (StringUtils.isNotBlank(str)) { | ||
205 | + //如果为确认样确认,或者AITEX/SGS测试判断是否存在ok。 | ||
206 | + if (eventEnum.getTitle().equals(EmailTemplateEnum.CONFIRM_SAMPLE_TEMPLATE.getTitle()) || | ||
207 | + eventEnum.getTitle().equals(EmailTemplateEnum.AITEX_SGS_TEST_TEMPLATE.getTitle()) || | ||
208 | + eventEnum.getTitle().equals(BIG_SAMPLE_TEMPLATE.getTitle())) { | ||
209 | + System.out.println("2222222222-22222222222222"); | ||
210 | + //不包含ok并且三次输入机会都为fail了 | ||
211 | + if (!str.contains("ok") && org.apache.commons.lang3.StringUtils.countMatches(str, "fail") == 3) { | ||
212 | + //直接发邮件。 | ||
213 | + for (List email : emails) { | ||
214 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
215 | + } | ||
216 | + //设置状态为1,下次不扫描。 | ||
217 | + } else if (str.contains("ok") || org.apache.commons.lang3.StringUtils.countMatches(str, "fail") < 3) { | ||
218 | + //只会有几种种情况,一种是fail,一种是fail+fail。一种是fail+ok,一种是ok,得到最后状态前的值就可以了,前面的不用管。 | ||
219 | + String[] parats = str.split("\\+"); | ||
220 | + //获取最后状态前的时间。 | ||
221 | + String time = parats[str.length() - 3]; | ||
222 | + LocalDate parse1 = LocalDate.parse(time, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
223 | + //得到订单的截止时间。 | ||
224 | + String orderHodTime = orderProductEmailVO.getBaseInfo().getOrderHodTime(); | ||
225 | + LocalDate parse2 = LocalDate.parse(orderHodTime, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
226 | + //计算两个时间之间的天数间隔。parse2-parse1 | ||
227 | + long daysBetween = ChronoUnit.DAYS.between(parse2, parse1); | ||
228 | + if (eventEnum.getTitle().equals(EmailTemplateEnum.CONFIRM_SAMPLE_TEMPLATE.getTitle())) { | ||
229 | + if (daysBetween == 21) { | ||
230 | + //直接发邮件。 | ||
231 | + for (List email : emails) { | ||
232 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
233 | + } | ||
234 | + } | ||
235 | + } else if (eventEnum.getTitle().equals(EmailTemplateEnum.AITEX_SGS_TEST_TEMPLATE.getTitle())) { | ||
236 | + if (daysBetween == 3) { | ||
237 | + //直接发邮件。 | ||
238 | + for (List email : emails) { | ||
239 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
240 | + } | ||
241 | + } | ||
242 | + } else { | ||
243 | + if (daysBetween == 7) { | ||
244 | + //直接发邮件。 | ||
245 | + for (List email : emails) { | ||
246 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
247 | + } | ||
248 | + } | ||
249 | + } | ||
250 | + } | ||
251 | + } | ||
252 | + //没有ok或者fail状态,只通过时间来判断。 | ||
253 | + if (eventEnum.getTitle().equals(EmailTemplateEnum.OTHER_SAMPLE_TEMPLATE.getTitle())) { | ||
254 | + String orderHodTime = orderProductEmailVO.getBaseInfo().getOrderHodTime(); | ||
255 | + LocalDate parse1 = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
256 | + //订单的HOD截止时间。 | ||
257 | + LocalDate parse2 = LocalDate.parse(orderHodTime, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
258 | + long daysBetween = ChronoUnit.DAYS.between(parse2, parse1); | ||
259 | + if (daysBetween == 14) { | ||
260 | + for (List email : emails) { | ||
261 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
262 | + } | ||
263 | + } | ||
264 | + | ||
265 | + } else if (eventEnum.getTitle().equals(EmailTemplateEnum.BARCODE_ARRIVAL_TEMPLATE.getTitle())) { | ||
266 | + String orderHodTime = orderProductEmailVO.getBaseInfo().getOrderHodTime(); | ||
267 | + LocalDate parse1 = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
268 | + //订单的HOD截止时间。 | ||
269 | + LocalDate parse2 = LocalDate.parse(orderHodTime, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
270 | + long daysBetween = ChronoUnit.DAYS.between(parse2, parse1); | ||
271 | + if (daysBetween == 7) { | ||
272 | + for (List email : emails) { | ||
273 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
274 | + } | ||
275 | + } | ||
276 | + | ||
277 | + } else if (eventEnum.getTitle().equals(EmailTemplateEnum.LATEST_DC_TEMPLATE.getTitle())) { | ||
278 | + String orderHodTime = orderProductEmailVO.getBaseInfo().getOrderHodTime(); | ||
279 | + LocalDate parse1 = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
280 | + //订单的HOD截止时间。 | ||
281 | + LocalDate parse2 = LocalDate.parse(orderHodTime, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
282 | + long daysBetween = ChronoUnit.DAYS.between(parse2, parse1); | ||
283 | + if (daysBetween == 10) { | ||
284 | + for (List email : emails) { | ||
285 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
286 | + } | ||
287 | + } | ||
288 | + | ||
289 | + } else if (eventEnum.getTitle().equals(EmailTemplateEnum.END_CHECK_DATE_TEMPLATE.getTitle())) { | ||
290 | + String orderHodTime = orderProductEmailVO.getBaseInfo().getOrderHodTime(); | ||
291 | + LocalDate parse1 = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
292 | + //订单的HOD截止时间。 | ||
293 | + LocalDate parse2 = LocalDate.parse(orderHodTime, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
294 | + long daysBetween = ChronoUnit.DAYS.between(parse2, parse1); | ||
295 | + if (daysBetween == 2) { | ||
296 | + for (List email : emails) { | ||
297 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
298 | + } | ||
299 | + } | ||
300 | + } | ||
301 | + } | ||
302 | + } | ||
303 | + | ||
304 | + /*发送中期验货报告和尾期验货报告,疑问,是什么时候发送,质检信息中有中期验货字段,是否是填写了中期验货字段就发送还是说填写了跟单信息发送*/ | ||
305 | + public void send() { | ||
306 | + | ||
307 | + } | ||
308 | + | ||
309 | + /*发送生产指示书*/ | ||
310 | + public void sende(File file) { | ||
311 | + | ||
312 | + } | ||
313 | + | ||
314 | + /*返回邮箱 | ||
315 | + * 解释:根据订单的客户编码和事件类型,得到这个客户编码配置好的邮箱配置,返回邮箱*/ | ||
316 | + public List<List<String>> getEmails(EmailTemplateEnum eventEnum, OrderProductEmailVO orderProductEmailVO) { | ||
317 | + String customerCode = orderProductEmailVO.getBaseInfo().getCustomerCode(); | ||
318 | + List<ReceiveEmailMappingDO> receiveEmailMappingDOList = receiveEmailMappingService.list(new LambdaQueryWrapper<ReceiveEmailMappingDO>() | ||
319 | + .eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN) | ||
320 | + .eq(ReceiveEmailMappingDO::getTypeValue, customerCode)); | ||
321 | + Set<List<String>> emailList = receiveEmailMappingDOList.stream().map(x -> { | ||
322 | + ReceiveEmailConfigItemVO receiveEmailConfigItemVO = JSON.parseObject(x.getConfigInfos(), ReceiveEmailConfigItemVO.class); | ||
323 | + if (eventEnum.getTemplateId() == OrderEventEnum.getEventEnum(receiveEmailConfigItemVO.getEvent()).getTemplateId()) { | ||
324 | + List<String> emailsList = receiveEmailConfigItemVO.getEmails(); | ||
325 | + return emailsList; | ||
326 | + } | ||
327 | + return null; | ||
328 | + }).collect(Collectors.toSet()); | ||
329 | + return new ArrayList<>(emailList); | ||
330 | + } | ||
331 | + | ||
332 | + | ||
333 | + /* public void a(String str, OrderProductEmailVO orderProductEmailVO) { | ||
334 | + //包含ok。 | ||
335 | + String[] parats = str.split("\\+"); | ||
336 | + //获取ok前的时间。 | ||
337 | + String time = parats[str.length() - 3]; | ||
338 | + LocalDate parse1 = LocalDate.parse(time, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
339 | + //得到订单的截止时间。 | ||
340 | + String orderHodTime = orderProductEmailVO.getBaseInfo().getOrderHodTime(); | ||
341 | + LocalDate parse2 = LocalDate.parse(orderHodTime, DateTimeFormatter.ofPattern("yyyy-M-d")); | ||
342 | + //计算两个时间之间的天数间隔。parse2-parse1 | ||
343 | + long daysBetween = ChronoUnit.DAYS.between(parse2, parse1); | ||
344 | + if (daysBetween == 21) { | ||
345 | + //直接发邮件。 | ||
346 | + for (List email : emails) { | ||
347 | + emailSendUtils.sendEmail(eventEnum, email, orderProductEmailVO); | ||
348 | + } | ||
349 | + } | ||
350 | + }*/ | ||
61 | 351 | ||
62 | } | 352 | } |