package com.pj.api.jh.service;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.pj.api.jh.MerchantApiUtil;
import com.pj.api.wx.WxUtils;
import com.pj.api.wx.bo.Attach;
import com.pj.api.wx.service.WxService;
import com.pj.current.config.JhConfig;
import com.pj.current.config.MyConfig;
import com.pj.current.config.WxConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Auther: lzm
 * @Date: 2022/04/19/17:38
 */

@Service
@Transactional
@Slf4j
public class JhService {

    @Resource
    WxConfig wxConfig;
    @Resource
    MyConfig myConfig;
    @Resource
    JhConfig jhConfig;
    @Resource
    WxUtils wxUtils;

    public Map<String, String> initPay(HttpServletRequest request) throws Exception {
        String money = request.getParameter("money");
        String openid = request.getParameter("openid");
        String businessId = request.getParameter("b");
        String c = request.getParameter("c");
        String a = request.getParameter("a");
        Attach atchMap = new Attach();
        atchMap.setC(c).setB(businessId).setA(a);
        String out_trade_no = RandomUtil.randomString(30);
        Map<String, String> params = new HashMap<>();
        params.put("attach", JSONUtil.toJsonStr(atchMap));
        params.put("productType", jhConfig.getProductType());
        params.put("businessMerchantNo", jhConfig.getBusinessMerchantNo());
        params.put("openId", openid);
        params.put("merchantNo", jhConfig.getBusinessMerchantNo());
        params.put("tradeType", jhConfig.getTradeType());
        params.put("subTradeType", "");
        String total_free = NumberUtil.mul(money, 100 + "").intValue() + "";
        log.info("order price:{}", total_free);
        params.put("orderPrice", total_free);
        params.put("outTradeNo", out_trade_no);
        params.put("productName", "业务订单支付");
        params.put("orderIp", WxService.getIpAddress(request));
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        params.put("orderTime", now);
        params.put("returnUrl", "");
        params.put("notifyUrl", myConfig.getDomain() + "/wx/notify");
        params.put("remark", "业务订单支付");
        String paySecrit = jhConfig.getPaySecret();
        String sign = MerchantApiUtil.getSign(params, paySecrit);
        params.put("sign", sign);

        String jsonParam = JSONUtil.toJsonStr(params);
        String initPayUrl = "http://116.252.74.15:8081/uaps-web-gateway/cnpPay/initPay";
        String result = HttpUtil.createPost(initPayUrl).header("Content-Type", "application/json").body(jsonParam)
                .execute().body();
        JSONObject jsonResult = JSONUtil.parseObj(result);
        if(StrUtil.equals("0000", jsonResult.get("resultCode").toString())){
            JSONObject payMessage = JSONUtil.parseObj(jsonResult.get("payMessage"));
            Object prePayId = payMessage.get("prepay_id");
            String preId = prePayId == null ? "" : prePayId.toString();
            return getPayParams(openid, preId);
        }
        throw new Exception("支付信息有误");
    }

    public Map<String, String> getPayParams(String openid, String prePayId) {
        Map<String, String> signMap = new HashMap<>(10);
        signMap.put("appId", wxConfig.getAppId());
        signMap.put("timeStamp", System.currentTimeMillis() / 1000 + "");
        signMap.put("nonceStr", RandomUtil.randomString(32));
        signMap.put("signType", "MD5");
        signMap.put("package", "prepay_id=" + prePayId);
        String sign = wxUtils.sign(signMap, wxConfig.getKey());
        signMap.put("paySign", sign);
        signMap.put("openId", openid);
        return signMap;
    }




}
