feat: stock
This commit is contained in:
@@ -33,6 +33,11 @@ class PlaceOrderService extends BaseOrderService
|
||||
$this->isAutoSelectCoupon = 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $rollbackStockCache = [];
|
||||
|
||||
|
||||
/**
|
||||
* 统一下单逻辑
|
||||
@@ -45,12 +50,16 @@ class PlaceOrderService extends BaseOrderService
|
||||
$this->siteId = (int)$this->request->input('site_id');
|
||||
$this->couponId = (int)$this->request->input('coupon_id',0);
|
||||
|
||||
// 生成购物车信息 , 检测商品和库存等
|
||||
$this->check();
|
||||
|
||||
// 计算
|
||||
$this->compute();
|
||||
|
||||
// 下单 减少库存 写入数据库
|
||||
$this->placeOrder();
|
||||
|
||||
// 加入取消延迟队列
|
||||
$this->joinCancelDelayQueue();
|
||||
|
||||
return $this->return->success('success',$this->orderRes);
|
||||
@@ -73,22 +82,61 @@ class PlaceOrderService extends BaseOrderService
|
||||
$producer->produce($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
private function reduceStock(): void
|
||||
{
|
||||
$this->rollbackStockCache = [];
|
||||
|
||||
foreach ($this->cartFirstData as $goodId => $stock) {
|
||||
$this->rollbackStockCache[$goodId] = $stock;
|
||||
if (!($this->redis->zAdd($this->stockKey,'-'.$stock,$goodId))) {
|
||||
throw new Exception('cache error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function rollbackStock(): void
|
||||
{
|
||||
if (empty($this->rollbackStockCache)) return;
|
||||
|
||||
foreach ($this->rollbackStockCache as $goodId => $stock) {
|
||||
$this->redis->zAdd($this->stockKey,$stock,$goodId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下单
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function placeOrder(): void
|
||||
{
|
||||
try {
|
||||
Db::beginTransaction();
|
||||
|
||||
$this->reduceStock();
|
||||
|
||||
$this->insertOrder();
|
||||
|
||||
$this->insertOrderGoods();
|
||||
|
||||
Db::commit();
|
||||
} catch (Exception $e){
|
||||
//回滚数据库 和 缓存
|
||||
Db::rollBack();
|
||||
$this->rollbackStock();
|
||||
//意外抛出
|
||||
throw new ErrException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\ServiceTrait\Api;
|
||||
|
||||
use App\Amqp\Producer\OrderGoodStockProducer;
|
||||
use App\Cache\Redis\Api\ApiRedisKey;
|
||||
use App\Cache\Redis\Api\SiteCache;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
@@ -17,6 +18,8 @@ use App\Constants\ApiCode;
|
||||
use App\Constants\Common\GoodCode;
|
||||
use App\Constants\ConfigCode;
|
||||
use App\Exception\ErrException;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -260,4 +263,21 @@ trait OrderTrait
|
||||
$this->orderRes['total_price'] = bcadd($this->orderRes['total_good_price'],$this->orderRes['total_sundry_price'],2);
|
||||
$this->orderRes['actual_price'] = bcadd($this->orderRes['good_after_discount_price'],$this->orderRes['sundry_after_discount_price'],2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $orderId
|
||||
* @param int $orderStatus
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function sendStockMq(int $orderId,int $orderStatus): void
|
||||
{
|
||||
$message = new OrderGoodStockProducer([
|
||||
'order_id' => $orderId,
|
||||
'type' => $orderStatus
|
||||
]);
|
||||
$producer = ApplicationContext::getContainer()->get(Producer::class);
|
||||
$producer->produce($message);
|
||||
}
|
||||
}
|
||||
8
app/Service/ServiceTrait/Common/StockTrait.php
Normal file
8
app/Service/ServiceTrait/Common/StockTrait.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\ServiceTrait\Common;
|
||||
|
||||
trait StockTrait
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user