package com.ruoyi.task.transfer;

import com.ruoyi.ss.transfer.domain.TransferQuery;
import com.ruoyi.ss.transfer.domain.TransferVO;
import com.ruoyi.ss.transfer.domain.enums.TransferStatus;
import com.ruoyi.ss.transfer.service.TransferAssembler;
import com.ruoyi.ss.transfer.service.TransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wjh
 * 2024/8/12
 */
@Component
@Slf4j
public class TransferTask {

    @Autowired
    private TransferService transferService;

    @Autowired
    private TransferAssembler transferAssembler;

    /**
     * 定时刷新转账状态
     */
    public void refreshTransferStatus() {
        // 查询转账中的转账单
        TransferQuery query = new TransferQuery();
        query.setStatus(TransferStatus.TRANSFER_ING.getStatus());
        List<TransferVO> transferList = transferService.selectTransferList(query);

        // 拼接明细
        transferAssembler.assembleDetail(transferList);

        // 执行刷新
        for (TransferVO transfer : transferList) {
            try {
                transferService.refreshTransferStatus(transfer);
            } catch (Exception e) {
                log.error("刷新{}转账状态失败:{}", transfer.getBatchId(), e.getMessage());
            }
        }
    }


}
