390 lines
12 KiB
PHP
390 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Service\ServiceTrait\Admin\Catering;
|
|
|
|
use App\Cache\Redis\Admin\AdminRedisKey;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\Admin\CateringCode;
|
|
use App\Exception\ErrException;
|
|
use App\Extend\DateUtil;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
trait PrintTrait
|
|
{
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected string $printKey;
|
|
protected string $stopOrderKey;
|
|
protected string $pickupCodeKey;
|
|
protected string $isCateringKey;
|
|
|
|
|
|
/**
|
|
*
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function __initRedisKey(): void
|
|
{
|
|
$this->printKey = AdminRedisKey::optionCateringIsPrint($this->cycleId);
|
|
$this->stopOrderKey = AdminRedisKey::optionCateringStopOrder($this->cycleId);
|
|
$this->pickupCodeKey = AdminRedisKey::optionCateringBuildPickupCode($this->cycleId);
|
|
$this->isCateringKey = AdminRedisKey::optionIsCatering($this->cycleId);
|
|
|
|
$script = <<<lua
|
|
local isPrint = redis.call('exists',KEYS[1])
|
|
if isPrint == 0 then
|
|
redis.call('hSET',KEYS[1],0,ARGV[1])
|
|
redis.call('Expire',KEYS[1],ARGV[2])
|
|
end
|
|
|
|
local isStop = redis.call('exists',KEYS[2])
|
|
if isStop == 0 then
|
|
redis.call('hSET',KEYS[2],0,ARGV[1])
|
|
redis.call('Expire',KEYS[2],ARGV[2])
|
|
end
|
|
|
|
local isPick = redis.call('exists',KEYS[3])
|
|
if isPick == 0 then
|
|
redis.call('hSET',KEYS[3],0,ARGV[1])
|
|
redis.call('Expire',KEYS[3],ARGV[2])
|
|
end
|
|
|
|
local isCatering = redis.call('exists',KEYS[4])
|
|
if isCatering == 0 then
|
|
redis.call('hSET',KEYS[4],0,ARGV[1])
|
|
redis.call('Expire',KEYS[4],ARGV[2])
|
|
end
|
|
lua;
|
|
|
|
$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
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function closePrint(): void
|
|
{
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->printKey, (string)$this->logInfo->id)) return;
|
|
|
|
$this->redis->hSet($this->printKey, $this->logInfo->id, CateringCode::REDIS_FINISH_VALUE);
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function checkIsPrint(): bool
|
|
{
|
|
return CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->printKey, (string)$this->logInfo->id);
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @param int $cycleId
|
|
* @return bool
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function checkIsPrintByList(int $id,int $cycleId): bool
|
|
{
|
|
$printKey = AdminRedisKey::optionCateringIsPrint($this->cycleId);
|
|
|
|
return CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($printKey, (string)$id);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function closeSite(): void
|
|
{
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->stopOrderKey, (string)$this->logInfo->site_id)) return;
|
|
|
|
$this->redis->hSet($this->stopOrderKey, $this->logInfo->site_id, CateringCode::REDIS_FINISH_VALUE);
|
|
}
|
|
|
|
/**
|
|
* @param int $siteId
|
|
* @param int $cycleId
|
|
* @return bool
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function checkIsStopOrder(int $siteId,int $cycleId): bool
|
|
{
|
|
$this->stopOrderKey = AdminRedisKey::optionCateringStopOrder($cycleId);
|
|
|
|
return CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->stopOrderKey, (string)$siteId);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function closeWholeLine(): void
|
|
{
|
|
$siteArr = $this->siteModel->where('delivered_id',$this->siteInfo->delivered_id)->pluck('id')->toArray();
|
|
|
|
//没有日志数据 跳出
|
|
$logArr = $this
|
|
->orderOptionCateringLogModel
|
|
->where('cycle_id',$this->cycleId)
|
|
->whereIn('site_id',$siteArr)
|
|
->pluck('site_id')
|
|
->toArray();
|
|
if (empty($logArr)) return;
|
|
|
|
|
|
//没有缓存数据 跳出
|
|
$cacheData = $this->redis->hGetAll($this->stopOrderKey);
|
|
if (empty($cacheData)) return;
|
|
$cacheData = array_diff(array_keys($cacheData),[0]); //需要排除默认写的值
|
|
|
|
//数量不对等 跳出
|
|
if(count(array_diff($logArr, $cacheData)) !== 0) return;
|
|
|
|
//批量写入 hashmap
|
|
$insertCacheData = [];
|
|
foreach ($siteArr as $one) {
|
|
$insertCacheData[$one] = CateringCode::REDIS_FINISH_VALUE;
|
|
}
|
|
$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
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function isBuildPickupCode(): bool
|
|
{
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->pickupCodeKey, (string)$this->logInfo->id)) return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function buildPickupCodeCache(): void
|
|
{
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->pickupCodeKey, (string)$this->logInfo->id)) return;
|
|
|
|
$this->redis->hSet($this->pickupCodeKey, $this->logInfo->id, CateringCode::REDIS_FINISH_VALUE);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function delPickUpCodeByIdCache(): void
|
|
{
|
|
$this->redis->hDel($this->pickupCodeKey, $this->logInfo->id);
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function isCateringByCache(): bool
|
|
{
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->isCateringKey, (string)$this->logInfo->id)) return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function joinCateringCache(): void
|
|
{
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->isCateringKey, (string)$this->logInfo->id)) return;
|
|
|
|
$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, (string)$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, (string)$siteId);
|
|
}
|
|
|
|
/**
|
|
* @param int $skuId
|
|
* @param int $cycleId
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function closeMealSku(int $skuId, int $cycleId): void
|
|
{
|
|
$key = AdminRedisKey::mealIsCateringBySkuId($cycleId);
|
|
|
|
$hashKey = (string)$skuId;
|
|
|
|
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($key, $hashKey)) return;
|
|
|
|
$this->redis->hSet($key, $hashKey, CateringCode::REDIS_FINISH_VALUE);
|
|
}
|
|
|
|
/**
|
|
* @param int $skuId
|
|
* @param int $cycleId
|
|
* @return bool
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function checkSkuMeal(int $skuId, int $cycleId): bool
|
|
{
|
|
$key = AdminRedisKey::mealIsCateringBySkuId($cycleId);
|
|
|
|
$hashKey = (string)$skuId;
|
|
|
|
return CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($key, $hashKey);
|
|
}
|
|
} |