<?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.DeptMapper">
    
    <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="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="bmUnitInfo">
        select unit_id, unit_name, status, type_id, link_man, telphone, dept_id, del_flag, create_by, create_time,
               update_by, update_time, remark, company_id
		from bm_unit_info
    </sql>


    <select id="getUnitInfoAll"  resultMap="sysDeptResult">
       <!-- <include refid="bmUnitInfo"/>-->
        select dept_id, parent_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag,
        create_by, create_time, update_by, update_time
        from sys_dept
    </select>
    
    <select id="getDeptList" parameterType="com.bonus.sgzb.base.domain.BmUnitInfo" resultMap="sysDeptResult">
        select dept_id, parent_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag,
        create_by, create_time, update_by, update_time
        from sys_dept
    </select>
    <select id="getDeptBySocialCreditCode" resultMap="sysDeptResult">
        select * from sys_dept where social_credit_code = #{socialCreditCode}
    </select>


    <insert id="unitInfoAdd" parameterType="com.bonus.sgzb.base.domain.BmUnitInfo">
        insert into bm_unit_info (
			<if test="unitName != null and unitName != '' ">unit_name,</if>
			<if test="status != null and status != '' ">status,</if>
			<if test="typeId != null and typeId != '' ">type_id,</if>
			<if test="linkMan != null and linkMan != '' ">link_man,</if>
			<if test="telphone != null and telphone != ''">telphone,</if>
			<if test="deptId != null and deptId != ''">dept_id,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null and updateTime != ''">update_time,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="companyId != null and companyId != ''">company_id,</if>
            create_time
        )values(
			<if test="unitName != null and unitName != ''">#{unitName},</if>
			<if test="status != null and status != ''">#{status},</if>
			<if test="typeId != null and typeId != ''">#{typeId},</if>
			<if test="linkMan != null and linkMan != ''">#{linkMan},</if>
			<if test="telphone != null and telphone != ''">#{telphone},</if>
			<if test="deptId != null and deptId != ''">#{deptId},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">#{updateTime},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="companyId != null and companyId != ''">#{companyId},</if>
            sysdate()
		)
    </insert>
	 
    <update id="updateBmUnitInfo" parameterType="com.bonus.sgzb.base.domain.BmUnitInfo">
        update bm_unit_info
        <set>
            <if test="unitName != null and unitName != ''">unit_name = #{unitName},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="typeId != null and typeId != ''">type_id = #{typeId},</if>
            <if test="linkMan != null and linkMan != ''">link_man = #{linkMan},</if>
            <if test="telphone != null and telphone != ''">telphone = #{telphone},</if>
            <if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="createBy != null and createBy != ''">create_by =#{createBy},</if>
            <if test="createTime != null and createTime != ''">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by =#{updateBy},</if>

            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="companyId != null and companyId != ''">company_id = #{companyId},</if>
            update_time = sysdate()
        </set>
        where unit_id = #{unitId}
    </update>
	
    <delete id="deleteUnitInfoById" parameterType="Long">
        update bm_unit_info set del_flag = '2' where unit_id = #{unitId}
    </delete>
    
    <delete id="remove" parameterType="Long">
        delete from bm_unit_info where unit_id in
        <foreach item="unitId" collection="array" open="(" separator="," close=")">
        	#{unitId}
        </foreach>
    </delete>
    
</mapper>