package com.hfkj.channel.gas.newlink2;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.config.CommonSysConst;
import com.hfkj.entity.BsOrder;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 【团游】能链渠道服务请求
 * @author hurui
 */
public class NewLink2RequestService {
    private static final Logger log = LoggerFactory.getLogger(NewLink2RequestService.class);

    private static final String REQ_URL = CommonSysConst.getSysConfig().getNewLink2ReqUrl();
    private static final String APP_ID = CommonSysConst.getSysConfig().getNewLink2AppId();
    public static final String APP_SECRET = CommonSysConst.getSysConfig().getNewLink2AppSecret();

    /**
     * 获取(全量)油站信息
     *
     * @return 请求结果
     * @throws Exception
     */
    public static JSONObject getAllGasStation() {
        Map<String, Object> headMap = new HashMap<>();
        headMap.put("Content-Encoding", "gzip");
        return request(REQ_URL + "/open-api/getAllGasStation", headMap, new HashMap<>());
    }

    /**
     * 下单前校验
     * @return 请求结果
     * @throws Exception
     */
    public static JSONObject checkOrder(Map<String,Object> param) {
        return request(REQ_URL + "/open-api/checkOrder", null, new HashMap<>());
    }

    /**
     * 创建订单
     * @return 请求结果
     * @throws Exception
     */
    public static JSONObject createOrder(Map<String,Object> param) {
        return request(REQ_URL + "/open-api/createOrder", null, param);
    }

    /**
     * 支付回告
     * @return 请求结果
     * @throws Exception
     */
    public static JSONObject payCallBack(String orderNo, Integer payStatus, BigDecimal thirdPartyDiscountAmount, String uuid) {
        Map<String,Object> param = new HashMap<>();
        param.put("orderNo", orderNo);
        param.put("payStatus", payStatus);
        //param.put("thirdPartyDiscountAmount", thirdPartyDiscountAmount);
        param.put("uuid", uuid);
        return request(REQ_URL + "/open-api/callback/payCallBack", null, param);
    }

    /**
     * 查询油价信息
      * @param gasId  油站id
     * @param fuelAmount 加油金额
     * @param oilNo 油号
     * @param gunNo 抢号
     * @return
     */
    public static JSONObject getPriceInfo(String gasId,BigDecimal fuelAmount,String oilNo,String gunNo) {
        Map<String,Object> param = new HashMap<>();
        param.put("gasId", gasId);
        param.put("fuelAmount", fuelAmount.toString());
        param.put("oilNo", oilNo);
        param.put("gunNo", gunNo);
        return request(REQ_URL + "/open-api/getPriceInfo", null, param);
    }

    /**
     * 申请退款
      * @param orderNo   能链订单号
     * @param refundReason  该字段取值固定为4种：加错站了；不想加了；加油站拒绝加油；其他
     * @param refundRemark  退款备注
     * @param uuid 请求幂等流水号uuid
     * @return
     */
    public static JSONObject refundCallBack(String orderNo,String refundReason,String refundRemark,String uuid) {
        Map<String,Object> param = new HashMap<>();
        param.put("orderNo", orderNo);
        param.put("refundReason", refundReason);
        param.put("refundRemark", refundRemark);
        param.put("uuid", uuid);
        return request(REQ_URL + "/open-api/callback/refundCallBack", null, param);
    }

    /**
     * 请求
     * @return 请求结果
     * @throws Exception
     */
    public static JSONObject request(String url, Map<String,Object> heads, Map<String,Object> param) {
        Map<String,Object> headMap = heads == null ? new LinkedHashMap<>(): heads;
        headMap.put("Content-Type","application/json");
        headMap.put("app-id", APP_ID);
        headMap.put("biz-code", String.valueOf(System.currentTimeMillis()));
        headMap.put("pencil",
                toMd5(MapUtils.getString(headMap,"biz-code"),
                MapUtils.getString(headMap,"app-id"),
                APP_SECRET));
        System.out.println("请求地址：" + url);
        System.out.println("body：" + JSONObject.toJSONString(param));
        return HttpsUtils.doCustomizePost(url, param, headMap);
    }


    /**
     * 根据时间戳和 Appid、secretKey 进行 MD5 签名
     *
     * @param bzCode
     * @param appId
     * @param secretKey
     * @return
     */
    public static String toMd5(String bzCode, String appId, String secretKey) {
        StringBuffer strBuffer = new StringBuffer(bzCode).reverse();
        StringBuffer stringBufferSecret = new StringBuffer(secretKey).reverse();
        strBuffer.append(stringBufferSecret);
        return DigestUtils.md5DigestAsHex(strBuffer.append(appId).toString().getBytes());
    }

}
