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

    <!--开启缓存-->
    <cache/>

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

    <sql id="entityColumnList">
        `ID`,`RES_CODE`,`RES_NAME`,`RES_TYPE`,`RES_VALUE`,`SUP_CODE`,`SUP_NAME`,`FREE`,`VALID`,`ROW_VERSION`,`IS_DELETED`,`CREATE_BY`,`CREATE_TIME`,`LAST_UPDATE_BY`,`LAST_UPDATE_TIME`
    </sql>

    <resultMap id="res" type="${basePackage}.module.system.ent.Res">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="RES_CODE"  jdbcType="VARCHAR" property="resCode"/>
        <result column="RES_NAME"  jdbcType="VARCHAR" property="resName"/>
        <result column="RES_TYPE"  jdbcType="VARCHAR" property="resType"/>
        <result column="RES_VALUE"  jdbcType="VARCHAR" property="resValue"/>
        <result column="SUP_CODE"  jdbcType="VARCHAR" property="supCode"/>
        <result column="SUP_NAME"  jdbcType="VARCHAR" property="supName"/>
        <result column="FREE" jdbcType="BIT" property="free"/>
        <result column="VALID"  jdbcType="BIT" property="valid"/>
        <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.resCode,jdbcType=VARCHAR},
        ${r'#'}{request.resName,jdbcType=VARCHAR},
        ${r'#'}{request.resType,jdbcType=VARCHAR},
        ${r'#'}{request.resValue,jdbcType=VARCHAR},
        ${r'#'}{request.supCode,jdbcType=VARCHAR},
        ${r'#'}{request.supName,jdbcType=VARCHAR},
        ${r'#'}{request.free,jdbcType=BIT},
        ${r'#'}{request.valid,jdbcType=BIT},
        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.resCode,jdbcType=VARCHAR},
            ${r'#'}{item.resName,jdbcType=VARCHAR},
            ${r'#'}{item.resType,jdbcType=VARCHAR},
            ${r'#'}{item.resValue,jdbcType=VARCHAR},
            ${r'#'}{item.supCode,jdbcType=VARCHAR},
            ${r'#'}{item.supName,jdbcType=VARCHAR},
            ${r'#'}{item.free,jdbcType=BIT},
            ${r'#'}{item.valid,jdbcType=BIT},
            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
        `RES_CODE` = ${r'#'}{request.resCode,jdbcType=VARCHAR},
        `RES_NAME` = ${r'#'}{request.resName,jdbcType=VARCHAR},
        `RES_TYPE` = ${r'#'}{request.resType,jdbcType=VARCHAR},
        `RES_VALUE` = ${r'#'}{request.resValue,jdbcType=VARCHAR},
        `SUP_CODE` = ${r'#'}{request.supCode,jdbcType=VARCHAR},
        `SUP_NAME` = ${r'#'}{request.supName,jdbcType=VARCHAR},
        `FREE` = ${r'#'}{request.free,jdbcType=BIT},
        `VALID` = ${r'#'}{request.valid,jdbcType=BIT},
        `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="res">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
        <if test="request.resCode != null and request.resCode != ''">
            AND `RES_CODE` = ${r'#'}{request.resCode}
        </if>
        <if test="request.resName != null and request.resName != ''">
            AND `RES_NAME` = ${r'#'}{request.resName}
        </if>
        <if test="request.resType != null and request.resType != ''">
            AND `RES_TYPE` = ${r'#'}{request.resType}
        </if>
        <if test="request.resValue != null and request.resValue != ''">
            AND `RES_VALUE` = ${r'#'}{request.resValue}
        </if>
        <if test="request.supCode != null and request.supCode != ''">
            AND `SUP_CODE` = ${r'#'}{request.supCode}
        </if>
        <if test="request.supName != null and request.supName != ''">
            AND `SUP_NAME` = ${r'#'}{request.supName}
        </if>
        <if test="request.free != null">
            AND `FREE` = ${r'#'}{request.free}
        </if>
        <if test="request.valid != null">
            AND `VALID` = ${r'#'}{request.valid}
        </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="load" resultMap="res">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE `IS_DELETED` = 0
        <if test="request.userId != null">
            AND `ID` IN (
            SELECT
            t2.RES_ID
            FROM
            `sys_user_role` t1,
            `sys_role_res` t2
            WHERE t1.ROLE_ID = t2.ROLE_ID
            AND t1.IS_DELETED = '0'
            AND t2.IS_DELETED = '0'
            AND t1.USER_ID = ${r'#'}{request.userId}

            UNION

            SELECT `ID` FROM `sys_res` WHERE `IS_DELETED` = '0' AND `FREE` = '1'
            )
        </if>
        <if test="request.roleId != null">
            AND `ID` IN (
            SELECT `RES_ID` FROM `sys_role_res` WHERE ROLE_ID = ${r'#'}{request.roleId}

            UNION

            SELECT `ID` FROM `sys_res` WHERE `IS_DELETED` = '0' AND `FREE` = '1'
            )
        </if>
    </select>

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

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

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