From 665d770316abd62edcae3976e8645ffa4bda4ff7 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Sat, 2 Nov 2024 14:52:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=95=86=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarehouseGoodsCategoryController.java | 15 +++- .../dto/CreateWarehouseGoodsCategoryDto.java | 3 - .../dto/UpdateWarehouseGoodsCategoryDto.java | 4 - .../dto/WarehouseGoodsCategoryDto.java | 4 - .../dto/WarehouseGoodsCategoryTreeDto.java | 14 ++++ .../entity/WarehouseGoodsCategoryEntity.java | 23 +++--- .../mapper/WarehouseGoodsCategoryMapper.java | 14 +++- .../WarehouseGoodsCategoryRepository.java | 8 +- .../WarehouseGoodsCategoryService.java | 74 +++++++++++++++++++ .../tree/warehouseGoodsCategoryTree.groovy | 17 +++++ 10 files changed, 150 insertions(+), 26 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryTreeDto.java create mode 100644 src/main/resources/scripts/dict/tree/warehouseGoodsCategoryTree.groovy diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/controller/WarehouseGoodsCategoryController.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/controller/WarehouseGoodsCategoryController.java index 981f32fa..4e61184b 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/controller/WarehouseGoodsCategoryController.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/controller/WarehouseGoodsCategoryController.java @@ -7,6 +7,7 @@ import cn.lihongjie.coal.base.dto.IdRequest; import cn.lihongjie.coal.warehouseGoodsCategory.dto.CreateWarehouseGoodsCategoryDto; import cn.lihongjie.coal.warehouseGoodsCategory.dto.UpdateWarehouseGoodsCategoryDto; import cn.lihongjie.coal.warehouseGoodsCategory.dto.WarehouseGoodsCategoryDto; +import cn.lihongjie.coal.warehouseGoodsCategory.dto.WarehouseGoodsCategoryTreeDto; import cn.lihongjie.coal.warehouseGoodsCategory.service.WarehouseGoodsCategoryService; import lombok.extern.slf4j.Slf4j; @@ -18,9 +19,11 @@ 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("/warehouseGoodsCategory") -@SysLog(module = "仓库商品类目管理") +@SysLog(module = "仓库商品分类") @Slf4j @OrgScope public class WarehouseGoodsCategoryController { @@ -51,4 +54,14 @@ public class WarehouseGoodsCategoryController { public Page list(@RequestBody CommonQuery request) { return this.service.list(request); } + + @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/warehouseGoodsCategory/dto/CreateWarehouseGoodsCategoryDto.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/CreateWarehouseGoodsCategoryDto.java index 8be7724a..e94a4ba5 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/CreateWarehouseGoodsCategoryDto.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/CreateWarehouseGoodsCategoryDto.java @@ -2,12 +2,9 @@ package cn.lihongjie.coal.warehouseGoodsCategory.dto; import cn.lihongjie.coal.base.dto.OrgCommonDto; -import jakarta.persistence.ManyToOne; - import lombok.Data; @Data public class CreateWarehouseGoodsCategoryDto extends OrgCommonDto { - @ManyToOne private String parent; } diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/UpdateWarehouseGoodsCategoryDto.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/UpdateWarehouseGoodsCategoryDto.java index 63694dd0..64250c83 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/UpdateWarehouseGoodsCategoryDto.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/UpdateWarehouseGoodsCategoryDto.java @@ -2,13 +2,9 @@ package cn.lihongjie.coal.warehouseGoodsCategory.dto; import cn.lihongjie.coal.base.dto.OrgCommonDto; -import jakarta.persistence.ManyToOne; - import lombok.Data; @Data public class UpdateWarehouseGoodsCategoryDto extends OrgCommonDto { - - @ManyToOne private String parent; } diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryDto.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryDto.java index 9adeeb02..c42c4f34 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryDto.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryDto.java @@ -2,13 +2,9 @@ package cn.lihongjie.coal.warehouseGoodsCategory.dto; import cn.lihongjie.coal.base.dto.OrgCommonDto; -import jakarta.persistence.ManyToOne; - import lombok.Data; @Data public class WarehouseGoodsCategoryDto extends OrgCommonDto { - - @ManyToOne private String parent; } diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryTreeDto.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryTreeDto.java new file mode 100644 index 00000000..14cadb81 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/dto/WarehouseGoodsCategoryTreeDto.java @@ -0,0 +1,14 @@ +package cn.lihongjie.coal.warehouseGoodsCategory.dto; + +import cn.lihongjie.coal.base.dto.OrgCommonDto; + +import lombok.Data; + +import java.util.List; + +@Data +public class WarehouseGoodsCategoryTreeDto extends OrgCommonDto { + private List children; + + private String parent; +} diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/entity/WarehouseGoodsCategoryEntity.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/entity/WarehouseGoodsCategoryEntity.java index 72e8fde4..2d1f2a43 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/entity/WarehouseGoodsCategoryEntity.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/entity/WarehouseGoodsCategoryEntity.java @@ -2,7 +2,11 @@ package cn.lihongjie.coal.warehouseGoodsCategory.entity; import cn.lihongjie.coal.base.entity.OrgCommonEntity; -import jakarta.persistence.*; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import lombok.Data; @@ -10,17 +14,14 @@ import java.util.List; @Data @Entity - -@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_warehouse_goods_category_org_id", columnList = "organization_id")) - +@Table( + indexes = + @jakarta.persistence.Index( + name = "idx_warehouseGoodsCategory_org_id", + columnList = "organization_id")) public class WarehouseGoodsCategoryEntity extends OrgCommonEntity { - + @ManyToOne private WarehouseGoodsCategoryEntity parent; @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List children; - - @ManyToOne - private WarehouseGoodsCategoryEntity parent; - - -} \ No newline at end of file +} diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/mapper/WarehouseGoodsCategoryMapper.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/mapper/WarehouseGoodsCategoryMapper.java index aceba4cb..c266d2b1 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/mapper/WarehouseGoodsCategoryMapper.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/mapper/WarehouseGoodsCategoryMapper.java @@ -1,15 +1,18 @@ package cn.lihongjie.coal.warehouseGoodsCategory.mapper; - import cn.lihongjie.coal.base.mapper.BaseMapper; import cn.lihongjie.coal.base.mapper.CommonEntityMapper; import cn.lihongjie.coal.base.mapper.CommonMapper; import cn.lihongjie.coal.warehouseGoodsCategory.dto.CreateWarehouseGoodsCategoryDto; import cn.lihongjie.coal.warehouseGoodsCategory.dto.UpdateWarehouseGoodsCategoryDto; import cn.lihongjie.coal.warehouseGoodsCategory.dto.WarehouseGoodsCategoryDto; +import cn.lihongjie.coal.warehouseGoodsCategory.dto.WarehouseGoodsCategoryTreeDto; import cn.lihongjie.coal.warehouseGoodsCategory.entity.WarehouseGoodsCategoryEntity; import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.Named; import org.mapstruct.control.DeepClone; @Mapper( @@ -21,4 +24,11 @@ public interface WarehouseGoodsCategoryMapper WarehouseGoodsCategoryEntity, WarehouseGoodsCategoryDto, CreateWarehouseGoodsCategoryDto, - UpdateWarehouseGoodsCategoryDto> {} + UpdateWarehouseGoodsCategoryDto> { + @Mappings({@Mapping(target = "children", qualifiedByName = "toTreeDto")}) + @Named("toTreeDto") + WarehouseGoodsCategoryTreeDto toTreeDto(WarehouseGoodsCategoryEntity entity); + + @Mappings({@Mapping(target = "children", ignore = true)}) + WarehouseGoodsCategoryTreeDto toTreeDtoExcludeChildren(WarehouseGoodsCategoryEntity entity); +} diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/repository/WarehouseGoodsCategoryRepository.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/repository/WarehouseGoodsCategoryRepository.java index bd72c0b3..e654d28e 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/repository/WarehouseGoodsCategoryRepository.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/repository/WarehouseGoodsCategoryRepository.java @@ -3,8 +3,14 @@ package cn.lihongjie.coal.warehouseGoodsCategory.repository; import cn.lihongjie.coal.base.dao.BaseRepository; import cn.lihongjie.coal.warehouseGoodsCategory.entity.WarehouseGoodsCategoryEntity; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface WarehouseGoodsCategoryRepository - extends BaseRepository {} + extends BaseRepository { + @Query("select false") + boolean isLinked(List ids); +} diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/service/WarehouseGoodsCategoryService.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/service/WarehouseGoodsCategoryService.java index 79d9472a..a73803d2 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/service/WarehouseGoodsCategoryService.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsCategory/service/WarehouseGoodsCategoryService.java @@ -3,15 +3,20 @@ package cn.lihongjie.coal.warehouseGoodsCategory.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.warehouseGoodsCategory.dto.CreateWarehouseGoodsCategoryDto; import cn.lihongjie.coal.warehouseGoodsCategory.dto.UpdateWarehouseGoodsCategoryDto; import cn.lihongjie.coal.warehouseGoodsCategory.dto.WarehouseGoodsCategoryDto; +import cn.lihongjie.coal.warehouseGoodsCategory.dto.WarehouseGoodsCategoryTreeDto; import cn.lihongjie.coal.warehouseGoodsCategory.entity.WarehouseGoodsCategoryEntity; import cn.lihongjie.coal.warehouseGoodsCategory.mapper.WarehouseGoodsCategoryMapper; import cn.lihongjie.coal.warehouseGoodsCategory.repository.WarehouseGoodsCategoryRepository; 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; @@ -20,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 @@ -31,6 +41,8 @@ public class WarehouseGoodsCategoryService @Autowired private ConversionService conversionService; + @Autowired private DbFunctionService dbFunctionService; + public WarehouseGoodsCategoryDto create(CreateWarehouseGoodsCategoryDto request) { WarehouseGoodsCategoryEntity entity = mapper.toEntity(request); @@ -48,6 +60,12 @@ public class WarehouseGoodsCategoryService } public void delete(IdRequest request) { + boolean linked = this.repository.isLinked(request.getIds()); + + if (linked) { + throw new BizException("数据已被关联,无法删除"); + } + this.repository.deleteAllById(request.getIds()); } @@ -68,4 +86,60 @@ public class WarehouseGoodsCategoryService return page.map(this.mapper::toDto); } + + 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(WarehouseGoodsCategoryEntity.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, + WarehouseGoodsCategoryTreeDto::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/warehouseGoodsCategoryTree.groovy b/src/main/resources/scripts/dict/tree/warehouseGoodsCategoryTree.groovy new file mode 100644 index 00000000..794c5360 --- /dev/null +++ b/src/main/resources/scripts/dict/tree/warehouseGoodsCategoryTree.groovy @@ -0,0 +1,17 @@ + +package scripts.dict + +import cn.lihongjie.coal.base.dto.CommonQuery +import cn.lihongjie.coal.warehouseGoodsCategory.controller.WarehouseGoodsCategoryController +import org.springframework.context.ApplicationContext + +ApplicationContext ioc = ioc + +def controller = ioc.getBean(WarehouseGoodsCategoryController.class) + + + + +return controller.roots(new CommonQuery()) + +