<?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.bonus.bracelet.mapper.TeamMgeMapper">
    <!--添加班组-->
    <insert id="addTeam" useGeneratedKeys="true" keyProperty="teamId">
        INSERT INTO t_work_team
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="teamName != null and teamName != ''">team_name,</if>
            <if test="teamLeader != null and teamLeader != ''">team_leader,</if>
            <if test="leaderPhone != null and leaderPhone != ''">leader_phone,</if>
            <if test="proId != null">pro_id,</if>
            team_status,
            create_time,
            create_user,
            update_time,
            update_user,
            del_flag,
            team_id,
            team_leader_id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="teamName != null and teamName != ''">#{teamName},</if>
            <if test="teamLeader != null and teamLeader != ''">#{teamLeader},</if>
            <if test="leaderPhone != null and leaderPhone != ''">#{leaderPhone},</if>
            <if test="proId != null">#{proId},</if>
            #{teamStatus},
            #{createTime},
            #{createUser},
            #{updateTime},
            #{updateUser},
            0,
            null,
            #{teamLeaderId}
        </trim>
    </insert>
    <!--修改班组-->
    <update id="updateTeam">
        UPDATE t_work_team SET team_name = #{teamName},team_leader = #{teamLeader},leader_phone = #{leaderPhone},pro_id = #{proId},update_time = #{updateTime},update_user = #{updateUser},team_leader_id = #{teamLeaderId} WHERE team_id = #{teamId}
    </update>
    <!--解散班组-->
    <update id="dissolutionTeam">
        UPDATE t_work_team SET team_status = 1,js_time = #{jsTime},update_user = #{updateUser} WHERE team_id = #{id}
    </update>
    <!--删除班组-->
    <update id="delTeam">
        UPDATE t_work_team SET del_flag = 1 WHERE team_id = #{id}
    </update>
    <!--班组分配人员-->
    <update id="updatePersonsByTeam">
        UPDATE tb_people SET team_id = #{teamId}
        WHERE id IN
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>
    <!--清空班组已分配的班组人员-->
    <update id="updatePersonsByTeamId">
        UPDATE tb_people SET team_id = null WHERE team_id = #{teamId}
    </update>

    <!--班组列表-->
    <select id="getTeamLists" resultType="com.bonus.common.entity.bracelet.vo.TeamVo">
        SELECT twt.team_id AS teamId,
               twt.team_name AS teamName,
               twt.team_leader AS teamLeader,
               twt.leader_phone AS leaderPhone,
               twt.team_status AS teamStatus,
               twt.create_time AS createTime,
               tp.pro_name AS proName,
               twt.pro_id AS proId,
               twt.js_time AS jsTime
        FROM t_work_team twt
        LEFT JOIN tb_project tp ON twt.pro_id = tp.id AND twt.del_flag = 0
        WHERE twt.del_flag = 0
        <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>
        <if test="roleCode == 'administrators'">

        </if>
        <if test="roleCode == 'depart'">

        </if>
        <if test="roleCode == 'team'">

        </if>
        ORDER BY twt.create_time
    </select>
    <!--获取班组在施和未施人数-->
    <select id="getWorkNum" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM tb_people tp
        INNER JOIN tb_bracelet tb ON tp.id = tb.bid_id AND tb.del_flag = 0
        WHERE tp.team_id = #{teamId} AND tp.del_flag = 0
        UNION ALL
        SELECT COUNT(*)
        FROM tb_people tp
        LEFT JOIN tb_bracelet tb ON tp.id = tb.bid_id AND tb.del_flag = 0
        WHERE tp.team_id = #{teamId} AND tb.id IS NULL AND tp.del_flag = 0
    </select>
    <!--判断班组名称是否存在-->
    <select id="isExist" resultType="java.lang.Integer">
        <if test="teamId == null">
            SELECT COUNT(*) FROM t_work_team WHERE team_name = #{teamName} AND del_flag = 0 AND team_status = 0
        </if>
        <if test="teamId != null">
            SELECT COUNT(*) FROM t_work_team WHERE team_name = #{teamName} AND team_id !=#{teamId} AND del_flag = 0 AND team_status = 0
        </if>
    </select>
    <!--班组下是否存在人员-->
    <select id="isExistPerson" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_people WHERE team_id = #{id} AND del_flag = 0
    </select>
    <!--查询人员列表 type 1.全部人员 2.未分配人员 3.已分配人员 4.班组长-->
    <select id="getPersonList" resultType="java.util.Map">
        <if test="type == 4">
            SELECT id,
            name,
            id_card AS idCard,
            phone AS phone
            FROM tb_people
            WHERE del_flag = 0 AND post = 'team_leader'
        UNION ALL
            SELECT tp.id,
            tp.name,
            tp.id_card AS idCard,
            tp.phone AS phone
            FROM t_work_team twt
            LEFT JOIN tb_people tp ON twt.team_leader_id = tp.id AND tp.del_flag = 0
            WHERE twt.del_flag = 0 AND twt.team_id = #{teamId}
        </if>
        <if test="type!=4">
            SELECT id,
            name,
            id_card AS idCard,
            phone AS phone
            FROM tb_people
            WHERE del_flag = 0
            <if test="type == 2">
                AND team_id IS NULL
            </if>
            <if test="type == 3 and teamId==null">
                AND team_id IS NOT NULL
            </if>
            <if test="type == 3 and teamId!=null">
                AND team_id =#{teamId}
            </if>
        </if>
    </select>
    <!--班组详情-->
    <select id="getTeamInfo" resultType="com.bonus.common.entity.bracelet.vo.TeamVo">
        SELECT team_id AS teamId,
               team_name AS teamName,
               team_leader AS teamLeader,
               leader_phone AS leaderPhone,
               pro_id AS proId,
               team_leader_id AS teamLeaderId
        FROM t_work_team WHERE team_id = #{id}
    </select>
    <!--班组在施和未施人员-->
    <select id="getWorkPersonList" resultType="com.bonus.common.entity.bracelet.vo.TeamPeopleVo">
        SELECT tp.id,
               tp.name,
               tp.id_card AS idCard,
               tp.phone,
               tp.sex,
               IF(tb.id IS NULL,1,0) AS workStatus,
               tb.sh_code AS braceletCode
        FROM tb_people tp
        LEFT JOIN tb_bracelet tb ON tp.id = tb.bid_id AND tb.del_flag = 0
        WHERE tp.team_id = #{teamId} AND tp.del_flag = 0
        <if test="name != null and name!=''">
            AND INSTR(tp.name,#{name}) > 0
        </if>
        <if test="sex != null">
            AND tp.sex = #{sex}
        </if>
    </select>
</mapper>