package com.hfkj.qianzhu;

import com.alibaba.fastjson.JSON;
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.MD5Util;
import com.hfkj.common.utils.WxUtils;
import com.hfkj.config.CommonSysConst;
import com.hfkj.config.GenerateSign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class QianZhuService {

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

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

        log.info("============ 千猪请求-START =============");
        param.put("platformId", CommonSysConst.getSysConfig().getQianZhuPlatformId());
        param.put("timestamp", new Date().getTime());
        param.put("sign", MD5Util.encode(GenerateSign.paramSort(param,CommonSysConst.getSysConfig().getQianZhuSecret()).getBytes()).toLowerCase());

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

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

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

    }

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

        log.info("============ 千猪请求-START =============");
        Map<String,Object> param = new HashMap<>();
        param.put("platformId", CommonSysConst.getSysConfig().getQianZhuPlatformId());
        param.put("timestamp", new Date().getTime());
        param.put("action", postUrl);
        param.put("content", JSON.toJSONString(map));
        param.put("version", "1.0");
        param.put("traceId", WxUtils.makeNonStr());
        param.put("sign", MD5Util.encode(GenerateSign.paramSort(param,CommonSysConst.getSysConfig().getQianZhuSecret()).getBytes()).toLowerCase());

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

        JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getQianZhuH5url(), JSONObject.toJSONString(param));

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

    }

    public static JSONObject requestToken(String postUrl, Map<String,Object> map) throws Exception {

        log.info("============ 千猪请求-START =============");
        Map<String,Object> param = new HashMap<>();
        param.put("platformId", CommonSysConst.getSysConfig().getQianZhuPlatformId());
        param.put("timestamp", new Date().getTime());
        param.put("action", postUrl);
        param.put("content", JSON.toJSONString(map));
        param.put("version", "1.0");
        param.put("traceId", WxUtils.makeNonStr());
        param.put("sign", MD5Util.encode(GenerateSign.paramSort(param,CommonSysConst.getSysConfig().getQianZhuSecret()).getBytes()).toLowerCase());

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

        JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrlToken(), JSONObject.toJSONString(param));

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

    }

    /**
     * @Author Sum1Dream
     * @name getAllCities.java
     * @Description // 查询所有城市
     * @Date 15:45 2022/6/28
     * @Param []
     * @return com.alibaba.fastjson.JSONObject
     */
    public static JSONObject getAllCities() throws Exception {


        JSONObject object = QianZhuService.request("/openApi/v2/cities/listAll" , new HashMap<>());

        if (object.getBoolean("success") && object.getInteger("code") == 10000) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }
    }

    /**
     * @MethodName listByCityId
     * @Description:查询城市区域
     * @param
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/6/11 下午3:12
     */
    public static JSONObject listByCityId(Map<String,Object> param) throws Exception {
        JSONObject object = QianZhuService.request("/openApi/v1/regions/listByCityId" , param);

        if (object.getBoolean("success") && object.getInteger("code") == 10000) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }
    }


}
