|
|
|
@ -20,6 +20,8 @@ import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
import org.dromara.common.core.utils.StringUtils; |
|
|
|
import org.dromara.system.domain.vo.SysDeptVo; |
|
|
|
import org.dromara.system.service.ISysDeptService; |
|
|
|
|
|
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE) |
|
|
|
public class DataFilterUtil { |
|
|
|
@ -409,70 +411,81 @@ public class DataFilterUtil { |
|
|
|
|
|
|
|
// ======================== 用户执法单位查询方法 ========================
|
|
|
|
/** |
|
|
|
* 查询指定用户所在的执法单位的 vir_id。 |
|
|
|
* 查询指定用户所在的执法单位的 sys_dept_id。 |
|
|
|
* <p> |
|
|
|
* 逻辑: |
|
|
|
* <ol> |
|
|
|
* <li>通过 userId 查询用户的 dept_id</li> |
|
|
|
* <li>用 dept_id 匹配 efcode_virtual_unit.sys_dept_id</li> |
|
|
|
* <li>优先返回 vir_show_at_business='是' 的记录的 vir_id</li> |
|
|
|
* <li>如果 vir_show_at_business='是' 的有多条,抛出异常</li> |
|
|
|
* <li>如果没有 vir_show_at_business='是' 的记录,且只有一条匹配 sys_dept_id 的记录,也直接返回</li> |
|
|
|
* <li>用 dept_id 查找 efcode_virtual_unit(按 sys_dept_id 匹配)</li> |
|
|
|
* <li>若存在匹配记录,优先返回 vir_show_at_business='是' 的记录的 sys_dept_id; |
|
|
|
* 若无 business 记录且只有一条匹配也直接返回</li> |
|
|
|
* <li>若不存在匹配记录,通过 sys_dept 查找父节点,再用父节点 dept_id 重复上述查找</li> |
|
|
|
* <li>递归向上最多查 4 层父节点</li> |
|
|
|
* </ol> |
|
|
|
* |
|
|
|
* @param userId 用户ID |
|
|
|
* @return 用户所在执法单位的 vir_id;如果用户不存在或未分配部门则返回 null |
|
|
|
* @throws IllegalStateException 当匹配到多条 vir_show_at_business='是' 的记录时抛出 |
|
|
|
* @return 用户所在执法单位的 sys_dept_id;如果用户不存在、未分配部门或向上 4 层仍未找到则返回 null |
|
|
|
* @throws IllegalStateException 当某层匹配到多条 vir_show_at_business='是' 的记录,或多条匹配但无法确定唯一执法单位时抛出 |
|
|
|
*/ |
|
|
|
public static String getUserBureauDeptId(Long userId) { |
|
|
|
if (userId == null) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
// 1. 通过 userId 获取用户的 deptId
|
|
|
|
UserService userService = SpringUtils.getBean(UserService.class); |
|
|
|
List<UserDTO> users = userService.selectListByIds(Collections.singletonList(userId)); |
|
|
|
if (CollUtil.isEmpty(users)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
UserDTO user = users.get(0); |
|
|
|
if (user.getDeptId() == null) { |
|
|
|
String deptId = getUserDeptId(userId); |
|
|
|
if (StrUtil.isBlank(deptId)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
String deptId = String.valueOf(user.getDeptId()); |
|
|
|
|
|
|
|
// 2. 通过 deptId 查询 efcode_virtual_unit 中所有匹配 sys_dept_id 的记录
|
|
|
|
// 2. 从当前 deptId 开始,向上查找 sys_dept 父链,最多查 5 层(自身 + 4 层父节点)
|
|
|
|
IEfcodeVirtualUnitService virtualUnitService = SpringUtils.getBean(IEfcodeVirtualUnitService.class); |
|
|
|
ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class); |
|
|
|
|
|
|
|
String currentDeptId = deptId; |
|
|
|
for (int i = 0; i < 5; i++) { |
|
|
|
if (StrUtil.isBlank(currentDeptId)) { |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// 查询 efcode_virtual_unit 中所有匹配 sys_dept_id 的记录
|
|
|
|
EfcodeVirtualUnitBo bo = new EfcodeVirtualUnitBo(); |
|
|
|
bo.setSysDeptId(deptId); |
|
|
|
bo.setSysDeptId(currentDeptId); |
|
|
|
List<EfcodeVirtualUnitVo> allMatched = virtualUnitService.queryList(bo); |
|
|
|
if (CollUtil.isEmpty(allMatched)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
// 3. 筛选 vir_show_at_business='是' 的记录
|
|
|
|
if (CollUtil.isNotEmpty(allMatched)) { |
|
|
|
// 筛选 vir_show_at_business='是' 的记录
|
|
|
|
List<EfcodeVirtualUnitVo> businessUnits = allMatched.stream() |
|
|
|
.filter(v -> "是".equals(v.getVirShowAtBusiness())) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
if (businessUnits.size() == 1) { |
|
|
|
// 恰好一条业务记录,直接返回
|
|
|
|
return businessUnits.get(0).getVirId(); |
|
|
|
return businessUnits.get(0).getSysDeptId(); |
|
|
|
} |
|
|
|
if (businessUnits.size() > 1) { |
|
|
|
// 多条业务记录,抛出异常
|
|
|
|
throw new IllegalStateException( |
|
|
|
"用户(userId=" + userId + ")的部门(deptId=" + deptId |
|
|
|
"用户(userId=" + userId + ")的部门(deptId=" + currentDeptId |
|
|
|
+ ")在虚拟单位表中匹配到多条 vir_show_at_business='是' 的记录,无法确定唯一执法单位"); |
|
|
|
} |
|
|
|
|
|
|
|
// 4. 没有 vir_show_at_business='是' 的记录,如果只有一条匹配记录则返回
|
|
|
|
// 没有 business 记录,如果只有一条匹配则返回
|
|
|
|
if (allMatched.size() == 1) { |
|
|
|
return allMatched.get(0).getVirId(); |
|
|
|
return allMatched.get(0).getSysDeptId(); |
|
|
|
} |
|
|
|
|
|
|
|
// 多条匹配但无法确定唯一执法单位
|
|
|
|
// 多条匹配但无 business 标记,无法确定唯一执法单位
|
|
|
|
throw new IllegalStateException( |
|
|
|
"无法找到当前用户所在的执法单位!"); |
|
|
|
"用户(userId=" + userId + ")的部门(deptId=" + currentDeptId |
|
|
|
+ ")在虚拟单位表中匹配到多条记录且无 vir_show_at_business='是',无法确定唯一执法单位"); |
|
|
|
} |
|
|
|
|
|
|
|
// 不存在匹配记录,通过 sys_dept 查找父节点
|
|
|
|
SysDeptVo dept = sysDeptService.selectDeptById(currentDeptId); |
|
|
|
if (dept == null || StrUtil.isBlank(dept.getParentId()) || "0".equals(dept.getParentId())) { |
|
|
|
break; |
|
|
|
} |
|
|
|
currentDeptId = dept.getParentId(); |
|
|
|
} |
|
|
|
|
|
|
|
// 向上查找 4 层仍未找到对应的虚拟单位
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
// ======================== 私有辅助方法 ========================
|
|
|
|
|