【8.3.3】【org】从新整理通用树形的接口

pull/62/head
stylefeng 2025-01-26 20:59:50 +08:00
parent 179f0930f7
commit 8cb191fc0c
3 changed files with 151 additions and 126 deletions

View File

@ -0,0 +1,139 @@
package cn.stylefeng.roses.kernel.sys.modular.org.factory;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.mp.datascope.config.DataScopeConfig;
import cn.stylefeng.roses.kernel.db.mp.datascope.holder.DataScopeHolder;
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.sys.api.enums.org.OrgTypeEnum;
import cn.stylefeng.roses.kernel.sys.modular.org.entity.HrOrganization;
import cn.stylefeng.roses.kernel.sys.modular.org.pojo.request.CommonOrgTreeRequest;
import cn.stylefeng.roses.kernel.sys.modular.org.pojo.request.HrOrganizationRequest;
import cn.stylefeng.roses.kernel.sys.modular.org.service.HrOrganizationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.Set;
/**
*
*
* @author fengshuonan
* @since 2025/1/26 20:00
*/
public class OrgConditionFactory {
/**
*
*
* @author fengshuonan
* @since 2025/1/26 20:00
*/
public static void prepareRequest(CommonOrgTreeRequest commonOrgTreeRequest) {
// 获取数据范围的信息如果数据范围是包含全部数据则将parentId设置为-1
DataScopeConfig dataScopeConfig = DataScopeHolder.get();
if (dataScopeConfig == null || dataScopeConfig.isTotalDataScope()) {
if (commonOrgTreeRequest.getOrgParentId() == null) {
commonOrgTreeRequest.setOrgParentId(TreeConstants.DEFAULT_PARENT_ID);
}
}
// 如果查询带组织机构名称的搜索,则清空其他条件
if (ObjectUtil.isNotEmpty(commonOrgTreeRequest.getSearchText())) {
commonOrgTreeRequest.setOrgParentId(null);
commonOrgTreeRequest.setIndexOrgIdList(null);
}
// 如果查询待组织机构的状态信息则清空parentId
if (ObjectUtil.isNotEmpty(commonOrgTreeRequest.getIndexOrgIdList())) {
commonOrgTreeRequest.setOrgParentId(null);
}
}
/**
* Wrapper
* <p>
* idid
*
* @author fengshuonan
* @since 2025/1/26 20:13
*/
public static LambdaQueryWrapper<HrOrganization> createCommonTreeWrapper(CommonOrgTreeRequest commonOrgTreeRequest, HrOrganizationService hrOrganizationService) {
// 创建基本的wrapper
HrOrganizationRequest hrOrganizationRequest = new HrOrganizationRequest();
hrOrganizationRequest.setSearchText(commonOrgTreeRequest.getSearchText());
LambdaQueryWrapper<HrOrganization> queryWrapper = OrgConditionFactory.createWrapper(hrOrganizationRequest);
// 如果查询条件有orgId则查询指定机构下的子机构
Long parentId = commonOrgTreeRequest.getOrgParentId();
if (parentId != null) {
queryWrapper.eq(HrOrganization::getOrgParentId, parentId);
}
// 如果有定位状态的组织机构,则需要查询到定位的组织机构的所有子一级
Set<Long> indexOrgIdList = commonOrgTreeRequest.getIndexOrgIdList();
if (ObjectUtil.isNotEmpty(indexOrgIdList)) {
Set<Long> parentIdListTotal = hrOrganizationService.queryOrgIdParentIdList(indexOrgIdList);
if (ObjectUtil.isNotEmpty(parentIdListTotal)) {
queryWrapper.in(HrOrganization::getOrgParentId, parentIdListTotal);
}
}
// 如果有筛选公司的标识,则只查询公司列表
Boolean companySearchFlag = commonOrgTreeRequest.getCompanySearchFlag();
if (ObjectUtil.isNotEmpty(companySearchFlag) && companySearchFlag) {
queryWrapper.eq(HrOrganization::getOrgType, OrgTypeEnum.COMPANY.getCode());
}
// 只查询启用状态的机构
queryWrapper.eq(HrOrganization::getStatusFlag, StatusEnum.ENABLE.getCode());
return queryWrapper;
}
/**
* Wrapper
* <p>
* id
*
* @author fengshuonan
* @since 2025/1/26 20:20
*/
public static LambdaQueryWrapper<HrOrganization> createWrapper(HrOrganizationRequest hrOrganizationRequest) {
LambdaQueryWrapper<HrOrganization> queryWrapper = new LambdaQueryWrapper<>();
// 如果按文本查询条件不为空,则判断组织机构名称、简称、税号、备注是否有匹配
String searchText = hrOrganizationRequest.getSearchText();
if (StrUtil.isNotEmpty(searchText)) {
queryWrapper.nested(wrap -> {
wrap.like(HrOrganization::getOrgName, searchText);
wrap.or().like(HrOrganization::getOrgShortName, searchText);
wrap.or().like(HrOrganization::getTaxNo, searchText);
wrap.or().like(HrOrganization::getOrgCode, searchText);
wrap.or().like(HrOrganization::getRemark, searchText);
});
}
// 根据机构状态查询
Integer statusFlag = hrOrganizationRequest.getStatusFlag();
queryWrapper.eq(ObjectUtil.isNotNull(statusFlag), HrOrganization::getStatusFlag, statusFlag);
// 如果查询条件有orgId则查询指定机构下的子机构
Long orgId = hrOrganizationRequest.getOrgId();
if (orgId != null) {
// 查询orgId对应的所有子机构包含本orgId
queryWrapper.nested(wrap -> {
wrap.eq(HrOrganization::getOrgParentId, orgId).or().eq(HrOrganization::getOrgId, orgId);
});
}
// 根据排序正序查询
queryWrapper.orderByAsc(HrOrganization::getOrgSort);
return queryWrapper;
}
}

View File

@ -1,7 +1,6 @@
package cn.stylefeng.roses.kernel.sys.modular.org.pojo.request;
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -26,7 +25,7 @@ public class CommonOrgTreeRequest extends BaseRequest {
*
*/
@ChineseDescription("父级id一级节点父id是-1")
private Long orgParentId = TreeConstants.DEFAULT_PARENT_ID;
private Long orgParentId;
/**
* id

View File

@ -38,6 +38,7 @@ import cn.stylefeng.roses.kernel.sys.api.pojo.org.HrOrganizationDTO;
import cn.stylefeng.roses.kernel.sys.api.pojo.org.OrganizationLevelRequest;
import cn.stylefeng.roses.kernel.sys.modular.org.constants.OrgConstants;
import cn.stylefeng.roses.kernel.sys.modular.org.entity.HrOrganization;
import cn.stylefeng.roses.kernel.sys.modular.org.factory.OrgConditionFactory;
import cn.stylefeng.roses.kernel.sys.modular.org.factory.OrganizationFactory;
import cn.stylefeng.roses.kernel.sys.modular.org.mapper.HrOrganizationMapper;
import cn.stylefeng.roses.kernel.sys.modular.org.pojo.request.CommonOrgTreeRequest;
@ -63,7 +64,6 @@ import java.util.stream.Collectors;
import static cn.stylefeng.roses.kernel.rule.constants.SymbolConstant.LEFT_SQUARE_BRACKETS;
import static cn.stylefeng.roses.kernel.rule.constants.SymbolConstant.RIGHT_SQUARE_BRACKETS;
import static java.util.stream.Collectors.toCollection;
/**
*
@ -200,14 +200,14 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
@Override
public List<HrOrganization> findList(HrOrganizationRequest hrOrganizationRequest) {
LambdaQueryWrapper<HrOrganization> wrapper = this.createWrapper(hrOrganizationRequest);
LambdaQueryWrapper<HrOrganization> wrapper = OrgConditionFactory.createWrapper(hrOrganizationRequest);
return this.list(wrapper);
}
@Override
@DataScope(userIdFieldName = "create_user")
public PageResult<HrOrganization> findPage(HrOrganizationRequest hrOrganizationRequest) {
LambdaQueryWrapper<HrOrganization> wrapper = createWrapper(hrOrganizationRequest);
LambdaQueryWrapper<HrOrganization> wrapper = OrgConditionFactory.createWrapper(hrOrganizationRequest);
// 只查询需要的字段
wrapper.select(HrOrganization::getOrgId, HrOrganization::getOrgName, HrOrganization::getOrgCode, HrOrganization::getStatusFlag,
@ -238,7 +238,7 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
// 只查询未禁用的组织机构
hrOrganizationRequest.setStatusFlag(StatusEnum.ENABLE.getCode());
LambdaQueryWrapper<HrOrganization> wrapper = createWrapper(hrOrganizationRequest);
LambdaQueryWrapper<HrOrganization> wrapper = OrgConditionFactory.createWrapper(hrOrganizationRequest);
// 只查询需要的字段
wrapper.select(HrOrganization::getOrgId, HrOrganization::getOrgName, HrOrganization::getOrgCode, HrOrganization::getOrgType,
@ -262,62 +262,29 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
@DataScope(userIdFieldName = "create_user")
public CommonOrgTreeResponse commonOrgTree(CommonOrgTreeRequest commonOrgTreeRequest) {
// 如果查询带组织机构名称的搜索,则清空其他条件
if (ObjectUtil.isNotEmpty(commonOrgTreeRequest.getSearchText())) {
commonOrgTreeRequest.setOrgParentId(null);
commonOrgTreeRequest.setIndexOrgIdList(null);
}
// 如果查询待组织机构的状态信息则清空parentId
if (ObjectUtil.isNotEmpty(commonOrgTreeRequest.getIndexOrgIdList())) {
commonOrgTreeRequest.setOrgParentId(null);
}
// 预填充一些查询参数
OrgConditionFactory.prepareRequest(commonOrgTreeRequest);
// 根据条件查询组织机构列表
LambdaQueryWrapper<HrOrganization> wrapper = this.createCommonTreeWrapper(commonOrgTreeRequest);
LambdaQueryWrapper<HrOrganization> wrapper = OrgConditionFactory.createCommonTreeWrapper(commonOrgTreeRequest, this);
wrapper.select(HrOrganization::getOrgId, HrOrganization::getOrgPids, HrOrganization::getOrgParentId, HrOrganization::getOrgName,
HrOrganization::getOrgSort, HrOrganization::getOrgType);
List<HrOrganization> hrOrganizationList = this.list(wrapper);
if (ObjectUtil.isEmpty(hrOrganizationList)) {
return new CommonOrgTreeResponse(hrOrganizationList, new ArrayList<>());
}
// 如果查询条件不为空,则把相关的查询结果的父级也查询出来,组成一颗完整树
String searchText = commonOrgTreeRequest.getSearchText();
List<HrOrganization> parentOrgList = new ArrayList<>();
if (ObjectUtil.isNotEmpty(searchText)) {
Set<Long> orgParentIdList = OrganizationFactory.getOrgParentIdList(hrOrganizationList);
LambdaQueryWrapper<HrOrganization> parentWrapper = new LambdaQueryWrapper<>();
parentWrapper.in(HrOrganization::getOrgId, orgParentIdList);
parentOrgList = this.list(parentWrapper);
}
// 合并两个集合
hrOrganizationList.addAll(parentOrgList);
// 去重
List<HrOrganization> newNotRepeatList = hrOrganizationList.stream().collect(
Collectors.collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(HrOrganization::getOrgId))),
LinkedList::new));
// 从新排序根据sort字段排序
newNotRepeatList.sort(Comparator.comparing(HrOrganization::getOrgSort));
// 构建树形结构
if (ObjectUtil.isNotEmpty(commonOrgTreeRequest.getSearchText()) || ObjectUtil.isNotEmpty(
commonOrgTreeRequest.getIndexOrgIdList())) {
newNotRepeatList = new SortedTreeBuildFactory<HrOrganization>().doTreeBuild(newNotRepeatList);
}
hrOrganizationList = new SortedTreeBuildFactory<HrOrganization>().doTreeBuild(hrOrganizationList);
// 遍历所有节点查询这些节点有没有子级填充haveSubOrgFlag
this.fillHaveSubFlag(newNotRepeatList);
this.fillHaveSubFlag(hrOrganizationList);
// 遍历这些节点如果有children的都展开并搜集到数组里
List<Long> expandOrgIds = new ArrayList<>();
this.fillExpandFlag(newNotRepeatList, expandOrgIds);
this.fillExpandFlag(hrOrganizationList, expandOrgIds);
return new CommonOrgTreeResponse(newNotRepeatList, expandOrgIds);
return new CommonOrgTreeResponse(hrOrganizationList, expandOrgIds);
}
@Override
@ -711,46 +678,6 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
return hrOrganization;
}
/**
* wrapper
*
* @author fengshuonan
* @date 2023/06/10 21:23
*/
private LambdaQueryWrapper<HrOrganization> createWrapper(HrOrganizationRequest hrOrganizationRequest) {
LambdaQueryWrapper<HrOrganization> queryWrapper = new LambdaQueryWrapper<>();
// 如果按文本查询条件不为空,则判断组织机构名称、简称、税号、备注是否有匹配
String searchText = hrOrganizationRequest.getSearchText();
if (StrUtil.isNotEmpty(searchText)) {
queryWrapper.nested(wrap -> {
wrap.like(HrOrganization::getOrgName, searchText);
wrap.or().like(HrOrganization::getOrgShortName, searchText);
wrap.or().like(HrOrganization::getTaxNo, searchText);
wrap.or().like(HrOrganization::getOrgCode, searchText);
wrap.or().like(HrOrganization::getRemark, searchText);
});
}
// 根据机构状态查询
Integer statusFlag = hrOrganizationRequest.getStatusFlag();
queryWrapper.eq(ObjectUtil.isNotNull(statusFlag), HrOrganization::getStatusFlag, statusFlag);
// 如果查询条件有orgId则查询指定机构下的子机构
Long orgId = hrOrganizationRequest.getOrgId();
if (orgId != null) {
// 查询orgId对应的所有子机构包含本orgId
queryWrapper.nested(wrap -> {
wrap.eq(HrOrganization::getOrgParentId, orgId).or().eq(HrOrganization::getOrgId, orgId);
});
}
// 根据排序正序查询
queryWrapper.orderByAsc(HrOrganization::getOrgSort);
return queryWrapper;
}
/**
*
*
@ -773,46 +700,6 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
this.removeBatchByIds(totalOrgIdSet);
}
/**
* wrapper
*
* @author fengshuonan
* @date 2023/06/10 21:23
*/
private LambdaQueryWrapper<HrOrganization> createCommonTreeWrapper(CommonOrgTreeRequest commonOrgTreeRequest) {
// 创建基本的wrapper
HrOrganizationRequest hrOrganizationRequest = new HrOrganizationRequest();
hrOrganizationRequest.setSearchText(commonOrgTreeRequest.getSearchText());
LambdaQueryWrapper<HrOrganization> queryWrapper = this.createWrapper(hrOrganizationRequest);
// 如果查询条件有orgId则查询指定机构下的子机构
Long parentId = commonOrgTreeRequest.getOrgParentId();
if (parentId != null) {
queryWrapper.eq(HrOrganization::getOrgParentId, parentId);
}
// 如果有定位状态的组织机构,则需要查询到定位的组织机构的所有子一级
Set<Long> indexOrgIdList = commonOrgTreeRequest.getIndexOrgIdList();
if (ObjectUtil.isNotEmpty(indexOrgIdList)) {
Set<Long> parentIdListTotal = this.queryOrgIdParentIdList(indexOrgIdList);
if (ObjectUtil.isNotEmpty(parentIdListTotal)) {
queryWrapper.in(HrOrganization::getOrgParentId, parentIdListTotal);
}
}
// 如果有筛选公司的标识,则只查询公司列表
Boolean companySearchFlag = commonOrgTreeRequest.getCompanySearchFlag();
if (ObjectUtil.isNotEmpty(companySearchFlag) && companySearchFlag) {
queryWrapper.eq(HrOrganization::getOrgType, OrgTypeEnum.COMPANY.getCode());
}
// 只查询启用状态的机构
queryWrapper.eq(HrOrganization::getStatusFlag, StatusEnum.ENABLE.getCode());
return queryWrapper;
}
/**
*
*