package com.wb.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by yangxh on 2021/8/18.
 */
public class ColumnUpper {

    public static String toUpper(String str){
        JSONObject json = JSON.parseObject(str);
        JSONObject metaData = json.getJSONObject("metaData");
        if(metaData == null){
            return str;
        }
        JSONArray fields = metaData.getJSONArray("fields");
        JSONArray columns = json.getJSONArray("columns");
        JSONArray rows = json.getJSONArray("rows");
//        System.out.println("=================fields=================");
        JSONArray fieldsCpy = new JSONArray();
        metaData.put("fields", fieldsCpy);
        convertToVal(fields, fieldsCpy, "name");
//        System.out.println(fieldsCpy.toJSONString());
//
//        System.out.println("=================columns=================");
        JSONArray columnsCpy = new JSONArray();
        json.put("columns", columnsCpy);
        convertToVal(columns, columnsCpy, "dataIndex");
//        System.out.println(JSON.toJSONString(columnsCpy, SerializerFeature.WriteNullStringAsEmpty));
//
//        System.out.println("=================rows=================");
        JSONArray rowsCpy = new JSONArray();
        json.put("rows", rowsCpy);
        convertToKey(rows, rowsCpy);
//        System.out.println(JSON.toJSONString(rowsCpy, SerializerFeature.WriteNullStringAsEmpty));

        return JSON.toJSONString(json, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
    }

    private static void convertToVal(JSONArray fields, JSONArray fieldsCpy, String name){
        for (int i=0; i<fields.size(); i++){
            JSONObject field = fields.getJSONObject(i);
            Set<String> set = field.keySet();
            JSONObject fieldCpy = new JSONObject();
            fieldsCpy.add(fieldCpy);
            for (String key : set){
                if (name.equals(key)){
                    fieldCpy.put(key, field.getString(key).toUpperCase());
                }else {
                    fieldCpy.put(key, field.get(key));
                }
            }
        }
    }

    private static void convertToKey(JSONArray fields, JSONArray fieldsCpy){
        for (int i=0; i<fields.size(); i++){
            JSONObject field = fields.getJSONObject(i);
            Set<String> set = field.keySet();
            JSONObject fieldCpy = new JSONObject();
            fieldsCpy.add(fieldCpy);
            for (String key : set){
                fieldCpy.put(key.toUpperCase(), field.getString(key));
            }
        }
    }

    /**
     * map类型的数据key转换为大写
     * @param orgMap
     * @return
     */
    public static Map<String, Object> keyToUpperCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();
        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }
        Set<Map.Entry<String,Object>> entrySet = orgMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            resultMap.put(key.toUpperCase(), value);
        }
        return resultMap;
    }
}
