/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * 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 tk.mybatis.mapper.provider;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.EntityTable;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 * SpecialProvider实现类，特殊方法实现类
 *
 * @author liuzh
 */
public class SpecialProvider extends MapperTemplate {

    public SpecialProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 批量插入
     *
     * @param ms
     */
    public String insertList(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        EntityTable table = EntityHelper.getEntityTable(entityClass);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
        sql.append(" VALUES ");
        sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
        sql.append("(");
        boolean first = true;
        for (EntityColumn column : table.getEntityClassColumns()) {
            if (column.isId()) {
                continue;
            }
            if (!first) {
                sql.append(",");
            }
            sql.append(column.getColumnHolder("record"));
            first = false;
        }
        sql.append(")");
        sql.append("</foreach>");
        return sql.toString();
    }

    /**
     * 插入，主键id，自增
     *
     * @param ms
     */
    public String insertUseGeneratedKeys(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        //开始拼sql
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
        sql.append(SqlHelper.insertValuesColumns(entityClass, true, false, false));
        return sql.toString();
    }
}

@@ -1535,16 +1535,39 @@
 elper;%0A%0A
+import java.util.Set;%0A%0A
 /**%0A * S
@@ -1962,78 +1962,8 @@
 s);%0A
-        EntityTable table = EntityHelper.getEntityTable(entityClass);%0A
     
@@ -2328,42 +2328,88 @@
 nd(%22
-(%22);%0A        boolean first = true;
+%3Ctrim prefix=%5C%22VALUES (%5C%22 suffix=%5C%22)%5C%22 suffixOverrides=%5C%22,%5C%22%3E%22);%0A        //%E8%8E%B7%E5%8F%96%E5%85%A8%E9%83%A8%E5%88%97
 %0A   
@@ -2417,13 +2417,12 @@
     
-for (
+Set%3C
 Enti
@@ -2433,197 +2433,232 @@
 lumn
+%3E
  column
- : table.getEntityClassColumns()) %7B%0A            if (column.isId()) %7B%0A                continue;%0A            %7D%0A            if (!first) %7B%0A                sql.append(%22,%22);%0A            %7D%0A
+List = EntityHelper.getColumns(entityClass);%0A        //%E5%BD%93%E6%9F%90%E4%B8%AA%E5%88%97%E6%9C%89%E4%B8%BB%E9%94%AE%E7%AD%96%E7%95%A5%E6%97%B6%EF%BC%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E8%80%83%E8%99%91%E4%BB%96%E7%9A%84%E5%B1%9E%E6%80%A7%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%EF%BC%8C%E5%9B%A0%E4%B8%BA%E5%A6%82%E6%9E%9C%E4%B8%BA%E7%A9%BA%EF%BC%8C%E4%B8%80%E5%AE%9A%E4%BC%9A%E6%A0%B9%E6%8D%AE%E4%B8%BB%E9%94%AE%E7%AD%96%E7%95%A5%E7%BB%99%E4%BB%96%E7%94%9F%E6%88%90%E4%B8%80%E4%B8%AA%E5%80%BC%0A        for (EntityColumn column : columnList) %7B%0A            if (!column.isId() && column.isInsertable()) %7B%0A    
     
@@ -2708,16 +2708,22 @@
 record%22)
+ + %22,%22
 );%0A     
@@ -2733,22 +2733,9 @@
     
-first = false;
+%7D
 %0A   
@@ -2761,17 +2761,23 @@
 append(%22
-)
+%3C/trim%3E
 %22);%0A    

解决SpecialProvider不支持insertable的bug#77