mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善设备分类接口
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.deviceCategory.dto.CreateDeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryTreeDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.UpdateDeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.service.DeviceCategoryService;
|
||||
|
||||
@@ -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("/deviceCategory")
|
||||
@SysLog(module = "设备分类")
|
||||
@@ -51,4 +54,14 @@ public class DeviceCategoryController {
|
||||
public Page<DeviceCategoryDto> list(@RequestBody CommonQuery request) {
|
||||
return this.service.list(request);
|
||||
}
|
||||
|
||||
@PostMapping("/roots")
|
||||
public List<DeviceCategoryTreeDto> roots(@RequestBody CommonQuery request) {
|
||||
return this.service.getRoots(request);
|
||||
}
|
||||
|
||||
@PostMapping("/treeByIds")
|
||||
public List<DeviceCategoryTreeDto> treeByIds(@RequestBody IdRequest request) {
|
||||
return this.service.getTreeByIds(request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,5 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CreateDeviceCategoryDto extends OrgCommonDto {
|
||||
|
||||
private String parent;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.lihongjie.coal.deviceCategory.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.OrgCommonDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class DeviceCategoryTreeDto extends OrgCommonDto {
|
||||
private List<DeviceCategoryTreeDto> children;
|
||||
|
||||
private String parent;
|
||||
}
|
||||
@@ -14,12 +14,8 @@ import java.util.List;
|
||||
@Data
|
||||
@Entity
|
||||
public class DeviceCategoryEntity extends OrgCommonEntity {
|
||||
|
||||
@ManyToOne private DeviceCategoryEntity parent;
|
||||
|
||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
|
||||
private List<DeviceCategoryEntity> children;
|
||||
|
||||
@ManyToOne
|
||||
private DeviceCategoryEntity parent;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,10 +5,14 @@ import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
|
||||
import cn.lihongjie.coal.base.mapper.CommonMapper;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.CreateDeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryTreeDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.UpdateDeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.entity.DeviceCategoryEntity;
|
||||
|
||||
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 DeviceCategoryMapper
|
||||
DeviceCategoryEntity,
|
||||
DeviceCategoryDto,
|
||||
CreateDeviceCategoryDto,
|
||||
UpdateDeviceCategoryDto> {}
|
||||
UpdateDeviceCategoryDto> {
|
||||
@Mappings({@Mapping(target = "children", qualifiedByName = "toTreeDto")})
|
||||
@Named("toTreeDto")
|
||||
DeviceCategoryTreeDto toTreeDto(DeviceCategoryEntity entity);
|
||||
|
||||
@Mappings({@Mapping(target = "children", ignore = true)})
|
||||
DeviceCategoryTreeDto toTreeDtoExcludeChildren(DeviceCategoryEntity entity);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@ package cn.lihongjie.coal.deviceCategory.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.deviceCategory.dto.CreateDeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryTreeDto;
|
||||
import cn.lihongjie.coal.deviceCategory.dto.UpdateDeviceCategoryDto;
|
||||
import cn.lihongjie.coal.deviceCategory.entity.DeviceCategoryEntity;
|
||||
import cn.lihongjie.coal.deviceCategory.mapper.DeviceCategoryMapper;
|
||||
@@ -12,6 +15,7 @@ import cn.lihongjie.coal.deviceCategory.repository.DeviceCategoryRepository;
|
||||
|
||||
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 +24,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 +40,8 @@ public class DeviceCategoryService
|
||||
|
||||
@Autowired private ConversionService conversionService;
|
||||
|
||||
@Autowired private DbFunctionService dbFunctionService;
|
||||
|
||||
public DeviceCategoryDto create(CreateDeviceCategoryDto request) {
|
||||
DeviceCategoryEntity entity = mapper.toEntity(request);
|
||||
|
||||
@@ -68,4 +79,60 @@ public class DeviceCategoryService
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
|
||||
public List<DeviceCategoryTreeDto> getRoots(CommonQuery request) {
|
||||
|
||||
if (CollectionUtils.isEmpty(request.getItems())) {
|
||||
List<DeviceCategoryEntity> 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<DeviceCategoryEntity> page =
|
||||
repository.findAll(
|
||||
request.specification(conversionService),
|
||||
PageRequest.of(
|
||||
request.getPageNo(),
|
||||
request.getPageSize(),
|
||||
Sort.by(request.getOrders())));
|
||||
|
||||
List<String> selfAndParentIds =
|
||||
this.dbFunctionService.selfAndParentIds(
|
||||
dbFunctionService.entityToTableName(DeviceCategoryEntity.class),
|
||||
page.stream().map(x -> x.getId()).collect(Collectors.toList()),
|
||||
true);
|
||||
|
||||
List<DeviceCategoryTreeDto> selfAndParent =
|
||||
this.findAllByIds(selfAndParentIds).stream()
|
||||
.map(x -> (this.mapper.toTreeDtoExcludeChildren(x)))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return StreamSupport.stream(
|
||||
TreeUtils.buildTreeFromList(
|
||||
selfAndParent,
|
||||
DeviceCategoryTreeDto::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<DeviceCategoryTreeDto> 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.deviceCategory.controller.DeviceCategoryController
|
||||
import org.springframework.context.ApplicationContext
|
||||
|
||||
ApplicationContext ioc = ioc
|
||||
|
||||
def controller = ioc.getBean(DeviceCategoryController.class)
|
||||
|
||||
|
||||
|
||||
|
||||
return controller.roots(new CommonQuery())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user