package com.ruoyi.web.controller.qichacha;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: tjf
 * @Date: 2024/4/17 10:24
 * @Describe:
 */
public class QiChaCha extends BaseController {
    /**
     * 企查查
     */
    private static final String KEY = "e50f4b2011ea41a3ad489f0476160041";
    private static final String SECRET_KEY = "BD8D23FCD36CDEE7E0B25DAB28588163";
    /**
     * 通过企查查,查询企业信息
     *
     * @param searchKey
     * @return
     */
    @GetMapping("/qichachaSearch/{searchKey}")
    public AjaxResult qichachaSearch(@PathVariable("searchKey") String searchKey) {
        if (StringUtils.isNotBlank(searchKey)){
            //Headers头信息
            Map<String,String> map = new HashMap<>();
            StringBuilder sb = new StringBuilder();
            String unix = DateUtils.getUnix();
            map.put("Timespan", unix);
            sb.append(KEY).append(unix).append(SECRET_KEY);
            map.put("Token", Md5Utils.hash(sb.toString()).toUpperCase());
            StringBuilder sbd = new StringBuilder();
            sbd.append("key=").append(KEY).append("&").append("searchKey=").append(searchKey);
            String s = HttpUtils.sendGetMap("https://api.qichacha.com/FuzzySearch/GetList",sbd.toString(),map);
            JSONObject result = JSONObject.parseObject(s);
            JSONArray resultOut = result.getJSONArray("Result");
            if (resultOut != null){
                return AjaxResult.success(resultOut);
            }
        }
        return AjaxResult.success(new ArrayList<>());
    }
}
