content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.pager.binding;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.ibatis.mapping.MappedStatement;
  import org.apache.ibatis.mapping.ResultMap;
  import org.apache.ibatis.mapping.ResultMapping;
  import org.miaixz.bus.core.lang.Symbol;
  
  /**
   * 创建新的MappedStatement,主要是Count返回值int
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public class CountMappedStatement {
  
      private static final List<ResultMapping> EMPTY_RESULTMAPPING = new ArrayList<>(0);
  
      /**
       * 新建count查询的MappedStatement
       *
       * @param ms      MappedStatement
       * @param newMsId 标识
       * @return the mappedStatement
       */
      public static MappedStatement newCountMappedStatement(MappedStatement ms, String newMsId) {
          MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), newMsId, ms.getSqlSource(),
                  ms.getSqlCommandType());
          builder.resource(ms.getResource());
          builder.fetchSize(ms.getFetchSize());
          builder.statementType(ms.getStatementType());
          builder.keyGenerator(ms.getKeyGenerator());
          if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
              StringBuilder keyProperties = new StringBuilder();
              for (String keyProperty : ms.getKeyProperties()) {
                  keyProperties.append(keyProperty).append(Symbol.COMMA);
              }
              keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
              builder.keyProperty(keyProperties.toString());
          }
          builder.timeout(ms.getTimeout());
          builder.parameterMap(ms.getParameterMap());
          // count查询返回值int
          List<ResultMap> resultMaps = new ArrayList<>();
          ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, EMPTY_RESULTMAPPING)
                  .build();
          resultMaps.add(resultMap);
          builder.resultMaps(resultMaps);
          builder.resultSetType(ms.getResultSetType());
          builder.cache(ms.getCache());
          builder.flushCacheRequired(ms.isFlushCacheRequired());
          builder.useCache(ms.isUseCache());
  
          return builder.build();
      }
  
  }
  