<?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.sgzb.base.mapper.BmUnitTypeMapper">
    
    <resultMap type="com.bonus.sgzb.base.domain.BmUnitType" id="BmUnitTypeResult">
        <result property="typeId"    column="type_id"    />
        <result property="typeName"    column="type_name"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>
    <resultMap type="com.bonus.sgzb.system.api.domain.SysDept" id="SysDeptResult">
        <id     property="deptId"     column="dept_id"     />
        <result property="parentId"   column="parent_id"   />
        <result property="ancestors"  column="ancestors"   />
        <result property="deptName"   column="dept_name"   />
        <result property="orderNum"   column="order_num"   />
        <result property="leader"     column="leader"      />
        <result property="phone"      column="phone"       />
        <result property="email"      column="email"       />
        <result property="status"     column="status"      />
        <result property="delFlag"    column="del_flag"    />
        <result property="parentName" column="parent_name" />
        <result property="createBy"   column="create_by"   />
        <result property="createTime" column="create_time" />
        <result property="updateBy"   column="update_by"   />
        <result property="updateTime" column="update_time" />
    </resultMap>
    <sql id="selectBmUnitTypeVo">
        select type_id, type_name, status, del_flag, create_by, create_time, update_by, update_time, remark, company_id from bm_unit_type
    </sql>
    <sql id="selectSysDeptVo">
        select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time
        from sys_dept d
    </sql>
    <select id="selectBmUnitTypeList" parameterType="com.bonus.sgzb.base.domain.BmUnitType" resultMap="BmUnitTypeResult">
        <include refid="selectBmUnitTypeVo"/>
        <where>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="companyId != null  and companyId != ''"> and company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectTypeList" parameterType="com.bonus.sgzb.base.domain.BmUnitType" resultMap="BmUnitTypeResult">
        select a.type_id, a.type_name, a.status, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time,
        a.remark, a.company_id ,b.user_id ,c.user_name
        from bm_unit_info a
        left join bm_unit_person b on b.unit_id = a.unit_id
        left join sys_user c on c.user_id = b.user_id
        <where>
            <if test="typeName != null  and typeName != ''"> a.type_name like concat('%', #{typeName}, '%')</if>
        </where>
    </select>
    <select id="selectSysDeptTree" parameterType="com.bonus.sgzb.system.api.domain.SysDept" resultMap="SysDeptResult">
        <include refid="selectSysDeptVo"/>
    </select>
    <select id="selectSysDeptList" parameterType="com.bonus.sgzb.system.api.domain.SysDept" resultMap="SysDeptResult">
        <include refid="selectSysDeptVo"/>
        where d.del_flag = '0'
        <if test="deptId != null and deptId != 0">
            AND dept_id = #{deptId}
        </if>
        <if test="parentId != null and parentId != 0">
            AND parent_id = #{parentId}
        </if>
        <if test="deptName != null and deptName != ''">
            AND dept_name like concat('%', #{deptName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.parent_id, d.order_num
    </select>
    <select id="selectBmUnitTypeByTypeId" parameterType="String" resultMap="BmUnitTypeResult">
        <include refid="selectBmUnitTypeVo"/>
        where type_id = #{typeId}
    </select>
    <insert id="insertBmUnitType" parameterType="com.bonus.sgzb.base.domain.BmUnitType" useGeneratedKeys="true" keyProperty="typeId">
        insert into bm_unit_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updateBmUnitType" parameterType="com.bonus.sgzb.base.domain.BmUnitType">
        update bm_unit_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where type_id = #{typeId}
    </update>
    <update id="update" parameterType="String">
        update bm_unit_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = '1',</if>
        </trim>
        where type_id = #{typeId}
    </update>
    <update id="updates" parameterType="String">
        update bm_unit_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = '0',</if>
        </trim>
        where type_id = #{typeId}
    </update>

    <delete id="deleteBmUnitTypeByTypeId" parameterType="Long">
        delete from bm_unit_type where type_id = #{typeId}
    </delete>

    <delete id="deleteBmUnitTypeByTypeIds" parameterType="String">
        delete from bm_unit_type where type_id in 
        <foreach item="typeId" collection="array" open="(" separator="," close=")">
            #{typeId}
        </foreach>
    </delete>
</mapper>