|
@@ -0,0 +1,178 @@
|
|
|
+package com.jeeplus.test.materialManagement.materialType.service;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import com.jeeplus.core.service.TreeService;
|
|
|
+import com.jeeplus.core.service.dto.TreeDTO;
|
|
|
+import com.jeeplus.test.materialManagement.materialType.domain.MaterialTypeInfo;
|
|
|
+import com.jeeplus.test.materialManagement.materialType.mapper.MaterialTypeMapper;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: 王强
|
|
|
+ * @create: 2022-11-25 09:01
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+public class MaterialTypeForTreeDataService extends TreeService<MaterialTypeMapper, MaterialTypeInfo> {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MaterialTypeMapper typeMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取JSON树形数据。
|
|
|
+ *
|
|
|
+ * @param extId 排除的ID
|
|
|
+ * @param type 禁选类型
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MaterialTypeInfo> treeDataForType(String extId, String type) throws Exception{
|
|
|
+ //获取所有的业务类型为1的数据
|
|
|
+// List<CwReimbursementTypeInfo> allList = typeMapper.getAllList("1");
|
|
|
+ List<MaterialTypeInfo> allList = typeMapper.findList (new LambdaQueryWrapper<>( (Class <MaterialTypeInfo>) entityClass ).orderByAsc ( MaterialTypeInfo::getSort ));
|
|
|
+ MaterialTypeInfo root = entityClass.getConstructor ( ).newInstance ( );
|
|
|
+ root.setId ( TreeDTO.getRootId () );
|
|
|
+ List <MaterialTypeInfo> rootTree = this.formatListToTreeForType ( root, allList, extId, type );
|
|
|
+ return rootTree;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取JSON树形数据。
|
|
|
+ *
|
|
|
+ * @param extId 排除的ID
|
|
|
+ * @param type 禁选类型
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MaterialTypeInfo> treeDataForType1(String extId, String type) throws Exception{
|
|
|
+ //获取所有的业务类型为1的数据
|
|
|
+ List<MaterialTypeInfo> allList = typeMapper.getAllList();
|
|
|
+// List<CwReimbursementTypeInfo> allList = super.list (new LambdaQueryWrapper<>( (Class <CwReimbursementTypeInfo>) entityClass ).orderByAsc ( CwReimbursementTypeInfo::getSort ));
|
|
|
+ MaterialTypeInfo root = entityClass.getConstructor ( ).newInstance ( );
|
|
|
+ root.setId ( TreeDTO.getRootId () );
|
|
|
+ List <MaterialTypeInfo> rootTree = this.formatListToTreeForType ( root, allList, extId, type );
|
|
|
+ return rootTree;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取JSON树形数据。
|
|
|
+ *
|
|
|
+ * @param extId 排除的ID
|
|
|
+ * @param type 禁选类型
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MaterialTypeInfo> summaryTreeData(String extId, String type) throws Exception{
|
|
|
+ //获取所有的业务类型为1的数据
|
|
|
+ List<MaterialTypeInfo> allList = typeMapper.getAllList();
|
|
|
+ MaterialTypeInfo root = entityClass.getConstructor ( ).newInstance ( );
|
|
|
+ root.setId ( TreeDTO.getRootId () );
|
|
|
+ List <MaterialTypeInfo> rootTree = this.formatListToTreeForType ( root, allList, extId, type );
|
|
|
+ return rootTree;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取JSON树形数据。
|
|
|
+ *
|
|
|
+ * @param extId 排除的ID
|
|
|
+ * @param type 禁选类型
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MaterialTypeInfo> treeDataForType2(String extId, String type) throws Exception{
|
|
|
+ //获取所有的业务类型为1的数据
|
|
|
+ List<MaterialTypeInfo> allList = typeMapper.getAllList();
|
|
|
+// List<CwReimbursementTypeInfo> allList = super.list (new LambdaQueryWrapper<>( (Class <CwReimbursementTypeInfo>) entityClass ).orderByAsc ( CwReimbursementTypeInfo::getSort ));
|
|
|
+ MaterialTypeInfo root = entityClass.getConstructor ( ).newInstance ( );
|
|
|
+ root.setId ( TreeDTO.getRootId () );
|
|
|
+ List <MaterialTypeInfo> rootTree = this.formatListToTreeForType ( root, allList, extId, type );
|
|
|
+ return rootTree;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 以root为根节点, 将allList从线性列表转为树形列表
|
|
|
+ *
|
|
|
+ * @param root 根节点, 为空抛出空指针异常
|
|
|
+ * @param allList 所有需要参与构造为树的列表
|
|
|
+ * @param extId 需要排除在树之外的节点(子节点一并被排除)
|
|
|
+ * @return java.util.List<T>
|
|
|
+ * @Author 滕鑫源
|
|
|
+ * @Date 2020/10/23 17:04
|
|
|
+ **/
|
|
|
+ public List <MaterialTypeInfo> formatListToTreeForType (MaterialTypeInfo root, List <MaterialTypeInfo> allList, String extId, String type) {
|
|
|
+ String rootId = root.getId ( );
|
|
|
+ // 最终的树形态
|
|
|
+ List <MaterialTypeInfo> trees = Lists.newArrayList ( );
|
|
|
+
|
|
|
+ // 把需要构造树的所有列表, 根据以父id作为key, 整理为列表
|
|
|
+ Map<String, List <MaterialTypeInfo>> treeMap = Maps.newHashMap ( );
|
|
|
+ for (MaterialTypeInfo entity : allList) {
|
|
|
+ List <MaterialTypeInfo> entities = treeMap.get ( entity.getParentId ( ) );
|
|
|
+ if ( entities == null ) {
|
|
|
+ entities = Lists.newLinkedList ( );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 剔除排除项, 构造treeMap, 转递归为线性操作
|
|
|
+ if ( StrUtil.isBlank ( extId ) || (!extId.equals ( entity.getId ( ) ) && entity.getParentIds ( ).indexOf ( "," + extId + "," ) == -1) ) {
|
|
|
+ entities.add ( entity );
|
|
|
+ treeMap.put ( entity.getParentId ( ), entities );
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(type)) {
|
|
|
+ //禁选类型设置disable值为true
|
|
|
+ if ("last".equals(type)) {
|
|
|
+ allList.stream().forEach(item -> {
|
|
|
+ if (StringUtils.isNotBlank(item.getParentId())) {
|
|
|
+ if (item.getParentId().equals(entity.getId())) {
|
|
|
+ entity.setDisabled(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ if (type.contains(entity.getLevel())) {
|
|
|
+ entity.setDisabled(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没有给定的子树, 返回空树
|
|
|
+ if ( treeMap.get ( rootId ) == null || treeMap.get ( rootId ).isEmpty ( ) ) {
|
|
|
+ return trees;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始递归格式化
|
|
|
+ List <MaterialTypeInfo> children = treeMap.get ( rootId );
|
|
|
+ for (MaterialTypeInfo parent : children) {
|
|
|
+ formatFillChildren ( parent, treeMap );
|
|
|
+ trees.add ( parent );
|
|
|
+ }
|
|
|
+ if ( StrUtil.equals ( rootId, TreeDTO.getRootId () ) ) {
|
|
|
+ return children;
|
|
|
+ } else {
|
|
|
+ root.setChildren ( trees );
|
|
|
+ return Lists.newArrayList ( root );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 从treeMap中取出子节点填入parent, 并递归此操作
|
|
|
+ *
|
|
|
+ * @param parent
|
|
|
+ * @param treeMap
|
|
|
+ * @return void
|
|
|
+ * @Author 滕鑫源
|
|
|
+ * @Date 2020/9/30 16:33
|
|
|
+ **/
|
|
|
+ private void formatFillChildren(MaterialTypeInfo parent, Map <String, List <MaterialTypeInfo>> treeMap) {
|
|
|
+ List <MaterialTypeInfo> children = treeMap.get ( parent.getId ( ) );
|
|
|
+ parent.setChildren ( children );
|
|
|
+ if ( children != null && !children.isEmpty ( ) ) {
|
|
|
+ for (MaterialTypeInfo child : children) {
|
|
|
+ formatFillChildren ( child, treeMap );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|