package com.ag.web.agCommon;

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

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by Administrator on 2017/12/29.
 */
public class AgCommonUtil {
    /**
     * 驼峰写法转表字段
     *
     * @param JsonString
     * @return
     */
    public static String upJson(String JsonString) {
        if (JsonString == null) {
            return "";
        }
        JSONArray jsonArray = JSONArray.parseArray(JsonString);

        List jsonList = new ArrayList();
        for (int i = 0; jsonArray != null && i < jsonArray.size(); i++) {
            Map jsonMap = new HashMap();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (jsonObject != null) {
                Set<String> set = jsonObject.keySet();
                for (String key : set) {
                    jsonMap.put(underscoreName(key), jsonObject.get(key));
                }
                jsonList.add(jsonMap);
            }
        }
        //关闭引用检测后，重复引用对象时就不会被$ref代替，但是在循环引用时也会导致StackOverflowError异常。
        return JSON.toJSONString(jsonList, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 处理字符
     *
     * @param name
     * @return
     */
    public static String underscoreName(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && name.length() > 0) {
            // 将第一个字符处理成大写
            result.append(name.substring(0, 1).toUpperCase());
            // 循环处理其余字符
            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                // 在大写字母前添加下划线
                if (s.equals(s.toUpperCase())
                        && !Character.isDigit(s.charAt(0))) {
                    result.append("_");
                }
                // 其他字符直接转成大写
                result.append(s.toUpperCase());
            }
        }
        return result.toString();
    }

    /**
     * 得到路径
     *
     * @return
     */
    public static String getPath() {
        String str = AgCommonUtil.class.getResource("/").getPath();
        String basicPath = str.substring(0, str.indexOf("target"));
        String path = basicPath + "target/Neptune/wb/document/";
        return path;
    }

    // 判断文件是否存在
    public static void judeFileExists(File file) {
        if (file.exists()) {
            System.out.println("file exists");
        } else {
            System.out.println("file not exists, create it ...");
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 判断文件夹是否存在
    public static void judeDirExists(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("dir exists");
            } else {
                System.out.println("the same name file exists, can not create dir");
            }
        } else {
            System.out.println("dir not exists, create it ...");
            file.mkdir();
        }
    }

    //创建多次文件
    public static void judeDirsExists(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("dir exists");
            } else {
                System.out.println("the same name file exists, can not create dir");
            }
        } else {
            System.out.println("dir not exists, create it ...");
            file.mkdirs();
        }
    }
    public static void main(String[] args) {
        System.out.println(AgCommonUtil.getPath());
    }
}
