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

import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.ss.app.domain.AppVO;
import com.ruoyi.ss.app.domain.enums.AppType;
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.PayBillVO;
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.UserService;
import com.ruoyi.ss.vipOrder.domain.VipOrder;
import com.ruoyi.ss.vipOrder.domain.bo.AddPayVipOrderBO;
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 {

    /**
     * 计算渠道成本
     */
    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();
        SmUserVO user = bo.getUser();
        AppVO app = bo.getApp();
        ChannelVO channel = bo.getChannel();
        if (order == null || channel == null || user == null || app == 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()));
        po.setIp(IpUtils.getIpAddr());
        po.setAppId(user.getAppId());

        // 支付人
        if (AppType.WX.getType().equals(app.getType())) {
            po.setAccount(user.getWxOpenId());
        } else if (AppType.ALI.getType().equals(app.getType())) {
            po.setAccount(user.getAliOpenId());
        }

        return po;
    }

    @Override
    public PayBill toPoByVipOrderBO(AddPayVipOrderBO bo) {
        if (bo == null) {
            return null;
        }
        VipOrder order = bo.getOrder();
        SmUserVO user = bo.getUser();
        ChannelVO channel = bo.getChannel();
        AppVO app = bo.getApp();
        if (order == null || channel == null || user == null || app == null) {
            return null;
        }

        PayBill pay = new PayBillVO();
        pay.setBstType(PayBillBstType.VIP_ORDER.getType());

        // 订单数据
        pay.setBstId(order.getId());
        pay.setAmount(order.getAmount());
        pay.setDescription("VIP订单:" + order.getOrderNo());
        // 渠道数据
        pay.setChannelId(channel.getChannelId());
        pay.setChannelCost(this.calcChannelCost(channel, order.getAmount()));
        // 用户数据
        pay.setAppId(user.getAppId());
        // 支付人
        if (AppType.WX.getType().equals(app.getType())) {
            pay.setAccount(user.getWxOpenId());
        } else if (AppType.ALI.getType().equals(app.getType())) {
            pay.setAccount(user.getAliOpenId());
        }
        // 基础数据
        pay.setIp(IpUtils.getIpAddr());
        pay.setStatus(PayBillStatus.WAIT_PAY.getStatus());

        return pay;
    }
}
