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

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

    <sql id="entityColumnList">
        `ID`,`ROLE_ID`,`ROLE_CODE`,`RES_ID`,`RES_CODE`,`ROW_VERSION`,`IS_DELETED`,`CREATE_BY`,`CREATE_TIME`,`LAST_UPDATE_BY`,`LAST_UPDATE_TIME`
    </sql>

    <resultMap id="roleRes" type="${domain}.module.wsys.ent.RoleRes">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="ROLE_ID"  jdbcType="BIGINT" property="roleId"/>
        <result column="ROLE_CODE"  jdbcType="VARCHAR" property="roleCode"/>
        <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.roleId,jdbcType=BIGINT},
        ${r'#'}{request.roleCode,jdbcType=VARCHAR},
        ${r'#'}{request.resId,jdbcType=BIGINT},
        ${r'#'}{request.resCode,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.roleId,jdbcType=BIGINT},
            ${r'#'}{item.roleCode,jdbcType=VARCHAR},
            ${r'#'}{item.resId,jdbcType=BIGINT},
            ${r'#'}{item.resCode,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
        `ROLE_ID` = ${r'#'}{request.roleId,jdbcType=BIGINT},
        `ROLE_CODE` = ${r'#'}{request.roleCode,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` = sysdate()
        WHERE `IS_DELETED` = 0
        AND `ID` = ${r'#'}{request.id}
        AND `ROW_VERSION` = ${r'#'}{request.rowVersion}
    </update>

    <select id="find" resultMap="roleRes">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
        <if test="request.roleId != null and request.roleId != 0">
            AND `ROLE_ID` = ${r'#'}{request.roleId}
        </if>
        <if test="request.roleCode != null and request.roleCode != ''">
            AND `ROLE_CODE` = ${r'#'}{request.roleCode}
        </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 `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="roleRes">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
    </select>

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

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