郑永安
2023-06-19 451e341df739f387201914b67323efa1c4f4c8d3
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.ruoyi.system.web;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.company.CompanyInfo;
import com.ruoyi.system.domain.company.CompanyWarning;
import com.ruoyi.system.domain.vo.CompanyWarningVO;
import com.ruoyi.system.service.company.ICompanyInfoService;
import com.ruoyi.system.service.company.ICompanyWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
 
@RestController
public class CompanyWarningController {
 
 
    @Value("${expire-warning-day}")
    private Integer expireWarningData;
 
    @Autowired
    ICompanyWarningService companyWarningService;
 
    @Autowired
    ICompanyInfoService companyInfoService;
 
    @RequestMapping("/checkCompanyIsSoonExpire")
    @Transactional(rollbackFor = Exception.class)
    public void checkCompanyIsSoonExpire() {
        Date now;
        try {
            now = DateUtils.parseDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date()), DateUtils.YYYY_MM_DD);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Date expireDate = DateUtils.addDays(now, expireWarningData);
        //查询还有30天即将过期的企业
        QueryWrapper expireQueryWrapper = new QueryWrapper();
        expireQueryWrapper.eq(CompanyInfo.IS_DELETE, 0);
        expireQueryWrapper.lt(CompanyInfo.VALIDITY_DATE_END, expireDate);
        //过期的企业列表
        List<CompanyInfo> soonExpireCompanyInfoList = companyInfoService.list(expireQueryWrapper);
 
 
        QueryWrapper companyWarningQueryWrapper = new QueryWrapper();
        companyWarningQueryWrapper.eq(CompanyInfo.IS_DELETE, 0);
        List<CompanyWarning> companyWarningList = companyWarningService.list(companyWarningQueryWrapper);
 
        Map<String, CompanyWarning> companyWarningMap = companyWarningList
                .stream().collect(Collectors.toMap(CompanyWarning::getCompanyId, CompanyWarning -> CompanyWarning));
 
        List<CompanyWarning> updateWarning = new ArrayList<>();
        List<CompanyWarning> addWarningList = new ArrayList<>();
 
        soonExpireCompanyInfoList.forEach(companyInfo -> {
            //如果已经产生报警信息
            CompanyWarning companyWarning = companyWarningMap.get(companyInfo.getId().toString());
            if (companyWarning == null) {
                CompanyWarning newCompanyWarn = new CompanyWarning();
                String msg = "";
                if (companyInfo.getValidityDateEnd().before(now)) {
                    msg = "【" + companyInfo.getCompanyName() + "】证书已到期!";
                    newCompanyWarn.setIsExpire(1);
                } else {
                    msg = "【" + companyInfo.getCompanyName() + "】证书有效期不足" + expireWarningData + "天!";
                    newCompanyWarn.setIsExpire(0);
                }
                newCompanyWarn.setMsg(msg);
                newCompanyWarn.setCompanyId(String.valueOf(companyInfo.getId()));
                newCompanyWarn.setCreateTime(new Date());
                addWarningList.add(newCompanyWarn);
            } else {
                Integer isExpire = companyWarning.getIsExpire();
                String msg = "";
                String oldMsg = companyWarning.getMsg();
                if (companyInfo.getValidityDateEnd().before(now)) {
                    msg = "【" + companyInfo.getCompanyName() + "】证书已到期!";
                    companyWarning.setIsExpire(1);
                } else {
                    msg = "【" + companyInfo.getCompanyName() + "】证书有效期不足" + expireWarningData + "天!";
                    companyWarning.setIsExpire(0);
                }
                companyWarning.setMsg(msg);
                companyWarning.setUpdateTime(new Date());
                //如果预警装填变更(即将到期-》到期),或者预警信息如预警天数发生变动,更新预警类型
                if (isExpire != companyWarning.getIsExpire() || !StringUtils.equals(msg, oldMsg)) {
                    companyWarning.setIsRead(0);
                    updateWarning.add(companyWarning);
                }
 
            }
        });
 
        List<String> companyIds = soonExpireCompanyInfoList.stream().map(CompanyInfo::getIdStr).collect(Collectors.toList());
 
        List<CompanyWarning> deleteWarning = companyWarningList.stream()
                .filter(n -> !companyIds.contains(n.getCompanyId())).collect(Collectors.toList());
 
        //报警信息修改,报警信息修改或者为过去-》过期 过期-》未过期
        if (CollectionUtils.isNotEmpty(updateWarning)) {
            companyWarningService.updateBatchById(updateWarning);
        }
        //添加新的警报信息
        if (CollectionUtils.isNotEmpty(addWarningList)) {
            companyWarningService.saveBatch(addWarningList);
        }
        // 证书续期成功删除对应报警信息
        if (CollectionUtils.isNotEmpty(deleteWarning)) {
            deleteWarning.forEach(n -> {
                n.setIsDelete(1);
                n.setUpdateTime(new Date());
            });
            companyWarningService.updateBatchById(deleteWarning);
        }
    }
 
    @PostMapping("/companyWarn/findCompanyWarningPage")
    public AjaxResult findCompanyWarningPage(@RequestBody CompanyWarningVO companyWarningVO) {
        if (companyWarningVO.getPageSize() == null || companyWarningVO.getPageNum() == null) {
            return AjaxResult.error("请求参数【pageSize,pageNum】不能为空!");
        }
        PageHelper.startPage(companyWarningVO.getPageNum(), companyWarningVO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(CompanyWarning.IS_DELETE, 0);
        queryWrapper.orderByDesc(CompanyWarning.CREATE_TIME);
        queryWrapper.orderByAsc(CompanyWarningVO.IS_READ);
        PageInfo pageInfo = new PageInfo(companyWarningService.list(queryWrapper));
        return AjaxResult.success(pageInfo);
    }
 
    @PostMapping("/companyWarn/warningRead")
    public AjaxResult warningRead(@RequestBody CompanyWarning companyWarning) {
        companyWarning.setIsRead(1);
        companyWarningService.updateById(companyWarning);
        return AjaxResult.success("已读成功!");
    }
 
 
}