| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.mapper.SupplierManageMapper; |
| | | import com.ruoyi.basic.pojo.SupplierManage; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.inventory.dto.StockinDto; |
| | | import com.ruoyi.inventory.excel.StockInExcelDto; |
| | |
| | | import com.ruoyi.inventory.pojo.StockManagement; |
| | | import com.ruoyi.inventory.service.StockInService; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerDto; |
| | | import com.ruoyi.purchase.mapper.ProductRecordMapper; |
| | | import com.ruoyi.purchase.pojo.ProductRecord; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | private StockInMapper stockInMapper; |
| | | @Autowired |
| | | private StockManagementMapper stockManagementMapper; |
| | | @Autowired |
| | | private ProductRecordMapper productRecordMapper; |
| | | @Autowired |
| | | private SupplierManageMapper supplierManageMapper; |
| | | |
| | | |
| | | // 新增方法 |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void saveStockin(StockIn stockIn) { |
| | | stockInMapper.insert(stockIn); |
| | | StockManagement stockManagement = new StockManagement(); |
| | | // 进行判断是否存在相同的产品id和供应商id |
| | | LambdaQueryWrapper<StockManagement> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(StockManagement::getProductId, stockIn.getProductId()); |
| | | queryWrapper.eq(StockManagement::getSupplierId, stockIn.getSupplierId()); |
| | | StockManagement stockManagement1 = stockManagementMapper.selectOne(queryWrapper); |
| | | if (stockManagement1!= null) { |
| | | stockManagement1.setStockQuantity(stockManagement1.getStockQuantity() + stockIn.getInboundQuantity()); |
| | | stockManagement1.setInboundTime(stockIn.getInboundTime()); |
| | | stockManagement1.setBoundTime(new Date()); |
| | | stockManagement1.setInboundPerson(stockIn.getInboundPerson()); |
| | | System.out.println(stockManagement1+"22"); |
| | | stockManagementMapper.updateById(stockManagement1); |
| | | //需要先判断产品记录表中是否有该产品 |
| | | ProductRecord productRecord = productRecordMapper.selectById(stockIn.getProductRecordid()); |
| | | if (productRecord == null) { |
| | | throw new RuntimeException("产品记录表中没有该产品"); |
| | | } |
| | | else { |
| | | stockManagement.setProductId(stockIn.getProductId()); |
| | | stockManagement.setStockQuantity(stockIn.getInboundQuantity()); |
| | | stockManagement.setBoundTime(stockIn.getInboundTime()); |
| | | stockManagement.setStockQuantity(stockIn.getInboundQuantity()); |
| | | stockManagement.setInboundPerson(stockIn.getInboundPerson()); |
| | | // 需要先判断供应商表中是否有该供应商 |
| | | SupplierManage supplierManage = supplierManageMapper.selectById(stockIn.getSupplierId()); |
| | | if (supplierManage == null) { |
| | | throw new RuntimeException("供应商表中没有该供应商"); |
| | | } |
| | | // 需要先判断库存管理表中是否有该产品 |
| | | LambdaQueryWrapper<StockManagement> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(StockManagement::getProductRecordid, stockIn.getProductRecordid()); |
| | | queryWrapper.eq(StockManagement::getSupplierId, stockIn.getSupplierId()); |
| | | queryWrapper.eq(StockManagement::getUserId, stockIn.getUserId()); |
| | | StockManagement stockManagement = stockManagementMapper.selectOne(queryWrapper); |
| | | if (stockManagement == null) { |
| | | // 如果没有该产品,需要先新增库存管理表 |
| | | stockManagement = new StockManagement(); |
| | | stockManagement.setProductRecordid(stockIn.getProductRecordid()); |
| | | stockManagement.setSupplierId(stockIn.getSupplierId()); |
| | | stockManagement.setTenantId(stockIn.getTenantId()); |
| | | stockManagement.setStockQuantity(stockIn.getInboundQuantity()); |
| | | stockManagement.setInboundTime(stockIn.getInboundTime()); |
| | | stockManagement.setBoundTime(new Date()); |
| | | stockManagement.setUserId(stockIn.getUserId()); |
| | | stockManagementMapper.insert(stockManagement); |
| | | } |
| | | // 如果有该产品,需要先更新库存管理表 |
| | | stockManagement.setStockQuantity(stockManagement.getStockQuantity() + stockIn.getInboundQuantity()); |
| | | stockManagement.setInboundTime(stockIn.getInboundTime()); |
| | | stockManagement.setBoundTime(new Date()); |
| | | stockManagement.setUserId(stockIn.getUserId()); |
| | | stockManagementMapper.updateById(stockManagement); |
| | | // 需要先新增库存入库表 |
| | | stockInMapper.insert(stockIn); |
| | | } |
| | | |
| | | @Override//根据id获取库存入库信息 |
| | |
| | | StockIn stockIn = stockInMapper.selectById(id); |
| | | return stockIn; |
| | | } |
| | | |
| | | @Override//更新库存入库信息 |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int updateStockIn(StockIn stockIn) { |
| | | StockIn stockIn1 = stockInMapper.selectById(stockIn.getId()); |
| | | // 进行判断是否存在相同的产品id和供应商id |
| | | LambdaQueryWrapper<StockManagement> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(StockManagement::getProductId, stockIn.getProductId()); |
| | | queryWrapper.eq(StockManagement::getSupplierId, stockIn.getSupplierId()); |
| | | StockManagement stockManagement1 = stockManagementMapper.selectOne(queryWrapper); |
| | | if (stockManagement1!= null) { |
| | | stockManagement1.setStockQuantity(stockManagement1.getStockQuantity() - stockIn1.getInboundQuantity() + stockIn.getInboundQuantity()); |
| | | stockManagement1.setInboundTime(stockIn.getInboundTime()); |
| | | stockManagement1.setBoundTime(new Date()); |
| | | stockManagement1.setInboundPerson(stockIn.getInboundPerson()); |
| | | System.out.println(stockManagement1 + "22"); |
| | | stockManagementMapper.updateById(stockManagement1); |
| | | } else { |
| | | StockManagement stockManagement = new StockManagement(); |
| | | stockManagement.setProductId(stockIn.getProductId()); |
| | | stockManagement.setStockQuantity(stockIn.getInboundQuantity()); |
| | | stockManagement.setBoundTime(stockIn.getInboundTime()); |
| | | stockManagement.setStockQuantity(stockIn.getInboundQuantity()); |
| | | stockManagement.setInboundPerson(stockIn.getInboundPerson()); |
| | | stockManagement.setSupplierId(stockIn.getSupplierId()); |
| | | stockManagement.setTenantId(stockIn.getTenantId()); |
| | | stockManagement.setBoundTime(new Date()); |
| | | System.out.println(stockManagement + "33"); |
| | | stockManagementMapper.insert(stockManagement); |
| | | public boolean updateStockIn(StockIn stockIn) { |
| | | // 1. 参数校验 |
| | | if (stockIn == null || stockIn.getId() == null) { |
| | | throw new IllegalArgumentException("入库记录ID不能为空"); |
| | | } |
| | | return stockInMapper.updateById(stockIn); |
| | | |
| | | // 2. 获取原始记录 |
| | | StockIn original = stockInMapper.selectById(stockIn.getId()); |
| | | if (original == null) { |
| | | throw new RuntimeException("要修改的入库记录不存在,ID: " + stockIn.getId()); |
| | | } |
| | | |
| | | // 3. 检查关联数据 |
| | | // 3.1 检查产品 |
| | | if (stockIn.getProductRecordid() != null && |
| | | productRecordMapper.selectById(stockIn.getProductRecordid()) == null) { |
| | | throw new RuntimeException("产品记录不存在,ID: " + stockIn.getProductRecordid()); |
| | | } |
| | | |
| | | // 3.2 检查供应商 |
| | | if (stockIn.getSupplierId() != null && |
| | | supplierManageMapper.selectById(stockIn.getSupplierId()) == null) { |
| | | throw new RuntimeException("供应商不存在,ID: " + stockIn.getSupplierId()); |
| | | } |
| | | |
| | | // 4. 确定最终使用的各ID值 |
| | | Integer finalProductId = stockIn.getProductRecordid() != null ? |
| | | stockIn.getProductRecordid() : original.getProductRecordid(); |
| | | Integer finalSupplierId = stockIn.getSupplierId() != null ? |
| | | stockIn.getSupplierId() : original.getSupplierId(); |
| | | Long finalUserId = stockIn.getUserId() != null ? |
| | | stockIn.getUserId() : original.getUserId(); |
| | | |
| | | // 5. 处理库存记录(确保存在对应记录) |
| | | LambdaQueryWrapper<StockManagement> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(StockManagement::getProductRecordid, finalProductId) |
| | | .eq(StockManagement::getSupplierId, finalSupplierId) |
| | | .eq(StockManagement::getUserId, finalUserId); |
| | | |
| | | StockManagement stock = stockManagementMapper.selectOne(wrapper); |
| | | |
| | | // 计算数量变化(如果有修改) |
| | | Integer quantityChange = stockIn.getInboundQuantity() != null ? |
| | | stockIn.getInboundQuantity() - original.getInboundQuantity() : 0; |
| | | |
| | | if (stock == null) { |
| | | // 5.1 不存在则创建新记录 |
| | | stock = new StockManagement(); |
| | | stock.setProductRecordid(finalProductId); |
| | | stock.setSupplierId(finalSupplierId); |
| | | stock.setUserId(finalUserId); |
| | | // 初始库存 = 原始库存 + 变化量(考虑修改后的值) |
| | | stock.setStockQuantity(original.getInboundQuantity() + quantityChange); |
| | | stockManagementMapper.insert(stock); |
| | | } else if (quantityChange != 0) { |
| | | // 5.2 存在且数量有变化则更新 |
| | | stock.setStockQuantity(stock.getStockQuantity() + quantityChange); |
| | | stockManagementMapper.updateById(stock); |
| | | } |
| | | |
| | | // 6. 更新入库记录 |
| | | int updateResult = stockInMapper.updateById(stockIn); |
| | | if (updateResult <= 0) { |
| | | throw new RuntimeException("入库记录更新失败"); |
| | | } |
| | | |
| | | return updateResult > 0; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int delStockin(List<Integer> ids) { |
| | | LambdaQueryWrapper<StockIn> delWrapper = new LambdaQueryWrapper<>(); |