<?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="com.webank.wedatasphere.exchangis.job.server.mapper.LaunchableTaskDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.webank.wedatasphere.exchangis.job.launcher.domain.LaunchableExchangisTask">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="create_time" property="createTime"/>
        <result column="last_update_time" property="lastUpdateTime"/>
        <result column="engine_type" property="engineType"/>
        <result column="execute_user" property="executeUser"/>
        <result column="linkis_job_name" property="linkisJobName"/>
        <result column="linkis_job_content" property="linkisJobContent"/>
        <result column="linkis_params" property="linkisParams"/>
        <result column="linkis_source" property="linkisSource"/>
        <result column="labels" property="labels"/>
        <result column="job_execution_id" property="jobExecutionId"/>
    </resultMap>

    <sql id="TableName">
        exchangis_launchable_task
    </sql>

    <!-- 通用查询结果列 -->
    <sql id="BaseColumnList">
        id, name, create_time, last_update_time, engine_type, execute_user, linkis_job_name, linkis_job_content, linkis_params, linkis_source, labels </sql>

    <insert id="addLaunchableTask" >
        <foreach collection="tasks" item="task">
        insert into
        <include refid="TableName"/>
        (id, name, job_execution_id, create_time, last_update_time, engine_type, execute_user, linkis_job_name, linkis_job_content, linkis_params, linkis_source, labels)
        values(
        #{task.id},
        #{task.name},
        #{task.jobExecutionId},
        #{task.createTime},
        #{task.lastUpdateTime},
        #{task.engineType},
        #{task.executeUser},
        #{task.linkisJobName},
        #{task.linkisJobContent},
        #{task.linkisParams},
        #{task.linkisSource},
        #{task.labels}
        );
        </foreach>
    </insert>

    <delete id="deleteLaunchableTask">
        delete from
        <include refid="TableName"/>
        where id = #{taskId}
    </delete>

    <update id="upgradeLaunchableTask">
        UPDATE <include refid="TableName"/>
        SET
        id = #{id},
        name = #{name},
        create_time = #{createTime},
        last_update_time = #{lastUpdateTime},
        engine_type = #{engineType},
        execute_user = #{execute_user},
        linkis_job_name = #{linkisJobName},
        linkis_job_content = #{linkisJobContent},
        linkis_params = #{linkisParams},
        linkis_source = #{linkisSource},
        labels = #{labels}
    </update>

    <select id="getLaunchableTask" parameterType="java.lang.String" resultType="com.webank.wedatasphere.exchangis.job.launcher.domain.LaunchableExchangisTask">
        select
        <include refid="BaseColumnList"/>
        from
        <include refid="TableName" />
        where id = #{taskId}
    </select>

    <select id="getTaskToLaunch" resultMap="BaseResultMap">
        <![CDATA[SELECT * FROM exchangis_launchable_task t
        left join exchangis_launched_task_entity l on t.id = l.id where l.task_id is NULL ORDER BY t.create_time ASC LIMIT #{limitSize};]]>
    </select>

    <select id="sumProgressByJobExecutionId">
        <![CDATA[SELECT SUM(progress) FROM exchangis_launched_task_entity WHERE job_execution_id = #{jobExecutionId};]]>
    </select>
</mapper>