wlzboy
2025-11-05 37de2f4b0f732ca5c19582d4a340ad7c987925b5
ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
@@ -18,6 +18,8 @@
        <result property="delFlag"      column="del_flag"     />
        <result property="loginIp"      column="login_ip"     />
        <result property="loginDate"    column="login_date"   />
        <result property="oaUserId"     column="oa_user_id"   />
        <result property="oaOrderClass" column="oa_order_class" />
        <result property="createBy"     column="create_by"    />
        <result property="createTime"   column="create_time"  />
        <result property="updateBy"     column="update_by"    />
@@ -47,7 +49,7 @@
    </resultMap>
   
   <sql id="selectUserVo">
        select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,
        select u.user_id, u.dept_id, u.user_name,u.oa_user_id, u.oa_order_class, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.oa_user_id, u.create_by, u.create_time, u.remark,
        d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status,
        r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
        from sys_user u
@@ -64,7 +66,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}
@@ -79,7 +81,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}
@@ -124,7 +163,11 @@
       <include refid="selectUserVo"/>
      where u.user_name = #{userName} and u.del_flag = '0'
   </select>
    <select id="selectUserByPhonenumber" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.phonenumber = #{phonenumber} and u.del_flag = '0'
    </select>
   <select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
      <include refid="selectUserVo"/>
      where u.user_id = #{userId}
@@ -154,6 +197,8 @@
          <if test="sex != null and sex != ''">sex,</if>
          <if test="password != null and password != ''">password,</if>
          <if test="status != null and status != ''">status,</if>
          <if test="oaUserId != null">oa_user_id,</if>
          <if test="oaOrderClass != null and oaOrderClass != ''">oa_order_class,</if>
          <if test="createBy != null and createBy != ''">create_by,</if>
          <if test="remark != null and remark != ''">remark,</if>
          create_time
@@ -168,6 +213,8 @@
          <if test="sex != null and sex != ''">#{sex},</if>
          <if test="password != null and password != ''">#{password},</if>
          <if test="status != null and status != ''">#{status},</if>
          <if test="oaUserId != null">#{oaUserId},</if>
          <if test="oaOrderClass != null and oaOrderClass != ''">#{oaOrderClass},</if>
          <if test="createBy != null and createBy != ''">#{createBy},</if>
          <if test="remark != null and remark != ''">#{remark},</if>
          sysdate()
@@ -185,6 +232,8 @@
          <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
          <if test="password != null and password != ''">password = #{password},</if>
          <if test="status != null and status != ''">status = #{status},</if>
          <if test="oaUserId != null">oa_user_id = #{oaUserId},</if>
          <if test="oaOrderClass != null">oa_order_class = #{oaOrderClass},</if>
          <if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
          <if test="loginDate != null">login_date = #{loginDate},</if>
          <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
@@ -216,5 +265,13 @@
          #{userId}
        </foreach> 
    </delete>
   <!-- 根据oaUserId查询用户 -->
   <select id="selectUserByOaUserId" parameterType="Integer" resultMap="SysUserResult">
      select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.sex, u.status, u.oa_user_id
      from sys_user u
      where u.oa_user_id = #{oaUserId} and u.del_flag = '0'
      limit 1
   </select>
   
</mapper>