diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectRenewalFundDeclarationService.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectRenewalFundDeclarationService.java index 2c1c1dc..48bcd4e 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectRenewalFundDeclarationService.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectRenewalFundDeclarationService.java @@ -16,5 +16,5 @@ import com.hz.pm.api.projectlib.model.req.ProjectRenewalListReq; */ public interface IProjectRenewalFundDeclarationService extends IService { - Page pageSql(Page page, ProjectRenewalListReq req); + void pageSql(Page page, ProjectRenewalListReq req); } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectRenewalFundDeclarationServiceImpl.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectRenewalFundDeclarationServiceImpl.java index a7d32b9..df44078 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectRenewalFundDeclarationServiceImpl.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectRenewalFundDeclarationServiceImpl.java @@ -25,8 +25,8 @@ public class ProjectRenewalFundDeclarationServiceImpl implements IProjectRenewalFundDeclarationService { @Override - public Page pageSql(Page page, ProjectRenewalListReq req) { - return baseMapper.pageSql(page, req); + public void pageSql(Page page, ProjectRenewalListReq req) { + baseMapper.pageSql(page, req); } } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/user/controller/MhUnitController.java b/hz-pm-api/src/main/java/com/hz/pm/api/user/controller/MhUnitController.java index b2ca941..502a009 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/user/controller/MhUnitController.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/user/controller/MhUnitController.java @@ -3,7 +3,6 @@ package com.hz.pm.api.user.controller; import com.hz.pm.api.user.manage.MhUnitManage; import com.hz.pm.api.user.model.dto.MhUnitTreeDTO; import com.hz.pm.api.user.model.dto.MhUnitUserTreeDTO; -import com.hz.pm.api.user.model.dto.UnitDTO; import com.hz.pm.api.user.model.po.MhUnitListReq; import com.hz.pm.api.user.model.po.UserTreeReq; import com.hz.pm.api.user.model.vo.MhUnitListVO; @@ -13,6 +12,7 @@ import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -35,8 +35,8 @@ public class MhUnitController { @GetMapping("/tree") @ApiOperation("单位树") - public List getMhUnitTree() { - return mhUnitManage.getMhUnitTree(); + public List getMhUnitTree(@RequestParam(required = false) String name) { + return mhUnitManage.getMhUnitTree(name); } @GetMapping("/wflowModelConfig/tree") @@ -54,7 +54,7 @@ public class MhUnitController { @GetMapping("/user/tree") @ApiOperation("用户组织树") public List getUserTree(UserTreeReq req) { - return mhUnitManage.getUserTree(req); + return mhUnitManage.getUnitUserTree(req); } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/MhUnitCache.java b/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/MhUnitCache.java index 815148a..e065f95 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/MhUnitCache.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/MhUnitCache.java @@ -2,6 +2,7 @@ package com.hz.pm.api.user.helper; import com.hz.pm.api.user.model.dto.UnitDTO; +import java.util.Collection; import java.util.List; /** @@ -20,6 +21,8 @@ public interface MhUnitCache { UnitDTO getById(Long id); + List listByIds(Collection unitIds); + List getChildren(Long id); List getChildrenIds(Long id); diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/impl/MhUnitCacheImpl.java b/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/impl/MhUnitCacheImpl.java index b1c02d5..80df800 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/impl/MhUnitCacheImpl.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/user/helper/impl/MhUnitCacheImpl.java @@ -20,10 +20,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; @@ -113,6 +110,11 @@ public class MhUnitCacheImpl implements MhUnitCache, InitializingBean { } @Override + public List listByIds(Collection unitIds) { + return new ArrayList<>(cache.getAll(unitIds).values()); + } + + @Override public List getChildren(Long id) { return CollUtils.convert(childIdMap.get(id), cache::get); } @@ -140,6 +142,9 @@ public class MhUnitCacheImpl implements MhUnitCache, InitializingBean { List nodeIdPaths = new ArrayList<>(); Long currUnitId = unitId, parentId; while ((parentId = parentIdMap.get(currUnitId)) != null) { + if (parentId.equals(currUnitId)) { + break; + } nodeIdPaths.add(0, currUnitId); currUnitId = parentId; } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/user/manage/MhUnitManage.java b/hz-pm-api/src/main/java/com/hz/pm/api/user/manage/MhUnitManage.java index 5a87085..4cfad8f 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/user/manage/MhUnitManage.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/user/manage/MhUnitManage.java @@ -1,7 +1,7 @@ package com.hz.pm.api.user.manage; import cn.hutool.core.bean.BeanUtil; -import com.baomidou.mybatisplus.core.conditions.Wrapper; +import cn.hutool.core.text.CharSequenceUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -23,12 +23,10 @@ import com.ningdatech.basic.model.PageVo; import com.ningdatech.basic.util.CollUtils; import com.ningdatech.basic.util.TreeUtil; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -44,18 +42,37 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class MhUnitManage { + @Value("${mh.unit.root-parent-id:0}") + private Long unitRootParentId; + private final IMhUnitService mhUnitService; private final MhUnitCache mhUnitCache; private final IUserInfoService userInfoService; - public List getMhUnitTree() { + public List getMhUnitTree(String name) { + List retUnits; List allUnits = mhUnitCache.all(); - List nodes = allUnits.stream().map(w -> { + if (StrUtils.isNotBlank(name)) { + allUnits.removeIf(w -> !w.getName().contains(name)); + retUnits = allUnits.stream() + .map(w -> mhUnitCache.getUnitIdPaths(w.getId())) + .flatMap(Collection::stream) + .distinct() + .map(mhUnitCache::getById) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } else { + retUnits = allUnits; + } + if (allUnits.isEmpty()) { + return Collections.emptyList(); + } + List nodes = retUnits.stream().map(w -> { MhUnitTreeDTO node = BeanUtil.copyProperties(w, MhUnitTreeDTO.class); node.setStrip(node.getType().getStrip()); return node; }).collect(Collectors.toList()); - return TreeUtil.convertToTree(nodes, 0L); + return TreeUtil.convertToTree(nodes, unitRootParentId); } public List getMhUnitStripTree() { @@ -73,8 +90,12 @@ public class MhUnitManage { } public PageVo page(MhUnitListReq req) { - LambdaQueryWrapper query = Wrappers.lambdaQuery(MhUnit.class) - .like(StrUtils.isNotBlank(req.getName()), MhUnit::getName, req.getName()); + LambdaQueryWrapper query = Wrappers.lambdaQuery(MhUnit.class); + if (StrUtils.isNotBlank(req.getName())) { + query.and(q1 -> q1.like(MhUnit::getName, req.getName()) + .or(q2 -> q2.like(MhUnit::getShortName, req.getName()))); + } + Page page = mhUnitService.page(req.page(), query); if (page.getTotal() == 0) { return PageVo.empty(); @@ -92,51 +113,47 @@ public class MhUnitManage { return PageVo.of(records, page.getTotal()); } - public List getUserTree(UserTreeReq req) { - List selectedUnitIds = new ArrayList<>(); - List selectedUsers = new ArrayList<>(); + public List getUnitUserTree(UserTreeReq req) { List selectedUnits = new ArrayList<>(); - if (req.getId() != null) { - // 查询单位 - Wrapper unitQuery = Wrappers.lambdaQuery(MhUnit.class) - .select(MhUnit::getId) - .like(StrUtils.isNotBlank(req.getName()), MhUnit::getName, req.getName()) - .eq(MhUnit::getParentId, req.getId()); - List units = mhUnitService.list(unitQuery); - selectedUnitIds.addAll(CollUtils.fieldList(units, MhUnit::getId)); - // 查询用户 - LambdaQueryWrapper userQuery = Wrappers.lambdaQuery(UserInfo.class) - .select(UserInfo::getMhUnitId, UserInfo::getId, UserInfo::getRealName) - .like(StrUtils.isNotBlank(req.getName()), UserInfo::getRealName, req.getName()) - .eq(UserInfo::getMhUnitId, req.getId()) - .last("limit 100"); - List userInfos = userInfoService.list(userQuery); - selectedUnitIds.addAll(CollUtils.fieldList(userInfos, UserInfo::getMhUnitId)); - userInfos.forEach(w -> selectedUsers.add(getMhUnitUserTree(w))); - if (selectedUnitIds.isEmpty()) { - return Collections.emptyList(); - } - String rootId = UnitUserNodeType.UNIT + "#" + req.getId(); - selectedUnitIds.forEach(w -> { - UnitDTO unit = mhUnitCache.getById(w); - if (unit != null) { - selectedUnits.add(getMhUnitUserTree(unit)); - } - }); - selectedUnits.addAll(selectedUsers); - return TreeUtil.convertToTree(selectedUnits, rootId); + Set viewUnitIds = new HashSet<>(); + boolean hasId = req.getId() != null; + if (hasId) { + List childIds = mhUnitCache.getChildrenIdsRecursion(req.getId()); + viewUnitIds.addAll(childIds); + viewUnitIds.add(req.getId()); + } + boolean hasName = CharSequenceUtil.isNotBlank(req.getName()); + LambdaQueryWrapper userQuery = Wrappers.lambdaQuery(UserInfo.class) + .select(UserInfo::getMhUnitId, UserInfo::getId, UserInfo::getRealName) + .in(!viewUnitIds.isEmpty(), UserInfo::getMhUnitId, viewUnitIds) + .gt(UserInfo::getMhUnitId, 0); + if (hasName) { + // 查询满足名字的 + userQuery.like(UserInfo::getRealName, req.getName()) + .last("limit 500"); + } else if (hasId) { + List units = mhUnitCache.listByIds(viewUnitIds); + selectedUnits.addAll(CollUtils.convert(units, this::getMhUnitUserTree)); } else { - // 如果没有查询指定组织,那么直接查询第一级组织 - List childrenUnits = mhUnitCache.getChildren(0L); - if (childrenUnits.isEmpty()) { - return Collections.emptyList(); - } - for (UnitDTO unit : childrenUnits) { - selectedUnits.add(getMhUnitUserTree(unit)); + List all = mhUnitCache.all(); + selectedUnits.addAll(CollUtils.convert(all, this::getMhUnitUserTree)); + } + List userInfos = userInfoService.list(userQuery); + if (userInfos.isEmpty()) { + return Collections.emptyList(); + } + if (hasName) { + Set tmpUnitIds = CollUtils.fieldSet(userInfos, UserInfo::getMhUnitId); + Set allUnitIds = new HashSet<>(); + for (Long unitId : tmpUnitIds) { + allUnitIds.addAll(mhUnitCache.getUnitIdPaths(unitId)); } - selectedUnits.sort(selectedUnits.get(0).comparator()); - return selectedUnits; + List allUnits = mhUnitCache.listByIds(allUnitIds); + selectedUnits.addAll(CollUtils.convert(allUnits, this::getMhUnitUserTree)); } + selectedUnits.addAll(CollUtils.convert(userInfos, this::getMhUnitUserTree)); + String rootId = UnitUserNodeType.UNIT.name() + "#" + (hasId ? req.getId() : unitRootParentId); + return TreeUtil.convertToTree(selectedUnits, rootId); } private MhUnitUserTreeDTO getMhUnitUserTree(UnitDTO unit) { @@ -161,27 +178,4 @@ public class MhUnitManage { return node; } - public static List convertToTree(List units) { - List ids = CollUtils.fieldList(units, MhUnitTreeDTO::getId); - List roots = new ArrayList<>(); - for (MhUnitTreeDTO node : units) { - if (!ids.contains(node.getParentId())) { - roots.add(node); - } else { - for (MhUnitTreeDTO parent : units) { - if (parent.getId().equals(node.getParentId())) { - List children = parent.getChildren(); - if (children == null) { - children = new ArrayList<>(); - parent.setChildren(children); - } - children.add(node); - break; - } - } - } - } - return roots; - } - }