src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -7,8 +7,17 @@
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.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
@@ -29,7 +38,7 @@
import com.ruoyi.stock.service.StockOutRecordService;
import com.ruoyi.stock.service.StockUninventoryService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@@ -51,15 +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 ProductModelMapper productModelMapper;
    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);
@@ -104,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;
    }
@@ -116,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);
@@ -167,6 +185,55 @@
        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;
    }
@@ -336,7 +403,7 @@
                        }
                        stockInventoryDto.setProductModelId(matchedProduct.getProductModelId());
                        this.addstockInventory(stockInventoryDto);
                        this.addStockInRecordOnly(stockInventoryDto);
                        successCount++;
                    }
@@ -392,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);
@@ -422,7 +489,7 @@
    @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("解冻数量不能超过冻结数量");
        }
        stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
@@ -433,4 +500,53 @@
    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);
        // 照片库里存的是上传时的签名URL(默认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;
        }
    }
}