158 lines
4.6 KiB
PHP
158 lines
4.6 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\GoodCode;
|
|
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;
|
|
}
|
|
|
|
|
|
/**
|
|
* 统一下单逻辑
|
|
* @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
|
|
*/
|
|
private function placeOrder(): void
|
|
{
|
|
try {
|
|
Db::beginTransaction();
|
|
|
|
$this->insertOrder();
|
|
|
|
$this->insertOrderGoods();
|
|
|
|
Db::commit();
|
|
} catch (Exception $e){
|
|
Db::rollBack();
|
|
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 = '123';
|
|
$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;
|
|
}
|
|
} |