From 63a19d67861a6610d17aed9d381b55b0bdcc2bdc Mon Sep 17 00:00:00 2001 From: sxu <602087911@qq.com> Date: Thu, 3 Apr 2025 16:30:36 +0800 Subject: [PATCH] areaTree --- .../alloc/controller/AllocAreaController.java | 20 +++ .../bonus/canteen/alloc/domain/AllocArea.java | 5 + .../canteen/alloc/domain/TreeSelect.java | 117 ++++++++++++++++++ .../alloc/service/IAllocAreaService.java | 9 ++ .../service/impl/AllocAreaServiceImpl.java | 79 ++++++++++++ 5 files changed, 230 insertions(+) create mode 100644 bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/TreeSelect.java diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/controller/AllocAreaController.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/controller/AllocAreaController.java index 72b3b7e..11cc16b 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/controller/AllocAreaController.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/controller/AllocAreaController.java @@ -2,10 +2,16 @@ package com.bonus.canteen.alloc.controller; import java.util.List; import javax.servlet.http.HttpServletResponse; + +import cn.hutool.core.lang.tree.Tree; import com.bonus.common.log.enums.OperaType; import com.bonus.canteen.common.annotation.PreventRepeatSubmit; +import com.bonus.common.security.annotation.InnerAuth; +import com.bonus.common.security.annotation.RequiresPermissionsOrInnerAuth; +import com.bonus.system.api.domain.SysDept; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.xmlbeans.impl.xb.xsdschema.All; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -37,6 +43,20 @@ public class AllocAreaController extends BaseController { @Autowired private IAllocAreaService allocAreaService; + /** + * 获取区域树列表 + */ + @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:user:list")) + @GetMapping("/areaTree") + public AjaxResult deptTree(AllocArea area) { + try { + return success(allocAreaService.selectAreaTreeList(area)); + } catch (Exception e) { + logger.error(e.toString(), e); + } + return error("系统异常,请联系管理员"); + } + /** * 查询区域列表 */ diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/AllocArea.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/AllocArea.java index 833ef18..01a86e1 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/AllocArea.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/AllocArea.java @@ -1,11 +1,15 @@ package com.bonus.canteen.alloc.domain; import com.bonus.common.core.annotation.Excel; +import com.bonus.system.api.domain.SysMenu; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.ToString; import com.bonus.common.core.web.domain.BaseEntity; +import java.util.ArrayList; +import java.util.List; + /** * 区域对象 alloc_area * @@ -55,5 +59,6 @@ public class AllocArea extends BaseEntity { /** 删除标志(0代表存在 2代表删除) */ private String delFlag; + private List children = new ArrayList(); } diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/TreeSelect.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/TreeSelect.java new file mode 100644 index 0000000..b239a51 --- /dev/null +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/domain/TreeSelect.java @@ -0,0 +1,117 @@ +package com.bonus.canteen.alloc.domain; + +import com.bonus.system.api.domain.SysDept; +import com.bonus.system.api.domain.SysMenu; +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.io.Serializable; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Treeselect树结构实体类 + * + * @author bonus + */ +public class TreeSelect implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * 节点ID + */ + private Long id; + + /** 父部门ID */ + private Long parentId; + + /** + * 节点名称 + */ + private String label; + + private String status; + + private Integer level; + + + /** + * 子节点 + */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private List children; + + public TreeSelect() { + + } + + public TreeSelect(SysDept dept) { + this.id = dept.getDeptId(); + this.parentId = dept.getParentId(); + this.status = dept.getStatus(); + this.label = dept.getDeptName(); + this.level = dept.getLevel(); + this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList()); + } + + public TreeSelect(SysMenu menu) { + this.id = menu.getMenuId(); + this.label = menu.getMenuName(); + this.status = menu.getStatus(); + this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList()); + } + + public TreeSelect(AllocArea area) { + this.id = area.getAreaId(); + this.label = area.getAreaName(); + this.status = area.getDelFlag(); + this.children = area.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList()); + } + + public Integer getLevel() { + return level; + } + + public void setLevel(Integer level) { + this.level = level; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getParentId() { + return parentId; + } + + public void setParentId(Long parentId) { + this.parentId = parentId; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/IAllocAreaService.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/IAllocAreaService.java index 61083f3..1f300cc 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/IAllocAreaService.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/IAllocAreaService.java @@ -2,6 +2,8 @@ package com.bonus.canteen.alloc.service; import java.util.List; import com.bonus.canteen.alloc.domain.AllocArea; +import com.bonus.canteen.alloc.domain.TreeSelect; +import com.bonus.system.api.domain.SysDept; /** * 区域Service接口 @@ -10,6 +12,9 @@ import com.bonus.canteen.alloc.domain.AllocArea; * @date 2025-04-03 */ public interface IAllocAreaService { + + public List selectAreaTreeList(AllocArea area); + /** * 查询区域 * @@ -57,4 +62,8 @@ public interface IAllocAreaService { * @return 结果 */ public int deleteAllocAreaByAreaId(Long areaId); + + public List buildAreaTreeSelect(List areas); + + public List buildAreaTree(List areas); } diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/impl/AllocAreaServiceImpl.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/impl/AllocAreaServiceImpl.java index 109fed0..e315461 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/impl/AllocAreaServiceImpl.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/alloc/service/impl/AllocAreaServiceImpl.java @@ -1,8 +1,16 @@ package com.bonus.canteen.alloc.service.impl; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.stream.Collectors; + +import com.bonus.canteen.alloc.domain.TreeSelect; import com.bonus.common.core.exception.ServiceException; import com.bonus.common.core.utils.DateUtils; +import com.bonus.common.core.utils.SpringUtils; +import com.bonus.common.core.utils.StringUtils; +import com.bonus.system.api.domain.SysDept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bonus.canteen.alloc.mapper.AllocAreaMapper; @@ -20,6 +28,12 @@ public class AllocAreaServiceImpl implements IAllocAreaService { @Autowired private AllocAreaMapper allocAreaMapper; + @Override + public List selectAreaTreeList(AllocArea area) { + List areas = selectAllocAreaList(area); //SpringUtils.getAopProxy(this).selectAllocAreaList(area); + return buildAreaTreeSelect(areas); + } + /** * 查询区域 * @@ -95,4 +109,69 @@ public class AllocAreaServiceImpl implements IAllocAreaService { public int deleteAllocAreaByAreaId(Long areaId) { return allocAreaMapper.deleteAllocAreaByAreaId(areaId); } + + @Override + public List buildAreaTreeSelect(List areas) + { + List deptTrees = buildAreaTree(areas); + return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); + } + + @Override + public List buildAreaTree(List areas) { + List returnList = new ArrayList(); + List tempList = areas.stream().map(AllocArea::getAreaId).collect(Collectors.toList()); + for (AllocArea area : areas) + { + // 如果是顶级节点, 遍历该父节点的所有子节点 + if (!tempList.contains(area.getParentId())) + { + recursionFn(areas, area); + returnList.add(area); + } + } + if (returnList.isEmpty()) + { + returnList = areas; + } + return returnList; + } + + private void recursionFn(List list, AllocArea t) + { + // 得到子节点列表 + List childList = getChildList(list, t); + t.setChildren(childList); + for (AllocArea tChild : childList) + { + if (hasChild(list, tChild)) + { + recursionFn(list, tChild); + } + } + } + + private List getChildList(List list, AllocArea t) + { + List tlist = new ArrayList(); + Iterator it = list.iterator(); + while (it.hasNext()) + { + AllocArea n = (AllocArea) it.next(); + if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getAreaId().longValue()) + { + tlist.add(n); + } + } + return tlist; + } + + /** + * 判断是否有子节点 + */ + private boolean hasChild(List list, AllocArea t) + { + return getChildList(list, t).size() > 0 ? true : false; + } + }