package com.hfkj.haioil;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.SignatureUtil;
import com.hfkj.config.CommonSysConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

public class HaiOilService {

    private static Logger log = LoggerFactory.getLogger(HaiOilService.class);


    /**
     * @MethodName pushPk
     * @Description: 推送优惠券包
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/9/9 下午2:57
     */
    public static JSONObject pushPk(Map<String , Object> map) throws Exception {

        JSONObject object = request("/openapi/discount/pushPk" , map);

        if (Objects.equals(object.getString("return_code"), "000000")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败！");
        }

    }

    /**
     * 请求
     * @param postUrl  接口请求地址
     * @param param       参数
     * @return
     * @throws Exception
     */
    public static JSONObject request(String postUrl, Map<String,Object> param) throws Exception {

        log.info("============ 嗨加油请求-START =============");
        param.put("appId", CommonSysConst.getSysConfig().getHaiOilAppid());
        param.put("reqId", new Date().getTime());
        param.put("sign", SignatureUtil.createSign(param , CommonSysConst.getSysConfig().getHaiOilAppSecret()));

        log.info("请求接口：" + postUrl);
        log.info("请求参数：" + JSONObject.toJSONString(param));

        JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getHaiOilPostUrl()+ postUrl, JSONObject.toJSONString(param));

        log.info("响应参数：" + response.toJSONString());
        log.info("============ 嗨加油请求-END ==============");
        return response;

    }

}
