<?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.fs.task.mapper.TaskDeliveryInfoMapper">
    
    <resultMap type="TaskDeliveryInfo" id="TaskDeliveryInfoResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="deliveryNo"    column="delivery_no"    />
        <result property="deliveryName"    column="delivery_name"    />
        <result property="validViews"    column="valid_views"    />
        <result property="viewCount"    column="view_count"    />
        <result property="projectLink"    column="project_link"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="deleted"    column="deleted"    />
        <result property="taskName"    column="task_name"    />
        <result property="taskType" column="task_type"/>
    </resultMap>

    <sql id="selectTaskDeliveryInfoVo">
        SELECT
        tdi.id,
        tdi.task_id,
        ti.task_type,
        ti.task_name,
        tdi.delivery_no,
        tdi.delivery_name,
        tdi.valid_views,
        tdi.view_count,
        tdi.project_link,
        tdi.create_time,
        tdi.update_time,
        tdi.create_by,
        tdi.update_by,
        tdi.deleted
        FROM task_delivery_info tdi
        LEFT JOIN task_info ti ON ti.id = tdi.task_id
    </sql>

    <select id="selectTaskDeliveryInfoList" parameterType="TaskDeliveryInfo" resultMap="TaskDeliveryInfoResult">
        <include refid="selectTaskDeliveryInfoVo"/>
        <where>
            <if test="taskId != null "> and tdi.task_id = #{taskId}</if>

            <!-- 如果要按任务名称搜，就加这条 -->
            <if test="taskName != null and taskName != ''">
                and ti.task_name like concat('%', #{taskName}, '%')
            </if>
            <if test="companyId != null">
                and ti.company_id = #{companyId}
            </if>
            <if test="deliveryNo != null  and deliveryNo != ''"> and tdi.delivery_no = #{deliveryNo}</if>
            <if test="deliveryName != null  and deliveryName != ''"> and tdi.delivery_name like concat('%', #{deliveryName}, '%')</if>
            <if test="validViews != null "> and tdi.valid_views = #{validViews}</if>
            <if test="viewCount != null "> and tdi.view_count = #{viewCount}</if>
            <if test="projectLink != null  and projectLink != ''"> and tdi.project_link = #{projectLink}</if>
            <if test="deleted != null "> and tdi.deleted = #{deleted}</if>
        </where>
        <!-- 添加排序：最新创建的任务在最前面 -->
        order by tdi.create_time desc
    </select>


    <select id="selectTaskDeliveryInfoById" parameterType="Long" resultMap="TaskDeliveryInfoResult">
        <include refid="selectTaskDeliveryInfoVo"/>
        where tdi.id = #{id}
    </select>
        
    <insert id="insertTaskDeliveryInfo" parameterType="TaskDeliveryInfo" useGeneratedKeys="true" keyProperty="id">
        insert into task_delivery_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="taskType != null">task_type,</if>
            <if test="deliveryNo != null">delivery_no,</if>
            <if test="deliveryName != null">delivery_name,</if>
            <if test="validViews != null">valid_views,</if>
            <if test="viewCount != null">view_count,</if>
            <if test="projectLink != null">project_link,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="deliveryNo != null">#{deliveryNo},</if>
            <if test="deliveryName != null">#{deliveryName},</if>
            <if test="validViews != null">#{validViews},</if>
            <if test="viewCount != null">#{viewCount},</if>
            <if test="projectLink != null">#{projectLink},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateTaskDeliveryInfo" parameterType="TaskDeliveryInfo">
        update task_delivery_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskType != null">task_type = #{taskType},</if> <!-- ✅ -->
            <if test="deliveryNo != null">delivery_no = #{deliveryNo},</if>
            <if test="deliveryName != null">delivery_name = #{deliveryName},</if>
            <if test="validViews != null">valid_views = #{validViews},</if>
            <if test="viewCount != null">view_count = #{viewCount},</if>
            <if test="projectLink != null">project_link = #{projectLink},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTaskDeliveryInfoById" parameterType="Long">
        delete from task_delivery_info where id = #{id}
    </delete>

    <delete id="deleteTaskDeliveryInfoByIds" parameterType="String">
        delete from task_delivery_info where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



    <select
            id="getDeliveryInfoByTaskTypeAndDeliveryNo"
            parameterType="TaskDeliveryInfo"
            resultMap="TaskDeliveryInfoResult"
    >
        SELECT
        tdi.id,
        tdi.task_id,
        tdi.task_type,
        ti.task_name,
        tdi.delivery_no,
        tdi.delivery_name,
        tdi.valid_views,
        tdi.view_count,
        tdi.project_link,
        tdi.create_time,
        tdi.update_time,
        tdi.create_by,
        tdi.update_by,
        tdi.deleted
        FROM task_delivery_info tdi
        LEFT JOIN task_info ti ON ti.id = tdi.task_id
        WHERE tdi.task_type = #{taskType}
        AND tdi.delivery_no = #{deliveryNo}
        AND tdi.deleted = 0
        LIMIT 1
    </select>

</mapper>