package com.keao.edu.user.service.impl;

import com.keao.edu.common.exception.BizException;
import com.keao.edu.thirdparty.adapay.Payment;
import com.keao.edu.thirdparty.yqpay.YqPayUtil;
import com.keao.edu.user.dao.SysConfigDao;
import com.keao.edu.user.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 支付服务类
 */
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private SysConfigDao sysConfigDao;

    /**
     * 返回订单提交数据
     *
     * @param amount       金额
     * @param orderNo      订单编号
     * @param notifyUrl    异步通知地址
     * @param returnUrl    支付返回地址
     * @param orderSubject 订单标题
     * @param orderBody    订单内容
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody) throws Exception {

        //支付通道决策
        Map<String, Object> unionPay = new HashMap<>();
        Map<String, Object> payMap = null;
        String type = null;
        String routingMerNos = null;
        String paymentChannel = sysConfigDao.findConfigValue("payment_channel");
        if (paymentChannel.equals("YQPAY")) {
            payMap = YqPayUtil.getPayMap(amount, orderNo, notifyUrl, returnUrl, orderSubject, orderBody);
            type = "YQPAY";
            routingMerNos = (String) payMap.get("merNo");
        } else if (paymentChannel.equals("ADAPAY")) {
            payMap = Payment.getPayMap(amount, orderNo, notifyUrl, returnUrl, orderSubject, orderBody);
            type = "ADAPAY";
            routingMerNos = Payment.merNo;
        } else {
            throw new BizException("收款渠道配置错误");
        }

        unionPay.put("orderNo", orderNo);
        unionPay.put("type", type);
        unionPay.put("payMap", payMap);
        unionPay.put("routingMerNos", routingMerNos);
        return unionPay;
    }

}
