<?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.system.mpr.FileMapper">

    <sql id="table">`SYSFILE`</sql>

    <sql id="entityColumnList">
        `ID`,`NAME`,`FILE_TYPE`,`ATTRIBUTE1`,`ATTRIBUTE2`,`LOCATION`,`URL`,`URL_DOWNLOAD`,`ROW_VERSION`,`IS_DELETED`,`CREATE_BY`,`CREATE_TIME`,`LAST_UPDATE_BY`,`LAST_UPDATE_TIME`
    </sql>

    <resultMap id="file" type="${basePackage}.module.system.ent.File">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="NAME"  jdbcType="VARCHAR" property="name"/>
        <result column="FILE_TYPE"  jdbcType="VARCHAR" property="fileType"/>
        <result column="ATTRIBUTE1"  jdbcType="VARCHAR" property="attribute1"/>
        <result column="ATTRIBUTE2"  jdbcType="VARCHAR" property="attribute2"/>
        <result column="LOCATION"  jdbcType="VARCHAR" property="location"/>
        <result column="URL"  jdbcType="VARCHAR" property="url"/>
        <result column="URL_DOWNLOAD"  jdbcType="VARCHAR" property="urlDownload"/>
        <result column="ROW_VERSION"  jdbcType="BIGINT" property="rowVersion"/>
        <result column="IS_DELETED"  jdbcType="BIT" property="isDeleted"/>
        <result column="CREATE_BY"  jdbcType="BIGINT" property="createBy"/>
        <result column="CREATE_TIME"  jdbcType="TIMESTAMP" property="createTime"/>
        <result column="LAST_UPDATE_BY"  jdbcType="BIGINT" property="lastUpdateBy"/>
        <result column="LAST_UPDATE_TIME"  jdbcType="TIMESTAMP" property="lastUpdateTime"/>
    </resultMap>

    <select id="find" resultMap="file">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        `IS_DELETED` = 0
        <if test="request.name != null and request.name != ''">
            AND `NAME` = ${r"#"}{request.name}
        </if>
        <if test="request.fileType != null and request.fileType != ''">
            AND `FILE_TYPE` = ${r"#"}{request.fileType}
        </if>
        <if test="request.attribute1 != null and request.attribute1 != ''">
            AND `ATTRIBUTE1` = ${r"#"}{request.attribute1}
        </if>
        <if test="request.attribute2 != null and request.attribute2 != ''">
            AND `ATTRIBUTE2` = ${r"#"}{request.attribute2}
        </if>
    </select>

    <select id="search" resultMap="file">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        `IS_DELETED` = 0
        <if test="request.keyword != null and request.keyword != ''">
            1 = 2
        </if>
    </select>

    <insert id="insert">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        (
        ${r"#"}{request.id},
        ${r"#"}{request.name,jdbcType=VARCHAR},
        ${r"#"}{request.fileType,jdbcType=VARCHAR},
        ${r"#"}{request.attribute1,jdbcType=VARCHAR},
        ${r"#"}{request.attribute2,jdbcType=VARCHAR},
        ${r"#"}{request.location,jdbcType=VARCHAR},
        ${r"#"}{request.url,jdbcType=VARCHAR},
        ${r"#"}{request.urlDownload,jdbcType=VARCHAR},
        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=",">
        (
        ${r"#"}{item.id},
        ${r"#"}{item.name,jdbcType=VARCHAR},
        ${r"#"}{item.fileType,jdbcType=VARCHAR},
        ${r"#"}{item.attribute1,jdbcType=VARCHAR},
        ${r"#"}{item.attribute2,jdbcType=VARCHAR},
        ${r"#"}{item.location,jdbcType=VARCHAR},
        ${r"#"}{item.url,jdbcType=VARCHAR},
        ${r"#"}{item.urlDownload,jdbcType=VARCHAR},
        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"#"}{request.id}
    </update>

    <update id="update">
        UPDATE
        <include refid="table"/>
        SET
        `NAME` = ${r"#"}{request.name,jdbcType=VARCHAR},
        `FILE_TYPE` = ${r"#"}{request.fileType,jdbcType=VARCHAR},
        `ATTRIBUTE1` = ${r"#"}{request.attribute1,jdbcType=VARCHAR},
        `ATTRIBUTE2` = ${r"#"}{request.attribute2,jdbcType=VARCHAR},
        `LOCATION` = ${r"#"}{request.location,jdbcType=VARCHAR},
        `URL` = ${r"#"}{request.url,jdbcType=VARCHAR},
        `URL_DOWNLOAD` = ${r"#"}{request.urlDownload,jdbcType=VARCHAR},
        `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="getAll" resultMap="file">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        `IS_DELETED` = 0
    </select>

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