<?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.backendsys.mapper.Ali.AliDashscopeTaskMapper">

    <sql id="includeDashscopeTask">
        id,
        user_id,
        request_id,
        task_id,
        task_status,
        COALESCE(thumb, '') thumb,
        COALESCE(submit_time, '') submit_time,
        COALESCE(scheduled_time, '') scheduled_time,
        COALESCE(end_time, '') end_time,
        COALESCE(task_metrics_total, '') task_metrics_total,
        COALESCE(task_metrics_succeeded, '') task_metrics_succeeded,
        COALESCE(task_metrics_failed, '') task_metrics_failed,
        create_time,
        update_time
    </sql>
    <sql id="includeDashscopeTaskDetail">
        t.id id,
        t.user_id user_id,
        t.request_id request_id,
        t.task_id task_id,
        t.task_status task_status,
        COALESCE(t.thumb, '') thumb,
        COALESCE(t.submit_time, '') submit_time,
        COALESCE(t.scheduled_time, '') scheduled_time,
        COALESCE(t.end_time, '') end_time,
        COALESCE(t.task_metrics_total, '') task_metrics_total,
        COALESCE(t.task_metrics_succeeded, '') task_metrics_succeeded,
        COALESCE(t.task_metrics_failed, '') task_metrics_failed,
        COALESCE(tr.url, '') url,
        COALESCE(tr.url_local, '') url_local,
        t.create_time create_time,
        t.update_time update_time
    </sql>

    <resultMap id="resultMapDashscopeTaskList" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" />
        <result property="request_id" column="request_id" />
        <result property="task_id" column="task_id" />
        <result property="task_status" column="task_status" />
        <result property="thumb" column="thumb" />
        <result property="submit_time" column="submit_time" />
        <result property="scheduled_time" column="scheduled_time" />
        <result property="end_time" column="end_time" />
        <result property="task_metrics_total" column="task_metrics_total" javaType="java.lang.Integer"/>
        <result property="task_metrics_succeeded" column="task_metrics_succeeded" javaType="java.lang.Integer"/>
        <result property="task_metrics_failed" column="task_metrics_failed" javaType="java.lang.Integer"/>
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <resultMap id="resultMapDashscopeTaskDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" />
        <result property="request_id" column="request_id" />
        <result property="task_id" column="task_id" />
        <result property="task_status" column="task_status" />
        <result property="thumb" column="thumb" />
        <result property="submit_time" column="submit_time" />
        <result property="scheduled_time" column="scheduled_time" />
        <result property="end_time" column="end_time" />
        <result property="task_metrics_total" column="task_metrics_total" javaType="java.lang.Integer"/>
        <result property="task_metrics_succeeded" column="task_metrics_succeeded" javaType="java.lang.Integer"/>
        <result property="task_metrics_failed" column="task_metrics_failed" javaType="java.lang.Integer"/>
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
        <collection property="results" javaType="java.util.List" column="task_id" ofType="java.util.LinkedHashMap"
            select="queryDashscopeTaskResultList">
            <id property="id" column="id" />
            <result property="url" column="url" />
            <result property="url_local" column="url_local" />
        </collection>
    </resultMap>


    <resultMap id="resultMapDashscopeTaskResultList" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="task_id" column="task_id" />
        <result property="url" column="url" />
        <result property="url_local" column="url_local" />
    </resultMap>

    <!-- 查询 列表 -->
    <select id="queryDashscopeTaskList" resultMap="resultMapDashscopeTaskList">
        SELECT <include refid="includeDashscopeTask" />
        FROM ali_dashscope_task
        WHERE user_id = #{user_id}
        ORDER BY create_time DESC
    </select>

    <!-- 查询 详情 -->
    <select id="queryDashscopeTaskDetail" resultMap="resultMapDashscopeTaskDetail">
        SELECT <include refid="includeDashscopeTask" />
        FROM ali_dashscope_task
        WHERE task_id = #{task_id} AND user_id = #{user_id}
    </select>

    <!-- 查询 结果列表 (子查询) -->
    <select id="queryDashscopeTaskResultList" resultMap="resultMapDashscopeTaskResultList">
        SELECT id, task_id, url, url_local
        FROM ali_dashscope_task_result
        WHERE task_id = #{task_id}
    </select>

    <!-- 创建 -->
    <insert id="insertDashscopeTask" parameterType="com.backendsys.entity.Ali.AliDashscopeTaskDTO"
        useGeneratedKeys="true" keyProperty="task_id">
        INSERT INTO ali_dashscope_task (user_id, request_id, task_id, task_status
            <if test="thumb != null and thumb != ''">, thumb</if>
            <if test="submit_time != null and submit_time != ''">, submit_time</if>
            <if test="scheduled_time != null and scheduled_time != ''">, scheduled_time</if>
            <if test="end_time != null and end_time != ''">, end_time</if>
            <if test="task_metrics_total != null and task_metrics_total != ''">, task_metrics_total</if>
            <if test="task_metrics_succeeded != null and task_metrics_succeeded != ''">, task_metrics_succeeded</if>
            <if test="task_metrics_failed != null and task_metrics_failed != ''">, task_metrics_failed</if>
        )
        VALUES (#{user_id}, #{request_id}, #{task_id}, #{task_status}
            <if test="thumb != null and thumb != ''">, #{thumb}</if>
            <if test="submit_time != null and submit_time != ''">, #{submit_time}</if>
            <if test="scheduled_time != null and scheduled_time != ''">, #{scheduled_time}</if>
            <if test="end_time != null and end_time != ''">, #{end_time}</if>
            <if test="task_metrics_total != null and task_metrics_total != ''">, #{task_metrics_total}</if>
            <if test="task_metrics_succeeded != null and task_metrics_succeeded != ''">, #{task_metrics_succeeded}</if>
            <if test="task_metrics_failed != null and task_metrics_failed != ''">, #{task_metrics_failed}</if>
        )
    </insert>

    <!-- 创建 结果 -->
    <insert id="insertDashscopeTaskResult" parameterType="java.util.List">
        INSERT INTO ali_dashscope_task_result (task_id, url, url_local)
        VALUES
        <foreach collection="results" item="result" separator=",">
        (
             #{result.task_id}, #{result.url}
            <if test="result.url_local != null and result.url_local != ''">, #{result.url_local}</if>
        )
        </foreach>
    </insert>

    <update id="updateDashscopeTask" parameterType="com.backendsys.entity.Ali.AliDashscopeTaskDTO"
        useGeneratedKeys="true" keyProperty="task_id">
        UPDATE ali_dashscope_task
        SET task_status = #{task_status}
        <if test="request_id != null and request_id != ''">, request_id = #{request_id}</if>
        <if test="thumb != null and thumb != ''">, thumb = #{thumb}</if>
        <if test="submit_time != null and submit_time != ''">, submit_time = #{submit_time}</if>
        <if test="scheduled_time != null and scheduled_time != ''">, scheduled_time = #{scheduled_time}</if>
        <if test="end_time != null and end_time != ''">, end_time = #{end_time}</if>
        <if test="task_metrics_total != null and task_metrics_total != ''">, task_metrics_total = #{task_metrics_total}</if>
        <if test="task_metrics_succeeded != null and task_metrics_succeeded != ''">, task_metrics_succeeded = #{task_metrics_succeeded}</if>
        <if test="task_metrics_failed != null and task_metrics_failed != ''">, task_metrics_failed = #{task_metrics_failed}</if>
        WHERE task_id = #{task_id}
    </update>


    <!-- 删除 (物理) -->
<!--    <delete id="deleteDashscopeTask" parameterType="java.lang.Long">-->
<!--        DELETE u, ui-->
<!--        FROM sys_user u-->
<!--        LEFT JOIN sys_user_info ui ON u.id = ui.user_id-->
<!--        WHERE u.id = #{id}-->
<!--    </delete>-->

</mapper>