package org.jeecg.modules.common.util;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

/**
 * niugeng
 *
 * @version V1.0
 * @Description：JSON工具类
 */
@Slf4j
public class JsonUtils {

    /**
     * 方欣入参 唯一标识，用于查询
     */
    private static final List<String> fxInGuid = Arrays.asList("sldh","kplsh","lzqdfphm","hzfpxxqrdbh","uuid","fphm","kprqq","qdfphm","tranSeq");

    /**
     * 方欣纳税人识别号
     */
    private static final List<String>  fxNsrsbh=Arrays.asList("nsrsbh","xsfnsrsbh","gsqysh","gfsh","xfsh");

    /**
     * 查询方欣 关键字
     * @param json
     * @return {@link String}
     */
    public static String getFxGuid(String json){
        for (String key : fxInGuid) {
            final String strFromJsonByKey = findStrFromJsonByKey(json, key);
            if (StrUtil.isNotBlank(strFromJsonByKey)){
                return strFromJsonByKey;
            }
        }
        return "";
    }

    /**
     * 查询方欣 纳税人识别号
     * @param json
     * @return {@link String}
     */
    public static String getFxNsrsbh(String json){
        for (String key : fxNsrsbh) {
            final String strFromJsonByKey = findStrFromJsonByKey(json, key);
            if (StrUtil.isNotBlank(strFromJsonByKey)){
                return strFromJsonByKey;
            }
        }
        return "";
    }


    /**
     * 查询json字符串 固定节点的值
     * @param json
     * @param key
     * @return {@link String}
     */
    public static String findStrFromJsonByKey(String json ,String key){
        try {
            List<String> list = JsonPath.read(json, "$.."+key);
            if (list.isEmpty()){
                return "";
            }
            return list.get(0);

        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 将空值转换成空字符串
     */
    public static final ValueFilter filterNullToK = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null) {
                return "";
            }
            return v;
        }
    };

    /**
     * 将空字符串转换成空值
     */
    public static final ValueFilter filterKToNull = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
//            System.out.println(obj);
            if (v == "") {
                return null;
            }
            return v;
        }
    };


    public static void main(String[] args) throws Exception {
        String json = "{\n" +
                "\t\"head\": {\n" +
                "\t\t\"requestId\": \"a49d81d2937841ad86dee5cb906f1897\",\n" +
                "\t\t\"appId\": \"20001100\",\n" +
                "\t\t\"encoding\": \"UTF-8\",\n" +
                "\t\t\"clientVersion\": \"v1\",\n" +
                "\t\t\"timeout\": 7000,\n" +
                "\t\t\"async\": false,\n" +
                "\t\t\"timestamp\": 1702003320559\n" +
                "\t},\n" +
                "\t\"extMap\": {\n" +
                "\t\t\"sldh\": \"c943c764cfe347c79de37efa6b58b48c\",\n" +

                "\t\t\"eventMessageType\": \"SLYW_SLJG\",\n" +
                "\t\t\"eventMessageTypeName\": \"受理结果\",\n" +
                "\t\t\"aaa\": \"受理结果\"\n" +
                "\t},\n" +
                "\t\"body\": {\n" +
                "\t\t\"sldh\": \"c943c764cfe347c79de37efa6b58b48c\",\n" +
                "\t\t\"errorCode\": \"1010110012\",\n" +
                "\t\t\"clzt\": \"EXECUTE_FAIl\",\n" +
                "\t\t\"slywlxDm\": \"02\",\n" +
                "\t\t\"errorMsg\": \"开票异常:确认单状态代码异常\"\n" +
                "\t}\n" +
                "}";
        System.out.println(json);
        System.out.println("----------------------");

        System.out.println(findStrFromJsonByKey(json,"sldh"));
    }

}
