问题遇到的现象和发生背景
现由前端传查询参数,后端判断参数是否为空,进行sql语句拼接
if判断条件过多,是否可优化
问题相关代码
String userName = "";
String userFlag = Problem.REVIEWER;
JSONObject json = JSONObject.parseObject(data);
String flag = json.getString("flag");
if ("modify".equals(flag)) {
userFlag = Problem.MODIFIER;
} else if ("recheck".equals(flag)) {
userFlag = Problem.RECHECKER;
}
String requestNum = json.getString("requestNum");
String projectNum = json.getString("projectNum");
String stationNum = json.getString("stationNum");
String problemDesc = json.getString("problem");
String advice = json.getString("advice");
String problemType = json.getString("problemType");
String reason = json.getString("reason");
String reviewStartTime = json.getString("reviewStartTime");// 审核开始时间
String reviewEndTime = json.getString("reviewEndTime");// 审核结束时间
String modifyStartTime = json.getString("modifyStartTime");// 修改开始时间
String modifyEndTime = json.getString("modifyEndTime");// 修改结束时间
String recheckStartTime = json.getString("recheckStartTime");// 复核开始时间
String recheckEndTime = json.getString("recheckEndTime");// 复核结束时间
String reviewer = json.getString("reviewer");
String modifier = json.getString("modifier");
String rechecker = json.getString("rechecker");
String whetherModify = json.getString("whetherModify");
String whetherModify1 = json.getString("whetherModify1");
String whetherModify2 = json.getString("whetherModify2");
String whetherPass = json.getString("whetherPass");
String whetherPass1 = json.getString("whetherPass1");
String whetherPass2 = json.getString("whetherPass2");
String notModifyReason = json.getString("notModifyReason");
String notModifyReason1 = json.getString("notModifyReason1");
String notModifyReason2 = json.getString("notModifyReason2");
String notPassReason = json.getString("notPassReason");
String notPassReason1 = json.getString("notPassReason1");
String notPassReason2 = json.getString("notPassReason2");
QuerySpec qs = new QuerySpec(Problem.class);
SearchCondition sc1 = new SearchCondition(Problem.class, userFlag, SearchCondition.EQUAL, userName);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
SimpleDateFormat daf = new SimpleDateFormat("yyyy-MM-dd");
Date toDate = null;
if (StringUtils.isNotBlank(notPassReason)) {
sc1 = new SearchCondition(Problem.class, Problem.NOT_PASS_REASON, SearchCondition.EQUAL,
notPassReason);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(notPassReason1)) {
sc1 = new SearchCondition(Problem.class, Problem.NOT_PASS_REASON1, SearchCondition.EQUAL,
notPassReason1);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(notPassReason2)) {
sc1 = new SearchCondition(Problem.class, Problem.NOT_PASS_REASON2, SearchCondition.EQUAL,
notPassReason2);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(notModifyReason)) {
sc1 = new SearchCondition(Problem.class, Problem.NOT_MODIFY_REASON, SearchCondition.EQUAL,
notModifyReason);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(notModifyReason1)) {
sc1 = new SearchCondition(Problem.class, Problem.NOT_MODIFY_REASON1, SearchCondition.EQUAL,
notModifyReason1);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(notModifyReason2)) {
sc1 = new SearchCondition(Problem.class, Problem.NOT_MODIFY_REASON2, SearchCondition.EQUAL,
notModifyReason2);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(whetherModify)) {
sc1 = new SearchCondition(Problem.class, Problem.WHETHER_MODIFY, SearchCondition.EQUAL,
whetherModify);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(whetherModify2)) {
sc1 = new SearchCondition(Problem.class, Problem.WHETHER_MODIFY1, SearchCondition.EQUAL,
whetherModify1);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(whetherModify2)) {
sc1 = new SearchCondition(Problem.class, Problem.WHETHER_MODIFY2, SearchCondition.EQUAL,
whetherModify1);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(whetherPass)) {
sc1 = new SearchCondition(Problem.class, Problem.WHETHER_PASS, SearchCondition.EQUAL, whetherPass);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(whetherPass1)) {
sc1 = new SearchCondition(Problem.class, Problem.WHETHER_PASS1, SearchCondition.EQUAL, whetherPass1);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(whetherPass2)) {
sc1 = new SearchCondition(Problem.class, Problem.WHETHER_PASS2, SearchCondition.EQUAL, whetherPass2);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(reviewStartTime)) {
toDate = daf.parse(reviewStartTime);
sc1 = new SearchCondition(Problem.class, Problem.REVIEW_TIME, SearchCondition.GREATER_THAN_OR_EQUAL,
new Timestamp(toDate.getTime()));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
if (StringUtils.isNotBlank(reviewEndTime)) {
toDate = daf.parse(reviewEndTime);
sc1 = new SearchCondition(Problem.class, Problem.REVIEW_TIME, SearchCondition.LESS_THAN,
new Timestamp(toDate.getTime()));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
}
if (StringUtils.isNotBlank(modifyStartTime)) {
toDate = daf.parse(modifyStartTime);
sc1 = new SearchCondition(Problem.class, Problem.MODIFY_TIME, SearchCondition.GREATER_THAN_OR_EQUAL,
new Timestamp(toDate.getTime()));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
if (StringUtils.isNotBlank(modifyEndTime)) {
toDate = daf.parse(modifyEndTime);
sc1 = new SearchCondition(Problem.class, Problem.MODIFY_TIME, SearchCondition.LESS_THAN,
new Timestamp(toDate.getTime()));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
}
if (StringUtils.isNotBlank(recheckStartTime)) {
toDate = daf.parse(recheckStartTime);
sc1 = new SearchCondition(Problem.class, Problem.RECHECK_TIME, SearchCondition.GREATER_THAN_OR_EQUAL,
new Timestamp(toDate.getTime()));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
if (StringUtils.isNotBlank(recheckEndTime)) {
toDate = daf.parse(recheckEndTime);
sc1 = new SearchCondition(Problem.class, Problem.RECHECK_TIME, SearchCondition.LESS_THAN,
new Timestamp(toDate.getTime()));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
}
if (StringUtils.isNotBlank(reviewer)) {
sc1 = new SearchCondition(Problem.class, Problem.REVIEWER, SearchCondition.LIKE,
"%" + reviewer + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(modifier)) {
sc1 = new SearchCondition(Problem.class, Problem.MODIFIER, SearchCondition.LIKE,
"%" + modifier + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(rechecker)) {
sc1 = new SearchCondition(Problem.class, Problem.RECHECKER, SearchCondition.LIKE,
"%" + rechecker + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(requestNum) && StringUtils.isNumeric(requestNum)) {
sc1 = new SearchCondition(Problem.class, Problem.REQUEST_NUM, SearchCondition.EQUAL,
Long.valueOf(requestNum));
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(projectNum)) {
sc1 = new SearchCondition(Problem.class, Problem.PROJECT_NUM, SearchCondition.EQUAL, projectNum);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(problemDesc)) {
sc1 = new SearchCondition(Problem.class, Problem.PROBLEM, SearchCondition.LIKE,
"%" + problemDesc + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(advice)) {
sc1 = new SearchCondition(Problem.class, Problem.ADVISES, SearchCondition.LIKE, "%" + advice + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(problemType)) {
sc1 = new SearchCondition(Problem.class, Problem.PROBLEM_TYPE, SearchCondition.LIKE,
"%" + problemType + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(reason)) {
sc1 = new SearchCondition(Problem.class, Problem.PROBLEM_REASON, SearchCondition.LIKE,
"%" + reason + "%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isNotBlank(stationNum)) {
sc1 = new SearchCondition(Problem.class, Problem.STATION_NUM, SearchCondition.EQUAL, stationNum);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
if (StringUtils.isBlank(flag)) {
sc1 = new SearchCondition(new KeywordExpression(Problem.MODIFIER), SearchCondition.IS_NULL);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
} else if ("recheck".equals(flag)) {// recheck
sc1 = new SearchCondition(Problem.class, Problem.STATE, SearchCondition.LIKE, "recheck%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
} else if ("modify".equals(flag)) {
sc1 = new SearchCondition(new KeywordExpression(Problem.MODIFIER), SearchCondition.NOT_NULL);
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
sc1 = new SearchCondition(Problem.class, Problem.STATE, SearchCondition.LIKE, "modify%");
qs.appendWhere(sc1, new int[] { 0 });
qs.appendAnd();
}
我想要达到的结果
是否有更优的设计模式?