feat : spu

This commit is contained in:
2025-03-25 17:34:26 +08:00
parent 6fca649c86
commit 3b7817bf02
4 changed files with 68 additions and 5 deletions

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Constants\Common;
class CategoryCode
{
/**
* 商品分类 1=菜品 2=主食 3=加饭
*/
CONST DISH = 1;
CONST STAPLE = 2;
CONST EXTRA_STAPLE = 3;
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Model;
use App\Constants\Common\CategoryCode;
use App\Constants\Common\GoodCode;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
@@ -102,4 +103,17 @@ class Spu extends Model
return $res->toArray();
}
/**
* @param int $cycle_id
* @return array
*/
public function getCycleIdAddStapleFoodSkuIds(int $cycle_id): array
{
return $this
->leftJoin('sku','sku.spu_id','=','spu.id')
->where('spu.cycle_id',$cycle_id)
->where('spu.category_id',CategoryCode::EXTRA_STAPLE)->pluck('sku.id')
->toArray();
}
}

View File

@@ -53,6 +53,19 @@ class OptionalListService extends BaseService
$this->goodCache->kitchenId = (int)$siteInfo['kitchen_id'];
$data = $this->goodCache->getOptionalGoodList();
$this->buildData(&$data);
return $this->return->success('success', ['list' => $data]);
}
/**
* @param $data
* @return void
*/
private function buildData(&$data): void
{
foreach ($data as &$spu) {
$spu['price'] = min(array_column($spu['sku_list'], 'price'));
}
}
}

View File

@@ -7,6 +7,8 @@ use App\Constants\Admin\CateringCode;
use App\Constants\Common\OrderCode;
use App\Model\OrderMealCateringLog;
use App\Model\OrderOptionCateringLog;
use App\Model\Sku;
use App\Model\Spu;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -65,8 +67,15 @@ trait CateringTrait
*/
private function manageSubOptionCateringLog(): void
{
// todo 减少可以不减少根据订单 cycle_id 获取加一碗米饭 的数量 减少查询压力
// $addStapleFoodNum = 0;
$extraStapleArr = $this->spuModel->getCycleIdAddStapleFoodSkuIds($this->orderInfo->cycle_id);
$orderGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('copies','sku_id')->toArray();
$addStapleFoodNum = 0;
foreach ($orderGoods as $key => $copies) {
if (!in_array($key, $extraStapleArr)) continue;
$addStapleFoodNum = $copies + $addStapleFoodNum;
}
$siteInfo = $this->siteCache->getSiteInfo($this->orderInfo->site_id);
@@ -83,8 +92,8 @@ trait CateringTrait
return;
}
$logInfo->quantity = $logInfo->quantity + $this->orderInfo->copies;
// $logInfo->add_staple_food_num = $this->orderInfo->add_staple_food_num - $addStapleFoodNum;
$logInfo->quantity = $logInfo->quantity - $this->orderInfo->copies;
$logInfo->add_staple_food_num = $this->orderInfo->add_staple_food_num - $addStapleFoodNum;
if (!$logInfo->save()) {
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubOptionCateringLog:修改配餐记录失败,订单信息:'.json_encode($this->orderInfo->toArray()));
@@ -121,6 +130,12 @@ trait CateringTrait
}
}
/**
* @var Spu
*/
#[Inject]
protected Spu $spuModel;
/**
* 添加自选配餐数据
* @return bool
@@ -129,8 +144,16 @@ trait CateringTrait
*/
private function manageAddOptionCateringLog(): bool
{
//todo 根据订单 cycle_id 获取加一碗米饭 的数量
//根据订单 cycle_id 获取加一碗米饭 的数量
$extraStapleArr = $this->spuModel->getCycleIdAddStapleFoodSkuIds($this->orderInfo->cycle_id);
$orderGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('copies','sku_id')->toArray();
$addStapleFoodNum = 0;
foreach ($orderGoods as $key => $copies) {
if (!in_array($key, $extraStapleArr)) continue;
$addStapleFoodNum = $copies + $addStapleFoodNum;
}
$siteInfo = $this->siteCache->getSiteInfo($this->orderInfo->site_id);
//需要绑定一个 kitchen_id