香洲二院小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mini_xzey/app/Http/Resources/Registration/Schedule/DeptListsResource.php

94 lines
3.1 KiB

<?php
namespace App\Http\Resources\Registration\Schedule;
use App\Models\Admin\Department;
use App\Models\Admin\DepartmentCategory;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Redis;
class DeptListsResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request = null): array
{
// 科室分类
$categories = $this->getDepartmentCategoryLists();
// 科室及排序
$departments = $this->getDepartmentLists();
foreach ($departments as $v) {
// 检查 departments 中是否存在当前 typeId
if (!isset($departments[$v['dept_id']])) {
continue;
}
$department = $departments[$v['dept_id']];
// 检查 category_id 是否设置且在 categories 中存在
if (!isset($department['category_id']) || !isset($categories[$department['category_id']])) {
continue;
}
$categories[$departments[$v['dept_id']]['category_id']]['item_lists'][] = [
'dept_id' => $v['dept_id'],
'dept_name' => $v['dept_name'],
'is_emergency' => $v['is_emergency'],
'show_title_tags' => $departments[$v['dept_id']]['show_title_tags'] ?? ''
// 'dept_intro' => $v['introduce'] ?? ''
];
}
return array_values($categories);
}
/**
* 获取科室分类
* @return array
*/
public function getDepartmentCategoryLists(): array
{
if (Redis::exists('department.category')) {
$category_lists = Redis::get('department.category');
$category_lists = json_decode($category_lists, true);
} else {
$category_lists = DepartmentCategory::select('id as category_id', 'name as category_name')
->orderBy('sort', 'DESC')
->orderBy('id', 'ASC')
->get();
$category_lists = $category_lists->keyBy('category_id')->map(function ($value) { return $value->toArray(); })->toArray();
Redis::setex('department.category', 4 * 3600, json_encode($category_lists, JSON_UNESCAPED_UNICODE));
}
return $category_lists;
}
/**
* 获取科室列表
* @return array
*/
public function getDepartmentLists(): array
{
if (Redis::exists('department.lists')) {
$department_lists = Redis::get('department.lists');
$department_lists = json_decode($department_lists, true);
} else {
$department_lists = Department::where('is_enable', 1)
->orderBy('sort', 'DESC')
->orderBy('id', 'ASC')
->get();
$department_lists = $department_lists->keyBy('dept_id')->map(function ($value) { return $value->toArray(); })->toArray();
Redis::setex('department.lists', 4 * 3600, json_encode($department_lists, JSON_UNESCAPED_UNICODE));
}
return $department_lists;
}
}