<?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.hzs.system.sys.mapper.SysUserTeamMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysUserTeam">
        <result column="USER_ID" property="userId"/>
        <result column="PK_TEAM" property="pkTeam"/>
    </resultMap>

    <!-- 批量新增用户查询体系信息 -->
    <insert id="insertUserTeamBatch">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            into sys_user_team(user_id, pk_team)
            values
            (#{item.userId},#{item.pkTeam})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 通过用户ID删除用户和团队关联 -->
    <delete id="deleteUserTeamByUserId">
        delete
        from sys_user_team
        where user_id = #{userId}
    </delete>

    <!-- 根据管理员ID查询团队列表 -->
    <select id="queryListByUserId" resultType="com.hzs.common.domain.system.sys.ext.SysUserTeamExt">
        select sut.pk_team, cmt.team_name
        from sys_user_team sut
                 left join cu_member_team cmt
                           on cmt.pk_id = sut.pk_team
                               and cmt.del_flag = 0
        where sut.user_id = #{userId}
        order by cmt.creation_time
    </select>

    <!-- 通过用户ID查询团队会员列表 -->
    <select id="queryTeamByUser" resultType="long">
        select cmt.pk_member
        from SYS_USER_TEAM sut
                 inner join cu_member_team cmt
                            on cmt.pk_id = sut.pk_team
                                and cmt.del_flag = 0
        where sut.user_id = #{userId}
        order by sut.PK_TEAM
    </select>

</mapper>
