<?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="${domain}.module.wsys.mpr.TaskSqlMapper">

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

    <sql id="entityColumnList">
        "ID","TASK_NAME","TASK_NOTE","TASK_TYPE","TYPE_VALUE","TASK_SQL","VALID","ROW_VERSION","IS_DELETED","CREATE_BY","CREATE_TIME","LAST_UPDATE_BY","LAST_UPDATE_TIME"
    </sql>

    <resultMap id="taskSql" type="${domain}.module.wsys.ent.TaskSql">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="TASK_NAME"  jdbcType="VARCHAR" property="taskName"/>
        <result column="TASK_NOTE"  jdbcType="VARCHAR" property="taskNote"/>
        <result column="TASK_TYPE"  jdbcType="VARCHAR" property="taskType"/>
        <result column="TYPE_VALUE"  jdbcType="VARCHAR" property="typeValue"/>
        <result column="TASK_SQL"  jdbcType="VARCHAR" property="taskSql"/>
        <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.taskName,jdbcType=VARCHAR},
        ${r"#"}{request.taskNote,jdbcType=VARCHAR},
        ${r"#"}{request.taskType,jdbcType=VARCHAR},
        ${r"#"}{request.typeValue,jdbcType=VARCHAR},
        ${r"#"}{request.taskSql,jdbcType=VARCHAR},
        ${r"#"}{request.valid,jdbcType=BIT},
        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.taskName,jdbcType=VARCHAR},
            ${r"#"}{item.taskNote,jdbcType=VARCHAR},
            ${r"#"}{item.taskType,jdbcType=VARCHAR},
            ${r"#"}{item.typeValue,jdbcType=VARCHAR},
            ${r"#"}{item.taskSql,jdbcType=VARCHAR},
            ${r"#"}{item.valid,jdbcType=BIT},
            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
        "TASK_NAME" = ${r"#"}{request.taskName,jdbcType=VARCHAR},
        "TASK_NOTE" = ${r"#"}{request.taskNote,jdbcType=VARCHAR},
        "TASK_TYPE" = ${r"#"}{request.taskType,jdbcType=VARCHAR},
        "TYPE_VALUE" = ${r"#"}{request.typeValue,jdbcType=VARCHAR},
        "TASK_SQL" = ${r"#"}{request.taskSql,jdbcType=VARCHAR},
        "VALID" = ${r"#"}{request.valid,jdbcType=BIT},
        "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="taskSql">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        "IS_DELETED" = 0
        <if test="request.taskName != null and request.taskName != ''">
            AND "TASK_NAME" = ${r"#"}{request.taskName}
        </if>
        <if test="request.taskNote != null and request.taskNote != ''">
            AND "TASK_NOTE" = ${r"#"}{request.taskNote}
        </if>
        <if test="request.taskType != null and request.taskType != ''">
            AND "TASK_TYPE" = ${r"#"}{request.taskType}
        </if>
        <if test="request.valid != null">
            AND "VALID" = ${r"#"}{request.valid}
        </if>
        <if test="request.startDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &gt;= ${r'#'}{request.startDate}
        </if>
        <if test="request.endDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &lt;= ${r'#'}{request.endDate}
        </if>
    </select>

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

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

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