package com.ruoyi.im.task;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.ruoyi.im.service.MedicalInsuranceAccountService;
|
import com.ruoyi.im.service.UserPolicyService;
|
import com.ruoyi.im.service.impl.UserPolicyServiceImpl;
|
import com.ruoyi.system.domain.MedicalInsuranceAccount;
|
import com.ruoyi.system.domain.PaymentRecord;
|
import com.ruoyi.system.domain.UserPolicy;
|
import com.ruoyi.system.service.PaymentRecordService;
|
import io.swagger.models.auth.In;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.time.LocalDate;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.stream.Collectors;
|
|
/**
|
* @program: ruoyiim
|
* @description:
|
* @create: 2025-09-21 14:21
|
**/
|
@Component
|
@Slf4j
|
public class MedicalInsuranceTask {
|
|
@Autowired
|
UserPolicyService userPolicyService;
|
|
private final AtomicBoolean syncINStockData = new AtomicBoolean(false);
|
|
private final Lock syncINStockDataLock = new ReentrantLock();
|
|
@Autowired
|
MedicalInsuranceAccountService medicalInsuranceAccountService;
|
|
@Autowired
|
PaymentRecordService paymentRecordService;
|
|
|
/**
|
* 定时筛选用户保单状态
|
*/
|
@Scheduled(cron = "0 0 0 * * ?")
|
public void insuranceExpires() {
|
if (syncINStockData.get()) { // 判断任务是否在处理中
|
return;
|
}
|
if (syncINStockDataLock.tryLock()) {
|
try {
|
syncINStockData.set(true); // 设置处理中标识为true
|
expires();
|
} finally {
|
syncINStockDataLock.unlock();
|
syncINStockData.set(false); // 设置处理中标识为false
|
}
|
}
|
}
|
|
private void expires() {
|
List<UserPolicy> list = userPolicyService.list(new LambdaQueryWrapper<UserPolicy>()
|
.eq(UserPolicy::getPolicyStatus, UserPolicy.PolicyStatus.ACTIVE)
|
);
|
List<Integer> ids = new ArrayList<>();
|
list.forEach(f->{
|
if(isExpired(f.getEndDate())){
|
f.setPolicyStatus(UserPolicy.PolicyStatus.EXPIRED);
|
ids.add(f.getId());
|
}
|
});
|
userPolicyService.updateBatchById(list);
|
List<MedicalInsuranceAccount> insuranceAccountList = medicalInsuranceAccountService.list(new LambdaQueryWrapper<MedicalInsuranceAccount>()
|
.in(MedicalInsuranceAccount::getPolicyId, ids)
|
);
|
insuranceAccountList.forEach(f->{
|
f.setAccountStatus(MedicalInsuranceAccount.AccountStatus.SUSPENDED);
|
});
|
medicalInsuranceAccountService.updateBatchById(insuranceAccountList);
|
}
|
|
public boolean isExpired(LocalDate endDate) {
|
return LocalDate.now().isAfter(endDate);
|
}
|
|
|
|
/**
|
* 定时清除未支付保单
|
*/
|
@Scheduled(cron = "0 */1 * * * ?")
|
public void executeWithFixedDelay() {
|
try {
|
log.info("定时清除未支付保单定时任务开始执行,时间:{}", System.currentTimeMillis());
|
doBusiness();
|
log.info("定时清除未支付保单定时任务执行完成");
|
} catch (Exception e) {
|
log.error("定时清除未支付保单定时任务执行异常", e);
|
}
|
}
|
|
private void doBusiness() {
|
// 计算5分钟前的时间
|
Date fiveMinutesAgo = new Date(System.currentTimeMillis() - 5 * 60 * 1000);
|
log.info("查询条件:创建时间早于 {} 的未支付订单", fiveMinutesAgo);
|
|
// 查询所有创建时间超过5分钟且状态为待支付的订单
|
List<UserPolicy> list = userPolicyService.list(new LambdaQueryWrapper<UserPolicy>()
|
.eq(UserPolicy::getPayStatus, 1) // payStatus = 1 (待支付)
|
.lt(UserPolicy::getCreatedAt, fiveMinutesAgo) // 创建时间早于5分钟前
|
.orderByAsc(UserPolicy::getCreatedAt)
|
);
|
|
// 提取orderId列表
|
List<Integer> ids = list.stream()
|
.map(UserPolicy::getId) // 提取orderId字段
|
.collect(Collectors.toList());
|
|
List<PaymentRecord> records = paymentRecordService.list(new LambdaQueryWrapper<PaymentRecord>()
|
.in(PaymentRecord::getOrderId, ids)
|
);
|
|
userPolicyService.removeByIds(list);
|
userPolicyService.removeByIds(records);
|
}
|
}
|