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

@@ -10,14 +10,31 @@ declare(strict_types=1);
namespace App\Service\ServiceTrait\Api;
use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Api\SiteCache;
use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode;
use App\Exception\ErrException;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait OrderTrait
{
/**
* @var SiteCache
*/
#[Inject]
protected SiteCache $siteCache;
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* 生成购物车数据
* @param array $data
* @return void
*/
@@ -40,19 +57,20 @@ trait OrderTrait
}
/**
* @param array $goodIds
* 检测商品
* @return void
*/
protected function checkGood(array $goodIds): void
protected function checkGood(): void
{
foreach ($this->cartFirstData as $key => $one) {
if (in_array($key, $goodIds)) continue;
if (in_array($key, $this->goodIds)) continue;
throw new ErrException('商品不存在');
throw new ErrException('商品不存在',ApiCode::ORDER_GOOD_IN_EXISTENCE);
}
}
/**
* 检测库存
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
@@ -62,7 +80,168 @@ trait OrderTrait
foreach ($this->cartFirstData as $key => $one) {
if (($this->redis->zScore($this->stockKey,$key) ?? 0) >= $one) continue;
throw new ErrException('商品库存不足');
throw new ErrException('商品库存不足',ApiCode::ORDER_GOOD_INSUFFICIENT_STOCK);
}
}
/**
* 检测地点
* @param int $siteId
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkSite(int $siteId): int
{
$siteInfo = $this->siteCache->getSiteInfo($siteId);
if (empty($siteInfo)) throw new ErrException('该地点暂时不支持点餐');
$kitchenId = (int)($siteInfo['kitchen_id'] ?? 0);
if ($kitchenId <= 0) throw new ErrException('该地点暂时不支持点餐');
$this->orderRes['site_id'] = $siteId;
$this->orderRes['site_info'] = $siteInfo;
return $kitchenId;
}
/**
* 获取商品信息
* @param int $kitchenId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected 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);
$skuArr = [];
foreach ($mealGood as $one){
$skuArr = array_merge($skuArr,$one['sku_list']);
}
foreach ($optionalGood as $one){
$skuArr = array_merge($skuArr,$one['sku_list']);
}
$this->skuArr = array_column($skuArr,null,'id');
$this->goodIds = array_column($skuArr,'id');
unset($skuArr);
}
/**
* @return int
*/
protected function getAutoCouponId(): int
{
return 0;
}
/**
* 计算价格
* @return void
*/
protected function computePrice(): void
{
$this->orderRes['good_ids'] = $this->cartSecondData;
foreach ($this->cartSecondData as $oneCopies) {
$oneCopiesTotalPrice = '0.00';
$copiesType = 1;
$oneCopiesGoodInfo = [];
foreach ($oneCopies as $oneGood) {
if (empty($oneCopiesGoodInfo[$oneGood]))
{
$oneCopiesGoodInfo[$oneGood] = [
'num' => 1,
'good_name' => '1',
'good_url' => '1',
'unit_price' => $this->skuArr[$oneGood]['price']
];
}
$oneCopiesTotalPrice = bcadd($oneCopiesTotalPrice, $this->skuArr[$oneGood]['price'],2);
if ($copiesType == 1 && $this->skuArr[$oneGood]['type'] == 2) {
$copiesType = 2;
}
}
$this->orderRes['good'][] = [
'good_ids' => $oneCopies,
'good_info' => $oneCopiesGoodInfo,
'price' => $oneCopiesTotalPrice,
];
if ($copiesType == 1) {
$this->orderRes['optional_copies']++;
} else {
$this->orderRes['meal_copies']++;
}
$this->orderRes['total_good_price'] = bcadd($this->orderRes['total_good_price'],$oneCopiesTotalPrice,2);
}
}
/**
* 计算服务费
* @return void
*/
protected function computeSundryPrice(): void
{
$this->orderRes['sundry_num'] = $this->orderRes['optional_copies'];
$this->orderRes['sundry_price'] = '1.00'; //todo 设置
$this->orderRes['total_sundry_price'] = bcmul($this->orderRes['sundry_price'],$this->orderRes['sundry_num'],2);
}
/**
* 计算优惠
* @return void
*/
protected function computeFavorable(): void
{
if ($this->couponId <= 0) {
$this->couponId = $this->getAutoCouponId();
}
if ($this->couponId <= 0) {
return;
}
$couponInfo = []; // todo 优惠券信息
$this->orderRes['coupon_id'] = $this->couponId;
$this->orderRes['coupon'] = $couponInfo;
//todo 优惠计算
$this->orderRes['favorable_good_price'] = '1.00';
$this->orderRes['favorable_sundry_price'] = '1.00';
}
/**
* 计算最终价格
* @return void
*/
protected function computeFinallyPrice(): void
{
$this->orderRes['good_after_discount_price'] = bcsub($this->orderRes['total_good_price'],$this->orderRes['favorable_good_price'],2);
$this->orderRes['sundry_after_discount_price'] = bcsub($this->orderRes['total_sundry_price'],$this->orderRes['favorable_sundry_price'],2);
$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);
}
}