<?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 id="${className}Result" type="${packageName}.entity.${ClassName}">
        <id column="${pkColumn.columnName}" property="${pkColumn.javaField}"/>
#foreach($column in $columns)
#if("1" != $column.isPk)
        <result column="${column.columnName}" property="${column.javaField}"/>
#end
#end
    </resultMap>
    <sql id="selectVo">
        select #foreach($column in $columns)${column.columnName}#if($velocityCount != $columns.size()), #end
#end
        from ${tableName}
    </sql>



    <select id="selectList" resultMap="${className}Result">
        <include refid="selectVo"/>
        <where>
#foreach($column in $columns)
#set($queryType=$column.queryType)
#set($javaField=$column.javaField)
#set($javaType=$column.javaType)
#set($columnName=$column.columnName)
#if("1" == $column.isQuery)
#if($column.queryType == "EQ")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName = #{$javaField}</if>
#elseif($queryType == "NE")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName != #{$javaField}</if>
#elseif($queryType == "GT")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &gt; #{$javaField}</if>
#elseif($queryType == "GTE")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &gt;= #{$javaField}</if>
#elseif($queryType == "LT")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &lt; #{$javaField}</if>
#elseif($queryType == "LTE")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &lt;= #{$javaField}</if>
#elseif($queryType == "LIKE")
            <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName like concat('%', #{$javaField}, '%')</if>
#elseif($queryType == "BETWEEN")
#set($AttrName=${table.capitalize($javaField)})
            <if test="begin${AttrName} != null and end${AttrName} != null"> and $columnName between #{begin${AttrName}} and #{end${AttrName}}</if>
#end
#end
#end
        </where>
    </select>

    <select id="selectById" resultMap="${className}Result">
        <include refid="selectVo"/>
        where ${pkColumn.columnName} = #{${pkColumn.javaField}}
    </select>

    <insert id="insert" #if($pkColumn.increment)useGeneratedKeys="true" keyProperty="$pkColumn.javaField"#end>
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in $columns)
#if(!$column.isSuperColumn($column.javaField))
#if(($column.columnName != $pkColumn.columnName || !$pkColumn.increment) && $column.isInsert == 1)
            <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">$column.columnName,</if>
#end
#end
#end
#if($table.hasCreateXMLColumns())
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
#end
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
#foreach($column in $columns)
#if(!$column.isSuperColumn($column.javaField))
#if(($column.columnName != $pkColumn.columnName || !$pkColumn.increment) && $column.isInsert == 1)
            <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">#{$column.javaField},</if>
#end
#end
#end
#if($table.hasCreateXMLColumns())
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
#end
        </trim>
    </insert>
    
    <update id="update">
        update ${tableName}
        <set>
#foreach($column in $columns)
#if(!$column.isSuperColumn($column.javaField))
#if(($column.columnName != $pkColumn.columnName || !$pkColumn.increment) && $column.isInsert == 1)
            <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">$column.columnName = #{$column.javaField},</if>
#end
#end
#end
#if($table.hasUpdateXMLColumns())
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
#end
        </set>
        where ${pkColumn.columnName} = #{${pkColumn.javaField}}
    </update>

    <delete id="deleteById">
        delete from ${tableName}
        where ${pkColumn.columnName} = #{${pkColumn.javaField}}
    </delete>

    <delete id="deleteBatchByIdList" parameterType="${pkColumn.javaType}">
        delete from ${tableName}
        where ${pkColumn.columnName} in
        <foreach collection="list" open="(" close=")" separator="," item="${pkColumn.javaField}">
            #{${pkColumn.javaField}}
        </foreach>
    </delete>

</mapper>