package com.ruoyi.task.refund;

import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.refund.domain.RefundQuery;
import com.ruoyi.ss.refund.domain.RefundVO;
import com.ruoyi.ss.refund.domain.enums.RefundStatus;
import com.ruoyi.ss.refund.service.RefundService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wjh
 * 2024/9/11
 */
@Component
@Slf4j
public class RefundTask {

    @Autowired
    private RefundService refundService;

    // 刷新退款结果
    public void refreshRefundResult() {
        // 查询退款中的退款订单
        RefundQuery query = new RefundQuery();
        query.setStatus(RefundStatus.REFUNDING.getStatus());
        List<RefundVO> list = refundService.selectRefundList(query);

        if (CollectionUtils.isEmptyElement(list)) {
            log.info("没有正在退款中的退款订单");
            return;
        }

        // 处理退款订单
        for (RefundVO refund : list) {
            try {
                log.info("退款订单处理：{}", refund.getRefundNo());
                int i = refundService.refreshResult(refund);
                log.info("处理结果：{}", i);
            } catch (Exception e) {
                log.error("退款订单{}处理失败：{}", refund.getRefundNo(), e.getMessage());
            }
        }
    }

}
