<?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="${basePackage}.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="${basePackage}.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
        "IS_DELETED" = 0
<#list table.fields as field>
<#if field.isQuery>
    <#if field.getFieldTypeJava() == "Boolean">
        <if test="request.${field.getFName()} != null">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Byte">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != 0">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Short">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != 0">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Integer">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != 0">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Long">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != 0">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Float">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != 0">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Double">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != 0">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Character">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != ''">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "String">
        <if test="request.${field.getFName()} != null and request.${field.getFName()} != ''">
            AND ${field.fieldName} = ${r"#{"}request.${field.getFName()}}
        </if>
    </#if>
    <#if field.getFieldTypeJava() == "Date">
        <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"/>
        WHERE
        "IS_DELETED" = 0
<#if table.hasSearchKey()>
        <if test="request.keyword != null and request.keyword != ''">
            AND (
  <#list table.searchKeyList() as field>
    <#if field_index == 0>
            "${field.fieldName}" LIKE '%'||${r"#{"}request.keyword}||'%'
    <#else>
            OR "${field.fieldName}" LIKE '%'||${r"#{"}request.keyword}||'%'
    </#if>
  </#list>
            )
        </if>
</#if>
    </select>

    <insert id="insert">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        (
        ${r"#{"}request.id},
        <#list table.fields as field>
            <#if !field.isSystem>
        ${r"#{"}request.${field.getFName()},jdbcType=${field.getFieldTypeJdbc()}},
            </#if>
        </#list>
        0,
        0,
        ${r"#{"}token.userId,jdbcType=NUMERIC},
        datetime('now','localtime'),
        NULL,
        NULL
        )
    </insert>

    <insert id="insertBatch">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
        (
        ${r"#{"}item.id},
        <#list table.fields as field>
            <#if !field.isSystem>
        ${r"#{"}item.${field.getFName()},jdbcType=${field.getFieldTypeJdbc()}},
            </#if>
        </#list>
        0,
        0,
        ${r"#{"}token.userId,jdbcType=NUMERIC},
        datetime('now','localtime'),
        NULL,
        NULL
        )
        </foreach >
    </insert>

    <update id="delete">
        UPDATE
        <include refid="table"/>
        SET "IS_DELETED" = 1
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r"#{"}request.id}
    </update>

    <update id="update">
        UPDATE
        <include refid="table"/>
        SET
        <#list table.fields as field>
            <#if !field.isPrimaryKey>
                <#if !field.isSystem || field.fieldName == 'ID'>
        ${field.fieldName} = ${r"#{"}request.${field.getFName()},jdbcType=${field.getFieldTypeJdbc()}},
                </#if>
            </#if>
        </#list>
        "ROW_VERSION" = "ROW_VERSION" + 1,
        "LAST_UPDATE_BY" = ${r"#{"}token.userId},
        "LAST_UPDATE_TIME" = datetime('now','localtime')
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r"#{"}request.id}
        AND "ROW_VERSION" = ${r"#{"}request.rowVersion}
    </update>

    <select id="get" resultMap="${table.getFName()}">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE IS_DELETED = 0
        AND ID = ${r"#{request.id}"}
    </select>
</mapper>