<?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.basic.mapper.BmUnitPersonMapper">

    <insert id="insertBmUnitPerson" parameterType="com.bonus.material.basic.domain.BmUnitPerson">
        insert into bm_unit_person
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unitId != null">unit_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unitId != null">#{unitId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <delete id="deleteBmUnitPersonByID" parameterType="com.bonus.material.basic.domain.BmUnit">
        delete from bm_unit_person where unit_id = #{unitId} AND user_id = #{userId}
    </delete>

    <select id="selectBmUnitList" resultType="com.bonus.material.basic.domain.BmUnit">
        SELECT
            bu.unit_id AS unitId,
            bu.unit_name AS unitName,
            bu.STATUS AS STATUS,
            bu.type_id AS typeId,
            bu.link_man AS linkMan,
            bu.telphone AS telphone,
            bu.dept_id AS deptId,
            bu.del_flag AS delFlag,
            bu.create_by AS createBy,
            bu.create_time AS createTime,
            bu.update_by AS updateBy,
            bu.update_time AS updateTime,
            bu.remark AS remark,
            bup.user_id as userId,
            sdd.dict_label as typeName
        FROM
            bm_unit bu
                left join bm_unit_person bup ON bu.unit_id = bup.unit_id
        left join sys_dict_data sdd on bu.type_id = sdd.dict_code
        and sdd.dict_type = 'bm_unit_type'
        <if test="userId != null  and userId != ''">
            AND bup.user_id = #{userId}
        </if>
        WHERE
            bu.del_flag = 0
        <if test="unitName != null  and unitName != ''">
            and bu.unit_name like concat('%', #{unitName}, '%')
        </if>

        group by bu.unit_id
        order by bu.create_time desc
    </select>

</mapper>
