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 String curd(DBAction action, T command) { Class tClass = (Class) 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"; } }