package com.jeeplus.common.utils;


import com.google.gson.Gson;
import com.jeeplus.modules.workreimbur.entity.WorkReimburDetails;
import net.sf.json.JSONArray;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by 付光裕 on 2018/1/22.
 */
public class JsonUtil {
    /**
     * 对象转json字符串
     * @param t
     * @return
     */
    public static <T> String  objectToJson(T t) {
        Gson gson = new Gson();
        String json = gson.toJson(t);
        try {
            json = URLEncoder.encode(json,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * json字符串转对象
     * @param value
     * @param clazz
     * @return
     */
    public static <T> T jsonToObject(String value, Class<T> clazz) {
        T t = null;
        Gson gson = new Gson();
        try {
            String decode = URLDecoder.decode(value, "UTF-8");
            t = gson.fromJson(decode, clazz);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return t;

    }
    /**
     *  将list转json
     */
    public static <T>String list_to_json(List<T> list){
        String jsonStr = "";
        try {
            Gson gson = new Gson();
            jsonStr = gson.toJson(list);
            jsonStr = URLDecoder.decode(jsonStr,"utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    /**
     *  将json字符串转换为list集合
     * @param jsonStr
     * @return
     */
    public static <T> List<T> json_to_list(String jsonStr,Class<T> clazz) {
        List<T> list = new ArrayList<T>();
        if(jsonStr == null || jsonStr.length() == 0)
            return list;
        try {
            jsonStr = URLDecoder.decode(jsonStr, "UTF-8");
            JSONArray jsonArray = JSONArray.fromObject(jsonStr);
            list =(List<T>) JSONArray.toCollection(jsonArray, clazz);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return list;
    }

    public  static void main(String[] args) {
        List<WorkReimburDetails> detailsList = new ArrayList<WorkReimburDetails>();
        for(int i=1;i<4;i++){
            WorkReimburDetails details = new WorkReimburDetails();
            details.setId(UUID.randomUUID().toString().replaceAll("-",""));
            details.setReimburAmount("1265.56");
            details.setReimburCategory("火车票");
            details.setReimburDetail("北京-上海，单程票");
            detailsList.add(details);
        }

        String jsonStr = "[{\"reimburAmount\":\"456.56\",\"reimburCategory\":\"火车票\",\"reimburDetail\":\"北京-上海\"},"
                + "{\"reimburAmount\":\"21.87\",\"reimburCategory\":\"活动\",\"reimburDetail\":\"2月10号团建\"}]";

        List<WorkReimburDetails> detailsList1 = json_to_list(jsonStr, WorkReimburDetails.class);
        for (int i = 0; i < detailsList1.size(); i++) {
            WorkReimburDetails workReimburDetails = detailsList1.get(i);
            System.err.println(workReimburDetails.getReimburAmount());
            System.err.println(workReimburDetails.getReimburCategory());
            System.err.println(workReimburDetails.getReimburDetail());
        }
    }
}
