You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
417 lines
13 KiB
PHP
417 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\UserService;
|
|
use Illuminate\Http\Request;
|
|
use App\Lib\Rs;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class DepartmentResourceController extends Controller
|
|
{
|
|
private $userService;
|
|
|
|
public function __construct(UserService $userService)
|
|
{
|
|
$this->userService = $userService;
|
|
}
|
|
|
|
/**
|
|
* 获取科室资源列表
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function list(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$name = $request->input('name', '');
|
|
$code = $request->input('code', '');
|
|
$status = $request->input('status', null);
|
|
$departmentId = $request->input('department_id', null);
|
|
$page = $request->input('page', 1);
|
|
$pageSize = $request->input('page_size', 10);
|
|
|
|
$query = DB::table('department_resource')
|
|
->where('deleted', 0);
|
|
|
|
// 权限控制:非管理员只能查看自己科室的资源
|
|
if (!$isAdmin && !empty($userDeptId)) {
|
|
$query->where('department_id', $userDeptId);
|
|
// 如果用户传入了科室参数,需要验证是否属于自己科室
|
|
if ($departmentId !== null && $departmentId !== '') {
|
|
if ($departmentId != $userDeptId) {
|
|
return Rs::error('无权限访问其他科室的数据');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($name)) {
|
|
$query->where('name', 'like', '%' . $name . '%');
|
|
}
|
|
|
|
if (!empty($code)) {
|
|
$query->where('code', 'like', '%' . $code . '%');
|
|
}
|
|
|
|
if ($status !== null && $status !== '') {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
if ($departmentId !== null && $departmentId !== '' && ($isAdmin || empty($userDeptId))) {
|
|
// 管理员或无科室限制的用户可以根据科室筛选
|
|
$query->where('department_id', $departmentId);
|
|
}
|
|
|
|
$total = $query->count();
|
|
|
|
$resources = $query->orderBy('id', 'desc')
|
|
->skip(($page - 1) * $pageSize)
|
|
->take($pageSize)
|
|
->get();
|
|
|
|
foreach ($resources as $resource) {
|
|
$department = DB::table('department')
|
|
->where('id', $resource->department_id)
|
|
->where('deleted', 0)
|
|
->first();
|
|
$resource->department_name = $department ? $department->name : '';
|
|
|
|
// 统计该项目绑定的检查项目数量
|
|
$itemCount = DB::table('exam_item_resource')
|
|
->where('resource_id', $resource->id)
|
|
->where('deleted', 0)
|
|
->count();
|
|
$resource->item_count = $itemCount;
|
|
}
|
|
|
|
$data = [
|
|
'list' => $resources,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'page_size' => $pageSize,
|
|
'total_pages' => ceil($total / $pageSize),
|
|
'is_admin' => $isAdmin,
|
|
'user_dept_id' => $userDeptId
|
|
];
|
|
|
|
return Rs::success($data);
|
|
}
|
|
|
|
/**
|
|
* 获取科室资源详情(包含渠道超量配额)
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getDetail(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$data = $request->validate([
|
|
'id' => 'required|integer',
|
|
]);
|
|
|
|
$resource = DB::table('department_resource')
|
|
->where('id', $data['id'])
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$resource) {
|
|
return Rs::error('科室资源不存在');
|
|
}
|
|
|
|
// 权限控制:非管理员只能查看自己科室的资源详情
|
|
if (!$isAdmin && !empty($userDeptId)) {
|
|
if ($resource->department_id != $userDeptId) {
|
|
return Rs::error('无权限访问该资源');
|
|
}
|
|
}
|
|
|
|
$channelQuotas = DB::table('resource_channel_over_quota')
|
|
->where('resouce_id', $data['id'])
|
|
->where('deleted', 0)
|
|
->get()
|
|
->keyBy('channel_id')
|
|
->toArray();
|
|
|
|
$resource->channel_quotas = $channelQuotas;
|
|
|
|
return Rs::success($resource);
|
|
}
|
|
|
|
/**
|
|
* 保存科室资源
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function Save(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$data = $request->validate([
|
|
'id' => 'required|integer',
|
|
'department_id' => 'required|integer',
|
|
'name' => 'required|string|max:50',
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
'max:50',
|
|
Rule::unique('department_resource', 'code')->ignore($request->id, 'id'),
|
|
],
|
|
'status' => 'required|integer|in:0,1',
|
|
'slot_mode' => 'nullable|integer|in:1,2',
|
|
'cancel_reason_required' => 'nullable|integer|in:0,1',
|
|
'is_overbooking_allowed' => 'nullable|integer|in:0,1',
|
|
'channel_quotas' => 'nullable|array',
|
|
]);
|
|
|
|
// 权限控制:非管理员只能操作自己科室的资源
|
|
if (!$isAdmin && !empty($userDeptId)) {
|
|
if ($data['department_id'] != $userDeptId) {
|
|
return Rs::error('无权限操作其他科室的数据');
|
|
}
|
|
|
|
// 如果是更新操作,检查资源是否属于该科室
|
|
if ($data['id'] > 0) {
|
|
$resource = DB::table('department_resource')
|
|
->where('id', $data['id'])
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$resource) {
|
|
return Rs::error('资源不存在');
|
|
}
|
|
|
|
if ($resource->department_id != $userDeptId) {
|
|
return Rs::error('无权限操作该资源');
|
|
}
|
|
}
|
|
}
|
|
|
|
$conditions = ['id' => $data['id']];
|
|
$values = [
|
|
'department_id' => $data['department_id'],
|
|
'name' => $data['name'],
|
|
'code' => $data['code'],
|
|
'status' => $data['status'],
|
|
'slot_mode' => $data['slot_mode'] ?? null,
|
|
'cancel_reason_required' => $data['cancel_reason_required'] ?? 0,
|
|
'is_overbooking_allowed' => $data['is_overbooking_allowed'] ?? 0,
|
|
];
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($data['id'] == 0) {
|
|
// 新增操作
|
|
$values['created_at'] = now();
|
|
$values['updated_at'] = now();
|
|
$resourceId = DB::table('department_resource')->insertGetId($values);
|
|
} else {
|
|
// 更新操作
|
|
$values['updated_at'] = now();
|
|
$updated = DB::table('department_resource')
|
|
->where('id', $data['id'])
|
|
->update($values);
|
|
if ($updated === false) {
|
|
DB::rollBack();
|
|
return Rs::error('操作失败:资源不存在');
|
|
}
|
|
$resourceId = $data['id'];
|
|
}
|
|
|
|
if (isset($data['channel_quotas']) && is_array($data['channel_quotas'])) {
|
|
DB::table('resource_channel_over_quota')
|
|
->where('resouce_id', $resourceId)
|
|
->delete();
|
|
|
|
foreach ($data['channel_quotas'] as $quota) {
|
|
if (!empty($quota['channel_id']) && !empty($quota['over_quota'])) {
|
|
DB::table('resource_channel_over_quota')->insert([
|
|
'resouce_id' => $resourceId,
|
|
'channel_id' => $quota['channel_id'],
|
|
'over_quota' => $quota['over_quota'],
|
|
'created_at' => now(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
return Rs::success(['id' => $resourceId]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return Rs::error('保存失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除科室资源
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function Delete(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$data = $request->validate([
|
|
'id' => 'required|integer',
|
|
]);
|
|
|
|
// 查询资源信息
|
|
$resource = DB::table('department_resource')
|
|
->where('id', $data['id'])
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$resource) {
|
|
return Rs::error('资源不存在');
|
|
}
|
|
|
|
// 权限控制:非管理员只能删除自己科室的资源
|
|
if (!$isAdmin && !empty($userDeptId)) {
|
|
if ($resource->department_id != $userDeptId) {
|
|
return Rs::error('无权限删除该资源');
|
|
}
|
|
}
|
|
|
|
$deleted = DB::table('department_resource')->where('id', $data['id'])->update(['deleted' => 1]);
|
|
|
|
if ($deleted) {
|
|
DB::table('resource_channel_over_quota')
|
|
->where('resouce_id', $data['id'])
|
|
->update(['deleted' => 1]);
|
|
return Rs::success();
|
|
}
|
|
|
|
return Rs::error('删除失败');
|
|
}
|
|
|
|
/**
|
|
* 获取所有科室列表(用于选择)
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getDepartments()
|
|
{
|
|
$departments = DB::table('department')
|
|
->where('deleted', 0)
|
|
->where('status', 1)
|
|
->select('id', 'name', 'code')
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
|
|
return Rs::success($departments);
|
|
}
|
|
|
|
/**
|
|
* 获取所有渠道列表(用于选择)
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getChannels()
|
|
{
|
|
$channels = DB::table('channel')
|
|
->where('status', 1)
|
|
->select('id', 'name', 'code', 'short_name')
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
|
|
return Rs::success($channels);
|
|
}
|
|
|
|
/**
|
|
* 获取资源已绑定的项目列表
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getBindItems(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'resource_id' => 'required|integer',
|
|
]);
|
|
|
|
$items = DB::table('exam_item_resource as eir')
|
|
->join('exam_item as ei', 'eir.item_id', '=', 'ei.id')
|
|
->where('eir.resource_id', $data['resource_id'])
|
|
->where('eir.deleted', 0)
|
|
->select(
|
|
'ei.id',
|
|
'ei.code',
|
|
'ei.name'
|
|
)
|
|
->orderBy('eir.id', 'asc')
|
|
->get();
|
|
|
|
return Rs::success([
|
|
'list' => $items
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 根据科室ID获取该科室下的资源列表
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getEnableResources(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'department_id' => 'required|integer',
|
|
]);
|
|
|
|
$resources = DB::table('department_resource')
|
|
->where('department_id', $data['department_id'])
|
|
->where('deleted', 0)
|
|
->where('status', 1)
|
|
->select('id', 'name', 'code')
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
|
|
return Rs::success($resources);
|
|
}
|
|
}
|