Files
hyperf_service/app/Service/Admin/Catering/Option/CompletePrintService.php
2025-04-02 11:21:43 +08:00

151 lines
4.5 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Catering\Option;
use App\Constants\Admin\CateringCode;
use App\Exception\ErrException;
use App\Model\Order;
use App\Model\OrderGood;
use App\Model\PickupCode;
use App\Model\User;
use App\Service\Admin\Catering\CateringBaseService;
use App\Service\Admin\Catering\Print\JdPrintService;
use App\Service\Admin\Catering\Print\PrintOrderFactory;
use App\Service\Admin\Catering\Print\YlyPrintService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CompletePrintService extends CateringBaseService
{
/**
* @var PickupCode
*/
#[Inject]
protected PickupCode $pickupCodeModel;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var PrintOrderFactory
*/
#[Inject]
protected PrintOrderFactory $printOrderFactory;
//
// /**
// * @var PickupCode
// */
// protected PickupCode $pickupCodeInfo;
//
// /**
// * @var Order
// */
// protected Order $orderInfo;
/**
* @var User
*/
#[Inject]
protected User $userModel;
/**
* @var YlyPrintService|JdPrintService
*/
protected YlyPrintService|JdPrintService $printService;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$pickupCodeInfo = $this->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);
if ($siteInfo->sequence <= 0) throw new ErrException('请先配置点位顺序');
$userInfo = $this->userModel->find($orderInfo->user_id);
$driverInfo = $this->driverSequenceModel->find($siteInfo->delivered_id);
if (empty($driverInfo)) throw new ErrException('配送员信息丢失,请联系管理员');
if ($driverInfo->driver_num <= 0) throw new ErrException('配送员编号丢失,请联系管理员');
if (empty($driverInfo->driver_name)) throw new ErrException('配送员姓名丢失,请联系管理员');
$this->printService = $this->printOrderFactory->handle((int)$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() {}
}