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

    <sql id="table">"NA_SERVICES"</sql>
	
    <sql id="entityColumnList">
        "ID","TITLE","DOMAIN","TYPE","PORT","VALID","FILTER","FILTER_CONF","ROW_VERSION","IS_DELETED","CREATE_BY","CREATE_TIME","LAST_UPDATE_BY","LAST_UPDATE_TIME"
    </sql>
	
    <resultMap id="services" type="xyz.wbsite.module.admin.ent.Services">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="TITLE"  jdbcType="VARCHAR" property="title"/>
        <result column="DOMAIN"  jdbcType="VARCHAR" property="domain"/>
        <result column="TYPE"  jdbcType="VARCHAR" property="type"/>
        <result column="PORT"  jdbcType="INTEGER" property="port"/>
        <result column="VALID"  jdbcType="BIT" property="valid"/>
        <result column="FILTER"  jdbcType="BIT" property="filter"/>
        <result column="FILTER_CONF"  jdbcType="VARCHAR" property="filterConf"/>
        <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.title,jdbcType=VARCHAR},
        #{request.domain,jdbcType=VARCHAR},
        #{request.type,jdbcType=VARCHAR},
        #{request.port,jdbcType=INTEGER},
        #{request.valid,jdbcType=BIT},
        #{request.filter,jdbcType=BIT},
        #{request.filterConf,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.title,jdbcType=VARCHAR},
        #{item.domain,jdbcType=VARCHAR},
        #{item.type,jdbcType=VARCHAR},
        #{item.port,jdbcType=INTEGER},
        #{item.valid,jdbcType=BIT},
        #{item.filter,jdbcType=BIT},
        #{item.filterConf,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
        TITLE = #{request.title,jdbcType=VARCHAR},
        DOMAIN = #{request.domain,jdbcType=VARCHAR},
        TYPE = #{request.type,jdbcType=VARCHAR},
        PORT = #{request.port,jdbcType=INTEGER},
        VALID = #{request.valid,jdbcType=BIT},
        FILTER = #{request.filter,jdbcType=BIT},
        FILTER_CONF = #{request.filterConf,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="services">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        <if test="request.title != null and request.title != ''">
            AND "TITLE" = #{request.title}
        </if>
        <if test="request.domain != null and request.domain != ''">
            AND "DOMAIN" = #{request.domain}
        </if>
        <if test="request.type != null and request.type != ''">
            AND "TYPE" = #{request.type}
        </if>
        <if test="request.port != null">
            AND "PORT" = #{request.port}
        </if>
        <if test="request.valid != null">
            AND "VALID" = #{request.valid}
        </if>
        <if test="request.filter != null">
            AND "FILTER" = #{request.filter}
        </if>
    </select>

    <select id="find" resultMap="services">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        <if test="request.title != null and request.title != ''">
            AND "TITLE" = #{request.title}
        </if>
        <if test="request.titleLike != null and request.titleLike != ''">
            AND "TITLE" LIKE '%'||#{request.titleLike}||'%'
        </if>
        <if test="request.domain != null and request.domain != ''">
            AND "DOMAIN" = #{request.domain}
        </if>
        <if test="request.type != null and request.type != ''">
            AND "TYPE" = #{request.type}
        </if>
        <if test="request.port != null">
            AND "PORT" = #{request.port}
        </if>
        <if test="request.valid != null">
            AND "VALID" = #{request.valid}
        </if>
        <if test="request.filter != null">
            AND "FILTER" = #{request.filter}
        </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="services">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
    </select>

    <select id="getById" resultMap="services">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        AND "ID" = #{id}
    </select>
	
    <select id="getByIds" resultMap="services">
        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>