mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-07-25 07:07:37 +08:00
feat(JsItemCategory): add tree structure support with parent-child relationships and new endpoints for category retrieval
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -12,4 +12,6 @@ public class JsItemCategoryDto extends OrgCommonDto {
|
||||
|
||||
@DictTranslate(dictKey = DictCode.ARCHIVESTATUS)
|
||||
private String archiveStatusName;
|
||||
|
||||
private String parent;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user