package com.chinaitop.depot.intelligent.utils;

import com.chinaitop.depot.intelligent.grainsituation.model.TPoints;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class JsonToObjectUtils {

    final static Logger logger  =  LoggerFactory.getLogger(JsonToObjectUtils.class );

    /**
     * 将字符串转换成list
     * @param str 要转换的字符json串
     * @param packageName 实体的全包名称
     * @return list对象
     */
    public static ArrayList<Object> jsonToObjects(String str, String packageName){
        ArrayList<Object> object = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Class.forName(packageName));
            object = mapper.readValue(str, javaType);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("json转换异常  "+e.getMessage());
        }
        return object;
    }
    /**
     * 将字符串转换成list
     * @param str 要转换的字符json串
     * @param classs 实体
     * @return list对象
     */
    public static ArrayList<Object> jsonToObjects(String str, Class<?> classs){
        ArrayList<Object> object = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, classs);
            object = mapper.readValue(str, javaType);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("json转换异常  "+e.getMessage());
        }
        return object;
    }

    /**
     * 将字符串转换成单个对象
     * @param str 要转换的字符json串
     * @param packageName 实体的全包名称
     * @return 返回单个对象
     */
    public static Object jsonToObject(String str, String packageName){
        Object object = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            object = mapper.readValue(str, Class.forName(packageName));
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("json转换异常  "+e.getMessage());
        }
        return object;
    }
    /**
     * 将字符串转换成单个对象
     * @param str 要转换的字符json串
     * @param classs 实体
     * @return 返回单个对象
     */
    public static Object jsonToObject(String str, Class<?> classs){
        Object object = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            object = mapper.readValue(str, classs);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("json转换异常  "+e.getMessage());
        }
        return object;
    }

    /**
     *
     * @param str json字符串
     * @param classs 要转换的类
     * @param diverse 转换的时间类型
     * @return
     */
    public static ArrayList<Object> jsonToObjectsDiverse(String str, Class<?> classs,String diverse){
        ArrayList<Object> object = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat(diverse));
            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, classs);
            object = mapper.readValue(str, javaType);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("json转换异常  "+e.getMessage());
        }
        return object;
    }

    /**
     * list转换字符串
     * @param points list参数
     * @return
     */
    public static String listToString(List<TPoints> points){
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(points);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("json转换异常  "+e.getMessage());
        }
        return null;
    }
}
