feat : order

This commit is contained in:
2025-03-06 16:25:25 +08:00
parent 5ad53e55be
commit 88fdd218b3
10 changed files with 82 additions and 57 deletions

View File

@@ -102,8 +102,10 @@ trait OrderTrait
protected function checkGood(): void
{
foreach ($this->cartFirstData as $key => $one) {
if (in_array($key, $this->goodIds)) continue;
if ($this->orderType == 0) $this->orderType = $this->skuArr[$key]['type'];
if ($this->skuArr[$key]['type'] != $this->orderType) throw new ErrException('自选菜品跟套餐菜品请分开订单下单');
if (in_array($key, $this->goodIds)) continue;
throw new ErrException('商品不存在',ApiCode::ORDER_GOOD_IN_EXISTENCE);
}
}
@@ -154,42 +156,51 @@ trait OrderTrait
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 = [];
$skuImageArr = [];
switch ($this->orderType) {
case OrderCode::ORDER_TYPE_OPTIONAL:
$optionalGoodKey = ApiRedisKey::optionalGoodListKey($this->cycleId,$kitchenId);
$optionalGood = $this->redis->get($optionalGoodKey);
if (empty($optionalGood)) throw new ErrException('商品不存在');
foreach ($mealGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = $one['title'];
$sku['type'] = GoodCode::SPU_TYPE_MEAL;
return $sku;
}, $one['sku_list']);
$optionalGood = json_decode($optionalGood, true);
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
}
foreach ($optionalGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = strtolower($one['title']);
$sku['type'] = GoodCode::SPU_TYPE_OPTIONAL;
return $sku;
}, $one['sku_list']);
foreach ($optionalGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = strtolower($one['title']);
$sku['type'] = GoodCode::SPU_TYPE_OPTIONAL;
return $sku;
}, $one['sku_list']);
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
}
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
break;
case OrderCode::ORDER_TYPE_MEAL:
$mealGoodKey = ApiRedisKey::mealGoodListKey($this->cycleId,$kitchenId);
$mealGood = $this->redis->get($mealGoodKey);
if (empty($mealGood)) throw new ErrException('商品不存在');
$mealGood = json_decode($mealGood, true);
foreach ($mealGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = $one['title'];
$sku['type'] = GoodCode::SPU_TYPE_MEAL;
return $sku;
}, $one['sku_list']);
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
}
break;
default:
throw new ErrException('还不支持下单');
}
$this->skuArr = array_column($skuArr,null,'id');
@@ -217,7 +228,7 @@ trait OrderTrait
foreach ($this->cartSecondData as $oneCopies) {
$oneCopiesTotalPrice = '0.00';
$copiesType = 1;
// $copiesType = 1;
$oneCopiesGoodInfo = [];
@@ -238,9 +249,9 @@ trait OrderTrait
$oneCopiesTotalPrice = bcadd($oneCopiesTotalPrice, bcmul($this->skuArr[$key]['price'],(string)$oneGood,2),2);
if ($copiesType == 1 && $this->skuArr[$key]['type'] == GoodCode::SPU_TYPE_MEAL) {
$copiesType = 2;
}
// if ($copiesType == 1 && $this->skuArr[$key]['type'] == GoodCode::SPU_TYPE_MEAL) {
// $copiesType = 2;
// }
}
$this->orderRes['good'][] = [
@@ -248,11 +259,11 @@ trait OrderTrait
'price' => $oneCopiesTotalPrice,
];
if ($copiesType == 1) {
$this->orderRes['optional_copies']++;
} else {
$this->orderRes['meal_copies']++;
}
// if ($copiesType == 1) {
// $this->orderRes['optional_copies']++;
// } else {
$this->orderRes['copies']++;
// }
$this->orderRes['total_good_price'] = bcadd($this->orderRes['total_good_price'],$oneCopiesTotalPrice,2);
}
@@ -268,8 +279,8 @@ trait OrderTrait
{
$this->orderRes['sundry_num'] = match ($this->configCache->getConfigValueByKey(ConfigCode::SUNDRY_PRICE_COMPUTE_TYPE))
{
1 => $this->orderRes['optional_copies'],
2 => $this->orderRes['meal_copies'],
1 => $this->orderType == OrderCode::ORDER_TYPE_OPTIONAL ? $this->copies : 0,
2 => $this->orderType == OrderCode::ORDER_TYPE_MEAL ? $this->copies : 0,
3 => $this->copies,
};