package com.ruoyi.pay.utils;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.exception.enums.CoreExceptionEnum;
import com.ruoyi.enums.WalletDetailEnums;
import com.ruoyi.frequency.givewallerecord.service.GiveWalletRecordService;
import com.ruoyi.frequency.wallerecord.service.WalletRecordService;
import com.ruoyi.pay.config.Notifyurl;
import com.ruoyi.pay.enums.PayEnums;
import com.ruoyi.pay.enums.PaySourceEnums;
import com.ruoyi.pay.type.ali.service.AliPayService;
import com.ruoyi.pay.type.weixin.service.WXPayService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.utils.lakala.LakalaUtil;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liwenlong
 * @date 2023/2/11 13:50
 */

@Component
public class PayUtil {


    @Value("${basepath.url}")
    private String basePath;

    //    @Value("${alipay.subject}")
    private String subject;

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private WXPayService wxPayService;

    @Autowired
    private WalletRecordService walletRecordService;

    @Autowired
    private BusinessUtil businessUtil;

    @Autowired
    private Notifyurl notifyurl;

    @Autowired
    private LakalaUtil lakalaUtil;

    /**
     * 支付
     *
     * @param payType    支付类型
     * @param payPrice   支付金额
     * @param givePrice  赠送支付金额
     * @param payOrderNo 三方支付单号
     * @return
     */
    public ResponseData pay(UserVo userVo, Long objectId, Integer payType, BigDecimal payPrice, BigDecimal givePrice, String payOrderNo, PaySourceEnums enums, WalletDetailEnums walletDetailEnums) {


        //支付宝回调地址
        String aliReturnUrl = notifyurl.getReturnUrl(enums, 1);

        //微信回调地址
        String wxReturnUrl = notifyurl.getReturnUrl(enums, 2);

        //拉卡拉回调地址
        String lklReturnUrl = notifyurl.getReturnUrl(enums, 3);


        ResponseData responseData = ResponseData.success();

        //判断支付金额为0时
        if (payType != -1 && payPrice.compareTo(BigDecimal.ZERO) == 0) {
            return responseData;
        }

        switch (PayEnums.getPayEnums(payType)) {
            case app_alipay:
                //app支付宝
                responseData = aliPayService.aliPayUserPay(payOrderNo, payPrice,
                        basePath + aliReturnUrl, objectId);
                break;
            case h5_alipay:
                //h5支付宝
                responseData = aliPayService.aliH5Pay(payOrderNo, payPrice,
                        basePath + aliReturnUrl, objectId);
                break;
            case app_weChat:
                //app微信
                responseData = wxPayService.wxPayUserPay(payOrderNo, payPrice,
                        basePath + wxReturnUrl, objectId);
                break;
            case h5_WeChat:
                //h5微信
                responseData = wxPayService.wxH5Pay(payOrderNo, payPrice,
                        basePath + wxReturnUrl, objectId);
                break;

            case applet:
                //获取openId
                String openId = (String) businessUtil.getUserFieldValue(userVo, "openId");

                //微信小程序
                responseData = wxPayService.wxAppletPay(payOrderNo, payPrice, openId,
                        basePath + wxReturnUrl, objectId);

                break;
            case apple:
                Map<String, Object> map = new HashMap<>();
                map.put("orderId", objectId);
                responseData.setData(map);
                break;
            case balance:
                walletRecordService.updateBalance(userVo, walletDetailEnums, objectId, payPrice.negate(), PublicCommon.Wallet.可提现);
                map = new HashMap<>();
                map.put("orderId", objectId);
                responseData.setData(map);

                //是否使用抵用金支付
                if (ObjectUtil.isNotEmpty(givePrice) && givePrice.compareTo(BigDecimal.ZERO) > 0) {
                    //抵用金
                    giveWalletRecordService.updateGiveBalance(userVo, WalletDetailEnums.GIVE_ORDER_PAY, objectId, givePrice.negate(), PublicCommon.Wallet.可提现);
                }

                break;
            case lakala_ccss:
                responseData = lakalaUtil.ccssCounterOrderTospecialCreate(payOrderNo, payPrice, objectId, basePath + lklReturnUrl);
                break;
            default:
                responseData = responseData.error(CoreExceptionEnum.ERROR_PARAM);
        }

        return responseData;

    }

    @Autowired
    private GiveWalletRecordService giveWalletRecordService;

    /**
     * 退款
     *
     * @param payType        支付类型
     * @param payTotalPrice  支付总金额
     * @param returnPrice    退款金额
     * @param payOrderNo     支付订单号
     * @param aliReturnPayId
     * @return
     */
    public ResponseData refund(UserVo userVo, Long objectId, Integer payType, BigDecimal payTotalPrice, BigDecimal returnPrice, String payOrderNo, String aliReturnPayId, BigDecimal givePrice) {


        ResponseData responseData = new ResponseData();

        //判断支付金额为0时
        if (payType != -1 && returnPrice.compareTo(BigDecimal.ZERO) == 0) {
            return responseData.error(CoreExceptionEnum.PRICE_ZERO);
        }

        switch (PayEnums.getPayEnums(payType)) {
            case app_alipay:
                responseData = aliPayService.alipayRefund(payOrderNo, aliReturnPayId, returnPrice);
                break;
            case h5_alipay:
                //h5支付宝
                responseData = aliPayService.alipayRefund(payOrderNo, aliReturnPayId, returnPrice);
                break;
            case app_weChat:
                //app微信
                responseData = wxPayService.wxRefund(payOrderNo, payTotalPrice, returnPrice);
                break;
            case h5_WeChat:
                responseData = wxPayService.wxRefund(payOrderNo, payTotalPrice, returnPrice);
                break;
            case applet:
                responseData = wxPayService.wxRefund(payOrderNo, payTotalPrice, returnPrice);
                break;
            case apple:
                break;
            case balance:
                walletRecordService.updateBalance(userVo, WalletDetailEnums.ORDER_RETURN, objectId, returnPrice, PublicCommon.Wallet.可提现);
                break;
            case lakala_ccss:
                walletRecordService.updateBalance(userVo, WalletDetailEnums.ORDER_RETURN, objectId, returnPrice, PublicCommon.Wallet.可提现);
                break;
            default:
                responseData = responseData.error(CoreExceptionEnum.ERROR_PARAM);
                ;

        }

        //退还抵用金
        if (ObjectUtil.isNotEmpty(givePrice) && givePrice.compareTo(BigDecimal.ZERO) > 0) {
            giveWalletRecordService.updateGiveBalance(userVo, WalletDetailEnums.GIVE_ORDER_RETURN, objectId, givePrice, PublicCommon.Wallet.可提现);
        }

        return responseData;

    }

}
