package com.ruoyi.business.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.business.dto.OfficialInventoryDto;
|
import com.ruoyi.business.dto.PendingInventoryDto;
|
import com.ruoyi.business.entity.*;
|
import com.ruoyi.business.mapper.InventorySummaryMapper;
|
import com.ruoyi.business.mapper.OfficialInventoryMapper;
|
import com.ruoyi.business.mapper.PendingInventoryMapper;
|
import com.ruoyi.business.service.InputInventoryRecordService;
|
import com.ruoyi.business.service.InventorySummaryService;
|
import com.ruoyi.business.service.OutputInventoryRecordService;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.util.List;
|
|
import static com.ruoyi.business.constant.InventoryRecordConstant.OFFICIAL_INVENTORY;
|
import static com.ruoyi.business.constant.InventoryRecordConstant.PENDING_INVENTORY;
|
|
/**
|
* <p>
|
* 库存汇总表 服务实现类
|
* </p>
|
*
|
* @author chenhj
|
* @since 2025-06-14
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class InventorySummaryServiceImpl extends ServiceImpl<InventorySummaryMapper, InventorySummary> implements InventorySummaryService {
|
@Autowired
|
private InventorySummaryMapper inventorySummaryMapper;
|
@Autowired
|
private InputInventoryRecordService inputInventoryRecordService;
|
@Autowired
|
private OutputInventoryRecordService outputInventoryRecordService;
|
@Autowired
|
private PendingInventoryMapper pendingInventoryMapper;
|
@Autowired
|
private OfficialInventoryMapper officialInventoryMapper;
|
|
@Override
|
public int updateInventory(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto) {
|
if ((pendingInventoryDto != null && officialInventoryDto != null) || (pendingInventoryDto == null && officialInventoryDto == null)) {
|
throw new RuntimeException("库存记录异常");
|
}
|
|
String inventoryType = pendingInventoryDto != null ? PENDING_INVENTORY : OFFICIAL_INVENTORY;
|
Long inventoryId = pendingInventoryDto != null ? pendingInventoryDto.getId() : officialInventoryDto.getId();
|
Long inputEndRecordId = 0L;
|
Long outputEndRecordId = 0L;
|
|
InventorySummary inventorySummary = inventorySummaryMapper.selectOne(new LambdaQueryWrapper<InventorySummary>()
|
.eq(InventorySummary::getInventoryId, inventoryId)
|
.eq(InventorySummary::getInventoryType, inventoryType));
|
if (inventorySummary != null) {
|
inputEndRecordId = inventorySummary.getInputEndRecordId();
|
outputEndRecordId = inventorySummary.getOutputEndRecordId();
|
}
|
// 查询节点以后所有入库记录
|
List<InputInventoryRecord> inputInventoryRecords = inputInventoryRecordService.list(new LambdaQueryWrapper<InputInventoryRecord>()
|
.eq(InputInventoryRecord::getInventoryId, inventoryId)
|
.eq(InputInventoryRecord::getInventoryType, inventoryType)
|
.gt(InputInventoryRecord::getId, inputEndRecordId));
|
// 查询节点以后所有出库记录
|
List<OutputInventoryRecord> outputInventoryRecords = outputInventoryRecordService.list(new LambdaQueryWrapper<OutputInventoryRecord>()
|
.eq(OutputInventoryRecord::getInventoryId, inventoryId)
|
.eq(OutputInventoryRecord::getInventoryType, inventoryType)
|
.gt(OutputInventoryRecord::getId, outputEndRecordId));
|
|
// 如果入库数量大于出库数量,则库存数量为入库数量减去出库数量,反之则为0
|
BigDecimal quantity = BigDecimal.ZERO;
|
BigDecimal totalInputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal totalOutputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
|
if (totalInputQuantity.compareTo(totalOutputQuantity) > 0) {
|
quantity = totalInputQuantity.subtract(totalOutputQuantity);
|
}
|
if (inventoryType.equals(PENDING_INVENTORY)) {
|
// 变更待入库值
|
PendingInventory pendingInventory = pendingInventoryMapper.getPendingInventoryForUpdateById(pendingInventoryDto.getId());
|
if (pendingInventory == null) {
|
throw new RuntimeException("库存记录不存在");
|
}
|
pendingInventory.setInventoryQuantity(quantity);
|
return pendingInventoryMapper.updateById(pendingInventory);
|
} else {
|
OfficialInventory officialInventory = officialInventoryMapper.getOfficialInventoryForUpdateById(officialInventoryDto.getId());
|
if (officialInventory == null) {
|
throw new RuntimeException("库存记录不存在");
|
}
|
officialInventory.setInventoryQuantity(quantity);
|
return officialInventoryMapper.updateById(officialInventory);
|
}
|
}
|
}
|