diff --git a/app/Cache/Redis/Admin/AdminRedisKey.php b/app/Cache/Redis/Admin/AdminRedisKey.php index e092387..782817d 100644 --- a/app/Cache/Redis/Admin/AdminRedisKey.php +++ b/app/Cache/Redis/Admin/AdminRedisKey.php @@ -94,4 +94,20 @@ class AdminRedisKey { return 'catering:option:is:cycle_id:'.$cycleId; } + + /** + * @return string + */ + public static function ylyPrinterList(): string + { + return '__system:yly:printer:list'; + } + + /** + * @return string + */ + public static function jdPrinterList(): string + { + return '__system:jd:printer:list'; + } } \ No newline at end of file diff --git a/app/Cache/Redis/Admin/PrinterCache.php b/app/Cache/Redis/Admin/PrinterCache.php new file mode 100644 index 0000000..806c283 --- /dev/null +++ b/app/Cache/Redis/Admin/PrinterCache.php @@ -0,0 +1,112 @@ +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); + } +} \ No newline at end of file diff --git a/app/Controller/Admin/CateringController.php b/app/Controller/Admin/CateringController.php index 811fc5b..22b26c3 100644 --- a/app/Controller/Admin/CateringController.php +++ b/app/Controller/Admin/CateringController.php @@ -6,6 +6,8 @@ namespace App\Controller\Admin; use App\Middleware\Admin\JwtAuthMiddleware; use App\Service\Admin\Catering\Meal\CycleListService as MealCycleListService; +use App\Service\Admin\Catering\Option\CompleteListService; +use App\Service\Admin\Catering\Option\CompletePrintService; use App\Service\Admin\Catering\Option\CycleListService as OptionCycleListService; use App\Service\Admin\Catering\Option\CateringService as OptionCateringService; use App\Service\Admin\Catering\Option\PrintService; @@ -61,6 +63,7 @@ class CateringController } /** + * 自选配餐 * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -72,19 +75,55 @@ class CateringController return (new OptionCateringService)->handle(); } + /** + * 自选是否打印 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ #[RequestMapping(path: "option/check_is_print", methods: "POST")] #[Scene(scene: "option_check_is_print")] public function optionCheckPrint() { - return (new PrintService)->check(); + return (new PrintService)->checkPrint(); } - #[RequestMapping(path: "option/catering", methods: "GET")] - #[Scene(scene: "option_catering")] + /** + * 打印机/喷码机列表 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: "option/print_list", methods: "GET")] + #[Scene(scene: "option_print_list")] public function optionPrintList() { return (new PrintService)->handle(); } + /** + * 自选补打印列表 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: "option/complete_list", methods: "GET")] + #[Scene(scene: "option_complete_list")] + public function completeList() + { + return (new CompleteListService)->handle(); + } + /** + * 自选补打印 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: "option/complete_print", methods: "GET")] + #[Scene(scene: "option_complete_print")] + public function completePrint() + { + return (new CompletePrintService)->handle(); + } } diff --git a/app/Model/Printer.php b/app/Model/Printer.php new file mode 100644 index 0000000..7c08fab --- /dev/null +++ b/app/Model/Printer.php @@ -0,0 +1,38 @@ + 'integer', 'type' => 'integer']; + + const CREATED_AT = 'create_time'; + const UPDATED_AT = 'update_time'; +} diff --git a/app/Service/Admin/Catering/Option/CateringService.php b/app/Service/Admin/Catering/Option/CateringService.php index c00ec31..63fa570 100644 --- a/app/Service/Admin/Catering/Option/CateringService.php +++ b/app/Service/Admin/Catering/Option/CateringService.php @@ -502,6 +502,7 @@ class CateringService extends CateringBaseService // 先加当前数量 $printBoxNum++; + //todo 数据加载做一下 $service->data = [ 'pickup_code' => $copiesItem['pickup_code'], 'driver_num' => '', diff --git a/app/Service/Admin/Catering/Option/CompleteListService.php b/app/Service/Admin/Catering/Option/CompleteListService.php new file mode 100644 index 0000000..48cdded --- /dev/null +++ b/app/Service/Admin/Catering/Option/CompleteListService.php @@ -0,0 +1,96 @@ +request->input('id'); + + // 获取数据源 + $this->logInfo = $this->orderOptionCateringLogModel->find($id); + if (empty($this->logInfo)) throw new ErrException('配餐记录不存在'); + + $this->__initRedisKey(); + + if (!$this->isBuildPickupCode() || !$this->isCateringByCache() || !$this->checkIsPrint()) throw new ErrException('先执行整线路配餐后才可以获取已配餐信息'); + + if ($this->logInfo->quantity <= 0) throw new ErrException('该点无任何配餐数据'); + + $siteInfo = $this->siteModel->getInfoById($this->logInfo->site_id); + if (empty($siteInfo)) throw new ErrException('配餐地址不存在,请确认信息'); + + $driverInfo = $this->driverSequenceModel->getInfoByDriverId($siteInfo->delivered_id); + if (empty($driverInfo)) throw new ErrException('线路司机信息不存在,请确认信息'); + + $orderIds = $this->orderModel + ->where('site_id', $siteInfo->id) + ->where('cycle_id', $this->cycleId) + ->where('type',OrderCode::ORDER_TYPE_OPTIONAL) + ->where('status',OrderCode::PAYED) + ->orderBy('id') + ->pluck('id') + ->toArray(); + if (empty($orderIds)) throw new ErrException('该点无任何配餐数据'); + + $pickUpCodeList = $this->pickupCodeModel->whereIn('order_id',$orderIds)->select(['pickup_code','order_id','copies','id'])->get(); + if ($pickUpCodeList->isEmpty()) throw new ErrException('该点未生成取餐码'); + + return $this->return->success('success', [ + 'list' => $pickUpCodeList->toArray(), + 'site_name' => $siteInfo->name, + 'driver_num' => $driverInfo->driver_num.'-'.$siteInfo->sequence + ]); + } +} \ No newline at end of file diff --git a/app/Service/Admin/Catering/Option/CompletePrintService.php b/app/Service/Admin/Catering/Option/CompletePrintService.php new file mode 100644 index 0000000..40422a4 --- /dev/null +++ b/app/Service/Admin/Catering/Option/CompletePrintService.php @@ -0,0 +1,147 @@ +pickupCodeModel->find($this->request->input('id')); + if (empty($pickupCodeInfo)) throw new ErrException('取餐数据不存在'); + + $orderInfo = $this->orderModel->find($pickupCodeInfo->order_id); + if (empty($this->orderInfo)) throw new ErrException('订单数据不存在'); + + $skuArr = $this->orderGoodModel->where('order_id',$pickupCodeInfo->order_id)->where('copies',$pickupCodeInfo->copies)->pluck('quantity','sku_id')->toArray(); + if (empty($skuArr)) throw new ErrException('订单商品数据不存在'); + $skuIds = array_keys($skuArr); + + $skuInfo = $this->skuModel->whereIn('id',$skuIds)->pluck('code_number','id'); + $skuCopies = []; + foreach ($skuArr as $skuId => $quantity) { + $skuCopies[] = [ + 'code_number' => $skuInfo[$skuId], + 'quantity' => $quantity, + ]; + } + + + $siteInfo = $this->siteModel->find($orderInfo->site_id); + + $userInfo = $this->userModel->find($orderInfo->user_id); + + $driverInfo = $this->driverSequenceModel->find($siteInfo->delivered_id); + + $this->printService = $this->printOrderFactory->handle($this->request->input('type')); + // 打印或者喷码 + match ($this->request->input('type')) { + CateringCode::OPTION_PRINT_YLY => $this->printOrderByYly(), + CateringCode::OPTION_PRINT_CODING => $this->printOrderByCoding(), + }; + + $this->printService->data = [ + 'pickup_code' => $pickupCodeInfo->pickup_code, + 'driver_num' => $driverInfo->driver_num, + 'site_order' => $siteInfo->sequence, + 'site_text' => $siteInfo->name, + 'order_sno' => $orderInfo->order_sno, + 'username' => $userInfo->nickname, + 'date' => date('Y-m-d'), + 'mobile' => $userInfo->mobile, + 'heapsort' => $pickupCodeInfo->heapsort, + 'sku' => $skuCopies + ]; + + $res = $this->printService->printSingleOrder(); + + return $this->return->success('success', ['data' => $res]); + } + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + private function printOrderByYly(): void + { + $this->printService->printId = (int)$this->request->input('print_id'); + $this->printService->handle(); + } + + /** + * @return void + */ + private function printOrderByCoding() {} +} \ No newline at end of file diff --git a/app/Service/Admin/Catering/Option/PrintService.php b/app/Service/Admin/Catering/Option/PrintService.php index 059e119..8fc2e89 100644 --- a/app/Service/Admin/Catering/Option/PrintService.php +++ b/app/Service/Admin/Catering/Option/PrintService.php @@ -10,17 +10,71 @@ declare(strict_types=1); namespace App\Service\Admin\Catering\Option; -use App\Service\Admin\BaseService; +use App\Cache\Redis\Admin\PrinterCache; +use App\Constants\Admin\CateringCode; +use App\Exception\ErrException; +use App\Model\OrderOptionCateringLog; +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 PrintService extends BaseService +class PrintService extends CateringBaseService { - public function handle() - { + /** + * @var PrinterCache + */ + #[Inject] + protected PrinterCache $printerCache; + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function handle(): array + { + $type = (int)$this->request->input('type'); + $res = match ($type) { + CateringCode::OPTION_PRINT_YLY => $this->printerCache->getYlyPrinter(), + CateringCode::OPTION_PRINT_CODING => $this->printerCache->getOtherPrinter(), + default => throw new ErrException('参数错误') + }; + + return $this->return->success('success', ['list' => $res]); } - public function check() - { + use PrintTrait; + /** + * @var OrderOptionCateringLog + */ + #[Inject] + protected OrderOptionCateringLog $orderOptionCateringLogModel; + + /** + * @var OrderOptionCateringLog + */ + protected OrderOptionCateringLog $logInfo; + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function checkPrint(): array + { + $id = (int)$this->request->input('id'); + + // 获取数据源 + $this->logInfo = $this->orderOptionCateringLogModel->find($id); + if (empty($this->logInfo)) throw new ErrException('配餐记录不存在'); + + $this->__initRedisKey(); + + $res = $this->checkIsPrint(); + + return $this->return->success('success', ['status' => $res]); } } \ No newline at end of file diff --git a/app/Service/Admin/Catering/Print/YlyPrintService.php b/app/Service/Admin/Catering/Print/YlyPrintService.php index f226553..fb6f1b9 100644 --- a/app/Service/Admin/Catering/Print/YlyPrintService.php +++ b/app/Service/Admin/Catering/Print/YlyPrintService.php @@ -12,6 +12,7 @@ namespace App\Service\Admin\Catering\Print; use App\Exception\ErrException; use App\Lib\Print\YlyBasicsLib; +use App\Model\Printer; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -45,13 +46,18 @@ class YlyPrintService implements PrintOrderInterface */ private string $content; - /** * @var YlyBasicsLib */ #[Inject] protected YlyBasicsLib $ylyBasicsLib; + /** + * @var Printer + */ + #[Inject] + protected Printer $printerModel; + /** * @return void * @throws ContainerExceptionInterface @@ -60,7 +66,7 @@ class YlyPrintService implements PrintOrderInterface public function handle(): void { //todo 加入打印机验证 orderOptionCateringLogModel - $printInfo = $this->orderOptionCateringLogModel->where('id',$this->printId)->first(); + $printInfo = $this->printerModel->where('id',$this->printId)->first(); if (empty($printInfo)) throw new ErrException('打印机不存在-1'); diff --git a/app/Service/ServiceTrait/Admin/Catering/PrintTrait.php b/app/Service/ServiceTrait/Admin/Catering/PrintTrait.php index 4546898..2c3a575 100644 --- a/app/Service/ServiceTrait/Admin/Catering/PrintTrait.php +++ b/app/Service/ServiceTrait/Admin/Catering/PrintTrait.php @@ -82,6 +82,16 @@ trait PrintTrait $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