package com.sptg.common.core.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

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

/**
 * title: JsonUtils
 * package: com.sptg.common.core.util
 * description: Json工具类
 *                  
 *               
 */
@Slf4j
public class JsonUtils {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        //  忽略value为null时，key的输出
        MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
        MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //  配置反序列化时,禁用对日期以时间戳方式输出的特性
        MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        MAPPER.registerModule(new JavaTimeModule());

        SimpleModule module = new SimpleModule();
        module.addSerializer(Long.TYPE, ToStringSerializer.instance);
        module.addSerializer(Long.class, ToStringSerializer.instance);
        MAPPER.registerModule(module);
    }

    /**
     * 将对象转为 Json 字符串
     * @param data 对象
     * @return json 字符串
     *                  
     *                       
     */
    public static String objectToJson(Object data){
        try{
            return MAPPER.writeValueAsString(data);
        }catch (JsonProcessingException e){
            log.error("将对象转为 Json 字符串时出现了异常");
            log.error(e.getMessage());
            return "";
        }
    }

    /**
     * 将 json 字符串转换为对象
     * @param jsonData json 字符串
     * @param beanType 对象类型
     * @return T
     *                  
     *                       
     */
    public static <T> T jsonToPojo(String jsonData,Class<T> beanType){
        try{
            return MAPPER.readValue(jsonData,beanType);
        }catch (Exception e){
            log.error("将 json 字符串转换为对象时出现了异常");
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 将 Json 字符串转换成对象集合
     * @param jsonData json 字符串
     * @param beanType 集合类型
     * @return List<T>
     *                  
     *                       
     */
    public static <T> List<T> jsonToList(String jsonData,Class<T> beanType){
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            log.error("将 Json 字符串转换成对象集合时出现了异常");
            log.error(e.getMessage());
            return new ArrayList<>(0);
        }
    }

    /**
     * list转成string类型，中间用逗号隔开
     * @param list
     * @return
     */
    public static String listToString(List list){
        String[] strArrStrings = new String[list.size()];
        String resultString = "";

        for(int i=0;i<=list.size()-1;i++){
            strArrStrings[i] = (String) list.get(i);
        }

        for(int j=0;j<=strArrStrings.length-1;j++){
            if(j < strArrStrings.length-1){
                resultString += strArrStrings[j] + ",";
            }else{
                resultString += strArrStrings[j];
            }
        }
        return resultString;
    }
}
