package com.xt.hb.gis.ctl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.xt.hb.gis.util.IdGenerator;
import com.yuanxd.tools.io.http.JsonResult;

/**
 * 视图控制器基类
 * 
 * @author 袁晓冬
 *
 */
public class BaseCtl {
	@Autowired
	protected IdGenerator idGenerator;
	protected Gson gson;

	/***
	 * 构造函数
	 */
	public BaseCtl() {
		gson = new GsonBuilder().create();
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class,
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	}

	/**
	 * 返回错误结果信息
	 * 
	 * @param message
	 * @return
	 */
	protected JsonResult error(String message) {
		return new JsonResult().setSuccess(false).setMessage(message);
	}

	/**
	 * 返回操作成功
	 * 
	 * @return
	 */
	protected JsonResult succes() {
		return new JsonResult().setSuccess(true);
	}

	/**
	 * 返回操作成功
	 * 
	 * @param data
	 * @return
	 */
	protected JsonResult succes(Object data) {
		return new JsonResult().setSuccess(true).setData(data);
	}

	/**
	 *
	 * 函数名称: parseData 函数描述: 将json字符串转换为map
	 * 
	 * @param data
	 * @return
	 */
	protected Map<String, Object> parseData(String data) {
		Map<String, Object> map = gson.fromJson(data, new TypeToken<Map<String, Object>>() {
		}.getType());
		return map;
	}
}
