更新商品分类接口

This commit is contained in:
2024-11-02 14:52:14 +08:00
parent 79a8db6426
commit 665d770316
10 changed files with 150 additions and 26 deletions

View File

@@ -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<WarehouseGoodsCategoryDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
@PostMapping("/roots")
public List<WarehouseGoodsCategoryTreeDto> roots(@RequestBody CommonQuery request) {
return this.service.getRoots(request);
}
@PostMapping("/treeByIds")
public List<WarehouseGoodsCategoryTreeDto> treeByIds(@RequestBody IdRequest request) {
return this.service.getTreeByIds(request);
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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<WarehouseGoodsCategoryTreeDto> children;
private String parent;
}

View File

@@ -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<WarehouseGoodsCategoryEntity> children;
@ManyToOne
private WarehouseGoodsCategoryEntity parent;
}
}

View File

@@ -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);
}

View File

@@ -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<WarehouseGoodsCategoryEntity> {}
extends BaseRepository<WarehouseGoodsCategoryEntity> {
@Query("select false")
boolean isLinked(List<String> ids);
}

View File

@@ -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<WarehouseGoodsCategoryTreeDto> getRoots(CommonQuery request) {
if (CollectionUtils.isEmpty(request.getItems())) {
List<WarehouseGoodsCategoryEntity> 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<WarehouseGoodsCategoryEntity> page =
repository.findAll(
request.specification(conversionService),
PageRequest.of(
request.getPageNo(),
request.getPageSize(),
Sort.by(request.getOrders())));
List<String> selfAndParentIds =
this.dbFunctionService.selfAndParentIds(
dbFunctionService.entityToTableName(WarehouseGoodsCategoryEntity.class),
page.stream().map(x -> x.getId()).collect(Collectors.toList()),
true);
List<WarehouseGoodsCategoryTreeDto> 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<WarehouseGoodsCategoryTreeDto> 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.warehouseGoodsCategory.controller.WarehouseGoodsCategoryController
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(WarehouseGoodsCategoryController.class)
return controller.roots(new CommonQuery())