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

import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.channel.domain.enums.ChannelPlatform;
import com.ruoyi.ss.channel.service.ChannelService;
import com.ruoyi.ss.payBill.domain.PayBill;
import com.ruoyi.ss.payBill.domain.enums.PayBillBstType;
import com.ruoyi.ss.payBill.domain.enums.PayBillStatus;
import com.ruoyi.ss.payBill.service.PayBillConverter;
import com.ruoyi.ss.transactionBill.domain.bo.RechargePayDepositBO;
import com.ruoyi.ss.transactionBill.domain.dto.RechargePayBO;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
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.math.RoundingMode;

/**
 * @author wjh
 * 2024/8/9
 */
@Service
public class PayBillConverterImpl implements PayBillConverter {

    @Autowired
    private ISmUserService userService;

    @Autowired
    private ChannelService channelService;

    /**
     * 计算渠道成本
     */
    private BigDecimal calcChannelCost(ChannelVO channel, BigDecimal amount) {
        if (channel == null || amount == null) {
            return null;
        }
        return channel.getCostRate().multiply(amount).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
    }

    @Override
    public PayBill toPoByRechargeMoney(RechargePayBO bo) {
        if (bo == null) {
            return null;
        }

        TransactionBillVO order = bo.getOrder();
        if (order == null) {
            return null;
        }

        ChannelVO channel = bo.getChannel();
        if (channel == null) {
            return null;
        }

        SmUserVO user = bo.getUser();
        if (user == null) {
            return null;
        }

        PayBill po = new PayBill();
        po.setBstType(PayBillBstType.RECHARGE_ORDER.getType());
        po.setBstId(order.getBillId());
        po.setAmount(order.getMoney());
        po.setChannelId(channel.getChannelId());
        po.setStatus(PayBillStatus.WAIT_PAY.getStatus());
        po.setDescription("充值订单:" + order.getBillNo());
        po.setChannelCost(this.calcChannelCost(channel, order.getMoney()));

        // 支付人
        if (ChannelPlatform.WX.getCode().equals(channel.getPlatform())) {
            po.setAccount(user.getWxOpenId());
        } else if (ChannelPlatform.ALI.getCode().equals(channel.getPlatform())) {
            po.setAccount(user.getAliOpenId());
        }

        return po;
    }

    @Override
    public PayBill toPoByRechargeDeposit(RechargePayDepositBO bo) {

        if (bo == null) {
            return null;
        }

        TransactionBillVO order = bo.getOrder();
        if (order == null) {
            return null;
        }

        ChannelVO channel = bo.getChannel();
        if (channel == null) {
            return null;
        }

        SmUserVO user = bo.getUser();
        if (user == null) {
            return null;
        }

        PayBill po = new PayBill();
        po.setBstType(PayBillBstType.RECHARGE_ORDER_DEPOSIT.getType());
        po.setBstId(order.getBillId());
        po.setAmount(order.getSuitDeposit());
        po.setChannelId(channel.getChannelId());
        po.setStatus(PayBillStatus.WAIT_PAY.getStatus());
        po.setDescription("充值订单押金:" + order.getBillNo());
        po.setChannelCost(this.calcChannelCost(channel, order.getSuitPrice()));

        // 支付人
        if (ChannelPlatform.WX.getCode().equals(channel.getPlatform())) {
            po.setAccount(user.getWxOpenId());
        } else if (ChannelPlatform.ALI.getCode().equals(channel.getPlatform())) {
            po.setAccount(user.getAliOpenId());
        }

        return po;
    }
}
