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

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

    <sql id="entityColumnList">
        "ID","SEQ_NAME","SEQ_NOTE","YEAR","MONTH","DATE","NEXT_VALUE","ROW_VERSION","IS_DELETED","CREATE_BY","CREATE_TIME","LAST_UPDATE_BY","LAST_UPDATE_TIME"
    </sql>

    <resultMap id="sequence" type="${basePackage}.module.system.ent.Sequence">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="SEQ_NAME"  jdbcType="VARCHAR" property="seqName"/>
        <result column="SEQ_NOTE"  jdbcType="VARCHAR" property="seqNote"/>
        <result column="YEAR"  jdbcType="VARCHAR" property="year"/>
        <result column="MONTH"  jdbcType="VARCHAR" property="month"/>
        <result column="DATE"  jdbcType="VARCHAR" property="date"/>
        <result column="NEXT_VALUE"  jdbcType="INTEGER" property="nextValue"/>
        <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.seqName,jdbcType=VARCHAR},
        ${r'#'}{request.seqNote,jdbcType=VARCHAR},
        ${r'#'}{request.year,jdbcType=VARCHAR},
        ${r'#'}{request.month,jdbcType=VARCHAR},
        ${r'#'}{request.date,jdbcType=VARCHAR},
        ${r'#'}{request.nextValue,jdbcType=INTEGER},
        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.seqName,jdbcType=VARCHAR},
            ${r'#'}{item.seqNote,jdbcType=VARCHAR},
            ${r'#'}{item.year,jdbcType=VARCHAR},
            ${r'#'}{item.month,jdbcType=VARCHAR},
            ${r'#'}{item.date,jdbcType=VARCHAR},
            ${r'#'}{item.nextValue,jdbcType=INTEGER},
            0,
            0,
            ${r'#'}{token.userId,jdbcType=NUMERIC},
            datetime('now','localtime'),
            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
        "SEQ_NAME" = ${r'#'}{request.seqName,jdbcType=VARCHAR},
        "SEQ_NOTE" = ${r'#'}{request.seqNote,jdbcType=VARCHAR},
        "YEAR" = ${r'#'}{request.year,jdbcType=VARCHAR},
        "MONTH" = ${r'#'}{request.month,jdbcType=VARCHAR},
        "DATE" = ${r'#'}{request.date,jdbcType=VARCHAR},
        "NEXT_VALUE" = ${r'#'}{request.nextValue,jdbcType=INTEGER},
        "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="find" resultMap="sequence">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        <if test="request.seqName != null and request.seqName != ''">
            AND "SEQ_NAME" = ${r'#'}{request.seqName}
        </if>
        <if test="request.year != null and request.year != ''">
            AND "YEAR" = ${r'#'}{request.year}
        </if>
        <if test="request.month != null and request.month != ''">
            AND "MONTH" = ${r'#'}{request.month}
        </if>
        <if test="request.date != null and request.date != ''">
            AND "DATE" = ${r'#'}{request.date}
        </if>
    </select>

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

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

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