feat: place Order

This commit is contained in:
2025-02-05 17:56:32 +08:00
parent fd94db463e
commit fda042da54
14 changed files with 504 additions and 11 deletions

View File

@@ -81,6 +81,11 @@ abstract class BaseOrderService extends BaseService
*/
protected int $siteId;
/**
* @var int
*/
protected int $orderId;
/**
* 构造方法
* @throws ContainerExceptionInterface

View File

@@ -10,12 +10,25 @@ 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
{
/**
* 统一下单逻辑
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
@@ -31,17 +44,108 @@ class PlaceOrderService extends BaseOrderService
$this->placeOrder();
$this->joinCancelDelayQueue();
return $this->return->success('success',$this->orderRes);
}
private function placeOrder()
/**
* 加入取消队列
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function joinCancelDelayQueue(): void
{
foreach ($this->orderRes['good'] as $oneCopies)
{
foreach ($oneCopies['good_info'] as $one)
{
$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;
}
}

View File

@@ -14,6 +14,7 @@ use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Api\SiteCache;
use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode;
use App\Constants\Common\GoodCode;
use App\Exception\ErrException;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
@@ -165,19 +166,21 @@ trait OrderTrait
$oneCopiesGoodInfo = [];
foreach ($oneCopies as $oneGood) {
if (empty($oneCopiesGoodInfo[$oneGood]))
{
if (empty($oneCopiesGoodInfo[$oneGood])) {
$oneCopiesGoodInfo[$oneGood] = [
'num' => 1,
'good_name' => '1',
'good_url' => '1',
'unit_price' => $this->skuArr[$oneGood]['price']
'unit_price' => $this->skuArr[$oneGood]['price'],
'type' => $this->skuArr[$oneGood]['type'],
];
} else {
$oneCopiesGoodInfo[$oneGood]['num'] += 1;
}
$oneCopiesTotalPrice = bcadd($oneCopiesTotalPrice, $this->skuArr[$oneGood]['price'],2);
if ($copiesType == 1 && $this->skuArr[$oneGood]['type'] == 2) {
if ($copiesType == 1 && $this->skuArr[$oneGood]['type'] == GoodCode::SPU_TYPE_MEAL) {
$copiesType = 2;
}
}