diff --git a/app/Cache/Redis/Admin/DriverCache.php b/app/Cache/Redis/Admin/DriverCache.php new file mode 100644 index 0000000..95a4c7e --- /dev/null +++ b/app/Cache/Redis/Admin/DriverCache.php @@ -0,0 +1,28 @@ +redis->set($siteKey,json_encode(array_column($siteList, null, 'id'), JSON_UNESCAPED_UNICODE),RedisCode::SYSTEM_DB); } +// /** +// * @param int $siteId +// * @return array|null +// * @throws ContainerExceptionInterface +// * @throws NotFoundExceptionInterface +// */ +// public function getSiteInfo(int $siteId): array|null +// { +// $this->setSiteRedis(); +// +// $siteKey = CommonRedisKey::siteListKey(); +// +// $siteListJsonDecode = json_decode($this->redis->get($siteKey,RedisCode::SYSTEM_DB), true); +// if (empty($siteListJsonDecode)) return null; +// +// $siteList = array_column($siteListJsonDecode, null, 'id'); +// +// unset($siteListJsonDecode); +// return $siteList[$siteId] ?? null; +// } + + private function setSiteInfo(int $siteId): void + { + $siteKey = CommonRedisKey::siteInfoKey($siteId); + + $info = $this->siteModel->getInfoById($siteId); + + if (empty($info)) return; + + if ($info->status == SiteCode::SITE_DISABLE || $info->is_del == SiteCode::SITE_DEL) return; + + $this->redis->hMset($siteKey, [ + 'id' => $info->id, + 'name' => $info->name, + 'kitchen_id' => $info->kitchen_id, + 'city_id' => $info->city_id, + 'address' => $info->address, + 'lng' => $info->lng, + 'lat' => $info->lat, + 'url' => $this->getOssObjectById($info->image_id) + ]); + } + /** * @param int $siteId - * @return array|null + * @return array|false|\Redis|null * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function getSiteInfo(int $siteId): array|null + public function getSiteInfo(int $siteId): false|array|\Redis|null { - $this->setSiteRedis(); + $siteKey = CommonRedisKey::siteInfoKey($siteId); - $siteKey = CommonRedisKey::siteListKey(); + if (!$this->redis->exists($siteKey)) $this->setSiteInfo($siteId); - $siteListJsonDecode = json_decode($this->redis->get($siteKey,RedisCode::SYSTEM_DB), true); - if (empty($siteListJsonDecode)) return null; - - $siteList = array_column($siteListJsonDecode, null, 'id'); - - unset($siteListJsonDecode); - return $siteList[$siteId] ?? null; + return $this->redis->hGetAll($siteKey) ?? null; } + + /** + * @param int $siteId + * @param string $key + * @param string $value + * @return bool|\Redis + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function updateSiteInfo(int $siteId,string $key,string $value) + { + $siteKey = CommonRedisKey::siteInfoKey($siteId); + + if (!$this->redis->exists($siteKey)) $this->setSiteInfo($siteId); + + return $this->redis->hMset($siteKey,[ + $key => $value + ]) ?? false; + } + + /** + * @param int $siteId + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function updateBatchValueInfo(int $siteId) + { + $siteKey = CommonRedisKey::siteInfoKey($siteId); + + if ($this->redis->exists($siteKey)) $this->delSiteInfo($siteId); + + $this->setSiteInfo($siteId); + } + + /** + * @param int $siteId + * @return bool|int + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function delSiteInfo(int $siteId) + { + $siteKey = CommonRedisKey::siteInfoKey($siteId); + + if (!$this->redis->exists($siteKey)) return true; + + return $this->redis->delete($siteKey); + } + } \ No newline at end of file diff --git a/app/Cache/Redis/Common/CommonRedisKey.php b/app/Cache/Redis/Common/CommonRedisKey.php index 26fc0a0..d988ee3 100644 --- a/app/Cache/Redis/Common/CommonRedisKey.php +++ b/app/Cache/Redis/Common/CommonRedisKey.php @@ -95,6 +95,15 @@ class CommonRedisKey return '__system:site:list'; } + /** + * @param int $siteId + * @return string + */ + public static function siteInfoKey(int $siteId): string + { + return '__system:site:info:id:'.$siteId; + } + /** * @return string */ diff --git a/app/Constants/Common/RoleCode.php b/app/Constants/Common/RoleCode.php index 7fb8ab7..62d5bca 100644 --- a/app/Constants/Common/RoleCode.php +++ b/app/Constants/Common/RoleCode.php @@ -5,14 +5,16 @@ namespace App\Constants\Common; class RoleCode { /** - * @var int 角色 1=超级管理员 2=管理员 3=财务 5=客服 6=市场部 7=厨师 8=司机 + * @var int 角色 1=超级管理员 2=管理员 3=财务 5=客服 6=市场部 7=厨师 8=司机 9=仓管 10=套餐餐配员 11=自选餐配员 */ - const int SUPER_ADMIN = 1; - const int ADMIN = 2; - const int FINANCE = 3; - const int CUSTOMER_SERVICE = 5; - const int MARKETPLACE = 6; - const int CHEF = 7; - const int DRIVER = 8; - const int WAREHOUSE = 9; + CONST INT SUPER_ADMIN = 1; + CONST INT ADMIN = 2; + CONST INT FINANCE = 3; + CONST INT CUSTOMER_SERVICE = 5; + CONST INT MARKETPLACE = 6; + CONST INT CHEF = 7; + CONST INT DRIVER = 8; + CONST INT WAREHOUSE = 9; + CONST INT MEAL_CATERING = 10; + CONST INT OPTION_CATERING = 11; } \ No newline at end of file diff --git a/app/Controller/Admin/MealCateringController.php b/app/Controller/Admin/MealCateringController.php new file mode 100644 index 0000000..b59da93 --- /dev/null +++ b/app/Controller/Admin/MealCateringController.php @@ -0,0 +1,36 @@ +handle(); + } + + #[RequestMapping(path: "option/cycle_list", methods: "GET")] + #[Scene(scene: "option_cycle_list")] + public function getOptionCycleList() + { + return(new OptionCycleListService)->handle(); + } + + +} diff --git a/app/Model/AdminUser.php b/app/Model/AdminUser.php index b508c5a..82a6509 100644 --- a/app/Model/AdminUser.php +++ b/app/Model/AdminUser.php @@ -69,9 +69,9 @@ class AdminUser extends Model /** * @param int $id - * @return \Hyperf\Database\Model\Model|Builder|null + * @return \Hyperf\Database\Model\Model|null */ - public function getAdminInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + public function getAdminInfoById(int $id): \Hyperf\Database\Model\Model|null { return $this->where('id', $id)->where('is_del',UserCode::IS_NO_DEL)->first(); } diff --git a/app/Model/DriverSequence.php b/app/Model/DriverSequence.php index 1da97e4..23ce6d9 100644 --- a/app/Model/DriverSequence.php +++ b/app/Model/DriverSequence.php @@ -9,7 +9,8 @@ use Hyperf\DbConnection\Model\Model; /** * @property int $id - * @property int $driver_id + * @property int $driver_id + * @property string $driver_name * @property int $driver_num * @property int $sequence * @property string $create_time diff --git a/app/Service/Admin/Catering/CateringBaseService.php b/app/Service/Admin/Catering/CateringBaseService.php new file mode 100644 index 0000000..277358d --- /dev/null +++ b/app/Service/Admin/Catering/CateringBaseService.php @@ -0,0 +1,191 @@ +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; + } +} \ No newline at end of file diff --git a/app/Service/Admin/Catering/Meal/CycleListService.php b/app/Service/Admin/Catering/Meal/CycleListService.php new file mode 100644 index 0000000..980102e --- /dev/null +++ b/app/Service/Admin/Catering/Meal/CycleListService.php @@ -0,0 +1,59 @@ +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)]); + } +} \ No newline at end of file diff --git a/app/Service/Admin/Catering/Option/CycleListService.php b/app/Service/Admin/Catering/Option/CycleListService.php new file mode 100644 index 0000000..eb8922f --- /dev/null +++ b/app/Service/Admin/Catering/Option/CycleListService.php @@ -0,0 +1,46 @@ +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)]); + } +} \ No newline at end of file diff --git a/app/Service/Admin/User/EmployeeService.php b/app/Service/Admin/User/EmployeeService.php index 683008f..3011cbf 100644 --- a/app/Service/Admin/User/EmployeeService.php +++ b/app/Service/Admin/User/EmployeeService.php @@ -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, }; diff --git a/app/Service/ServiceTrait/Admin/GetUserInfoTrait.php b/app/Service/ServiceTrait/Admin/GetUserInfoTrait.php index aba367c..e5b0daf 100644 --- a/app/Service/ServiceTrait/Admin/GetUserInfoTrait.php +++ b/app/Service/ServiceTrait/Admin/GetUserInfoTrait.php @@ -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)) { diff --git a/app/Service/ServiceTrait/Admin/RoleMembersTrait.php b/app/Service/ServiceTrait/Admin/RoleMembersTrait.php new file mode 100644 index 0000000..9d2e06b --- /dev/null +++ b/app/Service/ServiceTrait/Admin/RoleMembersTrait.php @@ -0,0 +1,96 @@ +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(); + } + +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/Common/CycleTrait.php b/app/Service/ServiceTrait/Common/CycleTrait.php index 4bb8de3..651f651 100644 --- a/app/Service/ServiceTrait/Common/CycleTrait.php +++ b/app/Service/ServiceTrait/Common/CycleTrait.php @@ -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);