Files
hyperf_service/app/Service/ServiceTrait/Admin/Catering/PrintTrait.php
2025-03-24 16:38:37 +08:00

216 lines
6.7 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);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function closePrint(): void
{
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->printKey, $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, $this->logInfo->id);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function closeSite(): void
{
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->stopOrderKey, $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, $siteId);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function closeWholeLine(): void
{
$siteArr = $this->siteModel->where('delivered_id',$this->siteInfo->delivered_id)->pluck('id');
//没有日志数据 跳出
$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($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->stopOrderKey, $insertCacheData);
}
/**
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function isBuildPickupCode(): bool
{
if (CateringCode::REDIS_FINISH_VALUE == $this->redis->hGet($this->pickupCodeKey, $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, $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, $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, $this->logInfo->id)) return;
$this->redis->hSet($this->isCateringKey, $this->logInfo->id, CateringCode::REDIS_FINISH_VALUE);
}
}