From d8c86797adaef6c416efae16586f350ae77bde4b Mon Sep 17 00:00:00 2001
From: 吴羡 <16976746+Xian-TT3@user.noreply.gitee.com>
Date: 星期六, 12 九月 2026 12:43:08 +0800
Subject: [PATCH] feat: 库存入库/出库字段全链路、采购台账申请提交时间、储罐租客信息、油库储罐模块

---
 src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java |  267 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 238 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
index 4d525c3..0f70cfe 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -4,12 +4,22 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.alibaba.fastjson2.JSON;
+import com.ruoyi.basic.dto.StorageBlobVO;
+import com.ruoyi.basic.mapper.CustomerMapper;
+import com.ruoyi.basic.mapper.OilDepotMapper;
+import com.ruoyi.basic.mapper.ProductModelMapper;
+import com.ruoyi.basic.mapper.TankInfoMapper;
+import com.ruoyi.basic.pojo.Customer;
+import com.ruoyi.basic.pojo.OilDepot;
+import com.ruoyi.basic.pojo.ProductModel;
+import com.ruoyi.basic.pojo.TankInfo;
+import com.ruoyi.basic.utils.FileUtil;
 import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
-import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.ServiceException;
-import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.web.domain.R;
@@ -21,18 +31,21 @@
 import com.ruoyi.stock.dto.StockUninventoryDto;
 import com.ruoyi.stock.execl.StockInventoryExportData;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
 import com.ruoyi.stock.pojo.StockInventory;
 import com.ruoyi.stock.service.StockInRecordService;
 import com.ruoyi.stock.service.StockInventoryService;
 import com.ruoyi.stock.service.StockOutRecordService;
 import com.ruoyi.stock.service.StockUninventoryService;
-import lombok.AllArgsConstructor;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
-import jakarta.servlet.http.HttpServletResponse;
 import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -47,14 +60,20 @@
  * @since 2026-01-21 04:16:36
  */
 @Service
-@AllArgsConstructor
+@RequiredArgsConstructor
 public class StockInventoryServiceImpl extends ServiceImpl<StockInventoryMapper, StockInventory> implements StockInventoryService {
 
-    private  StockInventoryMapper stockInventoryMapper;
-    private StockInRecordService stockInRecordService;
-    private StockOutRecordService stockOutRecordService;
-    private StockUninventoryService stockUninventoryService;
-    private SalesLedgerProductMapper salesLedgerProductMapper;
+    private final StockInventoryMapper stockInventoryMapper;
+    private final StockInRecordService stockInRecordService;
+    private final StockOutRecordService stockOutRecordService;
+    private final StockUninventoryService stockUninventoryService;
+    private final SalesLedgerProductMapper salesLedgerProductMapper;
+    private final ProductModelMapper productModelMapper;
+    private final OilDepotMapper oilDepotMapper;
+    private final TankInfoMapper tankInfoMapper;
+    private final CustomerMapper customerMapper;
+    private final FileUtil fileUtil;
+
     @Override
     public IPage<StockInventoryDto> pagestockInventory(Page page, StockInventoryDto stockInventoryDto) {
         return stockInventoryMapper.pagestockInventory(page, stockInventoryDto);
@@ -69,14 +88,15 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean addstockInventory(StockInventoryDto stockInventoryDto) {
+        String batchNo = StringUtils.trim(stockInventoryDto.getBatchNo());
+        if (StringUtils.isEmpty(batchNo)) {
+            batchNo = generateAutoBatchNo(stockInventoryDto.getProductModelId());
+        }
+        stockInventoryDto.setBatchNo(batchNo);
+
         LambdaQueryWrapper<StockInventory> eq = new QueryWrapper<StockInventory>().lambda()
                 .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId());
-        if (StringUtils.isEmpty(stockInventoryDto.getBatchNo())) {
-            eq.isNull(StockInventory::getBatchNo);
-            stockInventoryDto.setBatchNo(null);
-        } else {
-            eq.eq(StockInventory::getBatchNo, stockInventoryDto.getBatchNo());
-        }
+        eq.eq(StockInventory::getBatchNo, stockInventoryDto.getBatchNo());
         //鏂板鍏ュ簱璁板綍鍐嶆坊鍔犲簱瀛�
         StockInRecordDto stockInRecordDto = new StockInRecordDto();
         stockInRecordDto.setRecordId(stockInventoryDto.getRecordId());
@@ -98,9 +118,13 @@
             newStockInventory.setBatchNo(stockInventoryDto.getBatchNo());
             newStockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
             newStockInventory.setWarnNum(stockInventoryDto.getWarnNum());
-            stockInventoryMapper.insert(newStockInventory);
-        }else {
-             stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
+            try {
+                stockInventoryMapper.insert(newStockInventory);
+            } catch (org.springframework.dao.DuplicateKeyException e) {
+                stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
+            }
+        } else {
+            stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
         }
         return true;
     }
@@ -110,7 +134,7 @@
     @Transactional(rollbackFor = Exception.class)
     public Boolean subtractStockInventory(StockInventoryDto stockInventoryDto) {
         LambdaQueryWrapper<StockInventory> eq = new QueryWrapper<StockInventory>().lambda()
-            .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId());
+                .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId());
         if (StringUtils.isEmpty(stockInventoryDto.getBatchNo())) {
             eq.isNull(StockInventory::getBatchNo);
             stockInventoryDto.setBatchNo(null);
@@ -147,16 +171,147 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean addStockInRecordOnly(StockInventoryDto stockInventoryDto) {
+        String batchNo = StringUtils.trim(stockInventoryDto.getBatchNo());
+        if (StringUtils.isEmpty(batchNo)) {
+            batchNo = generateAutoBatchNo(stockInventoryDto.getProductModelId());
+        }
+        stockInventoryDto.setBatchNo(batchNo);
+
         StockInRecordDto stockInRecordDto = new StockInRecordDto();
         stockInRecordDto.setRecordId(stockInventoryDto.getRecordId());
         stockInRecordDto.setRecordType(stockInventoryDto.getRecordType());
         stockInRecordDto.setStockInNum(stockInventoryDto.getQualitity());
-        stockInRecordDto.setBatchNo(stockInventoryDto.getBatchNo());
+        stockInRecordDto.setBatchNo(batchNo);
         stockInRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
         stockInRecordDto.setType("0");
         stockInRecordDto.setRemark(stockInventoryDto.getRemark());
+        stockInRecordDto.setWarnNum(stockInventoryDto.getWarnNum());
+        stockInRecordDto.setOilDepotId(stockInventoryDto.getOilDepotId());
+        stockInRecordDto.setOilDepotName(stockInventoryDto.getOilDepotName());
+        stockInRecordDto.setInboundCategory(stockInventoryDto.getInboundCategory());
+        if (stockInventoryDto.getOilDepotId() != null && StringUtils.isEmpty(stockInRecordDto.getOilDepotName())) {
+            OilDepot oilDepot = oilDepotMapper.selectById(stockInventoryDto.getOilDepotId());
+            if (oilDepot != null) {
+                stockInRecordDto.setOilDepotName(oilDepot.getDepotName());
+            }
+        }
+        stockInRecordDto.setTankId(stockInventoryDto.getTankId());
+        stockInRecordDto.setTankNo(stockInventoryDto.getTankNo());
+        if (stockInventoryDto.getTankId() != null && StringUtils.isEmpty(stockInRecordDto.getTankNo())) {
+            TankInfo tankInfo = tankInfoMapper.selectById(stockInventoryDto.getTankId());
+            if (tankInfo != null) {
+                stockInRecordDto.setTankNo(tankInfo.getTankNo());
+            }
+        }
+        stockInRecordDto.setInboundTime(stockInventoryDto.getInboundTime());
+        stockInRecordDto.setOilProduct(stockInventoryDto.getOilProduct());
+        stockInRecordDto.setOriginalQuantity(stockInventoryDto.getOriginalQuantity());
+        stockInRecordDto.setCustomerId(stockInventoryDto.getCustomerId());
+        stockInRecordDto.setCustomerName(stockInventoryDto.getCustomerName());
+        stockInRecordDto.setContactPerson(stockInventoryDto.getContactPerson());
+        stockInRecordDto.setContactPhone(stockInventoryDto.getContactPhone());
+        stockInRecordDto.setDriverName(stockInventoryDto.getDriverName());
+        stockInRecordDto.setDriverPhone(stockInventoryDto.getDriverPhone());
+        stockInRecordDto.setEscortName(stockInventoryDto.getEscortName());
+        stockInRecordDto.setEscortPhone(stockInventoryDto.getEscortPhone());
+        stockInRecordDto.setTruckPlateNo(stockInventoryDto.getTruckPlateNo());
+        stockInRecordDto.setTrailerPlateNo(stockInventoryDto.getTrailerPlateNo());
+        stockInRecordDto.setTankInspectionStatus(stockInventoryDto.getTankInspectionStatus());
+        stockInRecordDto.setUnloadingCranePosition(stockInventoryDto.getUnloadingCranePosition());
+        stockInRecordDto.setDensity(stockInventoryDto.getDensity());
+        stockInRecordDto.setUnloadPhotos(stockInventoryDto.getUnloadPhotos() == null
+                ? null : JSON.toJSONString(stockInventoryDto.getUnloadPhotos()));
+        stockInRecordDto.setGrossWeight(stockInventoryDto.getGrossWeight());
+        stockInRecordDto.setTareWeight(stockInventoryDto.getTareWeight());
+        stockInRecordDto.setNetWeight(stockInventoryDto.getNetWeight());
+        stockInRecordDto.setLossQuantity(stockInventoryDto.getLossQuantity());
+        stockInRecordDto.setActualVolume(stockInventoryDto.getActualVolume());
+        stockInRecordDto.setPoundPhotos(stockInventoryDto.getPoundPhotos() == null
+                ? null : JSON.toJSONString(stockInventoryDto.getPoundPhotos()));
+        if (stockInventoryDto.getCustomerId() != null && StringUtils.isEmpty(stockInRecordDto.getCustomerName())) {
+            Customer customer = customerMapper.selectById(stockInventoryDto.getCustomerId());
+            if (customer != null) {
+                stockInRecordDto.setCustomerName(customer.getCustomerName());
+            }
+        }
         stockInRecordService.add(stockInRecordDto);
         return true;
+    }
+
+    //瑙勫垯鐢熸垚锛�20260424-浜у搧缂栧彿-001
+    private String generateAutoBatchNo(Long productModelId) {
+        if (productModelId == null) {
+            throw new ServiceException("浜у搧瑙勬牸ID涓嶈兘涓虹┖");
+        }
+        ProductModel productModel = productModelMapper.selectById(productModelId);
+        if (productModel == null) {
+            throw new ServiceException("浜у搧瑙勬牸涓嶅瓨鍦紝ID=" + productModelId);
+        }
+        String productCode = StringUtils.trim(productModel.getProductCode());
+        if (StringUtils.isEmpty(productCode)) {
+            throw new ServiceException("浜у搧瑙勬牸鏈淮鎶や骇鍝佺紪鐮侊紝ID=" + productModelId);
+        }
+
+        String dateText = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
+        String prefix = dateText + "-" + productCode + "-";
+        int maxSequence = resolveMaxSequence(prefix);
+        int sequence = maxSequence + 1;
+        while (sequence < 1_000_000) {
+            String batchNo = prefix + String.format("%03d", sequence);
+            if (!isBatchNoExists(batchNo)) {
+                return batchNo;
+            }
+            sequence++;
+        }
+        throw new ServiceException("鎵瑰彿搴忓彿瓒呭嚭鑼冨洿锛岃妫�鏌ユ壒鍙锋暟鎹�");
+    }
+
+    private int resolveMaxSequence(String prefix) {
+        int maxSequence = 0;
+        List<StockInventory> stockInventoryList = stockInventoryMapper.selectList(
+                Wrappers.<StockInventory>lambdaQuery()
+                        .select(StockInventory::getBatchNo)
+                        .likeRight(StockInventory::getBatchNo, prefix));
+        for (StockInventory stockInventory : stockInventoryList) {
+            maxSequence = Math.max(maxSequence, parseSequence(stockInventory.getBatchNo(), prefix));
+        }
+
+        List<StockInRecord> stockInRecordList = stockInRecordService.list(
+                Wrappers.<StockInRecord>lambdaQuery()
+                        .select(StockInRecord::getBatchNo)
+                        .likeRight(StockInRecord::getBatchNo, prefix));
+        for (StockInRecord stockInRecord : stockInRecordList) {
+            maxSequence = Math.max(maxSequence, parseSequence(stockInRecord.getBatchNo(), prefix));
+        }
+        return maxSequence;
+    }
+
+    private int parseSequence(String batchNo, String prefix) {
+        if (StringUtils.isEmpty(batchNo) || StringUtils.isEmpty(prefix) || !batchNo.startsWith(prefix)) {
+            return 0;
+        }
+        String sequenceText = batchNo.substring(prefix.length());
+        if (StringUtils.isEmpty(sequenceText) || !sequenceText.matches("\\d+")) {
+            return 0;
+        }
+        try {
+            return Integer.parseInt(sequenceText);
+        } catch (NumberFormatException ignored) {
+            return 0;
+        }
+    }
+
+    private boolean isBatchNoExists(String batchNo) {
+        if (StringUtils.isEmpty(batchNo)) {
+            return false;
+        }
+        Long inventoryCount = stockInventoryMapper.selectCount(
+                Wrappers.<StockInventory>lambdaQuery().eq(StockInventory::getBatchNo, batchNo));
+        if (inventoryCount != null && inventoryCount > 0) {
+            return true;
+        }
+        return stockInRecordService.count(
+                Wrappers.<StockInRecord>lambdaQuery().eq(StockInRecord::getBatchNo, batchNo)) > 0;
     }
 
     @Override
@@ -248,7 +403,7 @@
                         }
 
                         stockInventoryDto.setProductModelId(matchedProduct.getProductModelId());
-                        this.addstockInventory(stockInventoryDto);
+                        this.addStockInRecordOnly(stockInventoryDto);
                         successCount++;
                     }
 
@@ -256,7 +411,7 @@
                     if (dto.getUnQualifiedQuantity() != null && dto.getUnQualifiedQuantity().compareTo(BigDecimal.ZERO) > 0) {
                         StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
                         stockUninventoryDto.setRecordId(0L);
-                        stockUninventoryDto.setRecordType(StockInUnQualifiedRecordTypeEnum.CUSTOMIZATION_UNSTOCK_IN.getCode());
+                        stockUninventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_UNSTOCK_IN.getCode());
                         stockUninventoryDto.setQualitity(dto.getUnQualifiedQuantity());
                         stockUninventoryDto.setRemark(dto.getRemark());
 
@@ -304,28 +459,28 @@
 
         List<StockInventoryExportData> list = stockInventoryMapper.listStockInventoryExportData(stockInventoryDto);
         ExcelUtil<StockInventoryExportData> util = new ExcelUtil<>(StockInventoryExportData.class);
-        util.exportExcel(response,list, "搴撳瓨淇℃伅");
+        util.exportExcel(response, list, "搴撳瓨淇℃伅");
     }
 
     @Override
     public IPage<StockInRecordDto> stockInventoryPage(StockInventoryDto stockInventoryDto, Page page) {
-        return stockInventoryMapper.stockInventoryPage(stockInventoryDto,page);
+        return stockInventoryMapper.stockInventoryPage(stockInventoryDto, page);
     }
 
     @Override
     public IPage<StockInventoryDto> stockInAndOutRecord(StockInventoryDto stockInventoryDto, Page page) {
-        return stockInventoryMapper.stockInAndOutRecord(stockInventoryDto,page);
+        return stockInventoryMapper.stockInAndOutRecord(stockInventoryDto, page);
     }
 
     @Override
     public Boolean frozenStock(StockInventoryDto stockInventoryDto) {
         StockInventory stockInventory = stockInventoryMapper.selectById(stockInventoryDto.getId());
-        if (stockInventory.getQualitity().compareTo(stockInventoryDto.getLockedQuantity())<0) {
+        if (stockInventory.getQualitity().compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
             throw new RuntimeException("鍐荤粨鏁伴噺涓嶈兘瓒呰繃搴撳瓨鏁伴噺");
         }
         if (ObjectUtils.isEmpty(stockInventory.getLockedQuantity())) {
             stockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
-        }else {
+        } else {
             stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().add(stockInventoryDto.getLockedQuantity()));
         }
         return this.updateById(stockInventory);
@@ -334,10 +489,64 @@
     @Override
     public Boolean thawStock(StockInventoryDto stockInventoryDto) {
         StockInventory stockInventory = stockInventoryMapper.selectById(stockInventoryDto.getId());
-        if (stockInventory.getLockedQuantity().compareTo(stockInventoryDto.getLockedQuantity())<0) {
+        if (stockInventory.getLockedQuantity().compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
             throw new RuntimeException("瑙e喕鏁伴噺涓嶈兘瓒呰繃鍐荤粨鏁伴噺");
         }
         stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
         return this.updateById(stockInventory);
     }
+
+    @Override
+    public List<StockInventory> getByModelId(Long modelId) {
+        return stockInventoryMapper.getByModelId(modelId);
+    }
+
+    @Override
+    public IPage<StockInventoryDto> getBatchNoQty(Page page, StockInventoryDto stockInventoryDto) {
+        IPage<StockInventoryDto> result = stockInventoryMapper.getBatchNoQty(page, stockInventoryDto);
+        // 鐓х墖搴撻噷瀛樼殑鏄笂浼犳椂鐨勭鍚峌RL锛堥粯璁�2灏忔椂杩囨湡锛夛紝杩斿洖鍓嶉噸鏂扮鍙戯紝閬垮厤鍓嶇鍥剧墖鍔犺浇澶辫触
+        for (StockInventoryDto record : result.getRecords()) {
+            record.setUnloadPhotos(refreshPhotoUrls(record.getUnloadPhotos()));
+            record.setPoundPhotos(refreshPhotoUrls(record.getPoundPhotos()));
+            record.setLoadingPhotos(refreshPhotoUrls(record.getLoadingPhotos()));
+            record.setOutPoundPhotos(refreshPhotoUrls(record.getOutPoundPhotos()));
+        }
+        return result;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean addOilOutRecordOnly(StockOutRecordDto stockOutRecordDto) {
+        stockOutRecordDto.setStockOutNum(stockOutRecordDto.getOutTonnage());
+        stockOutRecordDto.setApprovalStatus(0);
+        return stockOutRecordService.add(stockOutRecordDto) > 0;
+    }
+
+    /**
+     * 閲嶆柊绛惧彂鐓х墖棰勮/涓嬭浇URL
+     */
+    private Object refreshPhotoUrls(Object photos) {
+        if (!(photos instanceof String)) {
+            return photos;
+        }
+        String json = (String) photos;
+        if (StringUtils.isEmpty(json)) {
+            return photos;
+        }
+        try {
+            List<StorageBlobVO> list = JSON.parseArray(json, StorageBlobVO.class);
+            if (list == null || list.isEmpty()) {
+                return photos;
+            }
+            for (StorageBlobVO vo : list) {
+                String previewUrl = fileUtil.buildSignedUrl(vo, "/preview/", null);
+                vo.setPreviewURL(previewUrl);
+                vo.setUrl(previewUrl);
+                vo.setDownloadURL(fileUtil.buildSignedUrl(vo, "/download/", null));
+            }
+            return JSON.toJSONString(list);
+        } catch (Exception e) {
+            return photos;
+        }
+    }
 }

--
Gitblit v1.9.3