<?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.hys.app.mapper.system.AdminUserMapper">

    <!--查询平台管理员列表-->
    <select id="selectPageDto" resultType="com.hys.app.model.system.dto.AdminUserDTO" useCache="false">
        select u.*,r.role_name,d.name deptName,p.name postName
        from erp_company_user u
        left join es_role r ON u.role_id=r.role_id
        left join erp_company_dept d ON u.dept_id=d.id
        left join erp_post p ON u.post_id=p.id
        <where>
            <if test="companyId != null ">
                and u.company_id = #{companyId}
            </if>
            <if test="keyword != null and keyword != ''">
                and (username like concat('%',#{keyword},'%') or department like concat('%' ,#{keyword} ,'%') or real_name like concat('%' ,#{keyword}, '%'))
            </if>
            <if test="userState != null">
                and u.user_state = #{userState}
            </if>
        </where>
        order by sort, date_line desc
    </select>
    <select id="getAllDept" resultType="com.hys.app.model.system.dos.DeptDO">
        SELECT * FROM `erp_company_dept`  where deleted = 0
    </select>
    <select id="getAllSubDept" resultType="java.lang.Long">
        WITH RECURSIVE SubDepartments AS (
            SELECT id
            FROM erp_company_dept
            WHERE id = #{deptId}
            UNION ALL
            SELECT d.id
            FROM erp_company_dept d
                     INNER JOIN SubDepartments sd ON d.parent_id = sd.id
        )
        SELECT * FROM erp_company_dept;
    </select>

</mapper>
