package com.persagy.dmp.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.persagy.dmp.common.helper.SpringHelper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Json 助手类
 * @author Charlie Yu
 * @date 2021-08-26
 */
@Slf4j
public class JsonHelper {

    /** ObjectMapper */
    public static ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);

    /**
     * 转换为Json字符串
     * @param obj
     * @return
     */
    public static String toJsonStrQuietly(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 转换为JsonObject
     * @param obj
     * @return
     */
    public static JSONObject toJsonObjectQuietly(Object obj) {
        String jsonStr = toJsonStrQuietly(obj);
        return JSONObject.parseObject(jsonStr);
    }

    /**
     * 转换为JSONArray
     * @param obj
     * @return
     */
    public static JSONArray toJsonArrayQuietly(Object obj) {
        String jsonStr = toJsonStrQuietly(obj);
        return JSONArray.parseArray(jsonStr);
    }

    /**
     * 转换为指定对象
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T toSingleEntityQuietly(Object obj, Class<T> clazz) {
        try {
            String jsonStr = toJsonStrQuietly(obj);
            return objectMapper.readValue(jsonStr, clazz);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 转换为指定对象集合
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> toMultiEntityQuietly(Object obj, Class<T> clazz) {
        try {
            String jsonStr = toJsonStrQuietly(obj);
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
            return objectMapper.readValue(jsonStr, javaType);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 转换为Json字符串
     * @param obj
     * @return
     */
    public static String toJsonStr(Object obj) throws IOException{
        return objectMapper.writeValueAsString(obj);
    }

    /**
     * 转换为JsonObject
     * @param obj
     * @return
     */
    public static JSONObject toJsonObject(Object obj) throws IOException {
        String jsonStr = toJsonStr(obj);
        return JSONObject.parseObject(jsonStr);
    }

    /**
     * 转换为JSONArray
     * @param obj
     * @return
     */
    public static JSONArray toJsonArray(Object obj) throws IOException {
        String jsonStr = toJsonStr(obj);
        return JSONArray.parseArray(jsonStr);
    }

    /**
     * 转换为指定对象
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T toSingleEntity(Object obj, Class<T> clazz) throws IOException {
        String jsonStr = toJsonStr(obj);
        return objectMapper.readValue(jsonStr, clazz);
    }

    /**
     * 转换为指定对象集合
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> toMultiEntity(Object obj, Class<T> clazz) throws IOException {
        String jsonStr = toJsonStr(obj);
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
        return objectMapper.readValue(jsonStr, javaType);
    }
}
