<?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')
        <id column="id" property="id" jdbcType="INTEGER" />
        #end
        #if(${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)#if(${foreach.index}!=0), #end${field.name}#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 test="$field.modelName!= null" >
                ${field.name},
            </if>
    #end
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
        #foreach($field in $model.fields)
            <if test="$field.modelName != null" >
                #{$field.modelName,jdbcType=${field.sqlType}},
            </if>
        #end
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.izouma.awesomeadmin.model.${model.className}" >
        update $model.tableName
        <set >
        #foreach($field in $model.fields)
            <if test="$field.modelName != null" >
               ${field.name}= #{$field.modelName,jdbcType=${field.sqlType}},
            </if>
         #end
        </set>
        where
        <if test="id != null and !&quot;&quot;.equals(id)">
            id = #{id,jdbcType=INTEGER}
        </if>

        <if test="idStr != null and !&quot;&quot;.equals(idStr)">
            id in (${esc.d}{idStr})
        </if>
    </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 del_flag = 'N'
            <if test="record.idStr != null and !&quot;&quot;.equals(record.idStr)">
                and id in (${esc.d}{record.idStr})
            </if>
        #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')
                  #if(${field.searchable})
                 OR  ${field.name} LIKE concat('%',#{record.searchKey},'%')
                  #end
                 #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>
                        <when test="item.indexOf('in') != -1">
                            <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                                <if test="detailIndex == 3">
                                    (${esc.d}{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

        <if test="record.orderByStr != null and !&quot;&quot;.equals(record.orderByStr)">


            <trim suffixOverrides=",">
                <foreach item="item" index="index" separator="," collection="record.orderByStr.split('_;')">
                    <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                        ${esc.d}{itemDetail}
                    </foreach>
                </foreach>
            </trim>
            ,
        </if>
        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 del_flag = 'N'
            <if test="idStr != null and !&quot;&quot;.equals(idStr)">
                and id in (${esc.d}{idStr})
            </if>
        #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')
                            #if(${field.searchable})
                            OR  ${field.name} LIKE concat('%',#{searchKey},'%')
                            #end
                        #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>
            <when test="item.indexOf('in') != -1">
                <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                    <if test="detailIndex == 3">
                        (${esc.d}{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

        <if test="orderByStr != null and !&quot;&quot;.equals(orderByStr)">


            <trim suffixOverrides=",">
                <foreach item="item" index="index" separator="," collection="orderByStr.split('_;')">
                    <foreach item="itemDetail" index="detailIndex" collection="item.split('_,')">
                        ${esc.d}{itemDetail}
                    </foreach>
                </foreach>
            </trim>
            ,
        </if>

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

        </where>
        LIMIT 1
    </select>
    <update id="delete">
        UPDATE ${model.tableName} SET del_flag = 'Y'
        where
        <if test="id != null and !&quot;&quot;.equals(id)">
            id = #{id,jdbcType=INTEGER}
        </if>

        <if test="idStr != null and !&quot;&quot;.equals(idStr)">
            id in (${esc.d}{idStr})
        </if>
    </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 del_flag = 'N'
            #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>

