package com.ruoyi.tms.task;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.ruoyi.tms.domain.TmsContract;
|
import com.ruoyi.tms.domain.TmsMessageNotify;
|
import com.ruoyi.tms.service.ITmsContractService;
|
import com.ruoyi.tms.service.ITmsMessageNotifyService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.time.LocalDate;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
@Slf4j
|
@Component("contractExpireNotifyTask")
|
public class ContractExpireNotifyTask {
|
|
@Autowired
|
ITmsContractService tmsContractService;
|
|
@Autowired
|
ITmsMessageNotifyService tmsMessageNotifyService;
|
|
/**
|
* 检查合同即将到期
|
*/
|
public void checkContractExpire() {
|
LocalDate today = LocalDate.now();
|
LocalDate targetDate = today.plusDays(90);
|
|
List<TmsContract> tmsContracts = tmsContractService.getBaseMapper().selectList(new LambdaQueryWrapper<TmsContract>()
|
.eq(TmsContract::getContractStatus, 0)
|
.between(TmsContract::getContractEndDate, today, targetDate)
|
);
|
if (tmsContracts.isEmpty()){
|
return;
|
}
|
log.info("即将到期的合同有{}个", tmsContracts.size());
|
// 提取合同ID
|
List<Integer> ids = tmsContracts.stream()
|
.map(TmsContract::getId)
|
.collect(Collectors.toList());
|
|
// 批量更新为“临期”
|
tmsContractService.getBaseMapper().update(
|
new LambdaUpdateWrapper<TmsContract>()
|
.in(TmsContract::getId, ids)
|
.set(TmsContract::getContractStatus, 1) // 1 = 临期
|
);
|
|
List<TmsMessageNotify> messageNotifies = tmsContracts.stream().map(item -> {
|
TmsMessageNotify notify = new TmsMessageNotify();
|
notify.setTitle("合同到期提醒");
|
notify.setContent("【合同编号" + item.getContractCode() + "】即将到期");
|
notify.setType(0);
|
notify.setStatus(0);
|
notify.setReadStatus(0);
|
notify.setExtraData("{\"contactStatus\":1,\"contractId\":\"" + item.getId() + "\"}");
|
return notify;
|
}).collect(Collectors.toList());
|
tmsMessageNotifyService.insertTmsMessageNotifyBatch(messageNotifies);
|
}
|
/**
|
* 检查合同已到期
|
*/
|
public void handleExpiredContracts() {
|
LocalDate today = LocalDate.now();
|
|
List<TmsContract> tmsContracts = tmsContractService.getBaseMapper().selectList(new LambdaQueryWrapper<TmsContract>()
|
.ne(TmsContract::getContractStatus, 2)
|
.le(TmsContract::getContractEndDate, today)
|
);
|
if (tmsContracts.isEmpty()){
|
return;
|
}
|
log.info("已到期的合同有{}个", tmsContracts.size());
|
// 提取合同ID
|
List<Integer> ids = tmsContracts.stream()
|
.map(TmsContract::getId)
|
.collect(Collectors.toList());
|
|
// 批量更新为“临期”
|
tmsContractService.getBaseMapper().update(
|
new LambdaUpdateWrapper<TmsContract>()
|
.in(TmsContract::getId, ids)
|
.set(TmsContract::getContractStatus, 2) // 1 = 临期
|
);
|
|
List<TmsMessageNotify> messageNotifies = tmsContracts.stream().map(item -> {
|
TmsMessageNotify notify = new TmsMessageNotify();
|
notify.setTitle("合同到期提醒");
|
notify.setContent("【合同编号" + item.getContractCode() + "】已到期");
|
notify.setType(0);
|
notify.setStatus(0);
|
notify.setReadStatus(0);
|
notify.setExtraData("{\"contactStatus\":2, \"contractId\":\"" + item.getId() + "\"}");
|
return notify;
|
}).collect(Collectors.toList());
|
tmsMessageNotifyService.insertTmsMessageNotifyBatch(messageNotifies);
|
}
|
|
|
}
|