<?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"/>
        <result column="mis_org_code" property="misOrgCode" jdbcType="VARCHAR"/>
        <result column="mis_org_name" property="misOrgName" jdbcType="VARCHAR"/>
        <result column="mis_include" property="misInclude" jdbcType="CHAR"/>
        <result column="mis_child" property="misChild" 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,
		 mis_org_code, mis_org_name, mis_include, mis_child
	</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>
            <if test="misOrgCode != null">
                mis_org_code,
            </if>
            <if test="misOrgName != null">
                mis_org_name,
            </if>
            <if test="misInclude != null">
                mis_include,
            </if>
            <if test="misChild != null">
                mis_child,
            </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>
            <if test="misOrgCode != null">
                #{misOrgCode,jdbcType=VARCHAR},
            </if>
            <if test="misOrgName != null">
                #{misOrgName,jdbcType=VARCHAR},
            </if>
            <if test="misInclude != null">
                #{misInclude,jdbcType=CHAR},
            </if>
            <if test="misChild != null">
                #{misChild,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>
            <if test="misOrgCode != null">
                mis_org_code = #{misOrgCode,jdbcType=VARCHAR},
            </if>
            <if test="misOrgName != null">
                mis_org_name = #{misOrgName,jdbcType=VARCHAR},
            </if>
            <if test="misInclude != null">
                mis_include = #{misInclude,jdbcType=CHAR},
            </if>
            <if test="misChild != null">
                mis_child = #{misChild,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="idStr != null and !&quot;&quot;.equals(idStr)">
                and id in (${idStr})
            </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>
    <select id="getChildDepartsByUserId" resultType="com.izouma.awesomeadmin.model.DepartInfo">
		SELECT
	      *
        FROM
            sys_depart
        WHERE
            sys_depart.del_flag = 'N'
        AND (
            FIND_IN_SET(
                sys_depart.id,
                (
                    SELECT
                        sys_user.mis_child
                    FROM
                        sys_user
                    WHERE
                        sys_user.del_flag = 'N'
                    AND sys_user.id =  #{userId}
                )
            )
            OR FIND_IN_SET(
                sys_depart.id,
                (
                    SELECT
                        child_depart.mis_child
                    FROM
                        sys_depart child_depart
                    WHERE
                        child_depart.id = (
                            SELECT
                                sys_user_depart.depart_id
                            FROM
                                sys_user_depart,
                                sys_user
                            WHERE
                                sys_user_depart.user_id = #{userId}
                            AND sys_user.inherit_flag = 'Y'
                            AND sys_user.id = #{userId}
                            ORDER BY
                                sys_user_depart.id DESC
                            LIMIT 1
                        )
                )
            )
        )
	</select>
</mapper>

