<?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.izouma.awesomeadmin.dao.DepartInfoMapper" >
	<resultMap id="BaseResultMap" type="com.izouma.awesomeadmin.model.DepartInfo" >
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="depart_name" property="departName" jdbcType="VARCHAR" />
		<result column="parent_id" property="parentId" jdbcType="INTEGER" />
		<result column="all_parent_id" property="allParentId" jdbcType="VARCHAR" />
		<result column="description" property="description" jdbcType="VARCHAR" />
		<result column="depart_code" property="departCode" jdbcType="VARCHAR" />
		<result column="org_code" property="orgCode" jdbcType="VARCHAR" />
		<result column="type" property="type" jdbcType="INTEGER" />
		<result column="remark" property="remark" jdbcType="VARCHAR" />
		<result column="sort" property="sort" jdbcType="INTEGER" />
		<result column="del_flag" property="delFlag" jdbcType="CHAR" />
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
		<result column="permission" property="permission" jdbcType="VARCHAR" />
	</resultMap>
	<sql id="Base_Column_List" >
		 id, depart_name, parent_id, all_parent_id, description, depart_code, org_code, type, remark, sort, del_flag, create_time, permission
	</sql>
	<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
		select
		<include refid="Base_Column_List" />
		from sys_depart
		where id = #{id,jdbcType=INTEGER}
	</select>
	<select id="selectByPrimaryKeys" resultMap="BaseResultMap" parameterType="java.lang.String" >
		select
		<include refid="Base_Column_List" />
		from sys_depart
		where FIND_IN_SET(id, #{ids,jdbcType=INTEGER}) and del_flag = 'N'
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
		delete from sys_depart
		where id = #{id,jdbcType=INTEGER}
	</delete>
	<insert id="insertSelective" parameterType="com.izouma.awesomeadmin.model.DepartInfo" useGeneratedKeys="true" keyProperty="id">
		insert into sys_depart
		<trim prefix="(" suffix=")" suffixOverrides="," >
			<if test="id != null" >
				id,	
			</if>
			<if test="departName != null" >
				depart_name,	
			</if>
			<if test="parentId != null" >
				parent_id,	
			</if>
			<if test="allParentId != null" >
				all_parent_id,	
			</if>
			<if test="description != null" >
				description,	
			</if>
			<if test="departCode != null" >
				depart_code,	
			</if>
			<if test="orgCode != null" >
				org_code,	
			</if>
			<if test="type != null" >
				type,	
			</if>
			<if test="remark != null" >
				remark,	
			</if>
			<if test="sort != null" >
				sort,
			</if>
			<if test="delFlag != null" >
				del_flag,	
			</if>
			<if test="createTime != null" >
				create_time,	
			</if>
			<if test="permission != null" >
				permission,	
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides="," >
			<if test="id != null" >
				#{id,jdbcType=INTEGER},	
			</if>
			<if test="departName != null" >
				#{departName,jdbcType=VARCHAR},	
			</if>
			<if test="parentId != null" >
				#{parentId,jdbcType=INTEGER},	
			</if>
			<if test="allParentId != null" >
				#{allParentId,jdbcType=VARCHAR},	
			</if>
			<if test="description != null" >
				#{description,jdbcType=VARCHAR},	
			</if>
			<if test="departCode != null" >
				#{departCode,jdbcType=VARCHAR},	
			</if>
			<if test="orgCode != null" >
				#{orgCode,jdbcType=VARCHAR},	
			</if>
			<if test="type != null" >
				#{type,jdbcType=INTEGER},	
			</if>
			<if test="remark != null" >
				#{remark,jdbcType=VARCHAR},	
			</if>
			<if test="sort != null" >
				#{sort,jdbcType=INTEGER},
			</if>
			<if test="delFlag != null" >
				#{delFlag,jdbcType=CHAR},	
			</if>
			<if test="createTime != null" >
				#{createTime,jdbcType=TIMESTAMP},	
			</if>
			<if test="permission != null" >
				#{permission,jdbcType=VARCHAR},	
			</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="com.izouma.awesomeadmin.model.DepartInfo" >
		UPDATE sys_depart
		<set >
			<if test="departName != null" >
				depart_name = #{departName,jdbcType=VARCHAR},	
			</if>
			<if test="parentId != null" >
				parent_id = #{parentId,jdbcType=INTEGER},	
			</if>
			<if test="allParentId != null" >
				all_parent_id = #{allParentId,jdbcType=VARCHAR},	
			</if>
			<if test="description != null" >
				description = #{description,jdbcType=VARCHAR},	
			</if>
			<if test="departCode != null" >
				depart_code = #{departCode,jdbcType=VARCHAR},	
			</if>
			<if test="orgCode != null" >
				org_code = #{orgCode,jdbcType=VARCHAR},	
			</if>
			<if test="type != null" >
				type = #{type,jdbcType=INTEGER},	
			</if>
			<if test="remark != null" >
				remark = #{remark,jdbcType=VARCHAR},	
			</if>
			<if test="sort != null" >
				sort = #{sort,jdbcType=INTEGER},
			</if>
			<if test="delFlag != null" >
				del_flag = #{delFlag,jdbcType=CHAR},	
			</if>
			<if test="createTime != null" >
				create_time = #{createTime,jdbcType=TIMESTAMP},	
			</if>
			<if test="permission != null" >
				permission = #{permission,jdbcType=VARCHAR},	
			</if>
		</set>
		where id = #{id,jdbcType=INTEGER}
	</update>
	<select id="queryDepartInfosByPage" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.DepartInfo">
		select <include refid="Base_Column_List"/> from sys_depart
		<where>
			and del_flag = 'N'
			<if test="record.id != null and !&quot;&quot;.equals(record.id)">
				and id = #{record.id}	
			</if>
			<if test="record.departName != null and !&quot;&quot;.equals(record.departName)">
				and depart_name = #{record.departName}	
			</if>
			<if test="record.parentId != null and !&quot;&quot;.equals(record.parentId)">
				and parent_id = #{record.parentId}	
			</if>
			<if test="record.allParentId != null and !&quot;&quot;.equals(record.allParentId)">
				and all_parent_id = #{record.allParentId}	
			</if>
			<if test="record.description != null and !&quot;&quot;.equals(record.description)">
				and description = #{record.description}	
			</if>
			<if test="record.departCode != null and !&quot;&quot;.equals(record.departCode)">
				and depart_code = #{record.departCode}	
			</if>
			<if test="record.orgCode != null and !&quot;&quot;.equals(record.orgCode)">
				and org_code = #{record.orgCode}	
			</if>
			<if test="record.type != null and !&quot;&quot;.equals(record.type)">
				and type = #{record.type}	
			</if>
			<if test="record.remark != null and !&quot;&quot;.equals(record.remark)">
				and remark = #{record.remark}	
			</if>
			<if test="record.sort != null and !&quot;&quot;.equals(record.sort)">
				and sort = #{record.sort}
			</if>
			<if test="record.delFlag != null and !&quot;&quot;.equals(record.delFlag)">
				and del_flag = #{record.delFlag}	
			</if>
			<if test="record.createTime != null and !&quot;&quot;.equals(record.createTime)">
				and create_time = #{record.createTime}	
			</if>
			<if test="record.permission != null and !&quot;&quot;.equals(record.permission)">
				and permission = #{record.permission}	
			</if>
		</where>
		order by id desc
	</select>
	<select id="queryAllDepartInfo" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.DepartInfo">
		select <include refid="Base_Column_List"/> from sys_depart
		<where>
			and del_flag = 'N'
			<if test="id != null and !&quot;&quot;.equals(id)">
				and id = #{id}	
			</if>
			<if test="departName != null and !&quot;&quot;.equals(departName)">
				and depart_name = #{departName}	
			</if>
			<if test="parentId != null and !&quot;&quot;.equals(parentId)">
				and parent_id = #{parentId}	
			</if>
			<if test="allParentId != null and !&quot;&quot;.equals(allParentId)">
				and all_parent_id = #{allParentId}	
			</if>
			<if test="description != null and !&quot;&quot;.equals(description)">
				and description = #{description}	
			</if>
			<if test="departCode != null and !&quot;&quot;.equals(departCode)">
				and depart_code = #{departCode}	
			</if>
			<if test="orgCode != null and !&quot;&quot;.equals(orgCode)">
				and org_code = #{orgCode}	
			</if>
			<if test="type != null and !&quot;&quot;.equals(type)">
				and type = #{type}	
			</if>
			<if test="remark != null and !&quot;&quot;.equals(remark)">
				and remark = #{remark}	
			</if>
			<if test="sort != null and !&quot;&quot;.equals(sort)">
				and sort = #{sort}
			</if>
			<if test="delFlag != null and !&quot;&quot;.equals(delFlag)">
				and del_flag = #{delFlag}	
			</if>
			<if test="createTime != null and !&quot;&quot;.equals(createTime)">
				and create_time = #{createTime}	
			</if>
			<if test="permission != null and !&quot;&quot;.equals(permission)">
				and permission = #{permission}	
			</if>
		</where>
		order by id desc
	</select>
	<select id="queryDepartInfo" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.DepartInfo">
		select <include refid="Base_Column_List"/> from sys_depart
		<where>
			and del_flag = 'N'
			<if test="id != null and !&quot;&quot;.equals(id)">
				and id = #{id}	
			</if>
			<if test="departName != null and !&quot;&quot;.equals(departName)">
				and depart_name = #{departName}	
			</if>
			<if test="parentId != null and !&quot;&quot;.equals(parentId)">
				and parent_id = #{parentId}	
			</if>
			<if test="allParentId != null and !&quot;&quot;.equals(allParentId)">
				and all_parent_id = #{allParentId}	
			</if>
			<if test="description != null and !&quot;&quot;.equals(description)">
				and description = #{description}	
			</if>
			<if test="departCode != null and !&quot;&quot;.equals(departCode)">
				and depart_code = #{departCode}	
			</if>
			<if test="orgCode != null and !&quot;&quot;.equals(orgCode)">
				and org_code = #{orgCode}	
			</if>
			<if test="type != null and !&quot;&quot;.equals(type)">
				and type = #{type}	
			</if>
			<if test="remark != null and !&quot;&quot;.equals(remark)">
				and remark = #{remark}	
			</if>
			<if test="sort != null and !&quot;&quot;.equals(sort)">
				and sort = #{sort}
			</if>
			<if test="delFlag != null and !&quot;&quot;.equals(delFlag)">
				and del_flag = #{delFlag}	
			</if>
			<if test="createTime != null and !&quot;&quot;.equals(createTime)">
				and create_time = #{createTime}	
			</if>
			<if test="permission != null and !&quot;&quot;.equals(permission)">
				and permission = #{permission}	
			</if>
		</where>
		LIMIT 1
	</select>
    <select id="findPower" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.DepartInfo">
		select * from sys_depart where all_parent_id  LIKE '${allParentId}%'  and id=  #{parentId}
	</select>
    <update id="delete">
		UPDATE sys_depart SET del_flag = 'Y'
		<where>
			AND id = #{id}
		</where>
	</update>

	<select id="getParentList" resultType="departInfo" parameterType="integer">
	    SELECT * from sys_depart WHERE FIND_IN_SET(id,(SELECT all_parent_id from sys_depart WHERE id  = #{id})) AND del_flag = 'N'
	</select>

	<select id="getUserDeparts" resultType="com.izouma.awesomeadmin.model.DepartInfo">
		select
		<include refid="Base_Column_List" />
		from sys_depart
		where id in (select depart_id from sys_user_depart where user_id = #{userId})
		and del_flag = 'N'
	</select>
    <insert id="setUserDeparts">
		insert into sys_user_depart (user_id, depart_id)
		values
		<foreach collection="departs" item="departId" separator=",">
			(#{userId}, #{departId})
		</foreach>
	</insert>
	<delete id="clearUserDeparts">
		delete from sys_user_depart where user_id = #{userId}
	</delete>
	<select id="getUserDepartId" resultType="java.lang.String">
		SELECT group_concat(depart_id) FROM sys_user_depart WHERE user_id = #{id}
	</select>
	<select id="getDepartUsers" resultType="com.izouma.awesomeadmin.model.UserInfo">
		select * from sys_user where id in (select user_id from sys_user_depart where depart_id = #{departId}) and del_flag = 'N'
	</select>
</mapper>

