香洲二院小程序
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

64 lines
1.8 KiB

<?php
namespace App\Http\Resources\Registration\Schedule;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\DB;
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
{
$department_lists = $this->getDepartmentLists();
$sort = collect($department_lists)->pluck('sort', 'dept_id');
$lists = [];
foreach ($this->resource['response'] as $v) {
$lists[] = [
'dept_id' => $v['typeId'],
'dept_name' => $v['typeName'],
// 'dept_intro' => $v['INTRODUCE']
];
}
// 根据科室的 sort 字段对 $lists 进行排序
usort($lists, function ($a, $b) use ($sort) {
$sort_a = $sort[$a['dept_id']] ?? 0;
$sort_b = $sort[$b['dept_id']] ?? 0;
return $sort_b <=> $sort_a; // 倒序排序
});
return $lists;
}
/**
* 获取医生列表
* @return array
*/
public function getDepartmentLists(): array
{
if (Redis::exists('department.lists')) {
$department_lists = Redis::get('doctor.lists');
$department_lists = json_decode($department_lists, true);
} else {
$db = DB::connection('mysql_admin')->table('departments');
$department_lists = $db
->where('is_enable', 1)
->orderBy('sort', 'DESC')
->orderBy('id', 'ASC')
->get();
Redis::setex('department.lists', 4 * 3600, json_encode($department_lists, JSON_UNESCAPED_UNICODE));
}
return $department_lists;
}
}