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.
232 lines
9.2 KiB
PHP
232 lines
9.2 KiB
PHP
<?php
|
|
namespace App\Services\Admin\YeWu;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DevicesService
|
|
{
|
|
public function GetList($searchInfo,$page,$pageSize){
|
|
$where=['is_del'=>0];
|
|
$list=DB::table('s_devices');
|
|
if(!empty($searchInfo['name'])){
|
|
$where[]=['device_name','like','%'.$searchInfo['name'].'%'];
|
|
}
|
|
$count= $list->where($where)->count();
|
|
$list= $list->where($where)->skip(($page-1)*$pageSize)
|
|
->take($pageSize)->get();
|
|
return \Yz::Return(true, '查询成功', ['list'=>$list,'count'=>$count]);
|
|
}
|
|
//获取启用的设备列表
|
|
public function GetEnableList($hisExecDepts = null){
|
|
$list = DB::table('s_devices')->where(['s_devices.is_del' => 0, 's_devices.status' => 1]);
|
|
|
|
// 反查关联诊室和科室状态:必须存在至少一条有效的关联链路
|
|
$list->whereExists(function ($query) {
|
|
$query->select(DB::raw(1))
|
|
->from('s_department_resources_device')
|
|
->join('s_department_resources', function ($join) {
|
|
$join->on('s_department_resources_device.rsourece_id', '=', 's_department_resources.id')
|
|
->where('s_department_resources.is_del', 0)
|
|
->where('s_department_resources.department_resources_status', 1);
|
|
})
|
|
->join('s_department', function ($join) {
|
|
$join->on('s_department_resources_device.department_id', '=', 's_department.id')
|
|
->where('s_department.is_del', 0)
|
|
->where('s_department.department_status', 1)
|
|
->where('s_department.appointment_enabled', 1);
|
|
})
|
|
->whereColumn('s_department_resources_device.device_id', 's_devices.id');
|
|
});
|
|
|
|
if (!empty($hisExecDepts)) {
|
|
$deptCodes = json_decode($hisExecDepts, true);
|
|
if (!empty($deptCodes) && is_array($deptCodes)) {
|
|
//找到对应科室
|
|
$deptIds = DB::table('s_department')
|
|
->whereIn('department_number', $deptCodes)
|
|
->where('is_del', 0)
|
|
->where('department_status', 1)
|
|
->where('appointment_enabled', 1)
|
|
->pluck('id')
|
|
->toArray();
|
|
|
|
//找到这些科室的父级科室IDs
|
|
$parentIds = DB::table('s_department')
|
|
->whereIn('id', function($q) use ($deptIds) {
|
|
$q->select('pid')->from('s_department')
|
|
->whereIn('id', $deptIds)
|
|
->where('pid', '>', 0);
|
|
})
|
|
->where('is_del', 0)
|
|
->where('department_status', 1)
|
|
->where('appointment_enabled', 1)
|
|
->pluck('id')
|
|
->toArray();
|
|
|
|
$allDeptIds = array_merge($deptIds, $parentIds);
|
|
|
|
//找到这些科室下的设备
|
|
$deviceIds = DB::table('s_department_resources_device')
|
|
->whereIn('department_id', $allDeptIds)
|
|
->pluck('device_id')
|
|
->toArray();
|
|
|
|
$list->whereIn('s_devices.id', $deviceIds);
|
|
}
|
|
}
|
|
|
|
return \Yz::Return(true, '查询成功', $list->get());
|
|
}
|
|
public function Save($data,$userid){
|
|
if(empty($data['id'])){
|
|
$data['adduser']=$userid;
|
|
$data['is_del']=0;
|
|
$id=DB::table('s_devices')->insertGetId($data);
|
|
if($id){
|
|
return \Yz::Return(true, '添加成功', $id);
|
|
}else{
|
|
return \Yz::Return(false, '添加失败');
|
|
}
|
|
}else{
|
|
$res=DB::table('s_devices')->where('id',$data['id'])->update($data);
|
|
if($res){
|
|
return \Yz::Return(true, '修改成功');
|
|
}else{
|
|
return \Yz::Return(false, '修改失败');
|
|
}
|
|
}
|
|
}
|
|
public function Del($id){
|
|
$res=DB::table('s_devices')->where('id',$id)->update(['is_del'=>1]);
|
|
if($res){
|
|
return \Yz::Return(true, '删除成功');
|
|
}else{
|
|
return \Yz::Return(false, '删除失败');
|
|
}
|
|
}
|
|
|
|
// 获取当前用户科室下的设备列表
|
|
public function GetDeptList($userid)
|
|
{
|
|
$user = DB::table('users')->where('id', $userid)->first();
|
|
if (!$user || !$user->department_id) {
|
|
return \Yz::Return(true, '查询成功', []);
|
|
}
|
|
$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)
|
|
->where('s_devices.status', 1)
|
|
->select('s_devices.*',
|
|
DB::raw('(SELECT COUNT(*) FROM s_check_item_device WHERE device_id = s_devices.id) AS bind_count'))
|
|
->distinct()
|
|
->orderBy('s_devices.id')
|
|
->get();
|
|
return \Yz::Return(true, '查询成功', $list);
|
|
}
|
|
|
|
// 获取设备已绑定的检查项目
|
|
public function GetBindItems($device_id)
|
|
{
|
|
$list = DB::table('s_check_item_device')
|
|
->join('s_check_item', 's_check_item_device.item_id', '=', 's_check_item.id')
|
|
->leftJoin('s_check_item_class', 's_check_item.item_class_id', '=', 's_check_item_class.id')
|
|
->where('s_check_item_device.device_id', $device_id)
|
|
->where('s_check_item.is_del', 0)
|
|
->select('s_check_item.id', 's_check_item.item_code', 's_check_item.item_name',
|
|
's_check_item_class.item_class_name')
|
|
->orderBy('s_check_item.id')
|
|
->get();
|
|
return \Yz::Return(true, '查询成功', $list);
|
|
}
|
|
|
|
// 绑定检查项目到设备
|
|
public function BindItem($item_id, $device_id)
|
|
{
|
|
// 检查是否已存在
|
|
$exists = DB::table('s_check_item_device')
|
|
->where('item_id', $item_id)
|
|
->where('device_id', $device_id)
|
|
->first();
|
|
if ($exists) {
|
|
return \Yz::Return(false, '该绑定关系已存在');
|
|
}
|
|
$id = DB::table('s_check_item_device')->insertGetId([
|
|
'item_id' => $item_id,
|
|
'device_id' => $device_id,
|
|
]);
|
|
if ($id) {
|
|
return \Yz::Return(true, '绑定成功');
|
|
} else {
|
|
return \Yz::Return(false, '绑定失败');
|
|
}
|
|
}
|
|
|
|
// 解除绑定
|
|
public function UnbindItem($item_id, $device_id)
|
|
{
|
|
$deleted = DB::table('s_check_item_device')
|
|
->where('item_id', $item_id)
|
|
->where('device_id', $device_id)
|
|
->delete();
|
|
if ($deleted) {
|
|
return \Yz::Return(true, '解绑成功');
|
|
} else {
|
|
return \Yz::Return(false, '解绑失败');
|
|
}
|
|
}
|
|
|
|
// 获取未绑定的检查项目(分页,排除已绑定的)
|
|
public function GetUnboundItems($device_id, $keyword, $page, $pageSize)
|
|
{
|
|
$list = DB::table('s_check_item')
|
|
->leftJoin('s_check_item_class', 's_check_item.item_class_id', '=', 's_check_item_class.id')
|
|
->where('s_check_item.is_del', 0)
|
|
->where('s_check_item.status', 1)
|
|
->whereNotNull('s_check_item.sheetType')
|
|
->where('s_check_item.sheetType', '!=', '')
|
|
->whereNotExists(function ($query) use ($device_id) {
|
|
$query->select(DB::raw(1))
|
|
->from('s_check_item_device')
|
|
->whereColumn('s_check_item_device.item_id', 's_check_item.id')
|
|
->where('s_check_item_device.device_id', $device_id);
|
|
});
|
|
|
|
//根据设备关联科室过滤 hisExecDepts
|
|
$deptNumbers = DB::table('s_department_resources_device')
|
|
->join('s_department', 's_department_resources_device.department_id', '=', 's_department.id')
|
|
->where('s_department_resources_device.device_id', $device_id)
|
|
->where('s_department.is_del', 0)
|
|
->pluck('s_department.department_number')
|
|
->toArray();
|
|
|
|
$list->whereNotNull('hisExecDepts')->where('hisExecDepts', '!=', '');
|
|
if (!empty($deptNumbers)) {
|
|
$list->where(function($q) use ($deptNumbers) {
|
|
foreach ($deptNumbers as $num) {
|
|
$q->orWhere('hisExecDepts', 'like', '%"' . $num . '"%');
|
|
}
|
|
});
|
|
} else {
|
|
$list->whereRaw('1 = 0');
|
|
}
|
|
|
|
if (!empty($keyword)) {
|
|
$list->where(function ($q) use ($keyword) {
|
|
$q->where('s_check_item.item_name', 'like', '%' . $keyword . '%')
|
|
->orWhere('s_check_item.item_code', 'like', '%' . $keyword . '%');
|
|
});
|
|
}
|
|
|
|
$count = $list->count();
|
|
$items = $list->select('s_check_item.id', 's_check_item.item_code', 's_check_item.item_name',
|
|
's_check_item_class.item_class_name')
|
|
->orderBy('s_check_item.id')
|
|
->skip(($page - 1) * $pageSize)
|
|
->take($pageSize)
|
|
->get();
|
|
|
|
return \Yz::Return(true, '查询成功', ['list' => $items, 'count' => $count]);
|
|
}
|
|
}
|