gongchunyi
7 小时以前 02cf72a4443db4f1a2406c3919718c38aa6367db
src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
@@ -4,23 +4,24 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockOutRecordDto;
import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.execl.StockInventoryExportData;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.pojo.StockUninventory;
import com.ruoyi.stock.execl.StockUnInventoryExportData;
import com.ruoyi.stock.mapper.StockUninventoryMapper;
import com.ruoyi.stock.pojo.StockUninventory;
import com.ruoyi.stock.service.StockInRecordService;
import com.ruoyi.stock.service.StockOutRecordService;
import com.ruoyi.stock.service.StockUninventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
/**
@@ -54,14 +55,20 @@
        stockInRecordDto.setStockInNum(stockUninventoryDto.getQualitity());
        stockInRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
        stockInRecordDto.setType("1");
        stockInRecordDto.setSalesLedgerId(stockUninventoryDto.getSalesLedgerId());
        stockInRecordDto.setSalesLedgerProductId(stockUninventoryDto.getSalesLedgerProductId());
        stockInRecordService.add(stockInRecordDto);
        //再进行新增库存数量库存
        //先查询库存表中的产品是否存在,不存在新增,存在更新
        StockUninventory oldStockUnInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId()));
        List<StockUninventory> stockUninventories = stockUninventoryMapper.selectList(new QueryWrapper<StockUninventory>().lambda()
                .eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId())
                .orderByDesc(StockUninventory::getId));
        StockUninventory oldStockUnInventory = (stockUninventories == null || stockUninventories.isEmpty()) ? null : stockUninventories.get(0);
        if (ObjectUtils.isEmpty(oldStockUnInventory)) {
            StockUninventory newStockUnInventory = new StockUninventory();
            newStockUnInventory.setProductModelId(stockUninventoryDto.getProductModelId());
            newStockUnInventory.setQualitity(stockUninventoryDto.getQualitity());
            newStockUnInventory.setLockedQuantity(stockUninventoryDto.getLockedQuantity());
            newStockUnInventory.setVersion(1);
            newStockUnInventory.setRemark(stockUninventoryDto.getRemark());
            stockUninventoryMapper.insert(newStockUnInventory);
@@ -82,20 +89,64 @@
        stockOutRecordDto.setStockOutNum(stockUninventoryDto.getQualitity());
        stockOutRecordDto.setProductModelId(stockUninventoryDto.getProductModelId());
        stockOutRecordDto.setType("1");
        stockOutRecordDto.setSalesLedgerId(stockUninventoryDto.getSalesLedgerId());
        stockOutRecordDto.setSalesLedgerProductId(stockUninventoryDto.getSalesLedgerProductId());
        stockOutRecordService.add(stockOutRecordDto);
        StockUninventory oldStockInventory = stockUninventoryMapper.selectOne(new QueryWrapper<StockUninventory>().lambda().eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId()));
        List<StockUninventory> stockUninventories = stockUninventoryMapper.selectList(new QueryWrapper<StockUninventory>().lambda()
                .eq(StockUninventory::getProductModelId, stockUninventoryDto.getProductModelId())
                .orderByDesc(StockUninventory::getId));
        StockUninventory oldStockInventory = (stockUninventories == null || stockUninventories.isEmpty()) ? null : stockUninventories.get(0);
        if (ObjectUtils.isEmpty(oldStockInventory)) {
            throw new RuntimeException("产品库存不存在");
        }else {
            stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto);
            int affectRows = stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto);
            if (affectRows <= 0) {
                throw new RuntimeException("库存不足无法出库");
            }
        }
        return 1;
    }
    @Override
    public void exportStockUninventory(HttpServletResponse response, StockUninventoryDto stockUninventoryDto) {
        List<StockInventoryExportData> list = stockUninventoryMapper.listStockInventoryExportData(stockUninventoryDto);
        ExcelUtil<StockInventoryExportData> util = new ExcelUtil<>(StockInventoryExportData.class);
        List<StockUnInventoryExportData> list = stockUninventoryMapper.listStockInventoryExportData(stockUninventoryDto);
        ExcelUtil<StockUnInventoryExportData> util = new ExcelUtil<>(StockUnInventoryExportData.class);
        util.exportExcel(response,list, "不合格库存信息");
    }
    @Override
    public Boolean frozenStock(StockInventoryDto stockInventoryDto) {
        StockUninventory stockUninventory = stockUninventoryMapper.selectById(stockInventoryDto.getId());
        if (ObjectUtils.isEmpty(stockUninventory)) {
            throw new RuntimeException("库存记录不存在");
        }
        if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("冻结数量必须大于0");
        }
        BigDecimal totalQty = ObjectUtils.isEmpty(stockUninventory.getQualitity()) ? BigDecimal.ZERO : stockUninventory.getQualitity();
        BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockUninventory.getLockedQuantity()) ? BigDecimal.ZERO : stockUninventory.getLockedQuantity();
        BigDecimal availableQty = totalQty.subtract(currentLockedQty);
        if (stockInventoryDto.getLockedQuantity().compareTo(availableQty) > 0) {
            throw new RuntimeException("冻结数量不能超过可冻结库存数量");
        }
        stockUninventory.setLockedQuantity(currentLockedQty.add(stockInventoryDto.getLockedQuantity()));
        return this.updateById(stockUninventory);
    }
    @Override
    public Boolean thawStock(StockInventoryDto stockInventoryDto) {
        StockUninventory stockUninventory = stockUninventoryMapper.selectById(stockInventoryDto.getId());
        if (ObjectUtils.isEmpty(stockUninventory)) {
            throw new RuntimeException("库存记录不存在");
        }
        if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("解冻数量必须大于0");
        }
        BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockUninventory.getLockedQuantity()) ? BigDecimal.ZERO : stockUninventory.getLockedQuantity();
        if (currentLockedQty.compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
            throw new RuntimeException("解冻数量不能超过冻结数量");
        }
        stockUninventory.setLockedQuantity(currentLockedQty.subtract(stockInventoryDto.getLockedQuantity()));
        return this.updateById(stockUninventory);
    }
}