<?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.BmUnitInfoMapper">
    
    <resultMap type="com.bonus.sgzb.base.domain.BmUnitInfo" id="BmUnitInfoResult">
    	<id     property="unitId"      column="unit_id"      />
        <result property="unitName"    column="unit_name"    />
        <result property="status"     column="status"     />
        <result property="typeId"   column="type_id"   />
        <result property="linkMan"    column="link_man"    />
        <result property="telphone"      column="telphone"      />
        <result property="deptId"    column="dept_id"    />
        <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"    />
        <result property="deptName"    column="dept_name"    />
        <result property="typeName"    column="name"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="nick_name"    />
    </resultMap>
    <resultMap type="com.bonus.sgzb.system.api.domain.SysUser" id="UsersResult">
        <id     property="userId"      column="user_id"    />
        <result property="userName"    column="nick_name"    />
    </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,type_name,dept_name
		from bm_unit_info
    </sql>


    <select id="getUnitInfoAll"  resultMap="BmUnitInfoResult">
       <!-- <include refid="bmUnitInfo"/>-->
        select a.unit_id, a.unit_name, a.status, a.type_id, a.link_man, a.telphone, a.dept_id, a.del_flag, a.create_by, a.create_time,
        a.update_by, a.update_time, a.remark, a.company_id,b.dept_name,c.name
        from bm_unit_info a
        left join sys_dept b on a.dept_id = b.dept_id
        left join sys_dic c on a.type_id = c.id
        where a.del_flag = '0'
        <if test="unitId != null and unitId != ''">
            AND a.unit_id = #{unitId}
        </if>
        <if test="unitName != null and unitName != ''">
            AND a.unit_name like concat('%', #{unitName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND a.status = #{status}
        </if>
        <if test="typeId != null and typeId != ''">
            AND a.type_id = #{typeId}
        </if>
        <if test="linkMan != null and linkMan != ''">
            and a.link_man = #{linkMan}
        </if>
        <if test="telphone != null and telphone != ''">
            and a.telphone = #{telphone}
        </if>
        <if test="deptId != null and deptId != ''">
            and a.dept_id = #{deptId}
        </if>
        <if test="delFlag != null and delFlag != ''">
            and a.del_flag = #{delFlag}
        </if>
        <if test="createBy != null and createBy != ''">
            and a.create_by = #{createBy}
        </if>
        <if test="createTime != null and createTime != ''">
            and a.create_time = #{createTime}
        </if>
        <if test="updateBy != null and updateBy != ''">
            and a.update_by = #{updateBy}
        </if>
        <if test="updateTime != null and updateTime != ''">
            and a.update_time = #{updateTime}
        </if>
        <if test="remark != null and remark != ''">
            and a.remark = #{remark}
        </if>
        <if test="companyId != null and companyId != ''">
            and a.company_id = #{companyId}
        </if>
        <if test="deptName != null and deptName != ''">
            AND a.dept_name = #{deptName}
        </if>
        <if test="typeName != null and typeName != ''">
            AND a.type_name = #{typeName}
        </if>
        ORDER BY a.create_time DESC
    </select>
    <select id="getUserAll"  resultMap="UsersResult">
        select user_id id,nick_name name
        from sys_user
        where
        <if test="userId != null and userId != ''">
            user_id = #{userId}
        </if>
        <if test="userName != null and userName != ''">
            AND nick_name like concat('%', #{userName}, '%')
        </if>
    </select>
    <select id="selectTypeList" parameterType="com.bonus.sgzb.base.domain.BmUnitInfo" resultMap="BmUnitInfoResult">
        SELECT a.unit_id, a.unit_name, a.status, a.type_id, a.link_man, a.telphone, a.dept_id, a.del_flag, a.create_by,
        a.create_time, a.update_by, a.update_time, a.remark, a.company_id, b.dept_name, c.name, be.user_id, cf.nick_name 
        FROM bm_unit_info a
        LEFT JOIN sys_dept b ON a.dept_id = b.dept_id
        LEFT JOIN sys_dic c ON a.type_id = c.id
        LEFT JOIN bm_unit_person be ON be.unit_id = a.unit_id
        LEFT JOIN sys_user cf ON cf.user_id = be.user_id
        <where>
            <if test="unitName != null  and unitName != ''"> a.unit_name like concat('%', #{unitName}, '%')</if>
        </where>
        ORDER BY CASE WHEN be.user_id = #{userId} THEN 0 ELSE 1 END, be.user_id DESC
    </select>
    <select id="getUnitInfo" parameterType="com.bonus.sgzb.base.domain.BmUnitInfo" resultMap="BmUnitInfoResult">
        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,type_name,dept_name
        from bm_unit_info
         where del_flag = '0'
			<if test="unitId != null and unitId != ''">
				AND unit_id = #{unitId}
			</if>
			<if test="unitName != null and unitName != ''">
                AND unit_name like concat('%', #{unitName}, '%')
			</if>
			<if test="status != null and status != ''">
				AND status = #{status}
			</if>
			<if test="typeId != null and typeId != ''">
                AND type_id = #{typeId}
			</if>
			<if test="linkMan != null and linkMan != ''">
				and link_man = #{linkMan}
			</if>
            <if test="telphone != null and telphone != ''">
                and telphone = #{telphone}
            </if>
            <if test="deptId != null and deptId != ''">
                and dept_id = #{deptId}
            </if>
            <if test="delFlag != null and delFlag != ''">
                and del_flag = #{delFlag}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="createTime != null and createTime != ''">
                and create_time = #{createTime}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="updateTime != null and updateTime != ''">
                and update_time = #{updateTime}
            </if>
            <if test="remark != null and remark != ''">
                and remark = #{remark}
            </if>
            <if test="companyId != null and companyId != ''">
                and company_id = #{companyId}
            </if>
            <if test="deptName != null and deptName != ''">
            AND dept_name = #{deptName}
            </if>
            <if test="typeName != null and typeName != ''">
            AND type_name = #{typeName}
            </if>
    </select>
    <select id="selectByName" resultType="com.bonus.sgzb.base.domain.BmUnitInfo">
        select * from bm_unit_info
        where
        del_flag = '0' and status = '0'
        <if test="unitName != null and unitName != '' ">and unit_name = #{unitName}</if>
    </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="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="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="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>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="deptName != null and deptName != ''">dept_name = #{deptName},</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=")">
        	#{unitIds}
        </foreach>
    </delete>
    
</mapper>