|
@@ -1,11 +1,21 @@
|
|
|
package com.fjhx.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.constant.LibraryStorageLockConstant;
|
|
|
+import com.fjhx.constant.StockJournalTypeConstant;
|
|
|
+import com.fjhx.constants.ErrorMsgConstant;
|
|
|
import com.fjhx.entity.Stock;
|
|
|
import com.fjhx.mapper.StockMapper;
|
|
|
import com.fjhx.service.StockService;
|
|
|
+import com.fjhx.utils.Assert;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.redis.lock.RedisLockClient;
|
|
|
+import org.springblade.core.secure.utils.AuthUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+
|
|
|
|
|
|
* <p>
|
|
|
* 库存 服务实现类
|
|
@@ -17,4 +27,68 @@ import org.springframework.stereotype.Service;
|
|
|
@Service
|
|
|
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisLockClient redisLockClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StockJournalServiceImpl stockJournalService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void changeQuantity(Long goodsId, Long warehouseId, BigDecimal quantity, Integer detailsType, String remarks) {
|
|
|
+
|
|
|
+
|
|
|
+ Boolean flag = redisLockClient.lockFair(LibraryStorageLockConstant.STOCK_LOCK + goodsId + warehouseId,
|
|
|
+ () -> {
|
|
|
+
|
|
|
+ Stock stock = getOne(Stock::getGoodsId, goodsId);
|
|
|
+
|
|
|
+
|
|
|
+ if (stock == null) {
|
|
|
+ stock = new Stock();
|
|
|
+ stock.setGoodsId(goodsId);
|
|
|
+ stock.setQuantity(quantity);
|
|
|
+ stock.setWarehouseId(warehouseId);
|
|
|
+ stock.setTime();
|
|
|
+ stock.setTenantId(AuthUtil.getTenantId());
|
|
|
+ save(stock);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal oldQuantity = stock.getQuantity();
|
|
|
+ BigDecimal newQuantity = oldQuantity.add(quantity);
|
|
|
+
|
|
|
+ int compareTo = newQuantity.compareTo(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ if (compareTo < 0) {
|
|
|
+ throw new ServiceException("库存不足,出库失败");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (compareTo == 0) {
|
|
|
+ remove(Stock::getGoodsId, goodsId);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ stock.setUpdateTime();
|
|
|
+ stock.setQuantity(newQuantity);
|
|
|
+ updateById(stock);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+
|
|
|
+ Assert.eqTrue(flag, ErrorMsgConstant.SYSTEM_BUSY_ERROR);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ stockJournalService.add(
|
|
|
+ quantity.compareTo(BigDecimal.ZERO) > 0 ? StockJournalTypeConstant.IN : StockJournalTypeConstant.OUT,
|
|
|
+ detailsType,
|
|
|
+ goodsId,
|
|
|
+ warehouseId,
|
|
|
+ quantity.compareTo(BigDecimal.ZERO) > 0 ? quantity : BigDecimal.ZERO.subtract(quantity),
|
|
|
+ remarks
|
|
|
+ );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|