You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
package core.service;
|
|
|
|
import core.dao.BaseDao1;
|
|
import core.dao.BaseDao2;
|
|
import core.util.DBAction;
|
|
import db.model.AbstractModel;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.Serializable;
|
|
|
|
/**
|
|
* Created by reborn on 2017/7/28.
|
|
*/
|
|
@Service
|
|
public class BaseService {
|
|
|
|
protected Logger log=LogManager.getLogger();
|
|
@Resource
|
|
protected BaseDao1 baseDao1;
|
|
|
|
@Resource
|
|
protected BaseDao2 baseDao2;
|
|
|
|
public<T extends AbstractModel> String curd(DBAction action, T command) {
|
|
Class<T> tClass = (Class<T>) command.getClass();
|
|
try {
|
|
switch (action) {
|
|
case C:
|
|
case U:
|
|
baseDao1.saveOrUpdate(command);
|
|
break;
|
|
case R:
|
|
if(command.primaryKey()!=null) {
|
|
T model = baseDao1.get(tClass, command.primaryKey());
|
|
return model == null ? "找不到主键=" + command.primaryKey() + "" : model.toString();
|
|
}else{
|
|
throw new RuntimeException(command.getClass()+".primaryKey()方法返回null");
|
|
}
|
|
case D:
|
|
if(command.getCheckeds()==null) {
|
|
baseDao1.delete(tClass, command.primaryKey());
|
|
}else{
|
|
for (Serializable checked:command.getCheckeds()){
|
|
baseDao1.delete(tClass,checked);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
return action.getEn() + " success";
|
|
} catch (Exception e) {
|
|
log.error((command.tableNote()==null?command.getClass():command.tableNote()) + "进行" + action.getCh() + "操作失败,原因是:" + e.getMessage());
|
|
}
|
|
return action.getEn() + " fail";
|
|
}
|
|
|
|
|
|
}
|
|
|