diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/controller/JsItemCategoryController.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/controller/JsItemCategoryController.java index aec2b4a7..b5a3ff30 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/controller/JsItemCategoryController.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/controller/JsItemCategoryController.java @@ -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 roots(@RequestBody CommonQuery request) { + return this.service.getRoots(request); + } + + @PostMapping("/treeByIds") + public List treeByIds(@RequestBody IdRequest request) { + return this.service.getTreeByIds(request); + } } diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/CreateJsItemCategoryDto.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/CreateJsItemCategoryDto.java index 6caf11d1..19ac0d90 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/CreateJsItemCategoryDto.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/CreateJsItemCategoryDto.java @@ -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; +} diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryDto.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryDto.java index 6d6d05f8..56eb08ed 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryDto.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryDto.java @@ -12,4 +12,6 @@ public class JsItemCategoryDto extends OrgCommonDto { @DictTranslate(dictKey = DictCode.ARCHIVESTATUS) private String archiveStatusName; + + private String parent; } diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryTreeDto.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryTreeDto.java new file mode 100644 index 00000000..e9282310 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/JsItemCategoryTreeDto.java @@ -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 children; + + private String parent; +} diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/UpdateJsItemCategoryDto.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/UpdateJsItemCategoryDto.java index a952a74e..3284bea8 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/UpdateJsItemCategoryDto.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/dto/UpdateJsItemCategoryDto.java @@ -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; +} diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/entity/JsItemCategoryEntity.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/entity/JsItemCategoryEntity.java index 13fe1d4f..1cc7b236 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/entity/JsItemCategoryEntity.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/entity/JsItemCategoryEntity.java @@ -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 children; } diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/mapper/JsItemCategoryMapper.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/mapper/JsItemCategoryMapper.java index 0e4d5a80..a9f118f1 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/mapper/JsItemCategoryMapper.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/mapper/JsItemCategoryMapper.java @@ -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); +} diff --git a/src/main/java/cn/lihongjie/coal/jsItemCategory/service/JsItemCategoryService.java b/src/main/java/cn/lihongjie/coal/jsItemCategory/service/JsItemCategoryService.java index 9af735eb..4fe60821 100644 --- a/src/main/java/cn/lihongjie/coal/jsItemCategory/service/JsItemCategoryService.java +++ b/src/main/java/cn/lihongjie/coal/jsItemCategory/service/JsItemCategoryService.java @@ -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 getRoots(CommonQuery request) { + + if (CollectionUtils.isEmpty(request.getItems())) { + List 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 page = + repository.findAll( + request.specification(conversionService), + PageRequest.of( + request.getPageNo(), + request.getPageSize(), + Sort.by(request.getOrders()))); + + List selfAndParentIds = + this.dbFunctionService.selfAndParentIds( + dbFunctionService.entityToTableName(JsItemCategoryEntity.class), + page.stream().map(x -> x.getId()).collect(Collectors.toList()), + true); + + List 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 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()); + } } diff --git a/src/main/resources/scripts/dict/tree/jsItemCategoryTree.groovy b/src/main/resources/scripts/dict/tree/jsItemCategoryTree.groovy new file mode 100644 index 00000000..bd6befa6 --- /dev/null +++ b/src/main/resources/scripts/dict/tree/jsItemCategoryTree.groovy @@ -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()) + +