<?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.TaskMaterialInfoMapper">

    <resultMap type="TaskMaterialInfo" id="TaskMaterialInfoResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialDesc"    column="material_desc"    />
        <result property="materialLink"    column="material_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="fileSize"    column="file_size"    />
    </resultMap>

    <sql id="selectTaskMaterialInfoVo">
        select
        tmi.id,
        tmi.task_id,
        tmi.material_name,
        tmi.material_desc,
        tmi.material_link,
        tmi.create_time,
        tmi.update_time,
        tmi.create_by,
        tmi.update_by,
        tmi.deleted,
        tmi.file_size
        from task_material_info tmi
        left join task_info ti on ti.id = tmi.task_id
    </sql>

    <select id="selectTaskMaterialInfoList" parameterType="TaskMaterialInfo" resultMap="TaskMaterialInfoResult">
        <include refid="selectTaskMaterialInfoVo"/>
        <where>
            <!-- 公司过滤（必须传入 companyId） -->
            <if test="companyId != null">
                and ti.company_id = #{companyId}
            </if>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialDesc != null  and materialDesc != ''"> and material_desc = #{materialDesc}</if>
            <if test="materialLink != null  and materialLink != ''"> and material_link = #{materialLink}</if>
            <if test="deleted != null "> and deleted = #{deleted}</if>
        </where>
        <!-- 添加排序：最新创建的材料在最前面 -->
        order by create_time desc
    </select>

    <select id="selectTaskMaterialInfoById" parameterType="Long" resultMap="TaskMaterialInfoResult">
        <include refid="selectTaskMaterialInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertTaskMaterialInfo" parameterType="TaskMaterialInfo" useGeneratedKeys="true" keyProperty="id">
        insert into task_material_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialDesc != null">material_desc,</if>
            <if test="materialLink != null">material_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="materialName != null">#{materialName},</if>
            <if test="materialDesc != null">#{materialDesc},</if>
            <if test="materialLink != null">#{materialLink},</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="updateTaskMaterialInfo" parameterType="TaskMaterialInfo">
        update task_material_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialDesc != null">material_desc = #{materialDesc},</if>
            <if test="materialLink != null">material_link = #{materialLink},</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="deleteTaskMaterialInfoById" parameterType="Long">
        delete from task_material_info where id = #{id}
    </delete>

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