<?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.yami.shop.dao.DeptUserMapper">

    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.DeptUser">
        <id column="id" property="id"/>
        <result column="dept_id" property="deptId"/>
        <result column="user_id" property="userId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>


    <!-- 根据用户ID获取部门用户关联列表 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT id, dept_id, user_id, create_time, update_time
        FROM tz_sys_dept_user
        WHERE user_id = #{userId}
    </select>

    <!-- 根据部门ID获取部门用户关联列表 -->
    <select id="selectByDeptId" resultMap="BaseResultMap">
        SELECT id, dept_id, user_id, create_time, update_time
        FROM tz_sys_dept_user
        WHERE dept_id = #{deptId}
    </select>

    <!-- 批量插入部门用户关联 -->
    <insert id="insertBatch">
        INSERT INTO tz_sys_dept_user (dept_id, user_id, create_time, update_time)
        VALUES
        <foreach collection="deptUsers" item="item" separator=",">
            (#{item.deptId}, #{item.userId}, #{item.createTime}, #{item.updateTime})
        </foreach>
    </insert>

    <!-- 根据用户ID删除部门用户关联 -->
    <delete id="deleteByUserId">
        DELETE FROM tz_sys_dept_user
        WHERE user_id = #{userId}
    </delete>

    <!-- 根据部门ID删除部门用户关联 -->
    <delete id="deleteByDeptId">
        DELETE FROM tz_sys_dept_user
        WHERE dept_id = #{deptId}
    </delete>

</mapper>
