From 8817c6ab14348b360db70ad748dccb4aef692552 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期五, 23 一月 2026 16:13:40 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New' into dev_New
---
src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java | 145 ++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 138 insertions(+), 7 deletions(-)
diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
index 35d0366..6b43c18 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
@@ -1,26 +1,157 @@
package com.ruoyi.production.service.impl;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.production.dto.ProductStructureDto;
import com.ruoyi.production.mapper.ProductStructureMapper;
import com.ruoyi.production.pojo.ProductStructure;
import com.ruoyi.production.service.ProductStructureService;
-import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
-@Service
-@RequiredArgsConstructor
@Slf4j
+@Service
public class ProductStructureServiceImpl extends ServiceImpl<ProductStructureMapper, ProductStructure> implements ProductStructureService {
@Autowired
- private ProductStructureMapper productStructureMapper;
+ private ProductStructureMapper productStructureMapper;
+
@Override
- public List<ProductStructure> listByProductId(Long productId) {
- return productStructureMapper.listByProductId( productId);
+ @Transactional
+ public Boolean addProductStructureDto(ProductStructureDto dto) {
+
+ Long bomId = dto.getBomId();
+
+ // 灏嗘爲鎵佸钩鍖�
+ List<ProductStructureDto> flatDtoList = new ArrayList<>();
+ flattenTree(dto.getChildren(), flatDtoList);
+
+ // 鏌ヨ鏁版嵁搴撲腑宸叉湁鐨� BOM 鏁版嵁
+ List<ProductStructure> dbList = this.list(new LambdaQueryWrapper<ProductStructure>().eq(ProductStructure::getBomId, bomId));
+
+ // 鏌ユ壘宸插瓨鍦ㄧ殑鑺傜偣 - ID
+ Set<Long> frontendIds = flatDtoList.stream()
+ .map(ProductStructureDto::getId)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toSet());
+
+ // 闇�瑕佸垹闄ょ殑鑺傜偣 - ID
+ Set<Long> deleteIds = dbList.stream()
+ .map(ProductStructure::getId)
+ .filter(id -> !frontendIds.contains(id))
+ .collect(Collectors.toSet());
+
+ if (!deleteIds.isEmpty()) {
+ this.removeByIds(deleteIds);
+ }
+
+ // 鏂板 / 鏇存柊
+ List<ProductStructure> insertList = new ArrayList<>();
+ List<ProductStructure> updateList = new ArrayList<>();
+
+ // 鐢ㄤ簬鍥炲啓 parentId
+ Map<String, ProductStructure> tempEntityMap = new HashMap<>();
+
+ for (ProductStructureDto psDto : flatDtoList) {
+ ProductStructure entity = new ProductStructure();
+ BeanUtils.copyProperties(psDto, entity);
+ entity.setBomId(bomId);
+
+ if (psDto.getId() == null) {
+ // 鏂板
+ entity.setId(null);
+ entity.setParentId(null);
+ insertList.add(entity);
+ tempEntityMap.put(psDto.getTempId(), entity);
+ } else {
+ // 鏇存柊
+ updateList.add(entity);
+ }
+ }
+
+ // 鎻掑叆鏂拌妭鐐�
+ if (!insertList.isEmpty()) {
+ this.saveBatch(insertList);
+ }
+
+ // 鍥炲啓鏂板鑺傜偣 parentId
+ List<ProductStructure> parentFixList = new ArrayList<>();
+ // 鐪熷疄鐨勭埗鑺傜偣 ID
+ Long realParentId;
+ for (ProductStructureDto psDto : flatDtoList) {
+ if (psDto.getId() == null && psDto.getParentTempId() != null) {
+ ProductStructure child = tempEntityMap.get(psDto.getTempId());
+ if (tempEntityMap.containsKey(psDto.getParentTempId())) {
+ // 鐖惰妭鐐规槸鏂拌妭鐐�
+ realParentId = tempEntityMap.get(psDto.getParentTempId()).getId();
+ } else {
+ // 鐖惰妭鐐规槸鑰佽妭鐐�
+ realParentId = Long.valueOf(psDto.getParentTempId());
+ }
+
+ child.setParentId(realParentId);
+ parentFixList.add(child);
+ }
+ }
+
+ if (!parentFixList.isEmpty()) {
+ this.updateBatchById(parentFixList);
+ }
+
+ if (!updateList.isEmpty()) {
+ this.updateBatchById(updateList);
+ }
+
+ return true;
}
+
+ /**
+ * 灏嗗墠绔紶鍏ョ殑鏍戣繘琛屾墎骞冲寲
+ *
+ * @param source 鏁版嵁鏍�
+ * @param result 鎵佸钩鍖栨暟鎹�
+ */
+ private void flattenTree(List<ProductStructureDto> source, List<ProductStructureDto> result) {
+ if (source == null) {
+ return;
+ }
+ for (ProductStructureDto node : source) {
+ result.add(node);
+ flattenTree(node.getChildren(), result);
+ }
+ }
+
+
+ @Override
+ public List<ProductStructureDto> listBybomId(Long bomId) {
+ List<ProductStructureDto> list = productStructureMapper.listBybomId(bomId);
+
+ Map<Long, ProductStructureDto> map = new HashMap<>();
+ for (ProductStructureDto node : list) {
+ node.setChildren(new ArrayList<>());
+ map.put(node.getId(), node);
+ }
+
+ List<ProductStructureDto> tree = new ArrayList<>();
+ for (ProductStructureDto node : list) {
+ Long parentId = node.getParentId();
+ if (parentId == null || parentId == 0) {
+ tree.add(node);
+ } else {
+ ProductStructureDto parent = map.get(parentId);
+ if (parent != null) {
+ parent.getChildren().add(node);
+ }
+ }
+ }
+ return tree;
+ }
+
}
--
Gitblit v1.9.3