zhenqingshi 2022-05-21 14:52 采纳率: 0%
浏览 76

多条件(if)判断的优化

问题遇到的现象和发生背景

现由前端传查询参数,后端判断参数是否为空,进行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();
        }

我想要达到的结果

是否有更优的设计模式?

  • 写回答

1条回答 默认 最新

  • 皮蛋不吃粥 2022-05-22 10:09
    关注

    你的很多处理逻辑相同的可以抽象出来作为一个方法,然后把需要处理的属性放进去;统一处理,这样可以避免很多重复的代码

    评论

报告相同问题?

问题事件

  • 创建了问题 5月21日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀