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.
131 lines
5.6 KiB
PHP
131 lines
5.6 KiB
PHP
<?php
|
|
namespace App\Services\Admin\YeWu;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DeptDeviceService
|
|
{
|
|
//获取当前科室下的设备列表(分页,支持搜索)
|
|
public function GetList($searchInfo, $page, $pageSize, $userid)
|
|
{
|
|
$user = DB::table('users')->where('id', $userid)->first();
|
|
if (!$user || !$user->department_id) {
|
|
return \Yz::Return(true, '查询成功', ['list' => [], 'count' => 0]);
|
|
}
|
|
|
|
$list = DB::table('s_devices')
|
|
->join('s_department_resources_device', 's_devices.id', '=', 's_department_resources_device.device_id')
|
|
->where('s_department_resources_device.department_id', $user->department_id)
|
|
->where('s_devices.is_del', 0);
|
|
|
|
if (!empty($searchInfo['name'])) {
|
|
$list->where('s_devices.device_name', 'like', '%' . $searchInfo['name'] . '%');
|
|
}
|
|
if (isset($searchInfo['status']) && $searchInfo['status'] !== '') {
|
|
$list->where('s_devices.status', $searchInfo['status']);
|
|
}
|
|
|
|
$count = $list->count();
|
|
$items = $list->select('s_devices.*', 'sr.execute_department_id')
|
|
->leftJoin('s_department_resources as sr', 's_department_resources_device.rsourece_id', '=', 'sr.id')
|
|
->orderBy('s_devices.id', 'desc')
|
|
->skip(($page - 1) * $pageSize)
|
|
->take($pageSize)
|
|
->get();
|
|
|
|
return \Yz::Return(true, '查询成功', ['list' => $items, 'count' => $count]);
|
|
}
|
|
|
|
//保存设备(新建时同时关联到当前科室)
|
|
public function Save($data, $userid)
|
|
{
|
|
$user = DB::table('users')->where('id', $userid)->first();
|
|
if (!$user || !$user->department_id) {
|
|
return \Yz::Return(false, '用户科室信息不存在');
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
if (empty($data['id'])) {
|
|
//新建设备
|
|
$deviceId = DB::table('s_devices')->insertGetId([
|
|
'device_name' => $data['device_name'],
|
|
'status' => $data['status'] ?? 1,
|
|
'is_del' => 0,
|
|
]);
|
|
//关联到当前科室
|
|
$linkId = DB::table('s_department_resources_device')->insertGetId([
|
|
'department_id' => $user->department_id,
|
|
'device_id' => $deviceId,
|
|
]);
|
|
//如果有执行诊室,创建资源记录并关联
|
|
if (!empty($data['execute_department_id'])) {
|
|
$resourceId = DB::table('s_department_resources')->insertGetId([
|
|
'department_resources_name' => $data['device_name'],
|
|
'department_id' => $user->department_id,
|
|
'execute_department_id' => $data['execute_department_id'],
|
|
'is_del' => 0,
|
|
]);
|
|
DB::table('s_department_resources_device')
|
|
->where('id', $linkId)
|
|
->update(['rsourece_id' => $resourceId]);
|
|
}
|
|
} else {
|
|
//更新设备
|
|
DB::table('s_devices')->where('id', $data['id'])->update([
|
|
'device_name' => $data['device_name'],
|
|
'status' => $data['status'] ?? 1,
|
|
]);
|
|
//如果有执行诊室,更新资源记录
|
|
$link = DB::table('s_department_resources_device')
|
|
->where('device_id', $data['id'])
|
|
->where('department_id', $user->department_id)
|
|
->first();
|
|
if ($link) {
|
|
if (!empty($data['execute_department_id'])) {
|
|
if ($link->rsourece_id) {
|
|
//更新已有资源
|
|
DB::table('s_department_resources')
|
|
->where('id', $link->rsourece_id)
|
|
->update([
|
|
'execute_department_id' => $data['execute_department_id'],
|
|
'department_resources_name' => $data['device_name'],
|
|
]);
|
|
} else {
|
|
//创建新资源并关联
|
|
$resourceId = DB::table('s_department_resources')->insertGetId([
|
|
'department_resources_name' => $data['device_name'],
|
|
'department_id' => $user->department_id,
|
|
'execute_department_id' => $data['execute_department_id'],
|
|
'is_del' => 0,
|
|
]);
|
|
DB::table('s_department_resources_device')
|
|
->where('id', $link->id)
|
|
->update(['rsourece_id' => $resourceId]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
DB::commit();
|
|
return \Yz::Return(true, '保存成功');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return \Yz::Return(false, '保存失败');
|
|
}
|
|
}
|
|
|
|
//删除设备(软删除 + 解除关联)
|
|
public function Del($id)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
DB::table('s_devices')->where('id', $id)->update(['is_del' => 1]);
|
|
DB::table('s_department_resources_device')->where('device_id', $id)->delete();
|
|
DB::commit();
|
|
return \Yz::Return(true, '删除成功');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return \Yz::Return(false, '删除失败');
|
|
}
|
|
}
|
|
} |