| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashSet; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import com.ruoyi.common.utils.DeptUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | |
| | | { |
| | | return getChildList(list, t).size() > 0; |
| | | } |
| | | |
| | | /** |
| | | * 计算指定用户的分公司列表 |
| | | * 逻辑: |
| | | * 1. 根据用户的oaOrderClass匹配分公司(服务单编码/调度单编码) |
| | | * 2. 附加用户所属的分公司(从deptId/ancestors解析) |
| | | * 3. 去重后返回 |
| | | * |
| | | * @param user 用户信息 |
| | | * @return 分公司列表 |
| | | */ |
| | | @Override |
| | | public List<SysDept> computeBranchCompaniesForUser(SysUser user) |
| | | { |
| | | List<SysDept> result = new ArrayList<>(); |
| | | Set<Long> seen = new HashSet<>(); |
| | | |
| | | if (user == null) { |
| | | return result; |
| | | } |
| | | |
| | | // 1. 根据oaOrderClass匹配分公司(优化:一次查询) |
| | | String orderClass = user.getOaOrderClass(); |
| | | if (StringUtils.isNotEmpty(orderClass)) |
| | | { |
| | | // 解析orderClass为编码列表 |
| | | List<String> orderCodes = new ArrayList<>(); |
| | | for (String raw : orderClass.split(",")) { |
| | | String code = raw.trim(); |
| | | if (!code.isEmpty()) { |
| | | orderCodes.add(code); |
| | | } |
| | | } |
| | | |
| | | // 一次性查询所有匹配的分公司 |
| | | if (!orderCodes.isEmpty()) { |
| | | List<SysDept> matchedDepts = deptMapper.selectBranchCompaniesByOrderCodes(orderCodes); |
| | | for (SysDept dept : matchedDepts) { |
| | | if (seen.add(dept.getDeptId())) { |
| | | result.add(dept); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 2. 附加该用户所属分公司(从deptId/ancestors解析) |
| | | if (user.getDeptId() != null) |
| | | { |
| | | SysDept userDept = selectDeptById(user.getDeptId()); |
| | | Long branchCompanyId = null; |
| | | |
| | | if (userDept != null) |
| | | { |
| | | // 判断是否本身就是分公司 |
| | | if (userDept.getParentId() != null && userDept.getParentId() == 100L) |
| | | { |
| | | branchCompanyId = userDept.getDeptId(); |
| | | } |
| | | // 否则从ancestors中查找 |
| | | else if (userDept.getAncestors() != null && !userDept.getAncestors().isEmpty()) |
| | | { |
| | | String[] ancestorIds = userDept.getAncestors().split(","); |
| | | for (int i = 0; i < ancestorIds.length; i++) |
| | | { |
| | | // 找到100的下一个就是分公司ID |
| | | if ("100".equals(ancestorIds[i]) && i + 1 < ancestorIds.length) |
| | | { |
| | | try { |
| | | branchCompanyId = Long.parseLong(ancestorIds[i + 1]); |
| | | } catch (NumberFormatException e) { |
| | | branchCompanyId = null; |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 如果找到了分公司ID,添加到结果中 |
| | | if (branchCompanyId != null) |
| | | { |
| | | SysDept branchCompany = selectDeptById(branchCompanyId); |
| | | if (branchCompany != null && seen.add(branchCompany.getDeptId())) |
| | | { |
| | | result.add(branchCompany); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public Long getBranchCompany(Long deptId) { |
| | | Long branchId = DeptUtil.getBranchId(deptId); |
| | | if(branchId!=null)return branchId; |
| | | branchId = deptMapper.selectBranchCompanyIdByDeptId(deptId); |
| | | if(branchId!=null){ |
| | | DeptUtil.setDeptIdBranchId(deptId,branchId); |
| | | return branchId; |
| | | } |
| | | else { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据service_class查询部门信息 |
| | | * |
| | | * @param serviceClass 服务类别编码 |
| | | * @return 部门信息 |
| | | */ |
| | | @Override |
| | | public SysDept selectDeptByServiceClass(String serviceClass) |
| | | { |
| | | return deptMapper.selectDeptByServiceClass(serviceClass); |
| | | } |
| | | } |