|
|
@ -1,33 +1,53 @@ |
|
|
|
package web.controller; |
|
|
|
package web.controller; |
|
|
|
|
|
|
|
|
|
|
|
import core.service.BaseService; |
|
|
|
import db.annotation.TableInfo; |
|
|
|
import db.AbstractModel; |
|
|
|
import db.config.HibernateConfig; |
|
|
|
import db.DBAction; |
|
|
|
import db.form.DBAction; |
|
|
|
import db.JsonResult; |
|
|
|
import db.form.JsonResult; |
|
|
|
import db.Type; |
|
|
|
import db.form.Type; |
|
|
|
|
|
|
|
import db.model.AbstractModel; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
|
import org.apache.logging.log4j.Logger; |
|
|
|
import org.apache.logging.log4j.Logger; |
|
|
|
|
|
|
|
import org.hibernate.criterion.DetachedCriteria; |
|
|
|
import org.springframework.dao.DataAccessException; |
|
|
|
import org.springframework.dao.DataAccessException; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
|
|
|
|
import web.service.BaseService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.ParameterizedType; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Created by reborn on 2017/9/14. |
|
|
|
* Created by reborn on 2017/9/14. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class BaseController<T extends AbstractModel,E extends BaseService>{ |
|
|
|
public abstract class BaseController<T extends AbstractModel,A extends T,E extends BaseService>{ |
|
|
|
protected static Logger log=LogManager.getLogger(); |
|
|
|
protected static Logger log=LogManager.getLogger(); |
|
|
|
|
|
|
|
|
|
|
|
protected E service; |
|
|
|
protected E service; |
|
|
|
|
|
|
|
|
|
|
|
protected abstract void setService(E service); |
|
|
|
protected abstract void setService(E service); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Class<T> tClass; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected final String tableName; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseController() { |
|
|
|
|
|
|
|
TableInfo tableInfo=HibernateConfig.getTableNote(getCommandClass()); |
|
|
|
|
|
|
|
if(tableInfo!=null&&StringUtils.isNotEmpty(tableInfo.comment())){ |
|
|
|
|
|
|
|
tableName=tableInfo.comment(); |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
tableName=getCommandClass().getName(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ResponseBody |
|
|
|
@ResponseBody |
|
|
|
@RequestMapping(value = "{action}", produces = MediaType.APPLICATION_JSON_VALUE) |
|
|
|
@RequestMapping(value = "{action}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
|
|
|
public <T extends AbstractModel> JsonResult curd(@PathVariable DBAction action, T model) { |
|
|
|
public JsonResult curd(@PathVariable DBAction action, T model) { |
|
|
|
log.info(model.tableNote()+ "进行" + action.getCh() + "操作请求"); |
|
|
|
log.info(tableName+ "进行" + action.getCh() + "操作请求"); |
|
|
|
if(checkAction(action)) { |
|
|
|
if(checkAction(action)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return new JsonResult<>(service.curd(action, model), Type.success, action); |
|
|
|
return new JsonResult<>(service.curd(action, model), Type.success, action); |
|
|
@ -36,28 +56,44 @@ public abstract class BaseController<T extends AbstractModel,E extends BaseServi |
|
|
|
return new JsonResult<>("非法操作", Type.fail, action); |
|
|
|
return new JsonResult<>("非法操作", Type.fail, action); |
|
|
|
} |
|
|
|
} |
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
return new JsonResult<>(model.tableNote() + "不允许" + action.getCh() + "操作", Type.fail,action); |
|
|
|
return new JsonResult<>(tableName + "不允许" + action.getCh() + "操作", Type.fail,action); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ResponseBody |
|
|
|
@ResponseBody |
|
|
|
@RequestMapping(value = "batch/{action}", produces = MediaType.APPLICATION_JSON_VALUE) |
|
|
|
@RequestMapping(value = "batch/{action}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
|
|
|
public <T extends AbstractModel> JsonResult find(@PathVariable DBAction action, T queryCommand,@RequestParam Integer firstResult,@RequestParam Integer maxResults){ |
|
|
|
public JsonResult find(@PathVariable DBAction action, A queryCommand, @RequestParam Integer firstResult, @RequestParam Integer maxResults){ |
|
|
|
log.info(queryCommand.tableNote()+ "进行批量" + action.getCh() + "操作请求"); |
|
|
|
log.info(tableName + "进行批量" + action.getCh() + "操作请求"); |
|
|
|
if(checkAction(action)) { |
|
|
|
if(checkAction(action)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return new JsonResult<>(service.find(queryCommand, firstResult, maxResults), Type.success, action); |
|
|
|
|
|
|
|
|
|
|
|
return new JsonResult<>(service.find(getDetachedCriteria(queryCommand), firstResult, maxResults), Type.success, action); |
|
|
|
} catch (DataAccessException e) { |
|
|
|
} catch (DataAccessException e) { |
|
|
|
log.error(e); |
|
|
|
log.error(e); |
|
|
|
return new JsonResult<>("非法操作", Type.fail, action); |
|
|
|
return new JsonResult<>("非法操作", Type.fail, action); |
|
|
|
} |
|
|
|
} |
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
return new JsonResult<>(queryCommand.tableNote() + "不允许" + action.getCh() + "操作", Type.fail,action); |
|
|
|
return new JsonResult<>(tableName + "不允许" + action.getCh() + "操作", Type.fail,action); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Class<T> getCommandClass(){ |
|
|
|
|
|
|
|
// log.info(ResolvableType.forField(ReflectionUtils.findField(this.getClass(),"service")));
|
|
|
|
|
|
|
|
if(tClass==null){ |
|
|
|
|
|
|
|
if(this.getClass().getGenericSuperclass() instanceof ParameterizedType){ |
|
|
|
|
|
|
|
tClass=(Class<T>) ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
|
|
|
|
|
|
|
}else{ |
|
|
|
|
|
|
|
tClass= (Class<T>) AbstractModel.class; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return tClass; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected DetachedCriteria getDetachedCriteria(A queryCommand){ |
|
|
|
|
|
|
|
return DetachedCriteria.forClass(getCommandClass()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected abstract boolean checkAction(DBAction action); |
|
|
|
protected abstract boolean checkAction(DBAction action); |
|
|
|
} |
|
|
|
} |
|
|
|