|
|
@@ -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<MhUnitTreeDTO> getMhUnitTree() { |
|
|
|
public List<MhUnitTreeDTO> getMhUnitTree(String name) { |
|
|
|
List<UnitDTO> retUnits; |
|
|
|
List<UnitDTO> allUnits = mhUnitCache.all(); |
|
|
|
List<MhUnitTreeDTO> 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<MhUnitTreeDTO> 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<MhUnitTreeDTO> getMhUnitStripTree() { |
|
|
@@ -73,8 +90,12 @@ public class MhUnitManage { |
|
|
|
} |
|
|
|
|
|
|
|
public PageVo<MhUnitListVO> page(MhUnitListReq req) { |
|
|
|
LambdaQueryWrapper<MhUnit> query = Wrappers.lambdaQuery(MhUnit.class) |
|
|
|
.like(StrUtils.isNotBlank(req.getName()), MhUnit::getName, req.getName()); |
|
|
|
LambdaQueryWrapper<MhUnit> 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<MhUnit> 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<MhUnitUserTreeDTO> getUserTree(UserTreeReq req) { |
|
|
|
List<Long> selectedUnitIds = new ArrayList<>(); |
|
|
|
List<MhUnitUserTreeDTO> selectedUsers = new ArrayList<>(); |
|
|
|
public List<MhUnitUserTreeDTO> getUnitUserTree(UserTreeReq req) { |
|
|
|
List<MhUnitUserTreeDTO> selectedUnits = new ArrayList<>(); |
|
|
|
if (req.getId() != null) { |
|
|
|
// 查询单位 |
|
|
|
Wrapper<MhUnit> unitQuery = Wrappers.lambdaQuery(MhUnit.class) |
|
|
|
.select(MhUnit::getId) |
|
|
|
.like(StrUtils.isNotBlank(req.getName()), MhUnit::getName, req.getName()) |
|
|
|
.eq(MhUnit::getParentId, req.getId()); |
|
|
|
List<MhUnit> units = mhUnitService.list(unitQuery); |
|
|
|
selectedUnitIds.addAll(CollUtils.fieldList(units, MhUnit::getId)); |
|
|
|
// 查询用户 |
|
|
|
LambdaQueryWrapper<UserInfo> 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<UserInfo> 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<Long> viewUnitIds = new HashSet<>(); |
|
|
|
boolean hasId = req.getId() != null; |
|
|
|
if (hasId) { |
|
|
|
List<Long> childIds = mhUnitCache.getChildrenIdsRecursion(req.getId()); |
|
|
|
viewUnitIds.addAll(childIds); |
|
|
|
viewUnitIds.add(req.getId()); |
|
|
|
} |
|
|
|
boolean hasName = CharSequenceUtil.isNotBlank(req.getName()); |
|
|
|
LambdaQueryWrapper<UserInfo> 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<UnitDTO> units = mhUnitCache.listByIds(viewUnitIds); |
|
|
|
selectedUnits.addAll(CollUtils.convert(units, this::getMhUnitUserTree)); |
|
|
|
} else { |
|
|
|
// 如果没有查询指定组织,那么直接查询第一级组织 |
|
|
|
List<UnitDTO> childrenUnits = mhUnitCache.getChildren(0L); |
|
|
|
if (childrenUnits.isEmpty()) { |
|
|
|
return Collections.emptyList(); |
|
|
|
} |
|
|
|
for (UnitDTO unit : childrenUnits) { |
|
|
|
selectedUnits.add(getMhUnitUserTree(unit)); |
|
|
|
List<UnitDTO> all = mhUnitCache.all(); |
|
|
|
selectedUnits.addAll(CollUtils.convert(all, this::getMhUnitUserTree)); |
|
|
|
} |
|
|
|
List<UserInfo> userInfos = userInfoService.list(userQuery); |
|
|
|
if (userInfos.isEmpty()) { |
|
|
|
return Collections.emptyList(); |
|
|
|
} |
|
|
|
if (hasName) { |
|
|
|
Set<Long> tmpUnitIds = CollUtils.fieldSet(userInfos, UserInfo::getMhUnitId); |
|
|
|
Set<Long> allUnitIds = new HashSet<>(); |
|
|
|
for (Long unitId : tmpUnitIds) { |
|
|
|
allUnitIds.addAll(mhUnitCache.getUnitIdPaths(unitId)); |
|
|
|
} |
|
|
|
selectedUnits.sort(selectedUnits.get(0).comparator()); |
|
|
|
return selectedUnits; |
|
|
|
List<UnitDTO> 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<MhUnitTreeDTO> convertToTree(List<MhUnitTreeDTO> units) { |
|
|
|
List<Long> ids = CollUtils.fieldList(units, MhUnitTreeDTO::getId); |
|
|
|
List<MhUnitTreeDTO> 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<MhUnitTreeDTO> children = parent.getChildren(); |
|
|
|
if (children == null) { |
|
|
|
children = new ArrayList<>(); |
|
|
|
parent.setChildren(children); |
|
|
|
} |
|
|
|
children.add(node); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return roots; |
|
|
|
} |
|
|
|
|
|
|
|
} |