package com.app.framework.core.support.controller;

import com.app.framework.core.log.annotation.LogOps;
import com.app.framework.core.log.constant.LogOpsType;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.grid.ExGridLoad;
import com.app.framework.core.support.view.grid.Paging;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Controller基类，所有Controller都必须继承该类
 * 	提供获取请求头信息的各种方法
 * 	提供获取参数的各种方法
 * 	提供基础的增删改查请求
 */
public abstract class CrudBaseControllerSupport<E> extends CrudControllerSupport {

	/**
	 * 查询列表的具体实现
	 * @param request	HttpServletRequest
	 * @param view		查询条件
	 * @param paging	分页条件
	 * @return 查询结果
	 */
	public abstract <V> List<V> list(HttpServletRequest request, final E view, Paging paging);
	
	/**
	 * 保存记录的具体实现
	 * @param request	HttpServletRequest
	 * @param entity	实体对象
	 */
	public abstract AjaxResult saveEntity(HttpServletRequest request, E entity);
	
	/**
	 * 批量删除记录,根据Id
	 * @param request	HttpServletRequest
	 * @param oids		主键Id数组
	 */
	public abstract AjaxResult batchDelete(HttpServletRequest request, Long oids[]);
	
	/**
	 * 查询所有记录,支持分页
	 */
	@RequestMapping(value = "/list")
	public @ResponseBody ExGridLoad listByPaging(HttpServletRequest request, E view, Paging paging) {
		List<E> views = this.list(request, view, paging);
		return ExGridLoad.wrapExGridLoad(views, paging);
	}
	
	/**
	 * 保存记录
	 */
	@RequestMapping(value = "/saveEntity")
	@LogOps(type = LogOpsType.UPDATE)
	public @ResponseBody AjaxResult saveByEntity(HttpServletRequest request, E entity){
		return this.saveEntity(request, entity);
	}
	
	/**
	 * 删除记录,根据Id
	 */
	@RequestMapping(value = "/deleteById")
	@LogOps(type = LogOpsType.DELETE)
	public @ResponseBody AjaxResult deleteById(HttpServletRequest request, Long oid) {
		Assert.notNull(oid, "id不能为空.");
		return this.batchDelete(request, new Long[]{oid});
	}
	
	/**
	 * 批量删除记录,根据Id
	 */
	@RequestMapping(value = "/batchDelete")
	@LogOps(type = LogOpsType.DELETE)
	public @ResponseBody AjaxResult deleteByBatch(HttpServletRequest request, Long oids[]) {
		Assert.notNull(oids, "ids不能为空.");
		return this.batchDelete(request, oids);
	}
}
