package com.zfire.jiasm.syncdata.utils;

import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

public class DataUtil {
	
	public static String fromDbName2ServiceName(String tableName) {
		
		StringBuffer result = new StringBuffer();
		String[] values = tableName.split("\\Q_\\E");
		for (int j = 0; j < values.length; j++) {
			String value = values[j];
			if (j == 0) {
				if(value.equals(value.toUpperCase())) {
					result.append(value.toLowerCase());
				}else {
					result.append(value);
				}
			} else {
				char[] ary = value.toCharArray();
				for (int i = 0; i < ary.length; i++) {
					if (i == 0) {
						result.append(String.valueOf(ary[i]).toUpperCase());
					} else {
						result.append(String.valueOf(ary[i]).toLowerCase());
					}
				}
			}
		}
		return result.toString();
	}
	
	public static String findValue(String srcTabCol, JSONObject aData) {

		Set<String> set = aData.keySet();
		for (String key : set) {
			if (key.equalsIgnoreCase(srcTabCol)) {
				return aData.getString(key);
			}
		}

		return null;

	}

	public static void initLastModifiedDate(JSONObject aData,String keyName) {
		if (!aData.containsKey(keyName) || aData.getString(keyName) == null
				|| aData.getString(keyName).trim().length() == 0) {
			aData.put(keyName,
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		}

	}


	public static void initOther(JSONObject aData,String keyName) {

		if (!aData.containsKey(keyName) || aData.getString(keyName) == null
				|| aData.getString(keyName).trim().length() == 0) {
			aData.put(keyName,"0");
		}

	}

}
