1
zj
2025-10-12 84d42b6997ae87fa64b75ae085fce11d05a6ca5a
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
package com.ruoyi.im.task;
 
import cn.hutool.core.stream.CollectorUtil;
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 org.springframework.util.CollectionUtils;
 
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());
        if(!CollectionUtils.isEmpty(ids)){
            List<PaymentRecord> records = paymentRecordService.list(new LambdaQueryWrapper<PaymentRecord>()
                    .in(PaymentRecord::getOrderId, ids)
            );
            userPolicyService.removeByIds(records);
        }
        if(!CollectionUtils.isEmpty(list)){
            paymentRecordService.removeByIds(list);
        }
    }
}