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

@@ -95,6 +95,26 @@ class AdminRedisKey
return 'catering:option:is:cycle_id:'.$cycleId;
}
/**
* 截单 (site_id)
* @param int $cycleId
* @return string
*/
public static function mealCateringStopOrder(int $cycleId): string
{
return 'catering:meal:stop_order:cycle_id:'.$cycleId;
}
/**
* @param int $cycleId
* @return string
*/
public static function mealIsCatering(int $cycleId): string
{
return 'catering:meal:is:cycle_id:'.$cycleId;
}
/**
* 套餐配餐点位顺序
* @param int $cycleId

View File

@@ -18,10 +18,15 @@ use App\Model\OrderGood;
use App\Model\OrderMealCateringLog;
use App\Model\Site;
use App\Service\Admin\Catering\CateringBaseService;
use App\Service\ServiceTrait\Admin\Catering\PrintTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CateringService extends CateringBaseService
{
use PrintTrait;
/**
* @var OrderMealCateringLog
*/
@@ -55,8 +60,15 @@ class CateringService extends CateringBaseService
*/
protected int $resCount = 0;
/**
* @var int 关闭站点标识 0 未关闭 1 关闭
*/
protected int $closeSiteFlag = 0;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
@@ -70,17 +82,44 @@ class CateringService extends CateringBaseService
// 检测数据
$this->check();
// 生成 key
$this->__initMealRedisKey();
// 配餐
$this->catering();
// todo
// 加缓存
$this->joinMealCateringCache();
// 查询该点所有套餐是否已经配餐 如果是就缓存关闭该点
$this->isCloseMealSite();
// 关闭线路
if ($this->closeSiteFlag == 1) $this->closeMealWholeLine();
return $this->return->success('success',['num' => $this->resCount]);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function isCloseMealSite(): void
{
$res = $this->orderMealCateringLogModel
->where('site_id', $this->siteInfo->id)
->where('cycle_id', $this->logInfo->cycle_id)
->where('quantity','>',0)
->count() ?? 0;
if ($res > 0) return; // 证明还没完结 配餐 跳出
$this->closeMealSite();
$this->closeSiteFlag = 1;
}
/**
* @return void
*/

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('该点已经截单');