package com.ym.mec.thirdparty.adapay;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ym.mec.thirdparty.exception.ThirdpartyException;

import org.springframework.util.DigestUtils;

public class Payment {
    /**
     * 执行一个支付交易
     *
     * @return 创建的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> executePayment(Map<String, Object> paymentParams) throws Exception {
        paymentParams.put("app_id", ConfigInit.appId);

        //调用sdk方法，创建支付，得到支付对象
        Map<String, Object> payment = new HashMap<>();
        payment = com.huifu.adapay.model.Payment.create(paymentParams);

        String errorCode = (String) payment.get("error_code");
        if (null != errorCode) {
            String errorMsg = (String) payment.get("error_msg");
            throw new ThirdpartyException(errorMsg);
        }

        return payment;
    }


    /**
     * 创建确认对象
     *
     * @return 查询的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> createConfirm(Map<String, Object> confirm) throws Exception {
        confirm = com.huifu.adapay.model.Payment.createConfirm(confirm);

        String error_code = (String) confirm.get("error_code");
        if (null != error_code) {
            String errorMsg = (String) confirm.get("error_msg");
            throw new ThirdpartyException(errorMsg);
        }
        return confirm;
    }

    /**
     * 关闭一个支付交易
     *
     * @param paymentId 要关闭的支付id
     * @return 关闭的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> closePayment(String paymentId) throws Exception {
        Map<String, Object> payment = new HashMap<>();
        Map<String, Object> paymentParams = new HashMap<>(10);
        paymentParams.put("payment_id", paymentId);
        paymentParams.put("reason", "reason");
        paymentParams.put("expend", "expend");
        paymentParams.put("notify_url", "notify_url");
        payment = com.huifu.adapay.model.Payment.close(paymentParams);

        String error_code = (String) payment.get("error_code");
        if (null != error_code) {
            String errorMsg = (String) payment.get("error_msg");
            throw new ThirdpartyException(errorMsg);
        }
        return payment;
    }

    /**
     * 查询一个支付交易
     *
     * @param paymentId 要查询的支付id
     * @return 查询的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> queryPayment(String paymentId) throws Exception {
        System.out.println("=======query payment begin=======");
        //查询支付对象的参数，全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id7
        //调用sdk方法，查询支付交易，得到支付对象
        Map<String, Object> payment = new HashMap<>();
        try {
            System.out.println("支付查询请求参数：" + JSON.toJSONString(paymentId));
            payment = com.huifu.adapay.model.Payment.query(paymentId);
        } catch (ThirdpartyException e) {
            e.printStackTrace();
        }

        System.out.println("支付查询返回参数：" + JSON.toJSONString(payment));

        String error_code = (String) payment.get("error_code");
        if (null != error_code) {
            String error_msg = (String) payment.get("error_msg");
            System.out.println("error_code:" + error_code + "............." + error_msg);
        }
        System.out.println("=======query payment end=======");
        return payment;
    }


    public static Map<String, Object> queryList(Integer pageIndex, Long createdGte, Long createdLte) throws Exception {
        Map<java.lang.String, java.lang.Object> paymentParams = new HashMap<>();
        paymentParams.put("app_id", ConfigInit.appId);
        paymentParams.put("page_index", pageIndex);
        paymentParams.put("page_size", "20");
        paymentParams.put("created_gte", createdGte);
        paymentParams.put("created_lte", createdLte);
        Map<String, Object> payment = com.huifu.adapay.model.Payment.queryList(paymentParams);
        String error_code = (String) payment.get("error_code");
        if (null != error_code) {
            String errorMsg = (String) payment.get("error_msg");
            throw new ThirdpartyException(errorMsg);
        }
        return payment;
    }

    public static Map<String, Object> queryConfirmList(String paymentId) throws Exception {
        Map<String, Object> paymentParams = new HashMap<>();
        paymentParams.put("app_id", ConfigInit.appId);
        paymentParams.put("payment_id", paymentId);

        Map<String, Object> payment = com.huifu.adapay.model.Payment.queryConfirmList(paymentParams);
        String error_code = (String) payment.get("error_code");
        if (null != error_code) {
            String errorMsg = (String) payment.get("error_msg");
            throw new ThirdpartyException(errorMsg);
        }
        return payment;
    }


    public static Map<String, Object> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sellerNo, List<Map<String, Object>> tempRoutingList) throws Exception {
        Map<String, Object> paymentParams = new LinkedHashMap<>();
        paymentParams.put("appId", ConfigInit.appId);
        paymentParams.put("amount", amount.setScale(2, BigDecimal.ROUND_HALF_UP));
        paymentParams.put("orderNo", orderNo);
//        paymentParams.put("notifyUrl", notifyUrl);
//        paymentParams.put("returnUrl", returnUrl);
        paymentParams.put("orderSubject", orderSubject);
        paymentParams.put("orderBody", orderBody);
        paymentParams.put("wxAppId", ConfigInit.wxAppId);

        String originalStr = JSONObject.toJSONString(paymentParams);
        String sign = DigestUtils.md5DigestAsHex(originalStr.getBytes());

        paymentParams.put("sign", sign);
        paymentParams.remove("appId");
        return paymentParams;
    }
}
