通过配置实现不同逻辑
思路
将逻辑不同的部分代码提取出来,放入到不同的方法中,在数据库存入方法名,根据方法名反射出方法。
将不同规则代码放入不同方法
@Controller
@Description("取号规则方法")
@RequestMapping("callRule")
public class CallNumberUtil {
@Resource
private CallnumberConfigDao callnumberConfigDao;
private static CallnumberConfigDao callDao;
@Resource
private WXSquenceInfoDao wxSquenceInfoDao;
private static WXSquenceInfoDao wxDao;
@PostConstruct
public void init(){
callDao = this.callnumberConfigDao;
wxDao=this.wxSquenceInfoDao;
}
@Description("方案一,取号递增一")
@RequestMapping("getnextSquence")
public static String getnextSquence(int isappointment, String date,
Integer isearly, Long deptid, Long businessType) {
String squence = wxDao.getSquenceNew(isappointment, date,
isearly, deptid.intValue(),businessType.intValue());
System.out.println("目前最大squence是:" + squence);
String head = "";
String perfix = null;
Callnumber_config callConfig =callDao.getObjectByBtype(businessType,deptid);
perfix =callConfig.getPerfix();
if(StringUtils.isBlank(perfix)) {
perfix = "Z";
}
if (isappointment == 0) {//5个业务分别五个字母开头
head = perfix;
} else {
head = "Y" + perfix;
}
//这里需要再次查询一边这个字母最大的号
squence = wxDao.getSquenceNew2(isappointment, date,
isearly, deptid.intValue(),head);
System.out.println("目前真正最大squence是:" + squence);
int squ = 1;
if (squence != null && !squence.equals("")) {//B001
if(isappointment == 0) {
squ = Integer.parseInt(squence.substring(1)) + 1;
}else {
squ = Integer.parseInt(squence.substring(2)) + 1;
}
}
StringBuffer next = new StringBuffer(head);
int i = String.valueOf(squ).length();
for (int j = i; j < 3; j++) {
next.append("0");
}
next.append(squ);
return next.toString();
}
@Description("方案二,取号递增二")
@RequestMapping("getnextSquence2")
public static String getNextSquens2(int isappointment, String date,
Integer isearly, Long deptid, Long businessType) {
String squence = wxDao.getSquenceNew(isappointment, date,
isearly, deptid.intValue(),businessType.intValue());
System.out.println("目前最大squence是:" + squence);
String head = "";
String perfix = null;
Callnumber_config callConfig =callDao.getObjectByBtype(businessType,deptid);
if(callConfig!=null){
perfix =callConfig.getPerfix();
if(StringUtils.isBlank(perfix)) {
perfix = "Z";
}
}else {
perfix = "Z";
}
if (isappointment == 0) {//5个业务分别五个字母开头
head = perfix;
} else {
head = "Y" + perfix;
}
//这里需要再次查询一边这个字母最大的号
squence = wxDao.getSquenceNew2(isappointment, date,
isearly, deptid.intValue(),head);
System.out.println("目前真正最大squence是:" + squence);
int squ = 50;
if (squence != null && !squence.equals("")) {//B001
if(isappointment == 0) {
squ = Integer.parseInt(squence.substring(1)) + 2;
}else {
squ = Integer.parseInt(squence.substring(2)) + 2;
}
}
StringBuffer next = new StringBuffer(head);
int i = String.valueOf(squ).length();
for (int j = i; j < 3; j++) {
next.append("0");
}
next.append(squ);
return next.toString();
}
}
读取数据库配置表查出方法名
@Override
public String getnextSquence(int i, String dateString, Object o, Integer deptid, int businessType) {
String squens=null;
try {
//获取配置表中的方法名
Callnumber_config config=callnumberConfigDao.getObjectByBtype((long)businessType,deptid.longValue());
CallNumberUtil invokeTest = new CallNumberUtil();
//反射出配置类
Class c = Class.forName("com.fwy.workbench.action.CallNumberUtil");
//获取方法
Method m1 = c.getMethod(config.getCallRule(), int.class,String.class,Integer.class,Long.class,Long.class);
Object returnS = m1.invoke(invokeTest, new Object[]{i,dateString,o,config.getDeptid(),config.getBussinesstype()});
squens=(String) returnS;
}catch (Exception e){
e.printStackTrace();
}
return squens;
}
获取配置工具类中的配置方法
Controller
@Description("动作列表Json")
@RequestMapping("actionJson")
@ResponseBody
public List<SelectHelper> actionJson(String controlValue, String actionValue) {
List<SelectHelper> actionList = new ArrayList<>();
try {
actionList = ControlManager
.getActionList(controlValue);
if(StringUtils.isNotBlank(actionValue)) {
for(SelectHelper helper : actionList) {
if(actionValue.equals(helper.getValue())) {
helper.setSelected(true);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return actionList;
}
获取动作列表
/**
* 控制器管理器
*
* @author USER
*
*/
@Component("controlManager")
public class ControlManager {
private static volatile List<Action> controllers;
private static LoadPackageClasses loadPackageClasses;
private static IAction actionList;
/**
* 获得控制器动作的集合
*
* @return
*/
public static List<Action> getControllers() {
if (controllers == null) {
synchronized (ControlManager.class) {
if (controllers == null) {
try {
Set<Class<?>> classSet = loadPackageClasses
.getClassSet();
controllers = actionList.getAction(classSet);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return controllers;
}
@Resource
public void setLoadPackageClasses(LoadPackageClasses loadPackageClasses) {
ControlManager.loadPackageClasses = loadPackageClasses;
}
@Resource
public void setActionList(IAction actionList) {
ControlManager.actionList = actionList;
}
public static List<SelectHelper> getControllerList(String areaValue) {
List<SelectHelper> list = new ArrayList<SelectHelper>();
List<Action> actionList = getControllers();
for (Action action : actionList) {
if (action.getArea().equals(areaValue)) {
SelectHelper select = new SelectHelper();
select.setSelected(false);
select.setText(action.getName());
select.setValue(action.getUrl().get(0));
list.add(select);
}
}
return list;
}
public static List<SelectHelper> getActionList(String controlValue) {
List<SelectHelper> list = new ArrayList<SelectHelper>();
List<Action> actionList = getControllers();
for (Action action : actionList) {
if (action.getUrl().get(0).equals(controlValue)) {
List<Action> actions = action.getActions();
for (Action a : actions) {
SelectHelper select = new SelectHelper();
select.setSelected(false);
select.setText(a.getName());
select.setValue(a.getUrl().get(0));
list.add(select);
}
break;
}
}
return list;
}
public static List<String> getArea() {
List<String> list = new ArrayList<String>();
List<Action> actionList = getControllers();
for (Action action : actionList) {
String area = action.getArea();
if (!list.contains(area)) {
list.add(area);
}
}
return list;
}
}
SelectHelper类
@data
public class SelectHelper {
@JsonProperty("Selected")
private Boolean selected;
@JsonProperty("Text")
private String text;
@JsonProperty("Value")
private String value;
}
Action类
/**
* 权限动作辅助类
*
*/
public class Action implements Serializable {
/**
* 树状结构表格参数
*/
private int id;
private int pid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
/**
* 名称
*/
private String name;
/**
* 路径
*/
private List<String> url = new ArrayList<String>();
/**
* 动作列表
*/
private List<Action> actions;
/**
* 域
*/
private String area;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getUrl() {
return url;
}
public void setUrl(List<String> url) {
this.url = url;
}
public List<Action> getActions() {
return actions;
}
public void setActions(List<Action> actions) {
this.actions = actions;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}