<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.mapper.${ClassName}Mapper">

    <resultMap type="${packageName}.domain.vo.${ClassName}Vo" id="${ClassName}Result">
    </resultMap>

    <select id="selectCustom${ClassName}VoList" resultMap="${ClassName}Result">
        select #foreach($column in $columns)t.$column.columnName#if($foreach.count != $columns.size()), #end#end
        from ${tableName} t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 根据ID查询详情 -->
    <select id="selectCustom${ClassName}VoById" resultMap="${ClassName}Result">
        select #foreach($column in $columns)t.$column.columnName#if($foreach.count != $columns.size()), #end#end
        from ${tableName} t
        where t.${pkColumn.columnName} = #{${pkColumn.javaField}}
    </select>

    <!-- 批量查询 - 根据ID列表 -->
    <select id="selectCustom${ClassName}VoByIds" resultMap="${ClassName}Result">
        select #foreach($column in $columns)t.$column.columnName#if($foreach.count != $columns.size()), #end#end
        from ${tableName} t
        where t.${pkColumn.columnName} in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 统计查询 -->
    <select id="countCustom${ClassName}" resultType="java.lang.Long">
        select count(1) from ${tableName} t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 分页查询（带自定义条件） -->
    <select id="selectCustom${ClassName}VoPage" resultMap="${ClassName}Result">
        select #foreach($column in $columns)t.$column.columnName#if($foreach.count != $columns.size()), #end#end
        from ${tableName} t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 批量插入 -->
    <insert id="batchInsert${ClassName}">
        insert into ${tableName}(
#foreach($column in $columns)
#if(!$column.increment)
            $column.columnName#if($foreach.count != $columns.size()),#end

#end
#end
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
#foreach($column in $columns)
#if(!$column.increment)
            #{item.$column.javaField}#if($foreach.count != $columns.size()),#end

#end
#end
            )
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="batchUpdate${ClassName}">
        <foreach collection="list" item="item" separator=";">
            update ${tableName}
            <set>
#foreach($column in $columns)
#if(!$column.pk && !$column.increment)
                <if test="item.$column.javaField != null#if($column.javaType == 'String') and item.$column.javaField != ''#end">
                    $column.columnName = #{item.$column.javaField}#if($foreach.count != $columns.size()),#end
                </if>
#end
#end
            </set>
            where ${pkColumn.columnName} = #{item.${pkColumn.javaField}}
        </foreach>
    </update>

    <!-- 根据自定义条件删除 -->
    <delete id="deleteCustom${ClassName}">
        delete from ${tableName}
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </delete>

    <!-- 根据ID列表批量删除 -->
    <delete id="deleteCustom${ClassName}ByIds">
        delete from ${tableName}
        where ${pkColumn.columnName} in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 检查是否存在 -->
    <select id="exists${ClassName}" resultType="java.lang.Boolean">
        select count(1) > 0 from ${tableName} t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

#if($table.sub)
    <!-- 批量插入子表数据 -->
    <insert id="batch${subClassName}">
        insert into ${subTableName}(
#foreach($column in $subTable.columns)
#if(!$column.increment)
            $column.columnName#if($foreach.count != $subTable.columns.size()),#end

#end
#end
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
#foreach($column in $subTable.columns)
#if(!$column.increment)
            #{item.$column.javaField}#if($foreach.count != $subTable.columns.size()),#end

#end
#end
            )
        </foreach>
    </insert>
#end

</mapper>
