112 lines
2.9 KiB
PHP
112 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Cache\Redis\Admin;
|
|
|
|
use App\Cache\Redis\Common\CommonRedisKey;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\Admin\CateringCode;
|
|
use App\Constants\RedisCode;
|
|
use App\Model\Printer;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class PrinterCache
|
|
{
|
|
/**
|
|
* @var Printer
|
|
*/
|
|
#[Inject]
|
|
protected Printer $printerModel;
|
|
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redisCache;
|
|
|
|
/**
|
|
* @var string 易联云打印机
|
|
*/
|
|
protected string $ylyPrinterKey;
|
|
|
|
/**
|
|
* @var string 集大自动化喷码机
|
|
*/
|
|
protected string $jdPrinterKey;
|
|
|
|
/**
|
|
* 构造函数注入 key
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->ylyPrinterKey = AdminRedisKey::ylyPrinterList();
|
|
$this->jdPrinterKey = AdminRedisKey::jdPrinterList();
|
|
}
|
|
|
|
/**
|
|
* 初始化打印机列表数据
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function __initPrinterListData(): void
|
|
{
|
|
$printerList = $this->printerModel->get();
|
|
|
|
if ($printerList->isEmpty()) return;
|
|
|
|
$ylyPrinterList = [];
|
|
$jdPrinterList = [];
|
|
foreach ($printerList->toArray() as $printer) {
|
|
match ($printer['type']) {
|
|
CateringCode::OPTION_PRINT_YLY => $ylyPrinterList[] = $printer,
|
|
CateringCode::OPTION_PRINT_CODING => $jdPrinterList[] = $printer,
|
|
};
|
|
}
|
|
|
|
$this->delCache();
|
|
|
|
$this->redisCache->set($this->ylyPrinterKey, json_encode($ylyPrinterList));
|
|
$this->redisCache->set($this->jdPrinterKey, json_encode($jdPrinterList));
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function getYlyPrinter(): array
|
|
{
|
|
if ($this->redisCache->exists($this->ylyPrinterKey,RedisCode::SYSTEM_DB)) $this->__initPrinterListData();
|
|
|
|
$res = $this->redisCache->get($this->ylyPrinterKey, RedisCode::SYSTEM_DB);
|
|
if (empty($res)) return [];
|
|
return json_decode($res, true);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function getOtherPrinter(): array
|
|
{
|
|
if ($this->redisCache->exists($this->jdPrinterKey,RedisCode::SYSTEM_DB)) $this->__initPrinterListData();
|
|
|
|
$res = $this->redisCache->get($this->jdPrinterKey, RedisCode::SYSTEM_DB);
|
|
if (empty($res)) return [];
|
|
return json_decode($res, true);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function delCache(): void
|
|
{
|
|
$this->redisCache->delete($this->ylyPrinterKey);
|
|
$this->redisCache->delete($this->jdPrinterKey);
|
|
}
|
|
} |