From d9b764868dbfa79aa79d79f676f60a28c4055b06 Mon Sep 17 00:00:00 2001
From: chenhj <1263187585@qq.com>
Date: 星期六, 14 六月 2025 17:00:07 +0800
Subject: [PATCH] 库存明细数据处理

---
 main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java |  140 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 137 insertions(+), 3 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java
index ac74173..53182f3 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java
@@ -3,19 +3,30 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.entity.CoalField;
+import com.ruoyi.basic.entity.CoalValue;
 import com.ruoyi.basic.mapper.CoalFieldMapper;
 import com.ruoyi.basic.mapper.CoalValueMapper;
 import com.ruoyi.business.dto.PendingInventoryDto;
+import com.ruoyi.business.entity.OfficialInventory;
 import com.ruoyi.business.entity.PendingInventory;
+import com.ruoyi.business.mapper.OfficialInventoryMapper;
 import com.ruoyi.business.mapper.PendingInventoryMapper;
+import com.ruoyi.business.service.InputInventoryRecordService;
+import com.ruoyi.business.service.InventorySummaryService;
 import com.ruoyi.business.service.PendingInventoryService;
+import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Objects;
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -31,15 +42,67 @@
 
     private final PendingInventoryMapper pendingInventoryMapper;
 
+    private final OfficialInventoryMapper officialInventoryMapper;
+
     private final CoalValueMapper coalValueMapper;
 
     private final CoalFieldMapper coalFieldMapper;
 
+    private final InputInventoryRecordService inputInventoryRecordService;
+
+    private final InventorySummaryService inventorySummaryService;
+
     @Override
-    public IPage<PendingInventory> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) {
+    public IPage<PendingInventoryDto> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) {
+        // 1. 鏋勫缓涓绘煡璇�
         LambdaQueryWrapper<PendingInventory> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.orderByDesc(PendingInventory::getCreateTime);
-        return pendingInventoryMapper.selectPage(page, queryWrapper);
+
+        // 2. 鎵ц涓昏〃鍒嗛〉鏌ヨ
+        IPage<PendingInventory> pendingInventoryPage = pendingInventoryMapper.selectPage(page, queryWrapper);
+
+        // 3. 鏃犳暟鎹揩閫熻繑鍥�
+        if (CollectionUtils.isEmpty(pendingInventoryPage.getRecords())) {
+            return new Page<>(page.getCurrent(), page.getSize(), pendingInventoryPage.getTotal());
+        }
+
+        // 4. 鎻愬彇鎵�鏈夊緟澶勭悊搴撳瓨ID
+        List<Long> pendingIds = pendingInventoryPage.getRecords().stream()
+                .map(PendingInventory::getId)
+                .collect(Collectors.toList());
+
+        // 5. 鎵归噺鏌ヨ鍏宠仈鐨勬寮忓簱瀛樹俊鎭�
+        Map<Long, Long> pendingToOfficialMap = getOfficialInventoryMap(pendingIds);
+
+        // 6. 浣跨敤MyBatis-Plus鐨刢onvert鏂规硶杞崲DTO
+        return pendingInventoryPage.convert(record -> {
+            PendingInventoryDto dto = new PendingInventoryDto();
+            BeanUtils.copyProperties(record, dto);
+
+            // 浠庨鍔犺浇鐨凪ap涓幏鍙杘fficialId
+            dto.setOfficialId(pendingToOfficialMap.getOrDefault(record.getId(), null));
+            return dto;
+        });
+    }
+
+    // 鎵归噺鑾峰彇寰呭鐞嗗簱瀛樹笌姝e紡搴撳瓨鐨勬槧灏勫叧绯�
+    private Map<Long, Long> getOfficialInventoryMap(List<Long> pendingIds) {
+        if (CollectionUtils.isEmpty(pendingIds)) {
+            return Collections.emptyMap();
+        }
+
+        // 鏌ヨ鍏宠仈鐨勬寮忓簱瀛樻暟鎹�
+        LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>();
+        wrapper.select(OfficialInventory::getId, OfficialInventory::getPendingId)
+                .in(OfficialInventory::getPendingId, pendingIds);
+
+        return officialInventoryMapper.selectList(wrapper)
+                .stream()
+                .collect(Collectors.toMap(
+                        OfficialInventory::getPendingId,
+                        OfficialInventory::getId,
+                        (existing, replacement) -> existing // 濡傛灉鏈夐噸澶嶏紝淇濈暀绗竴涓�
+                ));
     }
 
     @Override
@@ -66,4 +129,75 @@
         // 鎵ц鎵归噺閫昏緫鍒犻櫎
         return pendingInventoryMapper.update(null, updateWrapper);
     }
+
+    @Transactional
+    @Override
+    public int addOrEditCoalValue(PendingInventoryDto pendingInventoryDto) {
+        // Step 1: 鍒犻櫎鍘熸湁 planId 鐩稿叧鏁版嵁
+        coalValueMapper.delete(new LambdaQueryWrapper<CoalValue>()
+                .eq(CoalValue::getPlanId, pendingInventoryDto.getPId()));
+        // Step 2: 鏋勫缓鍩虹 CoalValue 瀵硅薄
+        CoalValue coalValue = new CoalValue();
+        BeanUtils.copyProperties(coalValue, pendingInventoryDto);
+        coalValue.setPlanId(pendingInventoryDto.getPId());
+        List<Map<String, String>> fieldValue = pendingInventoryDto.getFieldValue();
+        if (CollectionUtils.isEmpty(fieldValue)) {
+            throw new BaseException("瀛楁鍊间笉鑳戒负绌�");
+        }
+        // Step 3: 鎻愬墠鑾峰彇鎵�鏈� field -> CoalField 鏄犲皠锛岄伩鍏嶉噸澶嶆煡搴�
+        Set<String> allFields = fieldValue.stream()
+                .flatMap(map -> map.keySet().stream())
+                .collect(Collectors.toSet());
+        List<CoalField> coalFields = coalFieldMapper.selectList(
+                new LambdaQueryWrapper<CoalField>().in(CoalField::getFields, allFields));
+        Map<String, String> fieldMap = coalFields.stream()
+                .collect(Collectors.toMap(
+                        CoalField::getFields,
+                        CoalField::getFieldName));
+        // Step 4: 鎵归噺鎻掑叆
+        int i = 0;
+        for (Map<String, String> fieldMapEntry : fieldValue) {
+            for (Map.Entry<String, String> entry : fieldMapEntry.entrySet()) {
+                String key = entry.getKey();
+                String value = entry.getValue();
+                String fieldName = fieldMap.get(key);
+                if (fieldName == null) {
+                    throw new BaseException("瀛楁鍚嶄笉瀛樺湪");
+                }
+                coalValue.setId(null); // 娓呯┖ id锛岀‘淇濇瘡娆¢兘鏄柊璁板綍
+                coalValue.setCoalValue(value);
+                coalValue.setFields(key);
+                coalValue.setFieldName(fieldName);
+                i = coalValueMapper.insert(coalValue);
+            }
+        }
+        if (i > 0) {
+            BigDecimal quantity = pendingInventoryDto.getInventoryQuantity();
+            PendingInventory pendingInventory = pendingInventoryMapper.selectById(pendingInventoryDto.getPId());
+            if (pendingInventory == null) {
+                throw new BaseException("寰呭叆搴撹褰曚笉瀛樺湪");
+            }
+            BigDecimal left = pendingInventory.getInventoryQuantity().subtract(quantity);
+            if (left.compareTo(BigDecimal.ZERO) > 0) {
+                pendingInventory.setInventoryQuantity(left);
+                pendingInventoryMapper.updateById(pendingInventory);
+            } else {
+                pendingInventoryMapper.deleteById(pendingInventoryDto.getPId());
+            }
+            //姝e紡搴�
+            if (pendingInventoryDto.getOfficialId() == null) {
+                OfficialInventory officialInventory = new OfficialInventory();
+                BeanUtils.copyProperties(pendingInventory, officialInventory);
+                officialInventory.setId(null);
+                officialInventory.setPendingId(pendingInventoryDto.getPId());
+                officialInventory.setInventoryQuantity(quantity);
+                officialInventoryMapper.insert(officialInventory);
+            } else {
+                OfficialInventory officialInventory = officialInventoryMapper.selectById(pendingInventoryDto.getOfficialId());
+                officialInventory.setInventoryQuantity(quantity.add(officialInventory.getInventoryQuantity()));
+                officialInventoryMapper.updateById(officialInventory);
+            }
+        }
+        return i;
+    }
 }

--
Gitblit v1.9.3