This commit is contained in:
sxu 2025-04-03 16:30:36 +08:00
parent 3d5c2836ba
commit 63a19d6786
5 changed files with 230 additions and 0 deletions

View File

@ -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("系统异常,请联系管理员");
}
/**
* 查询区域列表
*/

View File

@ -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<AllocArea> children = new ArrayList();
}

View File

@ -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<TreeSelect> 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<TreeSelect> getChildren() {
return children;
}
public void setChildren(List<TreeSelect> children) {
this.children = children;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}

View File

@ -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<TreeSelect> selectAreaTreeList(AllocArea area);
/**
* 查询区域
*
@ -57,4 +62,8 @@ public interface IAllocAreaService {
* @return 结果
*/
public int deleteAllocAreaByAreaId(Long areaId);
public List<TreeSelect> buildAreaTreeSelect(List<AllocArea> areas);
public List<AllocArea> buildAreaTree(List<AllocArea> areas);
}

View File

@ -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<TreeSelect> selectAreaTreeList(AllocArea area) {
List<AllocArea> 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<TreeSelect> buildAreaTreeSelect(List<AllocArea> areas)
{
List<AllocArea> deptTrees = buildAreaTree(areas);
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
}
@Override
public List<AllocArea> buildAreaTree(List<AllocArea> areas) {
List<AllocArea> returnList = new ArrayList<AllocArea>();
List<Long> 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<AllocArea> list, AllocArea t)
{
// 得到子节点列表
List<AllocArea> childList = getChildList(list, t);
t.setChildren(childList);
for (AllocArea tChild : childList)
{
if (hasChild(list, tChild))
{
recursionFn(list, tChild);
}
}
}
private List<AllocArea> getChildList(List<AllocArea> list, AllocArea t)
{
List<AllocArea> tlist = new ArrayList<AllocArea>();
Iterator<AllocArea> 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<AllocArea> list, AllocArea t)
{
return getChildList(list, t).size() > 0 ? true : false;
}
}