liding
2 天以前 2656ae9bce8544d81da66c07aaede5386d6fbebb
main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
@@ -9,7 +9,6 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.entity.CoalInfo;
import com.ruoyi.basic.entity.Customer;
import com.ruoyi.basic.entity.Supply;
import com.ruoyi.basic.mapper.CoalInfoMapper;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.business.dto.SalesRecordDto;
@@ -40,13 +39,9 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.*;
/**
 * <p>
@@ -153,23 +148,20 @@
        // 参数校验
        validateSalesRecordDto(salesRecordDto);
        // 更新正式库待补库数量
        // 获取煤种库存信息
        OfficialInventory officialInventory = officialInventoryMapper.selectById(salesRecordDto.getCoalId());
        if (officialInventory == null) {
            throw new BaseException("正式库煤种信息不存在");
        }
        if (salesRecordDto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0) {
            throw new BaseException("销售数量不能大于库存数量");
        }
        officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(salesRecordDto.getSaleQuantity()));
        if (salesRecordDto.isAdd()){
            officialInventory.setPendingReplenishment(salesRecordDto.getSaleQuantity());
        }
        officialInventoryMapper.updateById(officialInventory);
        // 处理销售数量变更逻辑
        SalesRecord existingRecord = salesRecordDto.getId() == null ? null : salesRecordMapper.selectById(salesRecordDto.getId());
        handleQuantityChanges(salesRecordDto, officialInventory, existingRecord);
        // 构建销售记录实体
        SalesRecord salesRecord = buildSalesRecord(salesRecordDto, officialInventory.getCoalId());
        // 设置销售记录中的库存数量
        salesRecord.setInventoryQuantity(officialInventory.getInventoryQuantity());
        // 处理新增/更新逻辑
        if (salesRecordDto.getId() == null) {
@@ -179,18 +171,52 @@
        }
    }
    private void validateSalesRecordDto(SalesRecordDto dto) {
        if (dto == null) {
            throw new BaseException("销售记录数据不能为空");
    private void handleQuantityChanges(SalesRecordDto dto, OfficialInventory officialInventory, SalesRecord existingRecord) {
        if (existingRecord == null) {
            // 新增记录
            if (dto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0) {
                throw new BaseException("销售数量不能大于库存数量");
            }
            // 更新库存数量
            officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(dto.getSaleQuantity()));
            // 设置待补库数量
            if (dto.isAdd()) {
                officialInventory.setPendingReplenishment(
                        officialInventory.getPendingReplenishment() == null ?
                                dto.getSaleQuantity() :
                                officialInventory.getPendingReplenishment().add(dto.getSaleQuantity())
                );
            }
        } else {
            // 更新记录
            // 比较销售数量是否有变化
            int quantityComparison = dto.getSaleQuantity().compareTo(existingRecord.getSaleQuantity());
            if (quantityComparison != 0) {
                // 计算数量差值
                BigDecimal quantityDiff = dto.getSaleQuantity().subtract(existingRecord.getSaleQuantity());
                // 检查新数量是否会导致库存不足
                if (quantityComparison > 0 && quantityDiff.compareTo(officialInventory.getInventoryQuantity()) > 0) {
                    throw new BaseException("销售数量增加后不能大于库存数量");
                }
                // 更新库存数量
                officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(quantityDiff));
                // 更新待补库数量(如果是需要补库的记录)
                if (dto.isAdd()) {
                    BigDecimal pendingDiff = officialInventory.getPendingReplenishment() == null ?
                            quantityDiff :
                            officialInventory.getPendingReplenishment().add(quantityDiff);
                    officialInventory.setPendingReplenishment(pendingDiff);
                }
            }
        }
        if (dto.getRegistrantId() == null) {
            throw new BaseException("登记人ID不能为空");
        }
        if (dto.getCustomerId() == null) {
            throw new BaseException("客户ID不能为空");
        }
        if (dto.getCoalId() == null) {
            throw new BaseException("请选择一条煤种信息");
        // 更新库存记录
        int updateResult = officialInventoryMapper.updateById(officialInventory);
        if (updateResult <= 0) {
            throw new BaseException("库存更新失败");
        }
    }
@@ -234,6 +260,21 @@
        return record;
    }
    private void validateSalesRecordDto(SalesRecordDto dto) {
        if (dto == null) {
            throw new BaseException("销售记录数据不能为空");
        }
        if (dto.getRegistrantId() == null) {
            throw new BaseException("登记人ID不能为空");
        }
        if (dto.getCustomerId() == null) {
            throw new BaseException("客户ID不能为空");
        }
        if (dto.getCoalId() == null) {
            throw new BaseException("请选择一条煤种信息");
        }
    }
    private int insertSalesRecord(SalesRecord record) {
        int result = salesRecordMapper.insert(record);
        if (result <= 0) {