1
zj
2025-10-13 8f886e9bb2f9204cf0b923979558482249aa8aef
ruoyi-admin/src/main/java/com/ruoyi/im/task/MedicalInsuranceTask.java
@@ -94,44 +94,44 @@
    /**
     * 定时清除未支付保单
     */
    @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)){
            userPolicyService.removeByIds(list);
        }
    }
//    /**
//     * 定时清除未支付保单
//     */
//    @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)
//            );
//            paymentRecordService.removeByIds(records);
//        }
//        if(!CollectionUtils.isEmpty(list)){
//            userPolicyService.removeByIds(list);
//        }
//    }
}