205 lines
6.0 KiB
PHP
205 lines
6.0 KiB
PHP
<?php
|
||
/**
|
||
* This service file is part of item.
|
||
*
|
||
* @author ctexthuang
|
||
* @contact ctexthuang@qq.com
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Service\Api\Order;
|
||
|
||
use App\Amqp\Producer\CancelOrderProducer;
|
||
use App\Constants\Common\OrderCode;
|
||
use App\Constants\ConfigCode;
|
||
use App\Exception\ErrException;
|
||
use App\Extend\DateUtil;
|
||
use App\Model\Order;
|
||
use App\Model\OrderGood;
|
||
use Exception;
|
||
use Hyperf\Amqp\Producer;
|
||
use Hyperf\Context\ApplicationContext;
|
||
use Hyperf\DbConnection\Db;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
class PlaceOrderService extends BaseOrderService
|
||
{
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
$this->isAutoSelectCoupon = 2;
|
||
}
|
||
|
||
/**
|
||
* @var array
|
||
*/
|
||
private array $rollbackStockCache = [];
|
||
|
||
|
||
/**
|
||
* 统一下单逻辑
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function handle(): array
|
||
{
|
||
$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);
|
||
}
|
||
|
||
/**
|
||
* 加入取消队列
|
||
* @return void
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
private function joinCancelDelayQueue(): void
|
||
{
|
||
$message = new CancelOrderProducer([
|
||
'order_id' => $this->orderId,
|
||
'type' => OrderCode::ORDER_TYPE_GOOD
|
||
]);
|
||
$message->setDelayMs((int)$this->configCache->getConfigValue(ConfigCode::ORDER_CANCEL_TIME_KEY) * DateUtil::MINUTE * DateUtil::MS);
|
||
$producer = ApplicationContext::getContainer()->get(Producer::class);
|
||
$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());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 插入订单商品
|
||
* @return void
|
||
* @throws Exception
|
||
*/
|
||
private function insertOrderGoods(): void
|
||
{
|
||
$copiesNum = 0;
|
||
$orderGoodInsertArr = [];
|
||
foreach ($this->orderRes['good'] as $oneCopies)
|
||
{
|
||
$copiesNum++;
|
||
foreach ($oneCopies['good_info'] as $one)
|
||
{
|
||
$orderGoodInsertArr[] = [
|
||
'order_id' => $this->orderId,
|
||
'cycle_id' => $this->cycleId,
|
||
'sku_id' => $one['id'],
|
||
'spu_id' => $one['spu_id'],
|
||
'user_id' => $this->userId,
|
||
'quantity' => $one['num'],
|
||
'unit_price' => $one['unit_price'],
|
||
'is_comment' => OrderCode::GOOD_COMMENT_NULL,
|
||
'copies' => $copiesNum,
|
||
'type' => $one['type'],
|
||
'create_time' => date('Y-m-d H:i:s'),
|
||
'update_time' => date('Y-m-d H:i:s'),
|
||
];
|
||
}
|
||
}
|
||
|
||
if (!(new OrderGood)->insert($orderGoodInsertArr)) throw new Exception('写入订单商品失败');
|
||
}
|
||
|
||
/**
|
||
* 插入订单
|
||
* @return void
|
||
* @throws Exception
|
||
*/
|
||
private function insertOrder(): void
|
||
{
|
||
$orderInsertModel = new Order();
|
||
|
||
$orderInsertModel->order_sno = $this->generateOrderNo(OrderCode::ORDER_TYPE_GOOD, $this->userId);
|
||
$orderInsertModel->user_id = $this->userId;
|
||
$orderInsertModel->cycle_id = $this->cycleId;
|
||
$orderInsertModel->site_id = $this->siteId;
|
||
$orderInsertModel->city_id = $this->orderRes['site']['city_id'];
|
||
$orderInsertModel->coupon_id = $this->couponId;
|
||
$orderInsertModel->meal_copies = $this->orderRes['meal_copies'];
|
||
$orderInsertModel->optional_copies = $this->orderRes['optional_copies'];
|
||
$orderInsertModel->total_price = $this->orderRes['total_price'];
|
||
$orderInsertModel->actual_price = $this->orderRes['actual_price'];
|
||
$orderInsertModel->discount_price = $this->orderRes['favorable_sundry_price'] + $this->orderRes['favorable_good_price'];
|
||
$orderInsertModel->status = OrderCode::WAIT_PAY;
|
||
$orderInsertModel->is_refund_all = OrderCode::REFUND_NULL;
|
||
$orderInsertModel->order_json = json_encode($this->orderRes);
|
||
|
||
if (!$orderInsertModel->save()) throw new Exception('下单失败');
|
||
|
||
$this->orderId = $orderInsertModel->id;
|
||
}
|
||
} |