package com.uas.eis.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.uas.eis.entity.ErrorMsg;
import net.sf.json.JSONArray;
import net.sf.json.util.JSONUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uas.eis.exception.SystemException;


public class BaseUtil {

	
	
	/**
	 * 以抛出异常的方式将信息交给MyExceptionHandler MyExceptionHandler会捕捉信息并传给前台
	 * 前台捕捉错误信息，并显示给用户
	 * 
	 * @param error
	 *            要在前台显示的信息
	 */
	public static void showError(String error) {
		if (error != null && error.length() > 0)
			throw new SystemException(error);
	}

	public static void showError(ErrorMsg errorMsg) {
		if(errorMsg!=null){
			throw new SystemException(errorMsg);
		}
	}

	public static void showError(ErrorMsg errorMsg,String extraMsg) {
		if(errorMsg!=null){
			if (StringUtil.hasText(extraMsg)){
				errorMsg.setErrMsg(extraMsg);
			}
			throw new SystemException(errorMsg);
		}
	}

	/**
	 * List集合转化成字符串, null和空字符自动去掉
	 * 
	 * @param list
	 *            待转化集合
	 * @param ch
	 *            分割符
	 * @param repeat
	 *            是否去重 {true-是、false-否}
	 */
	public static String parseList2Str(List<?> list, String ch, boolean repeat) {
		StringBuffer sb = new StringBuffer();
		for (Object s : list) {
			if (s != null && !s.toString().trim().equals("")) {
				if (repeat) {
					if (!sb.toString().contains(s + ch)) {
						sb.append(s);
						sb.append(ch);
					}
				} else {
					sb.append(s);
					sb.append(ch);
				}
			}
		}
		if (sb.length() > 0 && ch.length() > 0) {
			return sb.substring(0, sb.lastIndexOf(ch));
		}
		return sb.toString();
	}
	
	/**
	 * 将gridStore解析成maps
	 * 
	 * @param gridStore
	 *            字符串形式的grid数据
	 * @return map形式的grid数据
	 */
	public static List<Map<Object, Object>> parseGridStoreToMaps(String gridStore) {
		List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
		try {
			if (!gridStore.startsWith("[")) {
				gridStore = "[" + gridStore + "]";
			}
			if (gridStore.length() > 409600) {
				list = JacksonUtil.fromJsonArray(gridStore);
			} else {
				list = FlexJsonUtil.fromJsonArray(gridStore, HashMap.class);
			}
		} catch (Exception e) {
			list = JSONUtil.toMapList(gridStore);
		}
		return list;
	}
	
	/**
	 * 数组转化成字符串, null和空字符自动去掉
	 * 
	 * @param arr
	 *            待转化数组
	 * @param ch
	 *            分割符
	 */
	public static String parseArray2Str(Object[] arr, String ch) {
		StringBuffer sb = new StringBuffer();
		for (Object s : arr) {
			if (s != null && !s.toString().trim().equals("")) {
				sb.append(s);
				sb.append(ch);
			}
		}
		if (sb.length() > 0 && ch.length() > 0) {
			return sb.substring(0, sb.lastIndexOf(ch));
		}
		return sb.toString();
	}
	
	
	/**
	 * 数据转换，将传入的数据转成需要调用接口规定的数据格式
	 * @param jsonStr	传入的数据
	 * @param keyMap	UAS与外部系统的键 映射
	 * @return
	 */
	public static Map<String, Object> jsonToMap(String jsonStr, Map<String, String> keyMap){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> jsonMap = JSONObject.parseObject(jsonStr);
		Set<String> set = jsonMap.keySet();
		Iterator<String> it = set.iterator();
		while(it.hasNext()){
			String key = it.next();
			if(keyMap.containsKey(key)){
				map.put(keyMap.get(key), jsonMap.get(key));
			}
		}
		return map;
	}

		public static Map<String,Object>convertJsonToMap(String json) throws Exception {
			ObjectMapper objectMapper = new ObjectMapper();
			return objectMapper.readValue(json, Map.class);
		}

	public static String parseDataToJson(Map<String, Object> formMap,List<Map<String, Object>> gridList){
		JSONObject json = new JSONObject();
		json.put("form", JSON.toJSON(formMap));
		json.put("grid", JSONArray.fromObject(gridList));
		return json.toString();
	}

	/**
	 * 将formStore解析成一个map
	 *
	 * @param formStore
	 *            字符串形式的表单数据
	 * @return map形式的表单数据
	 */
	public static Map<Object, Object> parseFormStoreToMap(String formStore) {
		try {
			return FlexJsonUtil.fromJson(formStore);
		} catch (Exception e) {
			return JSONUtil.toMap(formStore);
		}
	}

}
