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

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.enums.PayChannel;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.order.domain.Order;
import com.ruoyi.ss.order.domain.OrderBO;
import com.ruoyi.ss.order.domain.OrderVO;
import com.ruoyi.ss.payBill.domain.PayBill;
import com.ruoyi.ss.payBill.domain.enums.PayBillStatus;
import com.ruoyi.ss.payBill.service.PayBillConverter;
import com.ruoyi.ss.user.domain.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

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

    private static final Logger log = LoggerFactory.getLogger(PayBillConverterImpl.class);

    @Override
    public PayBill toPo(OrderBO bo) {
        if (bo == null) {
            return null;
        }

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

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

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

        PayBill po = new PayBill();
        String outTradeNo = null;
        if(PayChannel.CT_WX.equalsCode(channel.getCode())){
            outTradeNo = IdUtils.getOrderNo("wx");
        }else if(PayChannel.TM_WX.equalsCode(channel.getCode())){
            outTradeNo = IdUtils.getOrderNo("tmwx");
        }else{
            throw new ServiceException("非法的支付渠道");
        }
        po.setPayNo(outTradeNo);
        po.setOrderNo(order.getOrderNo());
        po.setAmount(order.getPayFee());
        po.setChannelId(channel.getChannelId());
        po.setStatus(PayBillStatus.WAIT_PAY.getStatus());//前缀

        String prefixStr;// 1-茶室订单；2-棋牌订单；3-台球订单；4-其他
        if(bo.getType().equals(ServiceConstants.ORDER_TYPE_TEA)){
            prefixStr = "茶室订单:";
        }else if(bo.getType().equals(ServiceConstants.ORDER_TYPE_MAHJONG)){
            prefixStr = "棋牌订单:";
        }else if(bo.getType().equals(ServiceConstants.ORDER_TYPE_BILLIARDS)) {
            prefixStr = "台球订单:";
        }else{
            prefixStr = "其他订单:";
        }
        po.setDescription(prefixStr + order.getOrderNo());
        po.setChannelCost(this.calcChannelCost(channel, order.getPayFee()));

        // 支付人
        po.setAccount(user.getWxopenid());
        log.info("转换后的支付订单对象：{}", JSON.toJSONString(po));
        return po;
    }
    /**
     * 计算渠道成本
     */
    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 (TransactionBillPayType.wxList().contains(channel.getChannelId())) {
//            po.setAccount(user.getWxOpenId());
//        }
//
//        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 (TransactionBillPayType.wxList().contains(channel.getChannelId())) {
//            po.setAccount(user.getWxOpenId());
//        }
//
//        return po;
//    }
}
