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 soonExpireCompanyInfoList = companyInfoService.list(expireQueryWrapper); QueryWrapper companyWarningQueryWrapper = new QueryWrapper(); companyWarningQueryWrapper.eq(CompanyInfo.IS_DELETE, 0); List companyWarningList = companyWarningService.list(companyWarningQueryWrapper); Map companyWarningMap = companyWarningList .stream().collect(Collectors.toMap(CompanyWarning::getCompanyId, CompanyWarning -> CompanyWarning)); List updateWarning = new ArrayList<>(); List 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 companyIds = soonExpireCompanyInfoList.stream().map(CompanyInfo::getIdStr).collect(Collectors.toList()); List 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("已读成功!"); } }