郑永安
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
package com.gk.firework.Scheduls.WarningTask;
 
import com.gk.firework.Domain.*;
import com.gk.firework.Domain.Enum.CertificateStatus;
import com.gk.firework.Service.*;
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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * @author : jingjy
 * @date : 2021/6/11 14:16
 */
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class TransportWarnTask {
    private Logger logger = LogManager.getLogger(StockWarnTask.class);
    private static final String WARN_TYPE = "运输证超期";
    private static final String WARN_LEVEL = "预警";
    private static final Integer WARN_PERIOD = 30;
    @Autowired
    private TransportCertService transportCertService;
    @Autowired
    private TransportCertificateService transportCertificateService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private WarnContentService warnContentService;
    @Autowired
    private EntryService entryService;
 
    @Scheduled(cron = "0 0 2 * * ?") //每天凌晨两点执行一次
    private void transportWarn(){
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) - 15);
        try {
            List<TransportCert> transportCerts = transportCertService.selectWarnList(calendar.getTime(),date);
            for (TransportCert transportCert : transportCerts){
                Boolean isEntry = entryService.isTransportCertEntry(transportCert.getCode());
                if (!isEntry && transportCert.getprocesstime().getTime() < date.getTime()){
                    insertWarnContent(transportCert.getEnterprisenumber(),transportCert.getCreatebyname(),transportCert.getCode(),
                            transportCert.getprocesstime(),date);
                }
            }
 
            List<TransportCertificate> certificates = transportCertificateService.selectWarnList(calendar.getTime(),date);
            for (TransportCertificate certificate : certificates){
                Boolean isEntry = entryService.isTransportCertEntry(certificate.getCode());
                if (!isEntry && certificate.getDeadline().getTime() < date.getTime()){
                    insertWarnContent(certificate.getEnterprisenumber(),certificate.getCreatebyname(),
                            certificate.getCode(),certificate.getDeadline(),date);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void insertWarnContent(String enterpriseNumber, String createByName, String code, Date deadline, Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
        String deadlineStr = dateFormat.format(deadline);
        Enterprise enterprise = enterpriseService.selectEnterpriseByNumber(enterpriseNumber);
        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 = createByName+"申请的运输证:"
                +code+"预计送达时间:"+deadlineStr+",超期未入库!";
        warnContentInfo.setWarncontent(content);
 
        WarnContentInfo warnContentInfoExist = warnContentService.selectByWarn(WARN_TYPE,WARN_LEVEL,null,content);
        if (warnContentInfoExist == null){
            warnContentService.save(warnContentInfo);
        }
    }
 
 
}