package com.hzya.frame.u9c.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.util.HttpUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * com.hzya.frame.u9c.util
 *
 * @author yqh
 * @date 2023-10 -25 15:26
 */

public class CrmUtil {
    protected Logger logger = LogManager.getLogger(super.getClass());
    public static String MANAGERCODE = "FSUID_DB2D1775B48BCDF893538F8CAB2A0EC7";
    public static JSONObject getToken(String permanentCode){
        JSONObject main = new JSONObject();
        main.put("appId","FSAID_131ca0b");
        main.put("appSecret","fdca93109f52427791039af863304887");
        main.put("permanentCode","34EF8A65DE83249BA8FCC4E533E9978E");
        String baseUrl = "http://bohua.tpddns.cn:9081/kangarooDataCenter/entranceController/externalCallInterface";
        String result = HttpRequest.post(baseUrl)
                .header("appId", "800009")//头信息，多个头信息多次调用此方法即可
                .header("apiCode", "8000090001")//头信息，多个头信息多次调用此方法即可
                .header("publicKey", "ZJYAP+Oa64gdYq1AG7vPzDGjP+AZCi3QJUBrJkO4NVwAmaFCe7eDIk+3zDUT+v578prj")//头信息，多个头信息多次调用此方法即可
                .header("secretKey", "svMDheByZfQSwmLEzJbFnvaMsgvCPf3SFNSCb9HJKIdlMDGJlKMwUmYHpZVd3SURj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=")//头信息，多个头信息多次调用此方法即可
                .body(JSON.toJSONString(main))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        if(StrUtil.isNotEmpty(result)){
            return analytic(result);
        }
        return null;
    }
    public static JSONObject  sendU9cTOCrmEsb(String parm,String apiCode){
        String baseUrl = "http://bohua.tpddns.cn:9081/kangarooDataCenter/entranceController/externalCallInterface";
//        String baseUrl = "http://192.168.2.78:9999/kangarooDataCenter/entranceController/externalCallInterface";
        System.out.println("推送参数"+parm);
        String result = HttpRequest.post(baseUrl)
                .header("appId", "800009")//头信息，多个头信息多次调用此方法即可
                .header("apiCode", apiCode)//头信息，多个头信息多次调用此方法即可
                .header("publicKey", "ZJYAP+Oa64gdYq1AG7vPzDGjP+AZCi3QJUBrJkO4NVwAmaFCe7eDIk+3zDUT+v578prj")//头信息，多个头信息多次调用此方法即可
                .header("secretKey", "svMDheByZfQSwmLEzJbFnvaMsgvCPf3SFNSCb9HJKIdlMDGJlKMwUmYHpZVd3SURj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=")//头信息，多个头信息多次调用此方法即可
                .body(parm)//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        System.out.println("返回参数"+result);
        if(StrUtil.isNotEmpty(result)){
            return analytic(result);
        }

        return null;
    }
    public static JSONObject analytic(String parm){
        JSONObject main = JSON.parseObject(parm);
        String status = main.getString("status");
        if("200".equals(status)){
            String attribute = main.getString("attribute");
            //解析token
            JSONObject attributeObj = JSON.parseObject(attribute);
            String errorMessage = attributeObj.getString("errorMessage");
            if("success".equals(errorMessage) || "OK".equals(errorMessage)){
                return attributeObj;
            }
        }
        return null;
    }

    public static String getCustIdByCustName(String custName,String token,String permanentCode,String corpId) {
        String apiCode = "8000090008";
        JSONObject main = new JSONObject();
        main.put("corpAccessToken",token);//token
        main.put("corpId",corpId);//公司
        main.put("currentOpenUserId",permanentCode);//操作员主键
        JSONObject data = new JSONObject();
        JSONObject search_query_info = new JSONObject();
        search_query_info.put("offset",0);
        search_query_info.put("limit",2);
        JSONArray filters = new JSONArray();
        JSONObject filtersObj = new JSONObject();
        filtersObj.put("field_name","name");
        filtersObj.put("field_values",custName);
        filtersObj.put("operator","EQ");
        filters.add(filtersObj);
        search_query_info.put("filters",filters);
        data.put("dataObjectApiName","AccountObj");
        data.put("search_query_info",search_query_info);
        main.put("data",data);
        JSONObject resultObj = CrmUtil.sendU9cTOCrmEsb(main.toString(),apiCode);
        JSONObject resultData = resultObj.getJSONObject("data");
        JSONArray dataList = resultData.getJSONArray("dataList");
        if(null != dataList && dataList.size() > 0 ){
            Object obj = dataList.get(0);
            JSONObject warehouse = JSON.parseObject(obj.toString());
            return warehouse.getString("_id");
        }
        return "";
    }

/**
 *
 * @content 解锁与锁定CRMapi队形
 * @author makejava
 * @date 2023/12/21 0021 0:10
 *
 */

    public static String unLockAndLock(String apiCode,String apiName,String crmId,String token,String permanentCode,String corpId) {
        JSONObject main = new JSONObject();
        main.put("corpAccessToken",token);//token
        main.put("corpId",corpId);//公司
        main.put("currentOpenUserId",permanentCode);//操作员主键
        JSONObject data = new JSONObject();
        data.put("dataObjectApiName",apiName);
        List<String> dataIds = new ArrayList<>();
        dataIds.add(crmId);
        data.put("dataIds",dataIds);
        data.put("detailObjStrategy","1");
        main.put("data",data);
        JSONObject resultObj = CrmUtil.sendU9cTOCrmEsb(main.toString(),apiCode);
       return  resultObj.toString();
    }


    public static String getProIdByCode(String code,String org,String token,String permanentCode,String corpId) {
        String apiCode = "8000090007";
        JSONObject main = new JSONObject();
        main.put("corpAccessToken",token);//token
        main.put("corpId",corpId);//公司
        main.put("currentOpenUserId",permanentCode);//操作员主键
        JSONObject data = new JSONObject();
        JSONObject search_query_info = new JSONObject();
        search_query_info.put("offset",0);
        search_query_info.put("limit",2);
        JSONArray filters = new JSONArray();
        JSONObject filtersObj = new JSONObject();
        filtersObj.put("field_name","product_code");
        filtersObj.put("field_values",code);
        filtersObj.put("operator","EQ");

        JSONObject filtersOrg = new JSONObject();
        filtersOrg.put("field_name","field_1J9OY__c");
        filtersOrg.put("field_values",org);
        filtersOrg.put("operator","EQ");
        filters.add(filtersOrg);
        filters.add(filtersObj);
        search_query_info.put("filters",filters);
        data.put("dataObjectApiName","ProductObj");
        data.put("search_query_info",search_query_info);
        main.put("data",data);
        JSONObject resultObj = CrmUtil.sendU9cTOCrmEsb(main.toString(),apiCode);
        JSONObject resultData = resultObj.getJSONObject("data");
        JSONArray dataList = resultData.getJSONArray("dataList");
        if(null != dataList && dataList.size() > 0 ){
            Object obj = dataList.get(0);
            JSONObject warehouse = JSON.parseObject(obj.toString());
            return warehouse.getString("_id");
        }
        return "";
    }

    public static String getProIdBySupId(String supId,String token,String permanentCode,String corpId) {
        String apiCode = "8000090007";
        JSONObject main = new JSONObject();
        main.put("corpAccessToken",token);//token
        main.put("corpId",corpId);//公司
        main.put("currentOpenUserId",permanentCode);//操作员主键
        JSONObject data = new JSONObject();
        JSONObject search_query_info = new JSONObject();
        search_query_info.put("offset",0);
        search_query_info.put("limit",2);
        JSONArray filters = new JSONArray();
        JSONObject filtersObj = new JSONObject();
        filtersObj.put("field_name","spu_id");
        filtersObj.put("field_values",supId);
        filtersObj.put("operator","EQ");
        filters.add(filtersObj);
        search_query_info.put("filters",filters);
        data.put("dataObjectApiName","ProductObj");
        data.put("search_query_info",search_query_info);
        main.put("data",data);
        JSONObject resultObj = CrmUtil.sendU9cTOCrmEsb(main.toString(),apiCode);
        JSONObject resultData = resultObj.getJSONObject("data");
        JSONArray dataList = resultData.getJSONArray("dataList");
        if(null != dataList && dataList.size() > 0 ){
            Object obj = dataList.get(0);
            JSONObject warehouse = JSON.parseObject(obj.toString());
            return warehouse.getString("_id");
        }
        return "";
    }
    public static void main(String[] args) {
        getToken("34EF8A65DE83249BA8FCC4E533E9978E");
    }

    public static String getProIdByName(String name, String token, String permanentCode, String corpId) {
        String apiCode = "8000090007";
        JSONObject main = new JSONObject();
        main.put("corpAccessToken",token);//token
        main.put("corpId",corpId);//公司
        main.put("currentOpenUserId",permanentCode);//操作员主键
        JSONObject data = new JSONObject();
        JSONObject search_query_info = new JSONObject();
        search_query_info.put("offset",0);
        search_query_info.put("limit",2);
        JSONArray filters = new JSONArray();
        JSONObject filtersObj = new JSONObject();
        filtersObj.put("field_name","name");
        filtersObj.put("field_values",name);
        filtersObj.put("operator","EQ");
        filters.add(filtersObj);
        search_query_info.put("filters",filters);
        data.put("dataObjectApiName","ProductObj");
        data.put("search_query_info",search_query_info);
        main.put("data",data);
        JSONObject resultObj = CrmUtil.sendU9cTOCrmEsb(main.toString(),apiCode);
        JSONObject resultData = resultObj.getJSONObject("data");
        JSONArray dataList = resultData.getJSONArray("dataList");
        if(null != dataList && dataList.size() > 0 ){
            Object obj = dataList.get(0);
            JSONObject warehouse = JSON.parseObject(obj.toString());
            return warehouse.getString("spu_id");
        }
        return "";
    }
}
