package com.fzm.mall.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.pay.config.WeChatPayConfig;
import com.fzm.mall.pay.entity.dto.WxRefundDTO;
import com.fzm.mall.pay.entity.dto.WxUnifiedOrderDTO;
import com.fzm.mall.pay.service.IWxNativePayService;
import com.fzm.mall.pay.util.WxPayHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author wangp
 * @date 2021/4/14 16:32
 * @description 微信Native支付
 * @since JDK 1.8
 */
@Slf4j
@Service
public class WxNativePayServiceImpl implements IWxNativePayService {

    @Autowired
    private WeChatPayConfig config;

    @Override
    public String CreateOrder(WxUnifiedOrderDTO dto) {
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native");

        // 请求body参数
        JSONObject reqdata = new JSONObject();
        reqdata.put("appid", config.getAppId());
        reqdata.put("mchid", config.getMchId());
        reqdata.put("description", dto.getDescription());
        reqdata.put("out_trade_no", dto.getOutTradeNo());
        reqdata.put("notify_url", config.getNotifyUrl());
        reqdata.put("amount", new JSONObject().put("total", dto.getTotal()));

        StringEntity entity = null;
        try {
            entity = new StringEntity(reqdata.toJSONString());
            entity.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            log.info(e.toString());
        }
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");

        //完成签名并执行请求
        return WxPayHttpUtil.weChatPayHttpRequest(httpPost);
    }

    @Override
    public String QueryOrderByTransactionId(String transactionId) {
        URI build = null;

        //请求URL
        try {
            URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/pay/transactions/id/" + transactionId);
            uriBuilder.setParameter("mchid", config.getMchId());
            build = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        //完成签名并执行请求
        HttpGet httpGet = new HttpGet(build);
        httpGet.addHeader("Accept", "application/json");

        return WxPayHttpUtil.weChatPayHttpRequest(httpGet);
    }

    public String QueryOrderByOutTradeNo(String outTradeNo) {
        URI build = null;
        //请求URL

        try {
            URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo);
            uriBuilder.setParameter("mchid", config.getMchId());
            build = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        //完成签名并执行请求
        HttpGet httpGet = new HttpGet(build);
        httpGet.addHeader("Accept", "application/json");

        return WxPayHttpUtil.weChatPayHttpRequest(httpGet);
    }

    public String CloseOrder(String outTradeNo) {
        //请求URL
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo + "/close");
        //请求body参数
        String reqdata = "{\"mchid\": \"" + config.getMchId() + "\"}";

        StringEntity entity = null;
        try {
            entity = new StringEntity(reqdata);
            entity.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");

        //完成签名并执行请求
        return WxPayHttpUtil.weChatPayHttpRequest(httpPost);
    }

    public String ApplyRefund(WxRefundDTO dto) {
        //请求URL
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
        // 请求body参数
        JSONObject reqdata = new JSONObject();
        reqdata.put("transaction_id", dto.getTransactionId());
        reqdata.put("out_trade_no", dto.getOutTradeNo());
        reqdata.put("out_refund_no", dto.getOutRefundNo());

        JSONObject amountJson = new JSONObject();
        amountJson.put("refund", dto.getRefund());
        amountJson.put("total", dto.getTotal());
        amountJson.put("currency", "CNY");

        reqdata.put("amount", amountJson);

        StringEntity entity = null;
        try {
            entity = new StringEntity(reqdata.toJSONString());
            entity.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");

        //完成签名并执行请求
        return WxPayHttpUtil.weChatPayHttpRequest(httpPost);
    }

    public String QueryRefund(String outRefundNo) {
        URI build = null;
        //请求URL

        try {
            URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/" + outRefundNo);
            uriBuilder.setParameter("mchid", config.getMchId());
            build = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        //完成签名并执行请求
        HttpGet httpGet = new HttpGet(build);
        httpGet.addHeader("Accept", "application/json");

        return WxPayHttpUtil.weChatPayHttpRequest(httpGet);
    }

    public String applyTradeBill(String billDate) {
        URI build = null;
        //请求URL

        try {
            URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/bill/tradebill");
            uriBuilder.setParameter("bill_date", billDate);
            build = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        //完成签名并执行请求
        HttpGet httpGet = new HttpGet(build);
        httpGet.addHeader("Accept", "application/json");

        return WxPayHttpUtil.weChatPayHttpRequest(httpGet);
    }

    public String applyFundFlowBill(String billDate) {
        URI build = null;
        //请求URL

        try {
            URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/bill/fundflowbill");
            uriBuilder.setParameter("bill_date", billDate);
            build = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        //完成签名并执行请求
        HttpGet httpGet = new HttpGet(build);
        httpGet.addHeader("Accept", "application/json");

        return WxPayHttpUtil.weChatPayHttpRequest(httpGet);
    }
}
