package com.persagy.cameractl.utils;

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

import com.fasterxml.jackson.core.JsonProcessingException;

public class ResultTools {
	public static final String err_param_not_found = "缺少必填参数";
	public static final String err_param_time_limit = "时间超过限制";
	public static final String err_param_format = "参数格式错误";
	public static final String err_param_obj_id = "未找到指定id对象";
	public static final String err_param_value_illegal = "参数值不合法：";
	public static final String err_service = "服务错误：";
	public static final String err_crud_save = "保存失败：";
	public static final String err_crud_update = "更新失败：";
	public static final String err_crud_delete = "删除失败：";
	public static final String err_crud_query = "查询失败：";
	public static final String result_success = "success";
	public static final String result_failure = "failure";
	public static final String key_result = "result";
	public static final String key_err_message = "reason";
	public static final String key_data = "data";
	
	public static String errorResult(String errorMsg) {
		Map<String, Object> map = getDataResult();
		map.put(key_result, result_failure);
		map.put(key_err_message, errorMsg);
		return map2str(map);
	}
	
	public static String illegalParamResult(String param) {
		Map<String, Object> map = getDataResult();
		map.put(key_result, result_failure);
		map.put(key_err_message, "param "+param+" is illegal");
		return map2str(map);
	}
	
	public static String successResult() {
		return map2str(getDataResult());
	}
	
	public static String successResult(String message) {
		Map<String, Object> map = getDataResult();
		map.put(key_err_message, message);
		return map2str(map);
	}
	
	public static String dataResult(Object data) {
		Map<String, Object> map = getDataResult();
		map.put(key_data, data);
		return map2str(map);
	}
	
	private static Map<String, Object> getDataResult() {
		Map<String, Object> map = new HashMap<>();
		map.put(key_result, result_success);
		return map;
	}
	
	private static String map2str(Map<String, Object> map) {
		String result  = "{}";
		try {
			result = JsonTools.obj2Str(map);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return result;
	}
}
