<?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="com.izouma.awesomeadmin.dao.${model.className}Mapper">
    <resultMap id="BaseResultMap" type="com.izouma.awesomeadmin.model.${model.className}">
        #foreach($field in $model.fields)
            #if(${field.name}=='id'||${field.name}=='ID')
                <id column="id" property="id" jdbcType="INTEGER"/>
            #end
            #if(${field.name}!='id'&&${field.name}!='ID')
                <result column="${field.name}" property="$field.modelName" jdbcType="$field.sqlType"/>
            #end
        #end
    </resultMap>
    <sql id="Base_Column_List">
        #foreach($field in $model.fields)
            ${field.name}#if($velocityCount!=$model.fields.size()),#end
        #end
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from $model.tableName
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from $model.tableName
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insertSelective" parameterType="com.izouma.awesomeadmin.model.${model.className}"
            useGeneratedKeys="true" keyProperty="id">
        insert into $model.tableName
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #foreach($field in $model.fields)
                #if(${field.name}!='id'&&${field.name}!='ID')
                    <if test="$field.modelName!= null">
                        ${field.name},
                    </if>
                #end
            #end
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #foreach($field in $model.fields)
                #if(${field.name}!='id'&&${field.name}!='ID')
                    <if test="$field.modelName != null">
                        #{$field.modelName,jdbcType=${field.sqlType}},
                    </if>
                #end
            #end
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.izouma.awesomeadmin.model.${model.className}">
        update $model.tableName
        <set>
            #foreach($field in $model.fields)
                #if(${field.name}!='id'&&${field.name}!='ID')
                    <if test="$field.modelName != null">
                        ${field.name}= #{$field.modelName,jdbcType=${field.sqlType}},
                    </if>
                #end
            #end
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <select id="query${model.className}ByPage" parameterType="java.util.Map"
            resultType="com.izouma.awesomeadmin.model.${model.className}">
        select
        <include refid="Base_Column_List"/>
        from ${model.tableName}
        <where>
            and FlagColumn = 0
            #foreach($field in $model.fields)
                <if test="record.$field.modelName != null and !&quot;&quot;.equals(record.$field.modelName)">
                    and  ${field.name} = #{record.$field.modelName}
                </if>
            #end

            <if test="record.searchKey != null and !&quot;&quot;.equals(record.searchKey)">
                <trim prefix="and (" suffix=")" prefixOverrides="OR" >
                    #foreach($field in $model.fields)
                        #if(${field.name}!='id'&&${field.name}!='ID')
                            OR  ${field.name} LIKE concat('%',#{record.searchKey},'%')
                        #end
                    #end
                </trim>
            </if>
            <if test="record.advancedQuery != null and !&quot;&quot;.equals(record.advancedQuery)">
                <foreach item="item" index="index" collection="record.advancedQuery.split('_;')">
                    <choose>
                        <when test="item.indexOf('like') != -1">
                            <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                                <if test="detailIndex == 3">
                                    concat('%',#{itemDetail},'%')
                                </if>

                                <if test="detailIndex &lt; 3">
                                    ${esc.d}{itemDetail}
                                </if>

                            </foreach>
                        </when>
                        <otherwise>
                            <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                                <if test="detailIndex == 3">
                                    #{itemDetail}
                                </if>

                                <if test="detailIndex &lt; 3">
                                    ${esc.d}{itemDetail}
                                </if>

                            </foreach>
                        </otherwise>
                    </choose>


                </foreach>


            </if>
        </where>
        order by id desc
    </select>
    <select id="queryAll${model.className}" parameterType="java.util.Map"
            resultType="com.izouma.awesomeadmin.model.${model.className}">
        select
        <include refid="Base_Column_List"/>
        from ${model.tableName}
        <where>
            and FlagColumn = 0
            #foreach($field in $model.fields)
                <if test="$field.modelName != null and !&quot;&quot;.equals($field.modelName)">
                    and  ${field.name} = #{$field.modelName}
                </if>
            #end

            <if test="searchKey != null and !&quot;&quot;.equals(searchKey)">
                <trim prefix="and (" suffix=")" prefixOverrides="OR" >
                    #foreach($field in $model.fields)
                        #if(${field.name}!='id'&&${field.name}!='ID')
                            OR  ${field.name} LIKE concat('%',#{searchKey},'%')
                        #end
                    #end
                </trim>
            </if>
            <if test="advancedQuery != null and !&quot;&quot;.equals(advancedQuery)">
                <foreach item="item" index="index" collection="advancedQuery.split('_;')">
                    <choose>
                        <when test="item.indexOf('like') != -1">
                            <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                                <if test="detailIndex == 3">
                                    concat('%',#{itemDetail},'%')
                                </if>

                                <if test="detailIndex &lt; 3">
                                    ${esc.d}{itemDetail}
                                </if>

                            </foreach>
                        </when>
                        <otherwise>
                            <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                                <if test="detailIndex == 3">
                                    #{itemDetail}
                                </if>

                                <if test="detailIndex &lt; 3">
                                    ${esc.d}{itemDetail}
                                </if>

                            </foreach>
                        </otherwise>
                    </choose>


                </foreach>


            </if>
        </where>
        order by id desc
    </select>
    <select id="query${model.className}" parameterType="java.util.Map"
            resultType="com.izouma.awesomeadmin.model.${model.className}">
        select top 1
        <include refid="Base_Column_List"/>
        from ${model.tableName}
        <where>
            and FlagColumn = 0
            #foreach($field in $model.fields)
                <if test="$field.modelName != null and !&quot;&quot;.equals($field.modelName)">
                    and ${field.name} = #{$field.modelName}
                </if>
            #end

        </where>
    </select>
    <update id="delete">
        UPDATE ${model.tableName} SET FlagColumn = 1
        <where>
            AND id = #{id}
        </where>
    </update>
    <select id="query" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.${model.className}">
        select
        <include refid="Base_Column_List"/>
        from ${model.tableName}
        <where>
            and FlagColumn = 0
            #foreach($field in $model.fields)
                #if(${field.searchable})
                    #if(${field.searchMethod}=="=")
                        and ${field.name} = #{$field.modelName}
                    #end
                    #if(${field.searchMethod}=="!=")
                        and ${field.name} != #{$field.modelName}
                    #end
                    #if(${field.searchMethod}==">")
                        and ${field.name} > #{$field.modelName}
                    #end
                    #if(${field.searchMethod}==">=")
                        and ${field.name} >= #{$field.modelName}
                    #end
                    #if(${field.searchMethod}=="<")
                        and ${field.name} < #{$field.modelName}
                    #end
                    #if(${field.searchMethod}=="<=")
                        and ${field.name} <= #{$field.modelName}
                    #end
                    #if(${field.searchMethod}=="like")
                        and ${field.name} like '%${$field.modelName}%'
                    #end
                    #if(${field.searchMethod}=="left like")
                        and ${field.name} like '%${$field.modelName}'
                    #end
                    #if(${field.searchMethod}=="right like")
                        and ${field.name} like '${$field.modelName}%'
                    #end
                #end
            #end
        </where>
        order by id desc
    </select>
</mapper>

