|
|
@@ -2,6 +2,7 @@ package com.zhongshu.card.server.core.service.dictionary;
|
|
|
|
|
|
import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
+import com.github.microservice.components.data.mongo.mongo.domain.SuperEntity;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.card.client.model.dictionary.*;
|
|
|
import com.zhongshu.card.client.type.DictionaryType;
|
|
|
@@ -9,6 +10,7 @@ import com.zhongshu.card.server.core.dao.dictionary.DictionaryDao;
|
|
|
import com.zhongshu.card.server.core.dao.dictionary.DictionaryItemDao;
|
|
|
import com.zhongshu.card.server.core.domain.dictionary.Dictionary;
|
|
|
import com.zhongshu.card.server.core.domain.dictionary.DictionaryItem;
|
|
|
+import com.zhongshu.card.server.core.model.dictionary.DItemChildrenResult;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.bson.types.ObjectId;
|
|
|
@@ -18,6 +20,12 @@ import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Service
|
|
|
public class DictionaryService extends SuperService {
|
|
|
|
|
|
@@ -106,7 +114,7 @@ public class DictionaryService extends SuperService {
|
|
|
};
|
|
|
|
|
|
Page<Dictionary> page = dictionaryDao.page(pageable, param.getProjectId(), param.getName(), param.getType());
|
|
|
- return PageEntityUtil.concurrent2PageModel(page, this::toModel);
|
|
|
+ return ResultContent.buildContent(PageEntityUtil.concurrent2PageModel(page, this::toModel));
|
|
|
}
|
|
|
|
|
|
private DictionaryModel toModel(Dictionary dictionary){
|
|
|
@@ -182,8 +190,51 @@ public class DictionaryService extends SuperService {
|
|
|
}
|
|
|
|
|
|
public Object pageItem(Pageable pageable, DictionaryItemQueryParam param){
|
|
|
- Page<DictionaryItem> page = dictionaryItemDao.page(pageable, param.getDictionaryId(), param.getName());
|
|
|
- return PageEntityUtil.concurrent2PageModel(page, this::toItemModel);
|
|
|
+ Page<DictionaryItem> page = dictionaryItemDao.page(pageable, param.getDictionaryId(), param.getName(), param.getParentId());
|
|
|
+ Page<DictionaryItemModel> pageModel = PageEntityUtil.concurrent2PageModel(page, it -> {
|
|
|
+ return toItemModel(it);
|
|
|
+ });
|
|
|
+
|
|
|
+ List<DictionaryItemModel> dItemChildrenResults = dictionaryItemDao.deepAggregate(param.getDictionaryId(), pageModel.getContent().stream().map(DictionaryItemModel::getKey).toList());
|
|
|
+ List<DictionaryItemModel> ret = new ArrayList<>();
|
|
|
+ dItemChildrenResults.forEach(it->{
|
|
|
+ if (it.getChildren()!=null && it.getChildren().size() > 0){
|
|
|
+ ret.addAll(it.getChildren());
|
|
|
+ }
|
|
|
+ it.setChildren(new ArrayList<>());
|
|
|
+ ret.add(it);
|
|
|
+ });
|
|
|
+
|
|
|
+ List<DictionaryItemModel> modelList = buildTree(ret, null);
|
|
|
+ return ResultContent.buildContent(PageEntityUtil.buildPage(modelList, pageable, page.getTotalElements()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static DictionaryItemModel buildChildren(List<DictionaryItemModel> allItems, String rootId) {
|
|
|
+ // 使用一个Map来根据idString查找节点
|
|
|
+ Map<String, DictionaryItemModel> itemMap = new HashMap<>();
|
|
|
+ for (DictionaryItemModel item : allItems) {
|
|
|
+ itemMap.put(item.getId(), item);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取根节点
|
|
|
+ DictionaryItemModel root = itemMap.get(rootId);
|
|
|
+ if (root != null) {
|
|
|
+ // 从根节点开始构建树
|
|
|
+ buildSubTree(itemMap, root);
|
|
|
+ }
|
|
|
+ return root;
|
|
|
+ }
|
|
|
+ // 递归构建子树
|
|
|
+ private static void buildSubTree(Map<String, DictionaryItemModel> itemMap, DictionaryItemModel parent) {
|
|
|
+ // 遍历所有节点,查找当前parent的子节点
|
|
|
+ for (DictionaryItemModel item : itemMap.values()) {
|
|
|
+ if (item.getParentId() != null && item.getParentId().equals(parent.getId())) {
|
|
|
+ // 找到子节点,添加到父节点的children列表中
|
|
|
+ parent.addChild(item);
|
|
|
+ // 递归处理子节点
|
|
|
+ buildSubTree(itemMap, item);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private DictionaryItemModel toItemModel(DictionaryItem dictionaryItem){
|
|
|
@@ -195,4 +246,155 @@ public class DictionaryService extends SuperService {
|
|
|
return dictionaryItemModel;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据字典编码获取字典
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent listDictionary(ListDictionaryParam param){
|
|
|
+
|
|
|
+ String projectId = param.getProjectId();
|
|
|
+ if (StringUtils.isBlank(projectId)){
|
|
|
+ projectId = getCurrentProjectOid();
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(projectId)){
|
|
|
+ return ResultContent.buildFail("项目id不能为空");
|
|
|
+ };
|
|
|
+
|
|
|
+ if (param.getKeys()==null || param.getKeys().isEmpty()){
|
|
|
+ return ResultContent.buildFail("字典编码不能为null或空");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Dictionary> dictionaryList = dictionaryDao.findTopByProjectIdAndKeyIn(projectId, param.getKeys());
|
|
|
+ return ResultContent.buildContent(dictionaryList.stream().map(this::toModel).toList());
|
|
|
+ }
|
|
|
+
|
|
|
+// public ResultContent listItem(String projectId,String dictionary, String key){
|
|
|
+// dictionaryItemDao.findTopByDictionary_IdAndKeyIn()
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据项编码获取项
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent listItem(ListDictionaryParam param){
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(param.getDictionaryKey())){
|
|
|
+ return ResultContent.buildFail("字典编码不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Dictionary dictionary = dictionaryDao.findTopByProjectIdAndKey(param.getProjectId(), param.getDictionaryKey());
|
|
|
+ if (dictionary == null){
|
|
|
+ return ResultContent.buildFail("字典不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<DictionaryItem> dictionaryItemList = new ArrayList<>();
|
|
|
+
|
|
|
+ if (param.getKeys()==null || param.getKeys().isEmpty()){
|
|
|
+ dictionaryItemList = dictionaryItemDao.findByDictionary_Id(dictionary.getId());
|
|
|
+ }else {
|
|
|
+ dictionaryItemList = dictionaryItemDao.findTopByDictionary_IdAndKeyIn(dictionary.getId(), param.getKeys());
|
|
|
+ }
|
|
|
+ return ResultContent.buildContent(dictionaryItemList.stream().map(this::toItemModel).toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object deepTreeItem(String projectId, String dictionaryKey, String dictionaryId){
|
|
|
+ if (StringUtils.isBlank(dictionaryKey) && StringUtils.isBlank(dictionaryId)){
|
|
|
+ return ResultContent.buildFail("字典编码和字典id不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ Dictionary dictionary = null;
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(dictionaryId)){
|
|
|
+ dictionary = dictionaryDao.findTopById(dictionaryId);
|
|
|
+ }else if (StringUtils.isNotBlank(dictionaryKey)){
|
|
|
+ dictionary = dictionaryDao.findTopByProjectIdAndKey(projectId, dictionaryKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dictionary == null){
|
|
|
+ return ResultContent.buildFail("字典不存在");
|
|
|
+ }
|
|
|
+ if (dictionary.isDisabled()){
|
|
|
+ return ResultContent.buildFail("字典已被禁用");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<DictionaryItem> list = dictionaryItemDao.findByDictionary_Id(dictionary.getId());
|
|
|
+ List<DictionaryItemModel> modelList = list.stream().map(this::toItemModel).toList();
|
|
|
+
|
|
|
+ return ResultContent.buildContent(buildTree(modelList, null));
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<DictionaryItemModel> buildTree(List<DictionaryItemModel> list, String parentId){
|
|
|
+ Map<String, DictionaryItemModel> itemMap = new HashMap<>();
|
|
|
+ itemMap = list.stream().collect(Collectors.toMap(DictionaryItemModel::getId, it->it));
|
|
|
+
|
|
|
+ List<DictionaryItemModel> rootItems = new ArrayList<>();
|
|
|
+
|
|
|
+// for (DictionaryItemModel item : list) {
|
|
|
+// if (item.getParentId() == null || item.getParentId().isEmpty()) {
|
|
|
+// // 没有父节点的项是根节点
|
|
|
+// rootItems.add(item);
|
|
|
+// } else {
|
|
|
+// // 将子节点添加到父节点的 children 中
|
|
|
+// DictionaryItemModel parentItem = itemMap.get(item.getParentId());
|
|
|
+// if (parentItem != null) {
|
|
|
+// parentItem.addChild(item);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(parentId)){
|
|
|
+ for (DictionaryItemModel item : list) {
|
|
|
+ if (item.getId().equals(parentId)) {
|
|
|
+ // 没有父节点的项是根节点
|
|
|
+ rootItems.add(item);
|
|
|
+ } else {
|
|
|
+ // 将子节点添加到父节点的 children 中
|
|
|
+ DictionaryItemModel parentItem = itemMap.get(item.getParentId());
|
|
|
+ if (parentItem != null) {
|
|
|
+ parentItem.addChild(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ for (DictionaryItemModel item : list) {
|
|
|
+ if (item.getParentId() == null || item.getParentId().isEmpty()) {
|
|
|
+ // 没有父节点的项是根节点
|
|
|
+ rootItems.add(item);
|
|
|
+ } else {
|
|
|
+ // 将子节点添加到父节点的 children 中
|
|
|
+ DictionaryItemModel parentItem = itemMap.get(item.getParentId());
|
|
|
+ if (parentItem != null) {
|
|
|
+ parentItem.addChild(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rootItems;
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 字典项向下查询
|
|
|
+// */
|
|
|
+// public Object deepListItem(DeepQueryParam queryParam){
|
|
|
+//
|
|
|
+// List<DeepQueryParam.KeyFilter> keyFilters = queryParam.getKeyFilters();
|
|
|
+//
|
|
|
+// keyFilters.stream().map(keyFilter -> {
|
|
|
+// Dictionary dictionary = dictionaryDao.findTopByProjectIdAndKey(queryParam.getProjectId(), keyFilter.getDictionaryKey());
|
|
|
+//
|
|
|
+// if (queryParam.isDeep() && !keyFilter.getKeys().isEmpty()){
|
|
|
+// List<DItemChildrenResult> itemList = dictionaryItemDao.deepAggregate(dictionary.getId(), keyFilter.getKeys());
|
|
|
+// }
|
|
|
+//
|
|
|
+// }).toList();
|
|
|
+//
|
|
|
+// Dictionary dictionary = dictionaryDao.findTopByProjectIdAndKey(projectId, dictionaryKey);
|
|
|
+//
|
|
|
+// dictionaryItemDao.f
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
}
|