<?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>

    <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},
        sysdate(),
        NULL,
        NULL
        )
    </insert>

    <insert id="insertBatch">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            ${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},
            sysdate(),
            NULL,
            NULL
        </foreach>
    </insert>

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

    <update id="deleteBatch">
        UPDATE
        <include refid="table"/>
        SET `IS_DELETED` = 1
        WHERE `IS_DELETED` = 0
        AND `ID` IN
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
		    ${r"#{"}item}
	    </foreach>
    </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` = sysdate()
        WHERE `IS_DELETED` = 0
        AND `ID` = ${r"#{"}request.id}
        AND `ROW_VERSION` = ${r"#{"}request.rowVersion}
    </update>

    <select id="select" 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 DATE_FORMAT(`${field.fieldName}`,"%Y-%m-%d %T") = DATE_FORMAT(${r"#{"}request.${field.getFName()}},"%Y-%m-%d %T")
        </if>
    </#if>
</#if>
</#list>
    </select>

    <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 DATE_FORMAT(`${field.fieldName}`,"%Y-%m-%d %T") = DATE_FORMAT(${r"#{"}request.${field.getFName()}},"%Y-%m-%d %T")
        </if>
    </#if>
</#if>
</#list>
        <if test="request.startDate != null">
            AND `CREATE_TIME` &gt;= ${r"#"}{request.startDate}
        </if>
        <if test="request.endDate != null">
            AND `CREATE_TIME` &lt;= ${r"#"}{request.endDate}
        </if>
    </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 CONCAT('%',${r"#{request.keyword}"},'%')
    <#else>
            OR `${field.fieldName}` LIKE CONCAT('%',${r"#{request.keyword}"},'%')
    </#if>
  </#list>
            )
        </if>
</#if>
    </select>

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

    <select id="getByIds" resultMap="${table.getFName()}">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
        AND `ID` IN
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
		    ${r"#{"}item}
	    </foreach>
    </select>
</mapper>