<?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.material.clz.mapper.BmTeamMapper">
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO bm_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="teamName != null">unit_name,</if>
            <if test="departId != null">depart_id,</if>
            <if test="relName != null">link_man,</if>
            <if test="relPhone != null">telphone,</if>
            create_time,
            <if test="createUser != null">create_by,</if>
            del_flag,
            <if test="projectId != null">project_id,</if>
            <if test="idCard != null and idCard != ''">bzz_idcard,</if>
            <if test="typeId != null">type_id,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="teamName != null">#{teamName},</if>
            <if test="departId != null">#{departId},</if>
            <if test="relName != null">#{relName},</if>
            <if test="relPhone != null">#{relPhone},</if>
            NOW(),
            <if test="createUser != null">#{createUser},</if>
            0,
            <if test="projectId != null">#{projectId},</if>
            <if test="idCard != null and idCard != ''">#{idCard},</if>
            <if test="typeId != null">#{typeId},</if>
        </trim>
    </insert>

    <update id="update">
        UPDATE bm_unit
        <set>
            <if test="teamName != null">unit_name = #{teamName},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="relName != null">link_man = #{relName},</if>
            <if test="relPhone != null">telphone = #{relPhone},</if>
            <if test="updateUser != null">update_by = #{updateUser},</if>
            <if test="status != null">status = #{status},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="idCard != null">bzz_idcard = #{idCard},</if>
            update_time = NOW()
        </set>
        WHERE unit_id = #{id}
    </update>

    <delete id="deleteById">
        update bm_unit
        set del_flag = '2'
        where unit_id = #{id}
    </delete>

    <select id="queryByPage" resultType="com.bonus.material.clz.domain.BmTeam">
        select t.unit_id              as id,
               t.unit_name       as teamName,
               t.depart_id       as departId,
               sd.depart_name    as departName,
               t.link_man        as relName,
               t.telphone       as relPhone,
               t.create_time     as createTime,
               t.create_by     as createUser,
               t.update_time    as updateTime,
               t.update_by     as updateUser,
               t.del_flag       as delFlag,
               t.bzz_idcard     as teamLeaderIdCard
        from bm_unit t
        left join clz_pro_depart sd on t.depart_id = sd.id
        where t.del_flag = 0 and t.status = '0'
        <if test="departId != null ">
            and t.depart_id =  #{departId}
        </if>
        <if test="createUser != null">
            and t.create_by = #{createUser}
        </if>
        <if test="typeId != null">
            and t.type_id = #{typeId}
        </if>
        <if test="idCard != null and idCard != ''">
            and t.bzz_idcard = #{idCard}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (
            t.unit_name like concat('%', #{keyWord}, '%') or
            sd.depart_name like concat('%', #{keyWord}, '%') or
            t.link_man like concat('%', #{keyWord}, '%') or
            t.telphone like concat('%', #{keyWord}, '%')
            )
        </if>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectByName" resultType="com.bonus.material.clz.domain.BmTeam">
        select unit_id as id, unit_name as teamName, link_man as relName,
        telphone as relPhone, create_time as createTime, create_by as createUser,
        update_time as updateTime, update_by as updateUser, del_flag as delFlag
        from bm_unit
        where del_flag = 0 and status = '0'
        <if test="teamName != null and teamName != ''">
            and unit_name = #{teamName}
        </if>
    </select>

    <select id="getTeamList" resultType="com.bonus.material.clz.domain.BmTeam">
        SELECT DISTINCT bu.unit_id AS id,
        bu.unit_name AS teamName,
        bu.link_man AS relName,
        bu.telphone AS relPhone
        FROM bm_project bpl
        LEFT JOIN clz_bm_agreement_info bai ON bpl.pro_id = bai.project_id AND bai.`status` = '1'
        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id AND bu.del_flag = '0'
        WHERE bpl.pro_id = #{proId} AND bpl.del_flag = '0'
    </select>
</mapper>