feat : catering

This commit is contained in:
2025-03-10 16:52:23 +08:00
parent c21fc1ef90
commit 2e67b921bd
14 changed files with 598 additions and 54 deletions

View File

@@ -0,0 +1,191 @@
<?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\Constants\Common\RoleCode;
use App\Exception\ErrException;
use App\Model\AdminUser;
use App\Model\DriverSequence;
use App\Model\Site;
use App\Service\Admin\BaseService;
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,
CycleTrait;
/**
* @var int
*/
protected int $cycleId;
/**
* @var int
*/
protected int $kitchenId;
/**
* @var Site
*/
#[Inject]
protected Site $siteModel;
/**
* @var SiteCache
*/
#[Inject]
protected SiteCache $siteCache;
/**
* @var DriverSequence
*/
#[Inject]
protected DriverSequence $driverSequenceModel;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct()
{
parent::__construct();
$this->checkRole();
$this->getCycleId();
}
/**
* @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('不存在该路由功能')
};
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getCycleId(): void
{
$cycleId = $this->initTodayCycleId();
if (empty($cycleId)) throw new ErrException('当日无数据');
$this->cycleId = (int)$cycleId;
}
/**
* @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']]['quantity'],
'id' => $cycleCateringLogs[$oneSite['id']]['id'],
// 'is_print' => (int)(RedisCache::sIsMember($key, $childVal['id'].':'.($one['distribution_floor_id'] ?? 0)))
];
}
}
return $res;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Catering\Meal;
use App\Exception\ErrException;
use App\Model\DriverSequence;
use App\Model\OrderMealCateringLog;
use App\Model\Site;
use App\Model\Sku;
use App\Service\Admin\Catering\CateringBaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CycleListService extends CateringBaseService
{
/**
* @var OrderMealCateringLog $orderMealCateringLogModel
*/
#[Inject]
protected OrderMealCateringLog $orderMealCateringLogModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$skuInfo = $this->skuModel->getInfoById((int)$this->request->input('sku_id'));
if (empty($skuInfo)) throw new ErrException('该套餐不存在,请刷新后重试');
$cycleCateringLogs = $this->orderMealCateringLogModel
->where('cycle_id',$this->cycleId)
->where('sku_id',$skuInfo->id)
->select(['site_id','quantity','status','id'])
->get();
$res = $this->buildResArr($cycleCateringLogs);
if (empty($res)) return $this->return->success('success',['list' => []]);
return $this->return->success('success',['list' => array_values($res)]);
}
}

View File

@@ -0,0 +1,46 @@
<?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\Model\OrderMealCateringLog;
use App\Model\OrderOptionCateringLog;
use App\Service\Admin\Catering\CateringBaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CycleListService extends CateringBaseService
{
/**
* @var OrderOptionCateringLog
*/
#[Inject]
protected OrderOptionCateringLog $orderOptionCateringLogModel;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$cycleCateringLogs = $this->orderOptionCateringLogModel
->where('cycle_id',$this->cycleId)
->select(['site_id','quantity','status','id'])
->get();
$res = $this->buildResArr($cycleCateringLogs);
if (empty($res)) return $this->return->success('success',['list' => []]);
return $this->return->success('success',['list' => array_values($res)]);
}
}

View File

@@ -21,12 +21,15 @@ use App\Model\Chef;
use App\Model\DriverSequence;
use App\Model\WarehouseKeeper;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Admin\RoleMembersTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
use function Hyperf\Config\config;
class EmployeeService extends BaseService
{
use RoleMembersTrait;
/**
* 注入用户模型
* @var AdminUser $adminUserModel
@@ -107,12 +110,8 @@ class EmployeeService extends BaseService
//写入关联表
$res = match ($model->role_id) {
RoleCode::DRIVER =>
(new DriverSequence)->insert([
'driver_id' => $model->id,
]),
RoleCode::CHEF =>
$this->addChef($model->id),
RoleCode::DRIVER => $this->addDriver($model->id,$model->chinese_name),
RoleCode::CHEF => $this->addChef($model->id),
RoleCode::WAREHOUSE => $this->addWarehouseKeeper($model->id),
default => true,
};
@@ -122,18 +121,6 @@ class EmployeeService extends BaseService
return $this->return->success();
}
public function addChef($id): bool
{
$chef = new Chef();
$chef->user_id = $id;
return $chef->save();
}
public function addWarehouseKeeper($id): bool
{
$warehouseKeeper = new WarehouseKeeper();
$warehouseKeeper->user_id = $id;
return $warehouseKeeper->save();
}
/**
* 修改
@@ -158,16 +145,14 @@ class EmployeeService extends BaseService
if ($info->role_id != $roleId) {
//写入关联表
$del = match ($info->role_id) {
RoleCode::DRIVER => (new DriverSequence)->where('driver_id', $info->id)->delete(),
RoleCode::CHEF => (new Chef)->where('user_id', $info->id)->delete(),
RoleCode::WAREHOUSE => (new WarehouseKeeper)->where('user_id', $info->id)->delete(),
RoleCode::DRIVER => $this->delDriver($info->id),
RoleCode::CHEF => $this->delChef($info->id),
RoleCode::WAREHOUSE => $this->delWarehouseKeeper($info->id),
default => true,
};
$add = match ($roleId) {
RoleCode::DRIVER => (new DriverSequence)->insert([
'driver_id' => $info->id,
]),
RoleCode::DRIVER => $this->addDriver($info->id,$info->chinese_name),
RoleCode::CHEF => $this->addChef($info->id),
RoleCode::WAREHOUSE => $this->addWarehouseKeeper($info->id),
default => true,
@@ -207,9 +192,9 @@ class EmployeeService extends BaseService
$info->is_del = UserCode::IS_DEL;
$del = match ($info->role_id) {
RoleCode::DRIVER => (new DriverSequence)->where('driver_id', $info->id)->update(['is_del' => UserCode::IS_DEL]),
RoleCode::CHEF => (new Chef)->where('user_id', $info->id)->update(['is_del' => UserCode::IS_DEL]),
RoleCode::WAREHOUSE => (new WarehouseKeeper)->where('user_id', $info->id)->update(['is_del' => UserCode::IS_DEL]),
RoleCode::DRIVER => $this->delDriver($info->id),
RoleCode::CHEF => $this->delChef($info->id),
RoleCode::WAREHOUSE => $this->delWarehouseKeeper($info->id),
default => true,
};

View File

@@ -29,9 +29,9 @@ trait GetUserInfoTrait
/**
* 单例获取用户信息
* @param $adminId
* @return false|Builder|Model|mixed|string
* @return AdminUser|false|Model|null
*/
protected function getUserInfo($adminId): mixed
protected function getUserInfo($adminId): AdminUser|Model|false|null
{
$key = 'admin_id:' . $adminId;
if (Context::has($key)) {

View File

@@ -0,0 +1,96 @@
<?php
namespace App\Service\ServiceTrait\Admin;
use App\Model\Chef;
use App\Model\DriverSequence;
use App\Model\WarehouseKeeper;
use Hyperf\Di\Annotation\Inject;
trait RoleMembersTrait
{
/**
* @var DriverSequence
*/
#[Inject]
protected DriverSequence $driverSequenceModel;
/**
* @var Chef
*/
#[Inject]
protected Chef $chefModel;
/**
* @var WarehouseKeeper
*/
#[Inject]
protected WarehouseKeeper $warehouseKeeperModel;
/**
* @param int $id
* @param string $name
* @return bool
*/
protected function addDriver(int $id, string $name): bool
{
$insertModel = new DriverSequence();
$insertModel->driver_id = $id;
$insertModel->driver_name = $name;
return $insertModel->save();
}
/**
* 添加厨师
* @param $id
* @return bool
*/
protected function addChef($id): bool
{
$chef = new Chef();
$chef->user_id = $id;
return $chef->save();
}
/**
* 添加仓管
* @param $id
* @return bool
*/
protected function addWarehouseKeeper($id): bool
{
$warehouseKeeper = new WarehouseKeeper();
$warehouseKeeper->user_id = $id;
return $warehouseKeeper->save();
}
/**
* @param int $id
* @return bool
*/
protected function delDriver(int $id): bool
{
return $this->driverSequenceModel->where('driver_id', $id)->delete();
}
/**
* 添加厨师
* @param $id
* @return bool
*/
protected function delChef($id): bool
{
return $this->chefModel->where('user_id', $id)->delete();
}
/**
* 添加仓管
* @param $id
* @return bool
*/
protected function delWarehouseKeeper($id): bool
{
return $this->warehouseKeeperModel->where('user_id', $id)->delete();
}
}

View File

@@ -33,11 +33,11 @@ trait CycleTrait
protected ConfigCache $configCache;
/**
* @return bool|float|Redis
* @return float|bool|Redis|int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function initTodayCycleId(): float|bool|Redis
protected function initTodayCycleId(): float|bool|Redis|int
{
$TodayCutOffTime = $this->configCache->getConfigValueByKey(ConfigCode::TODAY_CUT_OFF_TIME_KEY);