feat: place order

This commit is contained in:
2025-01-23 16:56:24 +08:00
parent b2f96de226
commit aadcf6c661
11 changed files with 504 additions and 111 deletions

View File

@@ -0,0 +1,158 @@
<?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\Cache\Redis\RedisCache;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\OrderTrait;
use App\Service\ServiceTrait\Common\CycleTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
abstract class BaseOrderService extends BaseService
{
use CycleTrait,OrderTrait;
/**
* @var int 周期id
*/
protected int $cycleId;
/**
* @var array
*/
protected array $goodIds = [];
/**
* @var string 库存key
*/
protected string $stockKey;
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @var array
*/
protected array $cartSecondData;
/**
* @var array
*/
protected array $cartFirstData;
/**
* @var int
*/
protected int $copies;
/**
* @var array
*/
protected array $skuArr;
/**
* 订单结果
* @var array
*/
protected array $orderRes = [];
/**
* 优惠券id
* @var int
*/
protected int $couponId;
/**
* 地点id
* @var int
*/
protected int $siteId;
/**
* 构造方法
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct()
{
parent::__construct();
$this->cycleId = (int)$this->initTodayCycleId();
$this->cartFirstData = [];
$this->cartSecondData = [];
$this->skuArr = [];
$this->goodIds = [];
$this->copies = 0;
$this->couponId = 0;
$this->siteId = 0;
$this->orderRes = [
'total_price' => '0.00', //总价
'total_good_price' => '0.00', //商品总价
'favorable_good_price' => '0.00', //商品优惠价格
'good_after_discount_price' => '0.00', //商品优惠后价格
'total_sundry_price' => '0.00', //服务总费用
'sundry_price' => '0.00', //服务费单价
'sundry_num' => 0, //服务费数量
'favorable_sundry_price' => '0.00', //服务费优惠价格
'sundry_after_discount_price' => '0.00', //服务费优惠后价格
'actual_price' => '0.00', //实际支付价格
'coupon_id' => 0,
'coupon' => [],
'site_id' => 0,
'site' => [],
'good_ids' => [],
'good' => [],
'optional_copies' => 0,
'meal_copies' => 0,
];
}
/**
* 检测
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function check(): void
{
$kitchenId = $this->checkSite($this->siteId);
$this->getGoodInfo($kitchenId);
$this->buildCartData(json_decode($this->request->input('cart'), true));
$this->checkGood();
$this->checkStock();
}
/**
* 计算
* @return void
*/
public function compute(): void
{
$this->computePrice();
$this->computeSundryPrice();
$this->computeFavorable();
$this->computeFinallyPrice();
}
}

View File

@@ -10,72 +10,11 @@ declare(strict_types=1);
namespace App\Service\Api\Order;
use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Api\GoodCache;
use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode;
use App\Exception\ErrException;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\OrderTrait;
use App\Service\ServiceTrait\Common\CycleTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Config\config;
class CheckCartService extends BaseService
class CheckCartService extends BaseOrderService
{
use CycleTrait,OrderTrait;
/**
* @var int 周期id
*/
private int $cycleId;
/**
* @var array
*/
private array $goodIds = [];
/**
* @var string 库存key
*/
private string $stockKey;
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @var array
*/
private array $cartSecondData;
/**
* @var array
*/
private array $cartFirstData;
/**
* @var int
*/
private int $copies;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct()
{
parent::__construct();
$this->cycleId = (int)$this->initTodayCycleId();
$this->cartFirstData = [];
$this->cartSecondData = [];
$this->copies = 0;
}
/**
* @return array
* @throws ContainerExceptionInterface
@@ -83,43 +22,10 @@ class CheckCartService extends BaseService
*/
public function handle(): array
{
throw new ErrException(ApiCode::getMessage(ApiCode::LOGIN_ERROR),ApiCode::LOGIN_ERROR);
$kitchenId = (int)$this->request->input('kitchen_id');
$this->siteId = (int)$this->request->input('site_id');
$this->getGoodInfo($kitchenId);
$this->check();
$this->buildCartData(json_decode($this->request->input('cart'), true));
$this->checkGood($this->goodIds);
$this->checkStock();
$this->return->success();
}
/**
* 获取商品信息
* @param int $kitchenId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getGoodInfo(int $kitchenId): void
{
$this->stockKey = ApiRedisKey::goodStockKey($this->cycleId,$kitchenId);
$mealGoodKey = ApiRedisKey::mealGoodListKey($this->cycleId,$kitchenId);
$optionalGoodKey = ApiRedisKey::optionalGoodListKey($this->cycleId,$kitchenId);
$mealGood = $this->redis->get($mealGoodKey);
$optionalGood = $this->redis->get($optionalGoodKey);
if (empty($mealGood) || empty($optionalGood)) throw new ErrException('商品不存在');
if ($this->redis->exists($this->stockKey)) throw new ErrException('库存不足');
$mealGood = json_decode($mealGood, true);
$optionalGood = json_decode($optionalGood, true);
$this->goodIds = array_merge(array_column($mealGood, 'id'), array_column($optionalGood, 'id'));
return $this->return->success();
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Order;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ConfirmationOrderService extends BaseOrderService
{
/**
* @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();
return $this->return->success('success',$this->orderRes);
}
}

View File

@@ -10,16 +10,31 @@ declare(strict_types=1);
namespace App\Service\Api\Order;
use App\Service\Api\BaseService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class PlaceOrderService extends BaseService
class PlaceOrderService extends BaseOrderService
{
/**
* @var int 1=微信支付 2=支付宝支付 3=余额支付
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private int $type;
public function handle()
{
$this->siteId = (int)$this->request->input('site_id');
$this->couponId = (int)$this->request->input('coupon_id',0);
$this->check();
$this->compute();
$this->placeOrder();
return $this->return->success('success',$this->orderRes);
}
private function placeOrder()
{
}