feat : meal catering

This commit is contained in:
2025-03-25 09:44:52 +08:00
parent 6d66843c38
commit 374257f06a
4 changed files with 196 additions and 2 deletions

View File

@@ -70,6 +70,39 @@ trait PrintTrait
$this->redis->eval($script, [$this->printKey, $this->stopOrderKey, $this->pickupCodeKey, $this->isCateringKey, CateringCode::REDIS_FINISH_VALUE, DateUtil::DAY], 4);
}
/**
* @var string
*/
protected string $stopMealOrderKey;
protected string $isMealCateringKey;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __initMealRedisKey(): void
{
$this->stopMealOrderKey = AdminRedisKey::mealCateringStopOrder($this->cycleId);
$this->isMealCateringKey = AdminRedisKey::mealIsCatering($this->cycleId);
$script = <<<lua
local isStop = redis.call('exists',KEYS[1])
if isStop == 0 then
redis.call('hSET',KEYS[1],0,ARGV[1])
redis.call('Expire',KEYS[1],ARGV[2])
end
local isCatering = redis.call('exists',KEYS[2])
if isCatering == 0 then
redis.call('hSET',KEYS[2],0,ARGV[1])
redis.call('Expire',KEYS[2],ARGV[2])
end
lua;
$this->redis->eval($script, [$this->stopMealOrderKey, $this->isMealCateringKey, CateringCode::REDIS_FINISH_VALUE, DateUtil::DAY], 2);
}
/**
* @return void
* @throws ContainerExceptionInterface
@@ -156,6 +189,45 @@ trait PrintTrait
$this->redis->hMset($this->stopOrderKey, $insertCacheData);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function closeMealWholeLine(): void
{
$siteArr = $this->siteModel->where('delivered_id',$this->siteInfo->delivered_id)->pluck('id');
//没有日志数据 跳出
$logArr = $this
->orderMealCateringLogModel
->where('cycle_id',$this->cycleId)
->whereIn('site_id',$siteArr)
->pluck('site_id')
->toArray();
if (empty($logArr)) return;
$logArr = array_unique($logArr);
//没有缓存数据 跳出
$cacheData = $this->redis->hGetAll($this->stopMealOrderKey);
if (empty($cacheData)) return;
$cacheData = array_diff(array_keys($cacheData),[0]); //需要排除默认写的值
//数量不对等 跳出
if (count($logArr) !== count($cacheData)) return;
//如果相反交集有一个不为空就是不对的 跳出
if (!empty(array_diff($logArr,$cacheData)) || !empty(array_diff($cacheData,$logArr))) return;
//批量写入 hashmap
$insertCacheData = [];
foreach ($siteArr as $one) {
$insertCacheData[$one] = CateringCode::REDIS_FINISH_VALUE;
}
$this->redis->hMset($this->stopMealOrderKey, $insertCacheData);
}
/**
* @return bool
* @throws ContainerExceptionInterface
@@ -213,4 +285,61 @@ trait PrintTrait
$this->redis->hSet($this->isCateringKey, $this->logInfo->id, CateringCode::REDIS_FINISH_VALUE);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function joinMealCateringCache(): void
{
$hashKey = $this->logInfo->site_id.':'.$this->logInfo->sku_id;
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->isMealCateringKey, $hashKey)) return;
$this->redis->hSet($this->isMealCateringKey, $hashKey, CateringCode::REDIS_FINISH_VALUE);
}
/**
* @param int $siteId
* @param int $skuId
* @param int $cycleId
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function isMealCateringByCache(int $siteId, int $skuId, int $cycleId): bool
{
$hashKey = $siteId.':'.$skuId;
$this->isMealCateringKey = AdminRedisKey::mealIsCatering($cycleId);
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->isMealCateringKey, $hashKey)) return true;
return false;
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function closeMealSite(): void
{
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->stopMealOrderKey, $this->logInfo->site_id)) return;
$this->redis->hSet($this->stopMealOrderKey, $this->logInfo->site_id, CateringCode::REDIS_FINISH_VALUE);
}
/**
* @param int $siteId
* @param int $cycleId
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkMealIsStopOrder(int $siteId,int $cycleId): bool
{
$this->stopMealOrderKey = AdminRedisKey::mealCateringStopOrder($cycleId);
return CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->stopMealOrderKey, $siteId);
}
}

View File

@@ -98,6 +98,8 @@ trait OrderTrait
/**
* 检测商品
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkGood(): void
{
@@ -106,6 +108,10 @@ trait OrderTrait
if ($this->orderType == 0) $this->orderType = $this->skuArr[$key]['type'];
if ($this->skuArr[$key]['type'] != $this->orderType) throw new ErrException('自选菜品跟套餐菜品请分开订单下单');
if ($this->orderType != OrderCode::ORDER_TYPE_MEAL) return;
if ($this->isMealCateringByCache($this->siteId, $key, $this->cycleId)) throw new ErrException('该点位的套餐'.$this->skuArr[$key]['title'].'已配餐截单,请选择其他套餐');
}
}
@@ -154,7 +160,7 @@ trait OrderTrait
$closeOrderFlag = match ($this->orderType) {
OrderCode::ORDER_TYPE_OPTIONAL => $this->checkIsStopOrder($siteId, $this->cycleId),
OrderCode::ORDER_TYPE_MEAL => false, //todo 这里逻辑补全
OrderCode::ORDER_TYPE_MEAL => $this->checkMealIsStopOrder($siteId, $this->cycleId),
};
if ($closeOrderFlag) throw new ErrException('该点已经截单');