package com.hai.common.pay;

import com.hai.common.pay.entity.WeChatPayReqInfo;
import com.hai.common.pay.entity.WechatCallBackInfo;
import com.hai.common.pay.util.HttpReqUtil;
import com.hai.common.pay.util.SignatureUtil;
import com.hai.common.pay.util.XmlUtil;
import com.hai.entity.HighPayRecord;
import com.hai.service.pay.PayRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Component
public class WechatPayUtil {

    private static Logger log = LoggerFactory.getLogger(WechatPayUtil.class);
    @Resource
    private PayRecordService payRecordService;

    /**
     * @throws
     * @Title: goAlipay
     * @Description: 微信支付请求体
     * @author: 魏真峰
     * @param: [orderId]
     * @return: java.lang.String
     */
    @RequestMapping("/goWechatPay")
    @ResponseBody
    public SortedMap<Object,Object> goWechatPay(WeChatPayReqInfo weChatPayReqInfo, Map<String,String> map) throws Exception{
        log.info("微信支付 -> 组装支付参数：开始");

        String sign = SignatureUtil.createSign(weChatPayReqInfo, map.get("api_key"), "UTF-8");
        weChatPayReqInfo.setSign(sign);
        String unifiedXmL = XmlUtil.toSplitXml(weChatPayReqInfo);

        String unifiedOrderResultXmL = HttpReqUtil.HttpsDefaultExecute("POST", map.get("unified_order_url"), null, unifiedXmL, null);
        // 签名校验
        SortedMap<Object,Object> sortedMap = null;
        if (SignatureUtil.checkIsSignValidFromWeiXin(unifiedOrderResultXmL, map.get("api_key"), "UTF-8")) {
            // 组装支付参数
            WechatCallBackInfo wechatCallBackInfo = XmlUtil.getObjectFromXML(unifiedOrderResultXmL, WechatCallBackInfo.class);
            Long timeStamp =  System.currentTimeMillis()/1000;
            sortedMap = new TreeMap<>();
            sortedMap.put("appId",map.get("app_id"));
//            sortedMap.put("partnerid",SysConst.getSysConfig().getMch_id());
//            sortedMap.put("prepayid",wechatCallBackInfo.getPrepay_id());
            sortedMap.put("nonceStr",wechatCallBackInfo.getNonce_str());
            sortedMap.put("timeStamp",timeStamp.toString());
            sortedMap.put("signType","MD5");
            sortedMap.put("package", "prepay_id=" + wechatCallBackInfo.getPrepay_id());
            String secondSign = SignatureUtil.createSign(sortedMap, map.get("api_key"), "UTF-8");
            sortedMap.put("sign",secondSign);

            // 将支付请求存入支付纪录
            HighPayRecord payRecord = new HighPayRecord();
            payRecord.setBodyInfo(unifiedXmL);
            payRecord.setResType(1);
            payRecord.setPayType(2);
            payRecord.setPayMoney(new BigDecimal(weChatPayReqInfo.getTotal_fee()/100D));
            payRecordService.addPayRecord(payRecord);

            log.info("微信支付 -> 组装支付参数：完成");
        } else {
            log.error("微信支付 -> 组装支付参数：支付信息错误");
            log.error("错误信息：" + unifiedOrderResultXmL);
        }

        return sortedMap;
    }

}
