<?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="xyz.wbsite.module.admin.mpr.ConfigMapper">

    <sql id="table">"NA_CONFIG"</sql>
	
    <sql id="entityColumnList">
        "ID","CONF_NAME","CONF_TYPE","CONF_VALUE","CONF_NOTE","ROW_VERSION","IS_DELETED","CREATE_BY","CREATE_TIME","LAST_UPDATE_BY","LAST_UPDATE_TIME"
    </sql>
	
    <resultMap id="config" type="xyz.wbsite.module.admin.ent.Config">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="CONF_NAME"  jdbcType="VARCHAR" property="confName"/>
        <result column="CONF_TYPE"  jdbcType="VARCHAR" property="confType"/>
        <result column="CONF_VALUE"  jdbcType="VARCHAR" property="confValue"/>
        <result column="CONF_NOTE"  jdbcType="VARCHAR" property="confNote"/>
        <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
        (
        #{request.id},
        #{request.confName,jdbcType=VARCHAR},
        #{request.confType,jdbcType=VARCHAR},
        #{request.confValue,jdbcType=VARCHAR},
        #{request.confNote,jdbcType=VARCHAR},
        0,
        0,
        #{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=")">
        #{item.id},
        #{item.confName,jdbcType=VARCHAR},
        #{item.confType,jdbcType=VARCHAR},
        #{item.confValue,jdbcType=VARCHAR},
        #{item.confNote,jdbcType=VARCHAR},
        0,
        0,
        #{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" = #{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=")">
		    #{item}
	    </foreach>
    </update>

    <update id="update">
        UPDATE
        <include refid="table"/>
        SET
        CONF_NAME = #{request.confName,jdbcType=VARCHAR},
        CONF_TYPE = #{request.confType,jdbcType=VARCHAR},
        CONF_VALUE = #{request.confValue,jdbcType=VARCHAR},
        CONF_NOTE = #{request.confNote,jdbcType=VARCHAR},
        "ROW_VERSION" = "ROW_VERSION" + 1,
        "LAST_UPDATE_BY" = #{token.userId},
        "LAST_UPDATE_TIME" = datetime('now','localtime')
        WHERE "IS_DELETED" = 0
        AND "ID" = #{request.id}
        AND "ROW_VERSION" = #{request.rowVersion}
    </update>

    <select id="select" resultMap="config">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        <if test="request.confName != null and request.confName != ''">
            AND "CONF_NAME" = #{request.confName}
        </if>
        <if test="request.confType != null and request.confType != ''">
            AND "CONF_TYPE" = #{request.confType}
        </if>
    </select>

    <select id="find" resultMap="config">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        <if test="request.confName != null and request.confName != ''">
            AND "CONF_NAME" = #{request.confName}
        </if>
        <if test="request.confNameLike != null and request.confNameLike != ''">
            AND "CONF_NAME" LIKE '%'||#{request.confNameLike}||'%'
        </if>
        <if test="request.confType != null and request.confType != ''">
            AND "CONF_TYPE" = #{request.confType}
        </if>
        <if test="request.confValueLike != null and request.confValueLike != ''">
            AND "CONF_VALUE" LIKE '%'||#{request.confValueLike}||'%'
        </if>
        <if test="request.confNoteLike != null and request.confNoteLike != ''">
            AND "CONF_NOTE" LIKE '%'||#{request.confNoteLike}||'%'
        </if>
        <if test="request.startDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &gt;= #{request.startDate}
        </if>
        <if test="request.endDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &lt;= #{request.endDate}
        </if>
    </select>

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

    <select id="getById" resultMap="config">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        AND "ID" = #{id}
    </select>
	
    <select id="getByIds" resultMap="config">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        AND "ID" IN
        <foreach collection="ids" item="item" index="index" separator="," open="(" close=")">
		    #{item}
	    </foreach>
    </select>
</mapper>