<?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="${ClassName}VO" id="${ClassName}Result" autoMapping="true">
#foreach ($column in $columns)
        <result property="${column.javaField}"    column="${column.columnName}"    />
#end
    </resultMap>
#if($table.sub)

    <resultMap id="${ClassName}${subClassName}Result" type="${ClassName}" extends="${ClassName}Result">
        <collection property="${subclassName}List" notNullColumn="sub_${subTable.pkColumn.columnName}" javaType="java.util.List" resultMap="${subClassName}Result" />
    </resultMap>

    <resultMap type="${subClassName}" id="${subClassName}Result">
#foreach ($column in $subTable.columns)
        <result property="${column.javaField}"    column="sub_${column.columnName}"    />
#end
    </resultMap>
#end

    <sql id="select${ClassName}Vo">
        select
        #foreach($column in $columns)
            $column.columnName#if($foreach.count != $columns.size()),#end
        #end
        from ${tableName}
    </sql>

    <sql id="searchCondition">
    #foreach($column in $columns)
    #set($queryType=$column.queryType)
    #set($javaField=$column.javaField)
    #set($javaType=$column.javaType)
    #set($columnName=$column.columnName)
    #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
    #if($column.query)
    #if($column.queryType == "EQ")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName = #{query.$javaField}</if>
    #elseif($queryType == "NE")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName != #{query.$javaField}</if>
    #elseif($queryType == "GT")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName &gt; #{query.$javaField}</if>
    #elseif($queryType == "GTE")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName &gt;= #{query.$javaField}</if>
    #elseif($queryType == "LT")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName &lt; #{query.$javaField}</if>
    #elseif($queryType == "LTE")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName &lt;= #{query.$javaField}</if>
    #elseif($queryType == "LIKE")
        <if test="query.$javaField != null #if($javaType == 'String' ) and query.$javaField.trim() != ''#end"> and $columnName like concat('%', #{query.$javaField}, '%')</if>
    #elseif($queryType == "BETWEEN")
        <if test="query.params.begin$AttrName != null and query.params.begin$AttrName != '' and query.params.end$AttrName != null and query.params.end$AttrName != ''"> and $columnName between #{query.params.begin$AttrName} and #{query.params.end$AttrName}</if>
    #end
    #end
    #end
        ${query.params.dataScope}
    </sql>

    <select id="select${ClassName}List" parameterType="${ClassName}Query" resultMap="${ClassName}Result">
        <include refid="select${ClassName}Vo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="select${ClassName}By${pkColumn.capJavaField}" parameterType="${pkColumn.javaType}" resultMap="#if($table.sub)${ClassName}${subClassName}Result#else${ClassName}Result#end">
#if($table.crud || $table.tree)
        <include refid="select${ClassName}Vo"/>
        where ${pkColumn.columnName} = #{${pkColumn.javaField}}
#elseif($table.sub)
        select#foreach($column in $columns) a.$column.columnName#if($foreach.count != $columns.size()),#end#end,
           #foreach($column in $subTable.columns) b.$column.columnName as sub_$column.columnName#if($foreach.count != $subTable.columns.size()),#end#end

        from ${tableName} a
        left join ${subTableName} b on b.${subTableFkName} = a.${pkColumn.columnName}
        where a.${pkColumn.columnName} = #{${pkColumn.javaField}}
#end
    </select>

    <insert id="insert${ClassName}" parameterType="${ClassName}"#if($pkColumn.increment) useGeneratedKeys="true" keyProperty="$pkColumn.javaField"#end>
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in $columns)
#if($column.columnName != $pkColumn.columnName || !$pkColumn.increment)
            <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">$column.columnName,</if>
#end
#end
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
#foreach($column in $columns)
#if($column.columnName != $pkColumn.columnName || !$pkColumn.increment)
            <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">#{$column.javaField},</if>
#end
#end
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="${ClassName}"#if($pkColumn.increment) useGeneratedKeys="true" keyProperty="$pkColumn.javaField"#end>
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #foreach($column in $columns)
                #if($column.columnName != $pkColumn.columnName || !$pkColumn.increment)
            $column.columnName,
                #end
            #end
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
            #foreach($column in $columns)
                #if($column.columnName != $pkColumn.columnName || !$pkColumn.increment)
                <if test="i.$column.javaField != null #if($column.javaType == 'String' && $column.required) and i.$column.javaField != ''#end">#{i.$column.javaField},</if>
                <if test="i.$column.javaField == null #if($column.javaType == 'String' && $column.required) or i.$column.javaField == ''#end">default,</if>
                #end
            #end
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update ${tableName}
        <trim prefix="SET" suffixOverrides=",">
            #foreach($column in $columns)
                #if($column.columnName != $pkColumn.columnName || !$pkColumn.increment)
            <foreach open="$column.columnName = CASE $pkColumn.columnName" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.$column.javaField != null #if($column.javaType == 'String' && $column.required) and item.$column.javaField != ''#end">
                        WHEN #{item.$pkColumn.columnName} THEN #{item.$column.javaField}
                    </when>
                    <otherwise>
                        WHEN #{item.$pkColumn.columnName} THEN `$column.columnName`
                    </otherwise>
                </choose>
            </foreach>
                #end
            #end
        </trim>
        where $pkColumn.columnName in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.$pkColumn.javaField}
        </foreach>
    </update>

    <update id="update${ClassName}" parameterType="${ClassName}">
        update ${tableName}
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where ${pkColumn.columnName} = #{data.${pkColumn.javaField}}
    </update>

    <sql id="updateColumns">
#foreach($column in $columns)
    #if($column.columnName != $pkColumn.columnName)
        <if test="data.$column.javaField != null#if($column.javaType == 'String' && $column.required) and data.$column.javaField != ''#end">$column.columnName = #{data.$column.javaField},</if>
    #end
#end
    </sql>

    <delete id="delete${ClassName}By${pkColumn.capJavaField}" parameterType="${pkColumn.javaType}">
        delete from ${tableName} where ${pkColumn.columnName} = #{${pkColumn.javaField}}
    </delete>

    <delete id="delete${ClassName}By${pkColumn.capJavaField}s" parameterType="String">
        delete from ${tableName} where ${pkColumn.columnName} in
        <foreach item="${pkColumn.javaField}" collection="array" open="(" separator="," close=")">
            #{${pkColumn.javaField}}
        </foreach>
    </delete>
#if($table.sub)

    <delete id="delete${subClassName}By${subTableFkClassName}s" parameterType="String">
        delete from ${subTableName} where ${subTableFkName} in
        <foreach item="${subTableFkclassName}" collection="array" open="(" separator="," close=")">
            #{${subTableFkclassName}}
        </foreach>
    </delete>

    <delete id="delete${subClassName}By${subTableFkClassName}" parameterType="${pkColumn.javaType}">
        delete from ${subTableName} where ${subTableFkName} = #{${subTableFkclassName}}
    </delete>

    <insert id="batch${subClassName}">
        insert into ${subTableName}(#foreach($column in $subTable.columns) $column.columnName#if($foreach.count != $subTable.columns.size()),#end#end) values
		<foreach item="item" index="index" collection="list" separator=",">
            (#foreach($column in $subTable.columns) #{item.$column.javaField}#if($foreach.count != $subTable.columns.size()),#end#end)
        </foreach>
    </insert>
#end
</mapper>
