| | |
| | | import com.ruoyi.stock.service.StockInRecordService; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AllArgsConstructor; |
| | | import org.jspecify.annotations.NonNull; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class StockInRecordServiceImpl extends ServiceImpl<StockInRecordMapper, StockInRecord> implements StockInRecordService { |
| | | |
| | | private static final String UNQUALIFIED_TYPE = "unqualified"; |
| | | private static final String WASTE_TYPE = "waste"; |
| | | |
| | | private StockInRecordMapper stockInRecordMapper; |
| | | private StockInventoryMapper stockInventoryMapper; |
| | |
| | | stockInventoryMapper.updateSubtractStockInventory(stockInRecordDto); |
| | | } |
| | | }else if (stockInRecord.getType().equals("1")) { |
| | | String uninventoryType = resolveUninventoryTypeByInRecordType(stockInRecord.getRecordType()); |
| | | LambdaQueryWrapper<StockUninventory> eq = new LambdaQueryWrapper<StockUninventory>() |
| | | .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()); |
| | | .eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()) |
| | | .eq(StockUninventory::getType, uninventoryType); |
| | | if (StringUtils.isEmpty(stockInRecord.getBatchNo())) { |
| | | eq.isNull(StockUninventory::getBatchNo); |
| | | } else { |
| | |
| | | stockUninventoryDto.setProductModelId(stockUninventory.getProductModelId()); |
| | | stockUninventoryDto.setBatchNo(stockUninventory.getBatchNo()); |
| | | stockUninventoryDto.setQualitity(stockInRecord.getStockInNum()); |
| | | stockUninventoryDto.setType(uninventoryType); |
| | | stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto); |
| | | } |
| | | } |
| | |
| | | return stockInventoryMapper.selectOne(eq); |
| | | } |
| | | |
| | | private StockUninventory getStockUninventory(Long productModelId, String batchNo) { |
| | | private StockUninventory getStockUninventory(Long productModelId, String batchNo, String uninventoryType) { |
| | | LambdaQueryWrapper<StockUninventory> eq = new LambdaQueryWrapper<>(); |
| | | eq.eq(StockUninventory::getProductModelId, productModelId); |
| | | if (StringUtils.isNotEmpty(uninventoryType)) { |
| | | eq.eq(StockUninventory::getType, uninventoryType); |
| | | } |
| | | if (StringUtils.isEmpty(batchNo)) { |
| | | eq.isNull(StockUninventory::getBatchNo); |
| | | } else { |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int batchApprove(List<Long> ids, Integer approvalStatus) { |
| | | if (CollectionUtils.isEmpty(ids)) { |
| | | public int batchApprove(Integer approvalStatus, List<StockInRecordDto.StockInRecordApproveItemDto> items) { |
| | | if (CollectionUtils.isEmpty(items)) { |
| | | throw new BaseException("请选择至少一条数据"); |
| | | } |
| | | if (approvalStatus == null || (!ReviewStatusEnum.APPROVED.getCode().equals(approvalStatus) && !ReviewStatusEnum.REJECTED.getCode().equals(approvalStatus))) { |
| | | throw new BaseException("审批状态值无效"); |
| | | } |
| | | for (Long id : ids) { |
| | | for (StockInRecordDto.StockInRecordApproveItemDto item : items) { |
| | | Long id = item.getId(); |
| | | if (id == null) { |
| | | throw new BaseException("入库记录ID不能为空"); |
| | | } |
| | | StockInRecord stockInRecord = stockInRecordMapper.selectById(id); |
| | | if (stockInRecord == null) { |
| | | throw new BaseException("入库记录不存在,无法审批!!!"); |
| | |
| | | if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) { |
| | | throw new BaseException("只有待审批状态的记录才能审批,入库批次:" + stockInRecord.getInboundBatches()); |
| | | } |
| | | |
| | | // 获取审批时修改的入库数量,如果没有修改则使用原数量 |
| | | final BigDecimal finalStockInNum; |
| | | if (item.getStockInNum() != null && item.getStockInNum().compareTo(BigDecimal.ZERO) > 0) { |
| | | finalStockInNum = item.getStockInNum(); |
| | | adjustPurchaseInboundAuditFields(stockInRecord, finalStockInNum); |
| | | // 更新入库记录的入库数量 |
| | | stockInRecord.setStockInNum(finalStockInNum); |
| | | } else { |
| | | finalStockInNum = stockInRecord.getStockInNum(); |
| | | } |
| | | |
| | | stockInRecord.setApprovalStatus(approvalStatus); |
| | | stockInRecordMapper.updateById(stockInRecord); |
| | | |
| | |
| | | if ("0".equals(stockInRecord.getType())) { |
| | | // 合格入库 -> 先查库存,存在则更新,不存在则新增 |
| | | StockInventory stockInventory = getStockInventory(stockInRecord.getProductModelId(), stockInRecord.getBatchNo()); |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setProductModelId(stockInRecord.getProductModelId()); |
| | | stockInventoryDto.setBatchNo(stockInRecord.getBatchNo()); |
| | | stockInventoryDto.setQualitity(stockInRecord.getStockInNum()); |
| | | stockInventoryDto.setRemark(stockInRecord.getRemark()); |
| | | StockInventoryDto stockInventoryDto = getStockInventoryDto(stockInRecord); |
| | | stockInventoryDto.setQualitity(finalStockInNum); |
| | | if (stockInventory == null) { |
| | | stockInventoryMapper.insert(new StockInventory() {{ |
| | | setProductModelId(stockInRecord.getProductModelId()); |
| | | setQualitity(stockInRecord.getStockInNum()); |
| | | setQualitity(finalStockInNum); |
| | | setBatchNo(stockInRecord.getBatchNo()); |
| | | setRemark(stockInRecord.getRemark()); |
| | | setWarnNum(stockInRecord.getWarnNum()); |
| | | setManufacturerId(stockInRecord.getManufacturerId()); |
| | | setSource(stockInRecord.getSource()); |
| | | setVersion(1); |
| | | }}); |
| | | } else { |
| | |
| | | } |
| | | } else if ("1".equals(stockInRecord.getType())) { |
| | | // 不合格入库 -> 先查库存,存在则更新,不存在则新增 |
| | | StockUninventory stockUninventory = getStockUninventory(stockInRecord.getProductModelId(), stockInRecord.getBatchNo()); |
| | | String uninventoryType = resolveUninventoryTypeByInRecordType(stockInRecord.getRecordType()); |
| | | StockUninventory stockUninventory = getStockUninventory(stockInRecord.getProductModelId(), stockInRecord.getBatchNo(), uninventoryType); |
| | | StockUninventoryDto stockUninventoryDto = new StockUninventoryDto(); |
| | | stockUninventoryDto.setProductModelId(stockInRecord.getProductModelId()); |
| | | stockUninventoryDto.setBatchNo(stockInRecord.getBatchNo()); |
| | | stockUninventoryDto.setQualitity(stockInRecord.getStockInNum()); |
| | | stockUninventoryDto.setQualitity(finalStockInNum); |
| | | stockUninventoryDto.setRemark(stockInRecord.getRemark()); |
| | | stockUninventoryDto.setManufacturerId(stockInRecord.getManufacturerId()); |
| | | stockUninventoryDto.setSource(stockInRecord.getSource()); |
| | | stockUninventoryDto.setType(uninventoryType); |
| | | if (stockUninventory == null) { |
| | | stockUninventoryMapper.insert(new StockUninventory() {{ |
| | | setProductModelId(stockInRecord.getProductModelId()); |
| | | setQualitity(stockInRecord.getStockInNum()); |
| | | setQualitity(finalStockInNum); |
| | | setBatchNo(stockInRecord.getBatchNo()); |
| | | setType(uninventoryType); |
| | | setRemark(stockInRecord.getRemark()); |
| | | setManufacturerId(stockInRecord.getManufacturerId()); |
| | | setSource(stockInRecord.getSource()); |
| | | setVersion(1); |
| | | }}); |
| | | } else { |
| | |
| | | } |
| | | } |
| | | } |
| | | return ids.size(); |
| | | return items.size(); |
| | | } |
| | | |
| | | private String resolveUninventoryTypeByInRecordType(String recordType) { |
| | | if (StockInQualifiedRecordTypeEnum.PRODUCTION_SCRAP.getCode().equals(recordType)) { |
| | | return WASTE_TYPE; |
| | | } |
| | | return UNQUALIFIED_TYPE; |
| | | } |
| | | |
| | | private void adjustPurchaseInboundAuditFields(StockInRecord stockInRecord, BigDecimal finalStockInNum) { |
| | | if (stockInRecord == null || finalStockInNum == null) { |
| | | return; |
| | | } |
| | | if (!StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode().equals(stockInRecord.getRecordType())) { |
| | | return; |
| | | } |
| | | BigDecimal theoryStockInNum = stockInRecord.getTheoryStockInNum(); |
| | | if (theoryStockInNum == null || theoryStockInNum.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return; |
| | | } |
| | | if (finalStockInNum.compareTo(theoryStockInNum) > 0) { |
| | | throw new BaseException("采购入库审核时,实际入库数量不能大于理论入库数量"); |
| | | } |
| | | if (Boolean.TRUE.equals(stockInRecord.getIsContainsWater())) { |
| | | BigDecimal waterContent = theoryStockInNum.subtract(finalStockInNum) |
| | | .multiply(new BigDecimal("100")) |
| | | .divide(theoryStockInNum, 4, RoundingMode.HALF_UP); |
| | | stockInRecord.setWaterContent(waterContent); |
| | | } |
| | | } |
| | | |
| | | private static @NonNull StockInventoryDto getStockInventoryDto(StockInRecord stockInRecord) { |
| | | StockInventoryDto stockInventoryDto = new StockInventoryDto(); |
| | | stockInventoryDto.setProductModelId(stockInRecord.getProductModelId()); |
| | | stockInventoryDto.setBatchNo(stockInRecord.getBatchNo()); |
| | | stockInventoryDto.setQualitity(stockInRecord.getStockInNum()); |
| | | stockInventoryDto.setRemark(stockInRecord.getRemark()); |
| | | stockInventoryDto.setManufacturerId(stockInRecord.getManufacturerId()); |
| | | stockInventoryDto.setSource(stockInRecord.getSource()); |
| | | return stockInventoryDto; |
| | | } |
| | | |
| | | @Override |