package com.ruoyi.ss.transfer.service.impl;

import com.ruoyi.ss.account.domain.enums.AccountType;
import com.ruoyi.ss.channelWithdraw.domain.ChannelWithdrawVO;
import com.ruoyi.ss.channelWithdraw.service.ChannelWithdrawService;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transfer.domain.TransferVO;
import com.ruoyi.ss.transfer.domain.enums.TransferBstType;
import com.ruoyi.ss.transfer.domain.enums.TransferStatus;
import com.ruoyi.ss.transfer.service.TransferConverter;
import com.ruoyi.ss.transferDetail.domain.TransferDetailVO;
import com.ruoyi.ss.transferDetail.domain.enums.TransferDetailStatus;
import com.ruoyi.ss.user.domain.SmUserVO;
import com.ruoyi.ss.user.service.ISmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjh
 * 2024/8/9
 */
@Service
public class TransferConverterImpl implements TransferConverter {

    @Autowired
    private ChannelWithdrawService channelWithdrawService;

    @Autowired
    private ISmUserService userService;


    /**
     * 提现转为转账单VO
     */
    @Override
    public TransferVO toVo(TransactionBillVO bill) {
        if (bill == null) {
            return null;
        }

        // 渠道
        ChannelWithdrawVO channel = channelWithdrawService.selectChannelWithdrawByChannelId(bill.getChannelId());

        // 用户
        SmUserVO user = userService.selectSimpleById(bill.getUserId());


        // 拼接数据
        TransferVO vo = new TransferVO();
        // 渠道转为账户类型
        if (channel != null) {
            AccountType accountType = AccountType.parse(channel.getAccountType());
            if (accountType != null) {
                vo.setAccountType(accountType.getType());
            }
        }
        vo.setBstType(TransferBstType.WITHDRAW.getType());
        vo.setBstId(bill.getBillId());
        vo.setStatus(TransferStatus.WAIT_TRANSFER.getStatus());
        vo.setBatchName("提现转账");
        vo.setBatchRemark(String.format("提现申请%s转账", bill.getBillNo()));
        vo.setTotalAmount(bill.getArrivalAmount());

        // 生成明细
        List<TransferDetailVO> detailList = new ArrayList<>();
        TransferDetailVO detail = new TransferDetailVO();
        detail.setStatus(TransferDetailStatus.WAIT_TRANSFER.getStatus());
        detail.setAmount(bill.getArrivalAmount());
        detail.setAccountNo(bill.getAccountNo());
        detail.setRemark(String.format("提现申请%s转账", bill.getBillNo()));
        // 当转账达到2000时，需要实名
        if (user != null && bill.getArrivalAmount().compareTo(new BigDecimal(2000)) >= 0) {
            detail.setUserName(user.getRealName());
        }
        detailList.add(detail);
        vo.setDetailList(detailList);

        return vo;
    }
}
