wlzboy
2025-10-19 3328aec7bc4cc2c090f015cba905a82d6d52870c
ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
@@ -65,7 +65,7 @@
         AND u.user_id = #{userId}
      </if>
      <if test="userName != null and userName != ''">
         AND u.user_name like concat('%', #{userName}, '%')
         AND u.nick_name like concat('%', #{userName}, '%')
      </if>
      <if test="status != null and status != ''">
         AND u.status = #{status}
@@ -80,7 +80,44 @@
         AND date_format(u.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
      </if>
      <if test="deptId != null and deptId != 0">
         AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
         <![CDATA[
         AND u.dept_id IN (
            -- 查询用户所属分公司及其所有子部门
            SELECT t.dept_id FROM sys_dept t
            WHERE t.del_flag = '0' AND (
               -- 情况1:传入的部门就是分公司(parent_id=100)
               (t.dept_id = ]]>#{deptId}<![CDATA[ AND EXISTS (
                  SELECT 1 FROM sys_dept d WHERE d.dept_id = ]]>#{deptId}<![CDATA[ AND d.parent_id = 100
               ))
               OR
               -- 查询该分公司的所有子部门
               (find_in_set(
                  (SELECT d.dept_id FROM sys_dept d WHERE d.dept_id = ]]>#{deptId}<![CDATA[ AND d.parent_id = 100),
                  t.ancestors
               ) > 0)
               OR
               -- 情况2:传入的是子部门,找到其所属分公司
               (t.dept_id IN (
                  SELECT branch.dept_id FROM sys_dept branch
                  WHERE branch.parent_id = 100
                    AND find_in_set(branch.dept_id, (
                     SELECT sub.ancestors FROM sys_dept sub WHERE sub.dept_id = ]]>#{deptId}<![CDATA[
                    )) > 0
               ))
               OR
               -- 查询该分公司的所有子部门
               (find_in_set(
                  (SELECT branch.dept_id FROM sys_dept branch
                   WHERE branch.parent_id = 100
                     AND find_in_set(branch.dept_id, (
                      SELECT sub.ancestors FROM sys_dept sub WHERE sub.dept_id = ]]>#{deptId}<![CDATA[
                     )) > 0
                  ),
                  t.ancestors
               ) > 0)
            )
         )
         ]]>
      </if>
      <!-- 数据范围过滤 -->
      ${params.dataScope}