feat(JsItemCategory): add tree structure support with parent-child relationships and new endpoints for category retrieval

This commit is contained in:
2025-11-08 10:15:37 +08:00
parent 220333a95b
commit 87e8de27fb
9 changed files with 138 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.jsItemCategory.dto.CreateJsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.dto.JsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.dto.JsItemCategoryTreeDto;
import cn.lihongjie.coal.jsItemCategory.dto.UpdateJsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.service.JsItemCategoryService;
@@ -18,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/jsItemCategory")
@SysLog(module = "")
@@ -63,4 +66,14 @@ public class JsItemCategoryController {
this.service.unarchive(request);
return true;
}
@PostMapping("/roots")
public List<JsItemCategoryTreeDto> roots(@RequestBody CommonQuery request) {
return this.service.getRoots(request);
}
@PostMapping("/treeByIds")
public List<JsItemCategoryTreeDto> treeByIds(@RequestBody IdRequest request) {
return this.service.getTreeByIds(request);
}
}

View File

@@ -5,4 +5,6 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class CreateJsItemCategoryDto extends OrgCommonDto {}
public class CreateJsItemCategoryDto extends OrgCommonDto {
private String parent;
}

View File

@@ -12,4 +12,6 @@ public class JsItemCategoryDto extends OrgCommonDto {
@DictTranslate(dictKey = DictCode.ARCHIVESTATUS)
private String archiveStatusName;
private String parent;
}

View File

@@ -0,0 +1,14 @@
package cn.lihongjie.coal.jsItemCategory.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import java.util.List;
@Data
public class JsItemCategoryTreeDto extends OrgCommonDto {
private List<JsItemCategoryTreeDto> children;
private String parent;
}

View File

@@ -5,4 +5,6 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
@Data
public class UpdateJsItemCategoryDto extends OrgCommonDto {}
public class UpdateJsItemCategoryDto extends OrgCommonDto {
private String parent;
}

View File

@@ -2,7 +2,10 @@ package cn.lihongjie.coal.jsItemCategory.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Data;
@@ -10,6 +13,8 @@ import lombok.Data;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Comment;
import java.util.List;
@Data
@Entity
@Table(
@@ -21,4 +26,9 @@ public class JsItemCategoryEntity extends OrgCommonEntity {
@Comment("归档状态")
@ColumnDefault("'0'")
private String archiveStatus = "0";
@ManyToOne private JsItemCategoryEntity parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<JsItemCategoryEntity> children;
}

View File

@@ -5,10 +5,14 @@ import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.jsItemCategory.dto.CreateJsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.dto.JsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.dto.JsItemCategoryTreeDto;
import cn.lihongjie.coal.jsItemCategory.dto.UpdateJsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.entity.JsItemCategoryEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.control.DeepClone;
@Mapper(
@@ -20,4 +24,11 @@ public interface JsItemCategoryMapper
JsItemCategoryEntity,
JsItemCategoryDto,
CreateJsItemCategoryDto,
UpdateJsItemCategoryDto> {}
UpdateJsItemCategoryDto> {
@Mappings({@Mapping(target = "children", qualifiedByName = "toTreeDto")})
@Named("toTreeDto")
JsItemCategoryTreeDto toTreeDto(JsItemCategoryEntity entity);
@Mappings({@Mapping(target = "children", ignore = true)})
JsItemCategoryTreeDto toTreeDtoExcludeChildren(JsItemCategoryEntity entity);
}

View File

@@ -3,10 +3,12 @@ package cn.lihongjie.coal.jsItemCategory.service;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.common.TreeUtils;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.jsItemCategory.dto.CreateJsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.dto.JsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.dto.JsItemCategoryTreeDto;
import cn.lihongjie.coal.jsItemCategory.dto.UpdateJsItemCategoryDto;
import cn.lihongjie.coal.jsItemCategory.entity.JsItemCategoryEntity;
import cn.lihongjie.coal.jsItemCategory.mapper.JsItemCategoryMapper;
@@ -14,6 +16,7 @@ import cn.lihongjie.coal.jsItemCategory.repository.JsItemCategoryRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
@@ -22,6 +25,11 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@Service
@Slf4j
@Transactional
@@ -91,4 +99,60 @@ public class JsItemCategoryService
public void unarchive(IdRequest dto) {
this.repository.unArchive(dto);
}
public List<JsItemCategoryTreeDto> getRoots(CommonQuery request) {
if (CollectionUtils.isEmpty(request.getItems())) {
List<JsItemCategoryEntity> roots =
this.repository.findAll(
(root, query, criteriaBuilder) ->
criteriaBuilder.isNull(root.get("parent")));
return roots.stream().map(de -> this.mapper.toTreeDto(de)).collect(Collectors.toList());
} else {
Page<JsItemCategoryEntity> page =
repository.findAll(
request.specification(conversionService),
PageRequest.of(
request.getPageNo(),
request.getPageSize(),
Sort.by(request.getOrders())));
List<String> selfAndParentIds =
this.dbFunctionService.selfAndParentIds(
dbFunctionService.entityToTableName(JsItemCategoryEntity.class),
page.stream().map(x -> x.getId()).collect(Collectors.toList()),
true);
List<JsItemCategoryTreeDto> selfAndParent =
this.findAllByIds(selfAndParentIds).stream()
.map(x -> (this.mapper.toTreeDtoExcludeChildren(x)))
.collect(Collectors.toList());
return StreamSupport.stream(
TreeUtils.buildTreeFromList(
selfAndParent,
JsItemCategoryTreeDto::getId,
x -> x.getParent(),
(x, y) -> {
if (x.getChildren() == null) {
x.setChildren(new ArrayList<>());
}
x.getChildren().add(y);
return null;
})
.spliterator(),
false)
.collect(Collectors.toList());
}
}
public List<JsItemCategoryTreeDto> getTreeByIds(IdRequest request) {
if (request.getIds().isEmpty()) {
return new ArrayList<>();
}
var roots =
this.repository.findAll(
(root, query, criteriaBuilder) -> root.get("id").in(request.getIds()));
return roots.stream().map(de -> this.mapper.toTreeDto(de)).collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,17 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.jsItemCategory.controller.JsItemCategoryController
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(JsItemCategoryController.class)
return controller.roots(new CommonQuery())