package com.bestpractice.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bestpractice.dao.mybatis.common.UUIDUtil;
import com.bestpractice.dao.mybatis.model.BaseModel;
import com.bestpractice.service.IDataService;

public abstract class AbstractRestfullController<T extends BaseModel> implements IController<T> {
	/**
	 * 获取该领域模型的Service
	 * 
	 * @return
	 */
	public abstract IDataService<T> getService();;

	@RequestMapping("select")
	@ResponseBody
	public T select(String uuid) {
		T t = getService().select(uuid);
		return t;
	}

	@RequestMapping("selectList")
	@ResponseBody
	public Pager<T> selectList(T t, 
			@RequestParam(value="beginPage", defaultValue="1")int beginPage, 
			@RequestParam(value="pageSize", defaultValue="10000")int pageSize) {
		List<T> resultList = getService().selectList(t, beginPage, pageSize);
		int totalCount = getService().count(t);
		return new Pager<T>(resultList, totalCount);
	}

	@RequestMapping("count")
	@ResponseBody
	public int count(T t) {
		return getService().count(t);
	}

	@RequestMapping("insert")
	@ResponseBody
	public boolean insert(T t) {
		t.setUuid(UUIDUtil.uuid());
		return getService().insert(t);
	}

	@RequestMapping("insertList")
	@ResponseBody
	public int insertList(List<T> list) {
		if(CollectionUtils.isNotEmpty(list)){
			for(T t : list){
				t.setUuid(UUIDUtil.uuid());
			}
		}
		int count = getService().insertList(list);
		return count;
	}

	@RequestMapping("update")
	@ResponseBody
	public int update(T t) {
		int count = getService().update(t);
		return count;
	}

	@RequestMapping("delete")
	@ResponseBody
	public boolean delete(String uuid) {
		return getService().delete(uuid);
	}

	@RequestMapping("deleteList")
	@ResponseBody
	public int deleteList(List<String> uuidList) {
		int count = getService().deleteList(uuidList);
		return count;
	}

}

@@ -110,16 +110,195 @@
 nUtils;%0A
+import org.springframework.beans.propertyeditors.CustomNumberEditor;%0Aimport org.springframework.web.bind.WebDataBinder;%0Aimport org.springframework.web.bind.annotation.InitBinder;%0A
 import o
@@ -726,16 +726,552 @@
 er%3CT%3E %7B%0A
+%09%0A%09private static class IntEditor extends CustomNumberEditor %7B%0A%09%09public IntEditor() %7B%0A%09%09%09super(Integer.class, true);%0A%09%09%7D%0A%0A%09%09public void setAsText(String text) throws IllegalArgumentException %7B%0A%09%09%09if (text == null %7C%7C text.trim().equals(%22%22)) %7B%0A%09%09%09%09// Treat empty String as null value.%0A%09%09%09%09setValue(0);%0A%09%09%09%7D else %7B%0A%09%09%09%09// Use default valueOf methods for parsing text.%0A%09%09%09%09super.setAsText(text);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09@InitBinder%0A%09public void initBinder(WebDataBinder binder) %7B%0A%09%09binder.registerCustomEditor(int.class, null, new IntEditor());%0A%09%7D%0A%09%0A
 %09/**%0A%09 *

支持model中的属性是number类型时，如果属性对应的参数为空时，避免抛出异常而导致400错误