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

import com.ruoyi.ss.channelWithdraw.domain.ChannelWithdrawVO;
import com.ruoyi.ss.channelWithdraw.service.ChannelWithdrawService;
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.UserVO;
import com.ruoyi.ss.user.service.IUserService;
import com.ruoyi.ss.withdraw.domain.WithdrawVO;
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 IUserService userService;


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

        // 用户
        UserVO user = userService.selectUserById(withdrawVO.getOwnerId());

        // 拼接数据
        TransferVO vo = new TransferVO();
        vo.setBstType(TransferBstType.WITHDRAW.getType());
        vo.setBstId(withdrawVO.getId());
        vo.setStatus(TransferStatus.WAIT_TRANSFER.getStatus());
        vo.setBatchName("提现转账");
        vo.setBatchRemark(String.format("提现申请%s转账", withdrawVO.getWithdrawNo()));
        vo.setTotalAmount(withdrawVO.getReceivedAmount());

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

        return vo;
    }
}
