BaseController.java 6.55 KB
package com.viontech.fanxing.commons.base;

import com.github.pagehelper.PageInfo;
import com.viontech.keliu.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import static com.viontech.keliu.util.JsonMessageUtil.getErrorJsonMsg;
import static com.viontech.keliu.util.JsonMessageUtil.getSuccessJsonMsg;

/**
 * Controller基类<br>
 * 所有Controller都需要继承自该类
 *
 * @param <T>
 *
 * @author suman 2016年8月15日 下午1:59:19
 */

public abstract class BaseController<O extends BaseModel, T extends VoInterface<O>> {


    /**
     * 通用message
     */
    public static final String MESSAGE_ADD_SUCCESS = "添加成功";
    public static final String MESSAGE_ADD_ERROR = "addError";
    public static final String MESSAGE_UPDATE_SUCCESS = "更新成功";
    public static final String MESSAGE_UPDATE_ERROR = "updateError";
    public static final String MESSAGE_DELETE_SUCCESS = "删除成功";
    public static final String MESSAGE_DELETE_ERROR = "delError";
    public static final String MESSAGE_COUNT_SUCCESS = "成功";
    public static final String MESSAGE_COUNT_ERROR = "countError";
    public static final String MESSAGE_SELECT_SUCCESS = "成功";
    public static final String MESSAGE_SELECT_ERROR = "selError";
    public static final String MESSAGE_LIST_SUCCESS = "成功";
    public static final String MESSAGE_LIST_ERROR = "listError";
    public static final String MESSAGE_PAGE_SUCCESS = "成功";
    public static final String MESSAGE_PAGE_ERROR = "pageError";
    public static final String MESSAGE_WEBROOT_NOTFOUND_ERROR = "webRootNotFoundError";
    public static final String MESSAGE_FILE_UPLOAD_SUCCESS = "fileUploadSuccess";
    public static final String MESSAGE_FILE_UPLOAD_ERROR = "fileUploadError";
    public static final String MESSAGE_FILE_DOWNLOAD_ERROR = "fileDownloadError";
    public static final String MESSAGE_FILE_NOTFOUND_ERROR = "fileNotFoundError";
    public static final String MESSAGE_ID_NOT_EMPTY = "idNotEmpty";
    public static final String MESSAGE_MODE_NOT_EXISTENCE = "modleNotExistence";
    public static final int EXAMPLE_TYPE_SIMPLE = 0;
    public static final int EXAMPLE_TYPE_NORMAL = 1;
    public static final int EXAMPLE_TYPE_COUNT = 3;
    public static final int EXAMPLE_TYPE_PAGE = 4;
    /**
     * slf4j 日志对象 用来记录log
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());

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

    public static boolean isNotNull(Object object) {
        if (object == null) {
            return false;
        }
        if (object.toString().trim().isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * 通用添加方法
     *
     * @param t 需要插入数据库的对象
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody T t) {
        getService().insertSelective(t.getModel());
        return getSuccessJsonMsg(MESSAGE_ADD_SUCCESS, t);
    }

    /**
     * 通用数量方法
     *
     * @param t 需要插入数据库的对象的条件
     *
     * @return
     */
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public Object count(T t) {
        int result = getService().countByExample(getExample(t, EXAMPLE_TYPE_COUNT));
        return getSuccessJsonMsg(MESSAGE_COUNT_SUCCESS, result);
    }

    /**
     * 通用更新方法
     *
     * @param t 需要更新的对象及内容
     *
     * @return
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable(value = "id") Long id, @RequestBody T t) {
        t.getModel().setId(id);
        getService().updateByPrimaryKeySelective(t.getModel());
        return getSuccessJsonMsg(MESSAGE_UPDATE_SUCCESS, t);

    }

    /**
     * 通用删除方法
     *
     * @param id 需要删除的数据id
     *
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Object del(@PathVariable(value = "id") Long id) {
        if (id == null) {
            return getErrorJsonMsg(MESSAGE_ID_NOT_EMPTY);
        }
        int result = getService().deleteByPrimaryKey(id);
        if (result == 0) {
            return getErrorJsonMsg(MESSAGE_MODE_NOT_EXISTENCE);
        }
        return getSuccessJsonMsg(MESSAGE_DELETE_SUCCESS, null);
    }

    /**
     * 通用列表查询方法,将数据从数据库中全部查出
     * saveFile
     *
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object selOne(@PathVariable(value = "id") Long id) {
        BaseModel t = getService().selectByPrimaryKey(id);
        return getSuccessJsonMsg(MESSAGE_LIST_SUCCESS, t);


    }

    /**
     * 通用分页查询方法,从数据库中查出一页数据
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public Object page(T t, @RequestParam(value = "page", defaultValue = "-1") int page, @RequestParam(value = "pageSize", defaultValue = "100") int pageSize, String sortName, String sortOrder) {
        BaseExample baseExample = getExample(t, EXAMPLE_TYPE_PAGE);
        if (isNotNull(sortOrder) && isNotNull(sortName)) {
            baseExample.setOrderByClause(baseExample.getTableAlias() + "." + sortName + " " + sortOrder);
        } else if (isNotNull(sortName) && !isNotNull(sortOrder)) {
            baseExample.setOrderByClause(sortName);
        }
        if (page <= 0) {
            List result = getService().selectByExample(baseExample);
            return getSuccessJsonMsg(MESSAGE_SELECT_SUCCESS, result);
        } else {
            PageInfo pageInfo = getService().pagedQuery(baseExample, page, pageSize);
            return getSuccessJsonMsg(MESSAGE_PAGE_SUCCESS, pageInfo);
        }
    }


    /**
     * 获取到执行各项操作需要的service
     *
     * @return
     */
    protected abstract BaseService<O> getService();

    /**
     * 得到执行查询操作需要的查询条件
     *
     * @param t 查询条件存储对象
     *
     * @return 查询条件对象
     */
    protected abstract BaseExample getExample(T t, int type);


}