<?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.VisitorResMapper">

    <sql id="table">"SYS_VISITOR_RES"</sql>

    <sql id="entityColumnList">
        "ID","APP_ID","APP_KEY","RES_ID","RES_CODE","ROW_VERSION","IS_DELETED","CREATE_BY","CREATE_TIME","LAST_UPDATE_BY","LAST_UPDATE_TIME"
    </sql>

    <resultMap id="visitorRes" type="${basePackage}.module.system.ent.VisitorRes">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="APP_ID"  jdbcType="BIGINT" property="appId"/>
        <result column="APP_KEY"  jdbcType="VARCHAR" property="appKey"/>
        <result column="RES_ID"  jdbcType="BIGINT" property="resId"/>
        <result column="RES_CODE"  jdbcType="VARCHAR" property="resCode"/>
        <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>

    <insert id="insert">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        (
        ${r'#'}{request.id},
        ${r'#'}{request.appId,jdbcType=BIGINT},
        ${r'#'}{request.appKey,jdbcType=VARCHAR},
        ${r'#'}{request.resId,jdbcType=BIGINT},
        ${r'#'}{request.resCode,jdbcType=VARCHAR},
        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="," open="(" close=")">
            ${r'#'}{item.id},
            ${r'#'}{item.appId,jdbcType=BIGINT},
            ${r'#'}{item.appKey,jdbcType=VARCHAR},
            ${r'#'}{item.resId,jdbcType=BIGINT},
            ${r'#'}{item.resCode,jdbcType=VARCHAR},
            0,
            0,
            ${r'#'}{token.userId,jdbcType=NUMERIC},
            datetime('now','localtime'),
            NULL,
            NULL
        </foreach >
    </insert>

    <select id="find" resultMap="visitorRes">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        "IS_DELETED" = 0
        <if test="request.appId != null and request.appId != 0">
            AND APP_ID = ${r'#'}{request.appId}
        </if>
        <if test="request.appKey != null and request.appKey != ''">
            AND APP_KEY = ${r'#'}{request.appKey}
        </if>
        <if test="request.resId != null and request.resId != 0">
            AND RES_ID = ${r'#'}{request.resId}
        </if>
        <if test="request.resCode != null and request.resCode != ''">
            AND RES_CODE = ${r'#'}{request.resCode}
        </if>
        <if test="request.startDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &gt;= ${r'#'}{request.startDate}
        </if>
        <if test="request.endDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &lt;= ${r'#'}{request.endDate}
        </if>
    </select>

    <select id="search" resultMap="visitorRes">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        "IS_DELETED" = 0
    </select>

    <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
        APP_ID = ${r'#'}{request.appId,jdbcType=BIGINT},
        APP_KEY = ${r'#'}{request.appKey,jdbcType=VARCHAR},
        RES_ID = ${r'#'}{request.resId,jdbcType=BIGINT},
        RES_CODE = ${r'#'}{request.resCode,jdbcType=VARCHAR},
        "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="getById" resultMap="visitorRes">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r'#'}{id}
    </select>

    <select id="getByIds" resultMap="visitorRes">
        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>