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


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

    <sql id="entityColumnList">
        `ID`,`DICT_ID`,`KEY`,`VALUE`,`SORT`,`VALID`,`ROW_VERSION`,`IS_DELETED`,`CREATE_BY`,`CREATE_TIME`,`LAST_UPDATE_BY`,`LAST_UPDATE_TIME`
    </sql>

    <resultMap id="dictItem" type="${basePackage}.module.system.ent.DictItem">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="DICT_ID"  jdbcType="BIGINT" property="dictId"/>
        <result column="KEY"  jdbcType="CHAR" property="key"/>
        <result column="VALUE"  jdbcType="VARCHAR" property="value"/>
        <result column="SORT"  jdbcType="INTEGER" property="sort"/>
        <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>

    <select id="find" resultMap="dictItem">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        `IS_DELETED` = 0
        <if test="request.id != null and request.id != ''">
            AND `ID` = ${r"#{"}request.id}
        </if>
        <if test="request.dictId != null and request.dictId != ''">
            AND `DICT_ID` = ${r"#{"}request.dictId}
        </if>
        <if test="request.key != null and request.key != ''">
            AND `KEY` = ${r"#{"}request.key}
        </if>
        <if test="request.value != null and request.value != ''">
            AND `VALUE` = ${r"#{"}request.value}
        </if>
        <if test="request.valid != null">
            AND `VALID` = ${r"#{"}request.valid}
        </if>
    </select>

    <select id="search" resultMap="dictItem">
        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.dictId,jdbcType=BIGINT},
        ${r"#{"}request.key,jdbcType=CHAR},
        ${r"#{"}request.value,jdbcType=VARCHAR},
        ${r"#{"}request.sort,jdbcType=INTEGER},
        ${r"#{"}request.valid,jdbcType=BIT},
        0,
        0,
        ${r"#{"}token.userId,jdbcType=NUMERIC},
        sysdate(),
        NULL,
        NULL
        )
    </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
        `KEY` = ${r"#{"}request.key,jdbcType=CHAR},
        `VALUE` = ${r"#{"}request.value,jdbcType=VARCHAR},
        `SORT` = ${r"#{"}request.sort,jdbcType=INTEGER},
        `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="getAll" resultMap="dictItem">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        `IS_DELETED` = 0
    </select>

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