/*
 * Copyright (c) 2011-2020, hubin (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.mybatisplus.core.conditions.segments;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import lombok.Getter;

/**
 * <p>
 * 合并 SQL 片段
 * </p>
 *
 * @author miemie
 * @since 2018-06-27
 */
@Getter
@SuppressWarnings("serial")
public class MergeSegments implements ISqlSegment {

    private final NormalSegmentList normal = new NormalSegmentList();
    private final GroupBySegmentList groupBy = new GroupBySegmentList();
    private final HavingSegmentList having = new HavingSegmentList();
    private final OrderBySegmentList orderBy = new OrderBySegmentList();

    public void add(ISqlSegment... iSqlSegments) {
        List<ISqlSegment> list = Arrays.asList(iSqlSegments);
        ISqlSegment sqlSegment = list.get(0);
        if (MatchSegment.ORDER_BY.match(sqlSegment)) {
            orderBy.addAll(list);
        } else if (MatchSegment.GROUP_BY.match(sqlSegment)) {
            groupBy.addAll(list);
        } else if (MatchSegment.HAVING.match(sqlSegment)) {
            having.addAll(list);
        } else {
            normal.addAll(list);
        }
    }

    @Override
    public String getSqlSegment() {
        if (normal.isEmpty()) {
            if (!groupBy.isEmpty() || !orderBy.isEmpty()) {
                return groupBy.getSqlSegment() + having.getSqlSegment() + orderBy.getSqlSegment();
            } else {
                return StringPool.EMPTY;
            }
        } else {
            return normal.getSqlSegment() + groupBy.getSqlSegment() + having.getSqlSegment() + orderBy.getSqlSegment();
        }
    }

}

@@ -680,57 +680,8 @@
 s;%0A%0A
-import java.util.Arrays;%0Aimport java.util.List;%0A%0A
 impo
@@ -794,17 +794,16 @@
 ngPool;%0A
-%0A
 import l
@@ -817,16 +817,65 @@
 etter;%0A%0A
+import java.util.Arrays;%0Aimport java.util.List;%0A%0A
 /**%0A * %3C
@@ -1320,16 +1320,111 @@
 ist();%0A%0A
+    private String sqlSegment = StringPool.EMPTY;%0A    private boolean cacheSqlSegment = true;%0A%0A
     publ
@@ -1908,24 +1908,57 @@
 ;%0A        %7D%0A
+        cacheSqlSegment = false;%0A
     %7D%0A%0A    @
@@ -1998,24 +1998,128 @@
 Segment() %7B%0A
+        if (cacheSqlSegment) %7B%0A            return sqlSegment;%0A        %7D%0A        cacheSqlSegment = true;%0A
         if (
@@ -2214,22 +2214,28 @@
         
-return
+sqlSegment =
  groupBy
@@ -2307,70 +2307,8 @@
 ();%0A
-            %7D else %7B%0A                return StringPool.EMPTY;%0A
     
@@ -2346,22 +2346,28 @@
         
-return
+sqlSegment =
  normal.
@@ -2474,13 +2474,39 @@
   %7D%0A    
+    return sqlSegment;%0A    
 %7D
-%0A
 %0A%7D%0A

优化一下 `MergeSegments` 获取 `getSqlSegment` 方式