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

import com.ruoyi.bst.app.domain.AppVO;
import com.ruoyi.bst.channel.domain.ChannelVO;
import com.ruoyi.bst.order.domain.Order;
import com.ruoyi.bst.order.domain.bo.OrderCreateBO;
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.userApp.domain.UserAppVO;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PayConverterImpl implements PayConverter {

    @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.setStatus(PayStatus.WAIT_PAY.getStatus());
        pay.setExpireTime(LocalDateTime.now().plusMinutes(10));
        pay.setRemark("充值订单:"+order.getOrderNo());

        // 订单信息
        pay.setBstType(PayBstType.ORDER.getType());
        pay.setBstId(order.getId());
        pay.setAmount(order.getPayAmount());

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

        // 店铺信息
        pay.setStoreId(order.getStoreId());

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

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

        // 渠道成本（放最后）
        pay.setChannelCost(MathUtils.mulDecimal(channel.getCostRate(), pay.getAmount()));
        return pay;
    }

}
