<?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.wsys.mpr.TokensMapper">

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

    <sql id="entityColumnList">
        `ID`,`TOKEN`,`USER_ID`,`USER_NAME`,`LOGIN_TIME`,`VALID_TIME`,`VALID`,`DEPT_ID`,`DEPT_CODE`,`DEPT_NAME`,`TERMINAL`,`TERMINAL_IP`,`TERMINAL_INFO`,`ROW_VERSION`,`IS_DELETED`,`CREATE_BY`,`CREATE_TIME`,`LAST_UPDATE_BY`,`LAST_UPDATE_TIME`
    </sql>

    <resultMap id="tokens" type="${domain}.module.wsys.ent.Tokens">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="TOKEN"  jdbcType="VARCHAR" property="token"/>
        <result column="USER_ID"  jdbcType="BIGINT" property="userId"/>
        <result column="USER_NAME"  jdbcType="VARCHAR" property="userName"/>
        <result column="LOGIN_TIME"  jdbcType="TIMESTAMP" property="loginTime"/>
        <result column="VALID_TIME"  jdbcType="TIMESTAMP" property="validTime"/>
        <result column="VALID"  jdbcType="BIT" property="valid"/>
        <result column="DEPT_ID"  jdbcType="BIGINT" property="deptId"/>
        <result column="DEPT_CODE"  jdbcType="VARCHAR" property="deptCode"/>
        <result column="DEPT_NAME"  jdbcType="VARCHAR" property="deptName"/>
        <result column="TERMINAL"  jdbcType="VARCHAR" property="terminal"/>
        <result column="TERMINAL_IP"  jdbcType="VARCHAR" property="terminalIp"/>
        <result column="TERMINAL_INFO"  jdbcType="VARCHAR" property="terminalInfo"/>
        <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.token,jdbcType=VARCHAR},
        ${r"#"}{request.userId,jdbcType=BIGINT},
        ${r"#"}{request.userName,jdbcType=VARCHAR},
        ${r"#"}{request.loginTime,jdbcType=TIMESTAMP},
        ${r"#"}{request.validTime,jdbcType=TIMESTAMP},
        ${r"#"}{request.valid,jdbcType=BIT},
        ${r"#"}{request.deptId,jdbcType=BIGINT},
        ${r"#"}{request.deptCode,jdbcType=VARCHAR},
        ${r"#"}{request.deptName,jdbcType=VARCHAR},
        ${r"#"}{request.terminal,jdbcType=VARCHAR},
        ${r"#"}{request.terminalIp,jdbcType=VARCHAR},
        ${r"#"}{request.terminalInfo,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="," open="(" close=")">
            ${r"#"}{item.id},
            ${r"#"}{item.token,jdbcType=VARCHAR},
            ${r"#"}{item.userId,jdbcType=BIGINT},
            ${r"#"}{item.userName,jdbcType=VARCHAR},
            ${r"#"}{item.loginTime,jdbcType=TIMESTAMP},
            ${r"#"}{item.validTime,jdbcType=TIMESTAMP},
            ${r"#"}{item.valid,jdbcType=BIT},
            ${r"#"}{item.deptId,jdbcType=BIGINT},
            ${r"#"}{item.deptCode,jdbcType=VARCHAR},
            ${r"#"}{item.deptName,jdbcType=VARCHAR},
            ${r"#"}{item.terminal,jdbcType=VARCHAR},
            ${r"#"}{item.terminalIp,jdbcType=VARCHAR},
            ${r"#"}{item.terminalInfo,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'#'}{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
        `TOKEN` = ${r"#"}{request.token,jdbcType=VARCHAR},
        `USER_ID` = ${r"#"}{request.userId,jdbcType=BIGINT},
        `USER_NAME` = ${r"#"}{request.userName,jdbcType=VARCHAR},
        `LOGIN_TIME` = ${r"#"}{request.loginTime,jdbcType=TIMESTAMP},
        `VALID_TIME` = ${r"#"}{request.validTime,jdbcType=TIMESTAMP},
        `VALID` = ${r"#"}{request.valid,jdbcType=BIT},
        `DEPT_ID` = ${r"#"}{request.deptId,jdbcType=BIGINT},
        `DEPT_CODE` = ${r"#"}{request.deptCode,jdbcType=VARCHAR},
        `DEPT_NAME` = ${r"#"}{request.deptName,jdbcType=VARCHAR},
        `TERMINAL` = ${r"#"}{request.terminal,jdbcType=VARCHAR},
        `TERMINAL_IP` = ${r"#"}{request.terminalIp,jdbcType=VARCHAR},
        `TERMINAL_INFO` = ${r"#"}{request.terminalInfo,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="find" resultMap="tokens">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
        <if test="request.token != null and request.token != ''">
            AND `TOKEN` = ${r"#"}{request.token}
        </if>
        <if test="request.userId != null and request.userId != 0">
            AND `USER_ID` = ${r"#"}{request.userId}
        </if>
        <if test="request.userName != null and request.userName != ''">
            AND `USER_NAME` = ${r"#"}{request.userName}
        </if>
        <if test="request.loginTime != null">
            AND DATE_FORMAT(`LOGIN_TIME`,"%Y-%m-%d %T") = DATE_FORMAT(${r"#"}{request.loginTime},"%Y-%m-%d %T")
        </if>
        <if test="request.validTime != null">
            AND DATE_FORMAT(`VALID_TIME`,"%Y-%m-%d %T") = DATE_FORMAT(${r"#"}{request.validTime},"%Y-%m-%d %T")
        </if>
        <if test="request.valid != null">
            AND `VALID` = ${r"#"}{request.valid}
        </if>
        <if test="request.deptId != null and request.deptId != 0">
            AND `DEPT_ID` = ${r"#"}{request.deptId}
        </if>
        <if test="request.deptCode != null and request.deptCode != ''">
            AND `DEPT_CODE` = ${r"#"}{request.deptCode}
        </if>
        <if test="request.deptName != null and request.deptName != ''">
            AND `DEPT_NAME` = ${r"#"}{request.deptName}
        </if>
        <if test="request.terminal != null and request.terminal != ''">
            AND `TERMINAL` = ${r"#"}{request.terminal}
        </if>
        <if test="request.terminalIp != null and request.terminalIp != ''">
            AND `TERMINAL_IP` = ${r"#"}{request.terminalIp}
        </if>
        <if test="request.terminalInfo != null and request.terminalInfo != ''">
            AND `TERMINAL_INFO` = ${r"#"}{request.terminalInfo}
        </if>
        <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="tokens">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
    </select>

    <select id="getById" resultMap="tokens">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
        AND `ID` = ${r'#'}{id}
    </select>

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