<?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.securitycontrol.background.mapper.TeamManageMapper">

    <!--新增/修改班组-->
    <insert id="addOrUpdateTeam">
        <if test="type == 1">
            INSERT INTO tb_work_team
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="teamId != null and teamId != ''">team_id,</if>
                <if test="teamName != null and teamName != ''">team_name,</if>
                <if test="teamLeader != null and teamLeader != ''">team_leader,</if>
                <if test="bidCode != null and bidCode!=''">bid_code,</if>
                <if test="teamLeaderPhone != null and teamLeaderPhone!=''">team_leader_phone,</if>
                <if test="idNumber != null and idNumber!=''">id_number,</if>
                <if test="status != null and status!=''">status,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="teamId != null and teamId != ''">#{teamId},</if>
                <if test="teamName != null and teamName != ''">#{teamName},</if>
                <if test="teamLeader != null and teamLeader != ''">#{teamLeader},</if>
                <if test="bidCode != null and bidCode!=''">#{bidCode},</if>
                <if test="teamLeaderPhone != null and teamLeaderPhone!=''">#{teamLeaderPhone},</if>
                <if test="idNumber != null and idNumber!=''">#{idNumber},</if>
                <if test="status != null and status!=''">#{status},</if>
            </trim>
        </if>
        <if test="type == 2">
            UPDATE tb_work_team
            <set>
                <if test="teamName != null and teamName != ''">team_name = #{teamName},</if>
                <if test="teamLeader != null and teamLeader != ''">team_leader = #{teamLeader},</if>
                <if test="bidCode != null and bidCode != ''">bid_code = #{bidCode},</if>
                <if test="teamLeaderPhone != null and teamLeaderPhone != ''">team_leader_phone = #{teamLeaderPhone},</if>
                <if test="idNumber != null and idNumber != ''">id_number = #{idNumber},</if>
                <if test="status != null and status != ''">status = #{status},</if>
            </set>
            WHERE team_id = #{teamId}
        </if>
    </insert>
    <!--添加班组人员-->
    <insert id="addTeamUser">
        <if test="type == 1">
            UPDATE t_team_people SET team_id = #{teamId} WHERE user_id = #{userId}
        </if>
        <if test="type == 2">
            UPDATE t_team_people SET team_id = NULL WHERE user_id = #{userId}
        </if>
    </insert>
    <!--移出班组人员-->
    <update id="removeTeamUser">
        UPDATE t_team_people SET team_id = NULL WHERE user_id = #{id}
    </update>
    <!--删除班组-->
    <delete id="delTeam">
        DELETE FROM tb_work_team WHERE team_id = #{id}
    </delete>

    <!--获取班组列表-->
    <select id="getTeamLists" resultType="com.securitycontrol.entity.background.vo.TeamManageVo">
        SELECT
        twt.team_id AS teamId,
        twt.team_name as teamName,
        twt.team_leader as teamleader,
        twt.team_leader_phone as teamleaderphone,
        tp.pro_name AS proName,
        twt.team_num as teamNum,
        twt.status,
        twt.bid_code AS bidCode
        FROM tb_work_team twt
        LEFT JOIN tb_project tp on twt.bid_code = tp.bid_code
        <where>
            <if test="teamName !=null and teamName!=''">
                AND INSTR(twt.team_name,#{teamName}) > 0
            </if>
            <if test="teamLeader !=null and teamLeader!=''">
                AND INSTR(twt.team_leader,#{teamLeader}) > 0
            </if>
        </where>
    </select>

    <!--班组是否存在-->
    <select id="isTeamExist" resultType="java.util.Map">
        <if test="teamId == null or teamId == ''">
            SELECT team_id AS id,id_number AS value FROM tb_work_team
        </if>
        <if test="teamId != null and teamId != ''">
            SELECT team_id AS id,id_number AS value FROM tb_work_team WHERE team_id != #{teamId}
        </if>
    </select>
    <!--班组详情-->
    <select id="getTeamDetailById" resultType="com.securitycontrol.entity.background.vo.TeamManageVo">
        SELECT
            twt.team_id AS teamId,
            twt.team_name as teamname,
            twt.team_leader as teamleader,
            twt.team_leader_phone as teamleaderphone,
            twt.status,
            twt.bid_code AS bidCode,
            twt.id_number AS idNumber
        FROM tb_work_team twt
        WHERE team_id = #{id}
    </select>
    <!--班组是否存在班组人员-->
    <select id="isPeopleByTeam" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM t_team_people WHERE team_id = #{id}
    </select>
    <!--班组人员数量-->
    <select id="getTeamUserNum" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM t_team_people WHERE team_id = #{teamId}
    </select>
    <!--获取班组人员列表-->
    <select id="getTeamUserLists" resultType="com.securitycontrol.entity.background.vo.HumanManageVo">
        SELECT
        ttp.user_id AS userId,
        ttp.user_name AS userName,
        sd.dict_name AS userType
        FROM t_team_people ttp
        LEFT JOIN sys_dict sd ON ttp.user_type = sd.dict_code
        <where>
            ttp.team_id = #{id}
            <if test="userName!=null and userName !=''">
                AND INSTR(ttp.user_name,#{userName}) > 0
            </if>
        </where>
    </select>
    <!--获取班组组员和未加入班组人员-->
    <select id="getTeamUserAndNoBandingUser" resultType="java.util.Map">
        SELECT user_id AS id,user_name AS userName,'1' AS type FROM t_team_people WHERE team_id = #{id}
        UNION ALL
        SELECT user_id AS id,user_name AS userName,'2' AS type FROM t_team_people WHERE team_id IS NULL OR team_id = ''
    </select>
</mapper>