From 94509204d25f7c0ad213ae2322be2bd5bfd17424 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期一, 14 七月 2025 16:30:28 +0800
Subject: [PATCH] 1.初始化配煤计算器数据 2。配煤到待入库

---
 main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java |  170 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 148 insertions(+), 22 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 d76fcf5..cacd7f6 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
@@ -6,36 +6,50 @@
 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.mchange.v2.lang.SystemUtils;
+import com.ruoyi.basic.dto.CoalFieldDto;
 import com.ruoyi.basic.entity.CoalField;
 import com.ruoyi.basic.entity.CoalInfo;
+import com.ruoyi.basic.entity.CoalPlan;
 import com.ruoyi.basic.entity.CoalValue;
 import com.ruoyi.basic.mapper.CoalFieldMapper;
 import com.ruoyi.basic.mapper.CoalInfoMapper;
+import com.ruoyi.basic.mapper.CoalPlanMapper;
 import com.ruoyi.basic.mapper.CoalValueMapper;
+import com.ruoyi.basic.service.CoalFieldService;
+import com.ruoyi.basic.service.CoalPlanService;
+import com.ruoyi.basic.service.CoalValueService;
 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.InventorySummaryMapper;
 import com.ruoyi.business.mapper.OfficialInventoryMapper;
 import com.ruoyi.business.mapper.PendingInventoryMapper;
+import com.ruoyi.business.mapper.SalesRecordMapper;
 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.SecurityUtils;
 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 org.springframework.transaction.interceptor.TransactionAspectSupport;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDate;
 import java.time.format.DateTimeParseException;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
 
 /**
  * <p>
@@ -64,33 +78,31 @@
     private final InputInventoryRecordService inputInventoryRecordService;
 
     private final InventorySummaryService inventorySummaryService;
+    private final CoalPlanMapper coalPlanMapper;
+    private final CoalValueService coalValueService;
 
     @Override
     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
-                }
+        if (pendingInventoryDto.getRegistrationDate() != null) {
+            queryWrapper.eq(PendingInventory::getRegistrationDate, pendingInventoryDto.getRegistrationDate());
+        }
+
+        // 鎸夌叅绉嶅悕绉版煡璇�
+        if (StringUtils.isNotBlank(pendingInventoryDto.getCoal())) {
+            LambdaQueryWrapper<CoalInfo> coalQueryWrapper = new LambdaQueryWrapper<>();
+            coalQueryWrapper.like(CoalInfo::getCoal, pendingInventoryDto.getCoal());
+            List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalQueryWrapper);
+
+            if (!coalInfos.isEmpty()) {
+                List<Long> coalIds = coalInfos.stream()
+                        .map(CoalInfo::getId)
+                        .collect(Collectors.toList());
+                queryWrapper.in(PendingInventory::getCoalId, coalIds);
+            } else {
+                // 濡傛灉娌℃湁鍖归厤鐨勭叅绉嶏紝鐩存帴杩斿洖绌虹粨鏋�
+                queryWrapper.eq(PendingInventory::getCoalId, -1L); // 浣跨敤涓嶅彲鑳藉瓨鍦ㄧ殑ID
             }
         }
 
@@ -286,4 +298,118 @@
         }
         return i;
     }
+
+    @Override
+    @Transactional
+    public boolean addPending(PendingInventoryDto pendingInventoryDto) {
+        try {
+            CoalPlan coalPlan = coalPlanMapper.selectOne(new LambdaQueryWrapper<CoalPlan>().eq(CoalPlan::getPlan, "閰嶇叅璁$畻鍣ㄦ柟妗�"));
+            if (coalPlan == null) {
+                return false;
+            }
+
+            // 娣诲姞鍒板緟鍏ュ簱
+            Long userId = SecurityUtils.getUserId();
+            PendingInventory pendingInventory = new PendingInventory();
+            pendingInventory.setUnit("t");
+            pendingInventory.setRegistrantId(userId);
+            pendingInventory.setRegistrationDate(LocalDate.now());
+
+            for (Map<String, Object> map : pendingInventoryDto.getFieldsResultList()) {
+                pendingInventory.setSupplierName("閰嶇叅璁$畻鍣ㄦ柟妗堝叆搴�");
+
+                // 澶勭悊鐓ょ偔ID
+                if (map.get("coalId") == null) {
+                    CoalInfo coalInfo = new CoalInfo();
+                    coalInfo.setCoal((String) map.get("createCoal"));
+                    coalInfo.setMaintainerId(userId);
+                    coalInfo.setMaintenanceDate(LocalDate.now());
+                    if (coalInfoMapper.insert(coalInfo) <= 0) {
+                        return false;
+                    }
+                    pendingInventory.setCoalId(coalInfo.getId());
+                } else {
+                    pendingInventory.setCoalId((Long) map.get("coalId"));
+                }
+
+                // 璁剧疆浠锋牸鍜屾暟閲�
+                BigDecimal cost = BigDecimal.valueOf((Double) map.get("cost"));
+                BigDecimal tonnage = BigDecimal.valueOf((Double) map.get("totalTonnage"));
+
+                pendingInventory.setPriceIncludingTax(cost);
+                pendingInventory.setInventoryQuantity(tonnage);
+                pendingInventory.setTotalPriceIncludingTax(cost.multiply(tonnage));
+
+                BigDecimal costExcludingTax = cost.divide(BigDecimal.valueOf(1.13), 2, RoundingMode.HALF_UP);
+                pendingInventory.setPriceExcludingTax(costExcludingTax);
+                pendingInventory.setTotalPriceExcludingTax(costExcludingTax.multiply(tonnage));
+                pendingInventory.setCoalPlanId(coalPlan.getId());
+
+                // 鐓よ川瀛楁鍊�
+                String coalFields = coalPlan.getCoalFields();
+                List<String> coalFieldList = Arrays.asList(coalFields.split(","));
+
+                for (String field : coalFieldList) {
+                    CoalField coalField = coalFieldMapper.selectOne(
+                            new LambdaQueryWrapper<CoalField>().eq(CoalField::getFields, field));
+                    if (coalField == null) {
+                        continue;
+                    }
+
+                    CoalValue coalValue = new CoalValue();
+                    coalValue.setPlanId(coalPlan.getId());
+                    coalValue.setFields(field);
+                    coalValue.setFieldName(coalField.getFieldName());
+                    coalValue.setType("1");
+
+                    switch (coalField.getFieldName()) {
+                        case "鍙戠儹閲�":
+                            coalValue.setCoalValue((String) map.get("cv"));
+                            break;
+                        case "纭垎":
+                            coalValue.setCoalValue((String) map.get("sulfur"));
+                            break;
+                        case "鐏板垎":
+                            coalValue.setCoalValue((String) map.get("ash"));
+                            break;
+                        case "姘村垎":
+                            coalValue.setCoalValue((String) map.get("moisture"));
+                            break;
+                    }
+
+                    // 淇濆瓨鐓よ川鍊�
+                    if (coalValueMapper.insert(coalValue) <= 0) {
+                        return false;
+                    }
+                }
+            }
+
+            // 鎻掑叆寰呭叆搴撹褰�
+            if (pendingInventoryMapper.insert(pendingInventory) <= 0) {
+                return false;
+            }
+
+            // 鏇存柊姝e紡搴�
+            for (Map<String, Object> coalResult : pendingInventoryDto.getCoalResultList()) {
+                Long officialId = (Long) coalResult.get("officialId");
+                BigDecimal quantity = (BigDecimal) coalResult.get("quantity");
+                OfficialInventory officialInventory = officialInventoryMapper.selectById(officialId);
+
+                if (officialInventory == null || officialInventory.getInventoryQuantity().compareTo(quantity) < 0) {
+                    throw new BaseException("搴撳瓨鏁伴噺涓嶈冻锛屾坊鍔犺嚦寰呭叆搴撳け璐�");
+                }
+
+                officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(quantity));
+                if (officialInventoryMapper.updateById(officialInventory) <= 0) {
+                    return false;
+                }
+            }
+
+            return true;
+        } catch (Exception e) {
+            // 璁板綍鏃ュ織
+            log.error("娣诲姞寰呭叆搴撳け璐�", e);
+            throw new BaseException("娣诲姞寰呭叆搴撳け璐�: " + e.getMessage());
+        }
+    }
 }

--
Gitblit v1.9.3