<?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="cn.sagacloud.mybatis.mapper.TaskMapper">
    <insert id="addTask" parameterType="cn.sagacloud.mybatis.model.TaskModel" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO scheduler.task(task_name, task_cmd, task_param, task_status, task_result_json, task_sent_time, task_last_client, task_expected_finish_time)
        VALUE(#{task_name},#{task_cmd},#{task_param},#{task_status},#{task_result_json}, #{task_sent_time}, #{task_last_client},#{task_expected_finish_time});
    </insert>

    <insert id="addDownload" parameterType="cn.sagacloud.mybatis.model.DownloadTaskModel" useGeneratedKeys="true" keyProperty="list.id">
        INSERT INTO scheduler.download(task_id,task_url,task_md5) VALUES
        <foreach collection="list" item="item" separator=",">
            (#{taskId},#{item.task_url},#{item.task_md5})
        </foreach>
    </insert>

    <select id="getTasksByIds" resultMap="taskModelList">
        select task.id, task_name, task_cmd, task_param, task_status, task_result_json, task_sent_time, task_last_client,
        task_expected_finish_time, download.id downloadId, task_id, task_url, task_md5
        from scheduler.task inner join scheduler.download on scheduler.task.id = scheduler.download.task_id where scheduler.task.id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>;
    </select>

    <select id="getTasksByStatus" resultMap="taskModelList">
        select task.id, task_name, task_cmd, task_param, task_status, task_result_json, task_sent_time, task_last_client,
        task_expected_finish_time, download.id downloadId, task_id, task_url, task_md5
        from scheduler.task inner join scheduler.download on scheduler.task.id = scheduler.download.task_id where scheduler.task.task_status in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>;
    </select>

    <select id="getTasksMapByIds" resultMap="taskModelList">
        select task.id, task_name, task_cmd, task_param, task_status, task_result_json, task_sent_time, task_last_client,
        task_expected_finish_time, download.id downloadId, task_id, task_url, task_md5
        from scheduler.task inner join scheduler.download on scheduler.task.id = scheduler.download.task_id where scheduler.task.id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>;
    </select>

    <select id="getTasksMapByStatus" resultMap="taskModelList">
        select task.id, task_name, task_cmd, task_param, task_status, task_result_json, task_sent_time, task_last_client,
        task_expected_finish_time, download.id downloadId, task_id, task_url, task_md5
        from scheduler.task inner join scheduler.download on scheduler.task.id = scheduler.download.task_id where scheduler.task.task_status in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>;
    </select>

    <resultMap id="taskModelList" type="cn.sagacloud.mybatis.model.TaskModel">
        <id property="id" column="id"/>
        <result property="task_name" column="task_name"/>
        <result property="task_cmd" column="task_cmd"/>
        <result property="task_param" column="task_param"/>
        <result property="task_status" column="task_status"/>
        <result property="task_result_json" column="task_result_json"/>
        <result property="task_last_client" column="task_last_client"/>
        <result property="task_expected_finish_time" column="task_expected_finish_time"/>
        <collection property="downloadTaskModelList" ofType="cn.sagacloud.mybatis.model.DownloadTaskModel">
            <id property="id" column="downloadId"/>
            <result property="task_id" column="task_id"/>
            <result property="task_url" column="task_url"/>
            <result property="task_md5" column="task_md5"/>
        </collection>
    </resultMap>

    <update id="updateSingleTask" parameterType="cn.sagacloud.mybatis.model.TaskModel">
        UPDATE scheduler.task
        <trim prefix="SET" suffixOverrides=",">
            <if test="task_status!=null">task_status=#{task_status},</if>
            <if test="task_result_json!=null">task_result_json=#{task_result_json},</if>
            <if test="task_last_client!=null">task_last_client=#{task_last_client},</if>
            <if test="task_sent_time!=null">task_sent_time=#{task_sent_time},</if>
        </trim>
        WHERE id = #{id};
    </update>

</mapper>
