香洲二院小程序
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\Department;
use App\Models\DepartmentCategory;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use mysql_xdevapi\Collection;
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();
$sorts = collect($departments)->pluck('sort', 'dept_id')->toArray();
// 根据科室的 sort 字段对 科室列表 进行排序
usort($this->resource['response'], function ($a, $b) use ($sorts) {
$sort_a = $sorts[$a['typeId']] ?? 0;
$sort_b = $sorts[$b['typeId']] ?? 0;
return $sort_b <=> $sort_a; // 倒序排序
});
foreach ($this->resource['response'] as $v) {
if (!isset($categories[$departments[$v['typeId']]['category_id']])) {
continue;
}
$categories[$departments[$v['typeId']]['category_id']]['item_lists'][] = [
'dept_id' => $v['typeId'],
'dept_name' => $v['typeName'],
// '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 (array)$value; })->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 (array)$value; })->toArray();
Redis::setex('department.lists', 4 * 3600, json_encode($department_lists, JSON_UNESCAPED_UNICODE));
}
return $department_lists;
}
}