<?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.Ai.Aizn.AiznImageTaskMapper">

    <sql id="includeAiznImageTask">
        id,
        user_id,
        COALESCE(task_type, '') task_type,
        task_id,
        image_url,
        COALESCE(result_image_url, '') result_image_url,
        task_submit_json,
        COALESCE(task_result_json, '') task_result_json,
        status,
        create_time,
        update_time
    </sql>
    <resultMap id="resultMapAiznImageTask" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" javaType="java.lang.Long"/>
        <result property="task_type" column="task_type" />
        <result property="task_id" column="task_id" />
        <result property="image_url" column="image_url" />
        <result property="result_image_url" column="result_image_url" />
        <result property="task_submit_json" column="task_submit_json" />
        <result property="task_result_json" column="task_result_json" />
        <result property="status" column="status" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryAiznImageTask" resultMap="resultMapAiznImageTask">
        SELECT <include refid="includeAiznImageTask" />
        FROM ai_zn_image_task
        <where>
            <if test="task_type != null and task_type != ''">
                AND task_type = #{task_type}
            </if>
            <if test="task_id != null and task_id != ''">
                AND task_id = #{task_id}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="user_id != null and user_id != ''">
                AND user_id = #{user_id}
            </if>
            <if test="create_time_begin != null and create_time_begin != '' and create_time_end != null and create_time_end != ''">
                AND create_time BETWEEN #{create_time_begin} AND #{create_time_end}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="queryAiznImageTaskDetail" resultMap="resultMapAiznImageTask">
        SELECT <include refid="includeAiznImageTask" />
        FROM ai_zn_image_task
        <where>
            task_id = #{task_id}
            <if test="user_id != null and user_id != ''">
                AND user_id = #{user_id}
            </if>
        </where>
    </select>

    <insert id="insertAiznImageTask" parameterType="com.backendsys.entity.Ai.Aizn.AiznImageTaskVO"
        useGeneratedKeys="true" keyProperty="task_id">
        INSERT INTO ai_zn_image_task (
             user_id, task_id, image_url, task_submit_json
            <if test="task_type != null and task_type != ''">, task_type</if>
        )
        VALUES (
            #{user_id}, #{task_id}, #{image_url}, #{task_submit_json}
            <if test="task_type != null and task_type != ''">, #{task_type}</if>
        )
    </insert>

    <update id="updateAiznImageTask" parameterType="com.backendsys.entity.Ai.Aizn.AiznImageTaskVO"
        useGeneratedKeys="true" keyProperty="task_id">
        UPDATE ai_zn_image_task
        SET status = #{status}
            <if test="result_image_url != null and result_image_url != ''">, result_image_url = #{result_image_url}</if>
            <if test="task_result_json != null and task_result_json != ''">, task_result_json = #{task_result_json}</if>
        WHERE task_id = #{task_id}
    </update>

</mapper>