<?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="org.xyzh.study.mapper.TaskUserMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbTaskUser">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="task_id" property="taskID" jdbcType="VARCHAR"/>
        <result column="user_id" property="userID" jdbcType="VARCHAR"/>
        <result column="dept_id" property="deptID" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="progress" property="progress" jdbcType="DECIMAL"/>
        <result column="complete_time" property="completeTime" jdbcType="TIMESTAMP"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, task_id, user_id, dept_id, status, progress, complete_time, 
        creator, create_time, update_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="taskID != null and taskID != ''">
                AND task_id = #{taskID}
            </if>
            <if test="userID != null and userID != ''">
                AND user_id = #{userID}
            </if>
            <if test="deptID != null and deptID != ''">
                AND dept_id = #{deptID}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </sql>

    <!-- selectTaskUsers -->
    <select id="selectTaskUsers" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_task_user
        <include refid="Where_Clause"/>
        ORDER BY create_time DESC
    </select>

    <!-- 根据关联ID查询关联信息 -->
    <select id="selectByRelationId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        WHERE id = #{relationId}
    </select>

    <!-- 根据任务ID查询用户关联列表 -->
    <select id="selectByTaskId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        WHERE task_id = #{taskId}
        ORDER BY create_time DESC
    </select>

    <!-- 根据用户ID查询任务关联列表 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        WHERE user_id = #{userId}
        ORDER BY create_time DESC
    </select>

    <!-- 根据任务ID和用户ID查询关联信息 -->
    <select id="selectByTaskIdAndUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        WHERE task_id = #{taskId} AND user_id = #{userId}
    </select>

    <!-- 根据状态查询任务用户关联列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        WHERE status = #{status}
        ORDER BY create_time DESC
    </select>

    <!-- 根据用户ID和状态查询任务关联列表 -->
    <select id="selectByUserIdAndStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        WHERE user_id = #{userId} AND status = #{status}
        ORDER BY create_time DESC
    </select>

    <!-- 检查任务用户关联是否存在 -->
    <select id="countByTaskIdAndUserId" resultType="int">
        SELECT COUNT(1)
        FROM tb_task_user
        WHERE task_id = #{taskId} AND user_id = #{userId}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入任务用户关联 -->
    <insert id="insertTaskUser" parameterType="org.xyzh.common.dto.study.TbTaskUser">
        INSERT INTO tb_task_user (
            id, task_id, user_id, dept_id, status, progress, complete_time,
            creator, create_time, update_time
        ) VALUES (
            #{id}, #{taskID}, #{userID}, #{deptID}, #{status}, #{progress}, #{completeTime},
            #{creator}, #{createTime}, #{updateTime}
        )
    </insert>

    <!-- 更新任务用户关联 -->
    <update id="updateTaskUser" parameterType="org.xyzh.common.dto.study.TbTaskUser">
        UPDATE tb_task_user
        <set>
            <if test="taskID != null and taskID != ''">
                task_id = #{taskID},
            </if>
            <if test="userID != null and userID != ''">
                user_id = #{userID},
            </if>
            <if test="deptID != null and deptID != ''">
                dept_id = #{deptID},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="progress != null">
                progress = #{progress},
            </if>
            <if test="completeTime != null">
                complete_time = #{completeTime},
            </if>
            <if test="creator != null and creator != ''">
                creator = #{creator},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除任务用户关联 -->
    <delete id="deleteTaskUser" parameterType="org.xyzh.common.dto.study.TbTaskUser">
        DELETE FROM tb_task_user
        WHERE id = #{id}
    </delete>

    <!-- 批量插入任务用户关联 -->
    <insert id="batchInsertTaskUsers" parameterType="java.util.List">
        INSERT INTO tb_task_user (
            id, task_id, user_id, dept_id, status, progress, complete_time,
            creator, create_time, update_time
        ) VALUES
        <foreach collection="taskUserList" item="item" separator=",">
            (
                #{item.id}, #{item.taskID}, #{item.userID}, #{item.deptID}, #{item.status}, 
                #{item.progress}, #{item.completeTime}, #{item.creator}, #{item.createTime}, #{item.updateTime}
            )
        </foreach>
    </insert>

    <!-- 批量删除任务用户关联 -->
    <delete id="batchDeleteTaskUsers">
        DELETE FROM tb_task_user
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 根据任务ID批量删除用户关联 -->
    <delete id="deleteByTaskId">
        DELETE FROM tb_task_user
        WHERE task_id = #{taskId}
    </delete>

    <!-- 根据用户ID批量删除任务关联 -->
    <delete id="deleteByUserId">
        DELETE FROM tb_task_user
        WHERE user_id = #{userId}
    </delete>

    <!-- 分页查询任务用户关联 -->
    <select id="selectTaskUsersPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_user
        <include refid="Where_Clause" />
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计任务用户关联总数 -->
    <select id="countTaskUsers" resultType="long">
        SELECT COUNT(1)
        FROM tb_task_user
        <include refid="Where_Clause" />
    </select>

</mapper>
