package com.oying.modules.pc.product.service.impl;
|
|
import com.oying.modules.pc.product.domain.Product;
|
import com.oying.modules.pc.product.mapper.ProductStockMapper;
|
import com.oying.modules.pc.product.service.ProductInventoryService;
|
import com.oying.modules.pc.product.service.ProductService;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
@Service
|
@RequiredArgsConstructor
|
public class ProductInventoryServiceImpl implements ProductInventoryService {
|
|
private final ProductStockMapper productStockMapper;
|
private final ProductService productService;
|
|
@Override
|
public Product getProductById(Long productId) {
|
return productService.getProduct(productId);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Product setStockQuantity(Long productId, Integer quantity, Long version) {
|
Product existingProduct = this.getProductById(productId);
|
productStockMapper.updateStock(productId, quantity, existingProduct.getVersion());
|
return this.getProductById(productId);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Product increaseStock(Long productId, Integer amount) {
|
Product existingProduct = this.getProductById(productId);
|
productStockMapper.increaseStock(productId, amount, existingProduct.getVersion());
|
return getProductById(productId);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Product decreaseStock(Long productId, Integer amount) {
|
Product existingProduct = this.getProductById(productId);
|
if (existingProduct.getStockQuantity() < amount) {
|
throw new RuntimeException("库存不足,当前库存: " + existingProduct.getStockQuantity() + ", 请求数量: " + amount);
|
}
|
productStockMapper.decreaseStock(productId, amount, existingProduct.getVersion());
|
return getProductById(productId);
|
}
|
|
@Override
|
public boolean isStockSufficient(Long productId, Integer requiredAmount) {
|
if (requiredAmount == null || requiredAmount <= 0) {
|
throw new IllegalArgumentException("需求数量必须大于0");
|
}
|
Product existingProduct = this.getProductById(productId);
|
return existingProduct.getStockQuantity() >= requiredAmount;
|
}
|
|
}
|