<?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="${domain}.module.${moduleName}.mpr.${table.getCName()}Mapper">

    <sql id="table">`${module.modulePrefix?default("")}${table.tableName}`</sql>

    <sql id="entityColumnList">
        <#list table.fields as field>`${field.fieldName}`<#if field_has_next>,</#if></#list>
    </sql>

    <resultMap id="${table.getFName()}" type="${domain}.module.${moduleName}.ent.${table.getCName()}">
    <#list table.fields as field>
        <result column="${field.fieldName}" jdbcType="${field.getFieldTypeJdbc()}" property="${field.getFName()}"/>
    </#list>
    </resultMap>

    <select id="find" resultMap="${table.getFName()}">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        1 = 1
<#list table.fields as field>
  <#if field.isQuery>
    <#if field.getFieldTypeJava() == "String">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != ''">
            AND `${field.fieldName}` = ${r"#{"}request.${field.getFName()}}
        </if>
    <#elseif field.getFieldTypeJava() == "Boolean">
        <if test="request.${field.getFName()} != null">
            AND `${field.fieldName}` = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
  </#if>
</#list>
    </select>

    <select id="search" resultMap="${table.getFName()}">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
<#if table.hasSearchKey()>
  <#list table.searchKeyList() as field>
    <#if field_index = 0>
        WHERE
        (
        `${field.fieldName}` LIKE CONCAT(CONCAT('%',${r"#{"}request.keyword}),'%')
    <#else>
        OR `${field.fieldName}` LIKE CONCAT(CONCAT('%',${r"#{"}request.keyword}),'%')
    </#if>
    <#if !field_has_next>
        )
    </#if>
  </#list>
<#else>
        WHERE 1 = 2
</#if>
    </select>

    <insert id="insert">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        (
        <#list table.fields as field>
        ${r"#{"}request.${field.getFName()},jdbcType=${field.getFieldTypeJdbc()}}<#if field_has_next>,</#if>
        </#list>
        )
    </insert>

    <delete id="delete">
        DELETE FROM
        <include refid="table"/>
<#if table.hasPrimaryKey()>
  <#list table.primaryKeyList() as field>
    <#if field_index = 0>
        WHERE `${field.fieldName}` = ${r"#{"}request.${field.getFName()}}
    <#else>
        AND `${field.fieldName}` = ${r"#{"}request.${field.getFName()}}
    </#if>
  </#list>
<#else>
        WHERE 1 = 2
</#if>
    </delete>

    <update id="update">
        UPDATE
        <include refid="table"/>
        SET
<#list table.fields as field>
  <#if !field.isPrimaryKey>
        `${field.fieldName}` = ${r"#{"}request.${field.getFName()},jdbcType=${field.getFieldTypeJdbc()}},
  </#if>
</#list>
<#if table.hasPrimaryKey()>
  <#list table.primaryKeyList() as field>
    <#if field_index = 0>
        WHERE `${field.fieldName}` = ${r"#{"}request.${field.getFName()}}
    <#else>
        AND `${field.fieldName}` = ${r"#{"}request.${field.getFName()}}
    </#if>
  </#list>
<#else>
        WHERE 1 = 2
</#if>
    </update>
</mapper>