package com.ruoyi.bst.pay.service.impl;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;

import com.ruoyi.bst.app.domain.AppVO;
import com.ruoyi.bst.channel.domain.ChannelVO;
import com.ruoyi.bst.channel.domain.enums.ChannelApiType;
import com.ruoyi.bst.order.domain.Order;
import com.ruoyi.bst.order.domain.bo.OrderCreateBO;
import com.ruoyi.bst.order.domain.bo.OrderPayRideFeeBO;
import com.ruoyi.bst.order.domain.vo.OrderCalcRideFeeVO;
import com.ruoyi.bst.pay.domain.Pay;
import com.ruoyi.bst.pay.domain.enums.PayBstType;
import com.ruoyi.bst.pay.domain.enums.PayStatus;
import com.ruoyi.bst.pay.service.PayConverter;
import com.ruoyi.bst.pay.utils.PayUtil;
import com.ruoyi.bst.userApp.domain.UserAppVO;
import com.ruoyi.bst.vipOrder.domain.VipOrder;
import com.ruoyi.bst.vipOrder.domain.bo.VipOrderCreateBO;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.ip.IpUtils;

@Service
public class PayConverterImpl implements PayConverter {

    /**
     * 设置基础信息
     * @param pay 支付单
     * @param app 应用
     * @param channel 渠道
     * @param userApp 用户应用
     */
    private void setBaseInfo(Pay pay, AppVO app, ChannelVO channel) {
        // 基础信息
        pay.setStatus(PayStatus.WAIT_PAY.getStatus());
        pay.setExpireTime(LocalDateTime.now().plusMinutes(10));

        // 用户信息
        pay.setIp(IpUtils.getIpAddr());

        // 应用信息
        pay.setAppId(app.getId());
        pay.setAppName(app.getName());

        // 渠道信息
        pay.setChannelId(channel.getChannelId());
        pay.setChannelName(channel.getName());

        // 渠道成本
        pay.setChannelCost(PayUtil.calcChannelCost(pay.getAmount(), channel.getCostRate()));
    }

    @Override
    public Pay toPayPO(OrderCreateBO bo) {
        if (bo == null) {
            return null;
        }
        Order order = bo.getOrder();
        UserVO user = bo.getUser();
        ChannelVO channel = bo.getChannel();
        UserAppVO userApp = bo.getUserApp();
        AppVO app = bo.getApp();
        if (order == null || user == null || channel == null || userApp == null || app == null) {
            return null;
        }

        Pay pay = new Pay();

        // 订单信息
        pay.setBstType(PayBstType.ORDER.getType());
        pay.setBstId(order.getId());
        pay.setAmount(order.getPayAmount());
        pay.setAreaId(order.getAreaId());
        pay.setDescription("骑行押金:" + order.getNo());

        // 设置基础信息
        this.setBaseInfo(pay, app, channel);

        // 设置支付账户
        if (ChannelApiType.CREDIT.getType().equals(channel.getApiType())) {
            if (order.getCreditUserId() != null) {
                pay.setAccount(String.valueOf(order.getCreditUserId()));
            }
        } else {
            pay.setAccount(userApp.getOpenId());
        }

        return pay;
    }

    @Override
    public Pay toPayPO(OrderPayRideFeeBO bo) {
        if (bo == null) {
            return null;
        }
        Order order = bo.getOrder();
        UserVO user = bo.getUser();
        ChannelVO channel = bo.getChannel();
        UserAppVO userApp = bo.getUserApp();
        AppVO app = bo.getApp();
        OrderCalcRideFeeVO fee = bo.getFee();
        if (order == null || user == null || channel == null || userApp == null || app == null || fee == null) {
            return null;
        }

        Pay pay = new Pay();

        // 订单信息
        pay.setBstType(PayBstType.ORDER_RIDE.getType());
        pay.setBstId(order.getId());
        pay.setAmount(fee.getPayAmount());
        pay.setAreaId(order.getAreaId());
        pay.setDescription("骑行费:" + order.getNo());

        // 设置基础信息
        this.setBaseInfo(pay, app, channel);

        // 设置支付账户
        if (ChannelApiType.CREDIT.getType().equals(channel.getApiType())) {
            if (order.getCreditUserId() != null) {
                pay.setAccount(String.valueOf(order.getCreditUserId()));
            }
        } else {
            pay.setAccount(userApp.getOpenId());
        }

        return pay;
    }

    @Override
    public Pay toPayPO(VipOrderCreateBO bo) {
        if (bo == null) {
            return null;
        }

        VipOrder order = bo.getOrder();
        UserVO user = bo.getUser();
        ChannelVO channel = bo.getChannel();
        UserAppVO userApp = bo.getUserApp();
        AppVO app = bo.getApp();
        if (order == null || user == null || channel == null || userApp == null || app == null) {
            return null;
        }

        Pay pay = new Pay();

        // 订单信息
        pay.setBstType(PayBstType.VIP_ORDER.getType());
        pay.setBstId(order.getId());
        pay.setAmount(order.getVipPrice());
        pay.setAreaId(order.getVipAreaId());
        pay.setDescription("卡券:" + order.getNo());

        // 设置基础信息
        this.setBaseInfo(pay, app, channel);

        // 设置支付账户
        pay.setAccount(userApp.getOpenId());

        return pay;
    }
}
