244 lines
6.8 KiB
PHP
244 lines
6.8 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;
|
|
|
|
use App\Cache\Redis\Api\SiteCache;
|
|
use App\Cache\Redis\Common\ConfigCache;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Constants\Common\RoleCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AdminUser;
|
|
use App\Model\Caterer;
|
|
use App\Model\DriverSequence;
|
|
use App\Model\Site;
|
|
use App\Model\Sku;
|
|
use App\Service\Admin\BaseService;
|
|
use App\Service\ServiceTrait\Admin\Catering\PrintTrait;
|
|
use App\Service\ServiceTrait\Admin\GetUserInfoTrait;
|
|
use App\Service\ServiceTrait\Common\CycleTrait;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
abstract class CateringBaseService extends BaseService
|
|
{
|
|
use GetUserInfoTrait;
|
|
use CycleTrait;
|
|
use PrintTrait;
|
|
|
|
/**
|
|
* @var int 1=自选 2=套餐
|
|
*/
|
|
protected int $type;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
protected int $cycleId;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
protected int $kitchenId;
|
|
|
|
/**
|
|
* @var Site
|
|
*/
|
|
#[Inject]
|
|
protected Site $siteModel;
|
|
|
|
/**
|
|
* @var SiteCache
|
|
*/
|
|
#[Inject]
|
|
protected SiteCache $siteCache;
|
|
|
|
/**
|
|
* @var ConfigCache
|
|
*/
|
|
#[Inject]
|
|
protected ConfigCache $configCache;
|
|
|
|
/**
|
|
* @var Sku
|
|
*/
|
|
#[Inject]
|
|
protected Sku $skuModel;
|
|
|
|
/**
|
|
* @var DriverSequence
|
|
*/
|
|
#[Inject]
|
|
protected DriverSequence $driverSequenceModel;
|
|
|
|
/**
|
|
* @var AdminUser
|
|
*/
|
|
#[Inject]
|
|
protected AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* @var Caterer
|
|
*/
|
|
#[Inject]
|
|
protected Caterer $catererModel;
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->checkRole();
|
|
|
|
$this->getCycleId();
|
|
|
|
$this->getKitchenId();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function checkRole(): void
|
|
{
|
|
$urlArr = explode('/',$this->request->path());
|
|
|
|
match ($urlArr[2]) {
|
|
'meal' => $this->roleId != RoleCode::MEAL_CATERING ? throw new ErrException('您没有权限访问该功能') : null,
|
|
'option' => $this->roleId != RoleCode::OPTION_CATERING ? throw new ErrException('您没有权限访问该功能') : null,
|
|
default => throw new ErrException('不存在该路由功能')
|
|
};
|
|
|
|
$this->type = match ($urlArr[2]) {
|
|
'meal' => OrderCode::ORDER_TYPE_OPTIONAL,
|
|
'option' => OrderCode::ORDER_TYPE_MEAL,
|
|
default => throw new ErrException('不存在该路由功能'),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function getCycleId(): void
|
|
{
|
|
$cycleId = $this->initTodayCycleId();
|
|
if (empty($cycleId)) throw new ErrException('当日无数据');
|
|
|
|
$this->cycleId = (int)$cycleId;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function getKitchenId(): void
|
|
{
|
|
$caterInfo = $this->catererModel->where('user_id',$this->adminId)->first();
|
|
if (empty($caterInfo) || empty($caterInfo->kitchen_id)) throw new ErrException('账号异常/该账号没有绑定厨房');
|
|
$this->kitchenId = $caterInfo->kitchen_id;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function buildDriverArr(array $data): array
|
|
{
|
|
// 核心算法
|
|
$groups = [];
|
|
$order = [];
|
|
|
|
foreach ($data as $item) {
|
|
$driverId = $item['delivered_id'];
|
|
|
|
// 构造条目数据单元
|
|
$entry = [
|
|
'id' => $item['id'],
|
|
'sequence' => $item['sequence']
|
|
];
|
|
|
|
// 记录首次出现的 driver_id 顺序
|
|
if (!isset($groups[$driverId])) {
|
|
$groups[$driverId] = [];
|
|
$order[] = $driverId;
|
|
}
|
|
|
|
// 追加条目到分组
|
|
$groups[$driverId][] = $entry;
|
|
}
|
|
|
|
// 构建最终结构
|
|
$result = [];
|
|
foreach ($order as $driverId) {
|
|
$result[$driverId] = $groups[$driverId];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @param $cycleCateringLogs
|
|
* @return null|array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
protected function buildResArr($cycleCateringLogs): null|array
|
|
{
|
|
if (empty($cycleCateringLogs)) return $this->return->success('success',['list' => []]);
|
|
$cycleCateringLogs = array_column($cycleCateringLogs->toArray(),null,'site_id');
|
|
|
|
$siteIds = array_unique(array_column($cycleCateringLogs,'site_id'));
|
|
|
|
$siteSequences = $this->siteModel->whereIn('id',$siteIds)->orderBy('sequence')->select('sequence','delivered_id','id')->get();
|
|
if ($siteSequences->isEmpty()) return null;
|
|
|
|
$driverSequences = $this
|
|
->driverSequenceModel
|
|
->whereIn('driver_id',array_unique(array_column($siteSequences->toArray(),'delivered_id')))
|
|
->orderBy('sequence')
|
|
->select('sequence','driver_name','driver_num','driver_id')
|
|
->get();
|
|
if ($driverSequences->isEmpty()) return null;
|
|
$siteSequences = $this->buildDriverArr($siteSequences->toArray());
|
|
|
|
$res = [];
|
|
foreach ($driverSequences->toArray() as $driver) {
|
|
if (empty($res[$driver['driver_id']])) {
|
|
$res[$driver['driver_id']] = [
|
|
'name' => $driver['driver_name'].'['.$driver['driver_num'].']',
|
|
'sequence' => $driver['sequence'],
|
|
'records' => [],
|
|
];
|
|
}
|
|
|
|
if (empty($siteSequences[$driver['driver_id']])) continue;
|
|
|
|
foreach ($siteSequences[$driver['driver_id']] as $oneSite) {
|
|
$oneSiteInfo = $this->siteCache->getSiteInfo((int)$oneSite['id']);
|
|
|
|
$res[$driver['driver_id']]['records'][] = [
|
|
'site_id' => $oneSite['id'],
|
|
'distribution_site_name' => !empty($oneSiteInfo['name']) ? '['.$driver['driver_num'].'-'.$oneSite['sequence'].']'.$oneSiteInfo['name'] : '',
|
|
'site_order' => $oneSite['sequence'],
|
|
'order_quantity' => $cycleCateringLogs[$oneSite['id']]['quantity'],
|
|
'status' => $cycleCateringLogs[$oneSite['id']]['status'],
|
|
'id' => $cycleCateringLogs[$oneSite['id']]['id'],
|
|
'is_print' => $this->type == OrderCode::ORDER_TYPE_OPTIONAL ? (int)$this->checkIsPrintByList((int)$cycleCateringLogs[$oneSite['id']]['id'],$this->cycleId) : 0
|
|
];
|
|
}
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
} |