From f6f57ba70679a0b050031f3cdf81b5bf5d4cbd60 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 11 七月 2025 15:28:37 +0800
Subject: [PATCH] 1.生产加工优化 2.销售,库存查询优化

---
 main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java |  167 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 154 insertions(+), 13 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 adbc5e9..d76fcf5 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
@@ -7,26 +7,34 @@
 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.CoalInfo;
 import com.ruoyi.basic.entity.CoalValue;
 import com.ruoyi.basic.mapper.CoalFieldMapper;
+import com.ruoyi.basic.mapper.CoalInfoMapper;
 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.entity.SalesRecord;
 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.core.domain.entity.SysUser;
 import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.system.mapper.SysUserMapper;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
+import java.time.LocalDate;
+import java.time.format.DateTimeParseException;
+import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -49,11 +57,133 @@
 
     private final CoalFieldMapper coalFieldMapper;
 
+    private final CoalInfoMapper coalInfoMapper;
+
+    private final SysUserMapper sysUserMapper;
+
+    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<>();
+        if (StringUtils.isNotBlank(pendingInventoryDto.getSearchAll())) {
+            String searchValue = pendingInventoryDto.getSearchAll();
+            // 1. 鍏堝皾璇曚綔涓烘棩鏈熸煡璇�
+            try {
+                LocalDate RegistrationDate = LocalDate.parse(searchValue);
+                queryWrapper.eq(PendingInventory::getRegistrationDate, RegistrationDate);
+            } catch (DateTimeParseException e) {
+                // 2. 濡傛灉涓嶆槸鏃ユ湡锛屽垯浣滀负鐓ょ鍚嶇О鏌ヨ
+                LambdaQueryWrapper<CoalInfo> coalQueryWrapper = new LambdaQueryWrapper<>();
+                coalQueryWrapper.like(CoalInfo::getCoal, searchValue);
+                List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalQueryWrapper);
+                if (!coalInfos.isEmpty()) {
+                    // 鎻愬彇鎵�鏈夊尮閰嶇殑鐓ょID
+                    List<Long> coalIds = coalInfos.stream()
+                            .map(CoalInfo::getId)
+                            .collect(Collectors.toList());
+                    // 浣跨敤in鏌ヨ鍖归厤浠绘剰涓�涓叅绉岻D
+                    queryWrapper.in(PendingInventory::getCoalId, coalIds);
+                } else {
+                    // 3. 濡傛灉鎵句笉鍒扮叅绉嶏紝鍙互杩斿洖绌虹粨鏋�
+                    queryWrapper.eq(PendingInventory::getCoalId, "-1"); // 浣跨敤涓嶅彲鑳藉瓨鍦ㄧ殑ID
+                }
+            }
+        }
+
         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. 鎵归噺鏌ヨ鍏宠仈鐨勭叅鐐俊鎭拰姝e紡搴撳瓨淇℃伅
+        List<Long> coalIds = pendingInventoryPage.getRecords().stream()
+                .map(PendingInventory::getCoalId)
+                .distinct()
+                .collect(Collectors.toList());
+
+        // 鎵归噺鏌ヨCoalInfo
+        Map<Long, CoalInfo> coalInfoMap;
+        if (!coalIds.isEmpty()) {
+            List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds));
+            coalInfoMap = coalInfos.stream().collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
+        } else {
+            coalInfoMap = new HashMap<>();
+        }
+
+        // 5. 鎵归噺鏌ヨ鐧昏浜篿d
+        List<Long> registrantIds = pendingInventoryPage.getRecords().stream()
+                .map(PendingInventory::getRegistrantId)
+                .distinct()
+                .toList();
+        // 鎵归噺鏌ヨ鐧昏浜�
+        Map<Long, SysUser> sysUserMap;
+        if (!registrantIds.isEmpty()) {
+            List<SysUser> sysUsers = sysUserMapper.selectList(registrantIds);
+            sysUserMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
+        } else {
+            sysUserMap = new HashMap<>();
+        }
+
+        // 鎵归噺鏌ヨ姝e紡搴撳瓨淇℃伅
+        Map<Long, Long> pendingToOfficialMap = getOfficialInventoryMap(pendingIds);
+
+        // 6. 杞崲DTO骞惰缃浉鍏冲瓧娈�
+        return pendingInventoryPage.convert(record -> {
+            PendingInventoryDto dto = new PendingInventoryDto();
+            BeanUtils.copyProperties(record, dto);
+
+            // 璁剧疆Coal淇℃伅
+            CoalInfo coalInfo = coalInfoMap.get(record.getCoalId());
+            if (coalInfo != null) {
+                dto.setCoal(coalInfo.getCoal());
+            }
+
+            // 璁剧疆鐧昏浜�
+            SysUser sysUser = sysUserMap.get(record.getRegistrantId());
+            if (sysUser != null) {
+                dto.setRegistrant(sysUser.getNickName());
+            }
+
+            // 浠庨鍔犺浇鐨凪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
@@ -119,6 +249,7 @@
                 coalValue.setCoalValue(value);
                 coalValue.setFields(key);
                 coalValue.setFieldName(fieldName);
+                coalValue.setType(String.valueOf(1));
                 i = coalValueMapper.insert(coalValue);
             }
         }
@@ -131,17 +262,27 @@
             BigDecimal left = pendingInventory.getInventoryQuantity().subtract(quantity);
             if (left.compareTo(BigDecimal.ZERO) > 0) {
                 pendingInventory.setInventoryQuantity(left);
+                pendingInventory.setCoalPlanId(pendingInventoryDto.getCoalPlanId());
                 pendingInventoryMapper.updateById(pendingInventory);
             } else {
                 pendingInventoryMapper.deleteById(pendingInventoryDto.getPId());
             }
-            officialInventoryMapper.delete(new LambdaQueryWrapper<OfficialInventory>().eq(OfficialInventory::getPendingId, pendingInventoryDto.getPId()));
-            OfficialInventory officialInventory = new OfficialInventory();
-            BeanUtils.copyProperties(pendingInventory, officialInventory);
-            officialInventory.setId(null);
-            officialInventory.setPendingId(pendingInventoryDto.getPId());
-            officialInventory.setInventoryQuantity(quantity);
-            officialInventoryMapper.insert(officialInventory);
+            //姝e紡搴�
+            if (pendingInventoryDto.getOfficialId() == null) {
+                OfficialInventory officialInventory = new OfficialInventory();
+                BeanUtils.copyProperties(pendingInventory, officialInventory);
+                officialInventory.setId(null);
+                officialInventory.setCoalPlanId(pendingInventoryDto.getCoalPlanId());
+                officialInventory.setPendingId(pendingInventoryDto.getPId());
+                officialInventory.setInventoryQuantity(quantity);
+                officialInventory.setRegistrantId(1L);
+                officialInventory.setSupplierId(pendingInventoryDto.getSupplierId());
+                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