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.

141 lines
5.5 KiB
PHP

<?php
namespace App\Services\Admin\YeWu;
use Illuminate\Support\Facades\DB;
class DepartmentResourceService
{
public function Save($info,$userid)
{
$id = $info['id'] ?? null;
$department_id = $info['department_id'];
//查询科室状态是否正常
$department = DB::table('s_department')
->where('id', $department_id)
->where('is_del', 0)
->where('department_status', 1)
->first();
if(!$department) {
return \Yz::Return(false, '科室状态异常', []);
}
DB::beginTransaction();
try {
if(isset($id)) {
// 修改诊室
$c = DB::table('s_department_resources')
->where('id', $id)
->update($info);
// 检查是否已关联服务组
$bind = DB::table('s_department_resources_device')
->where('rsourece_id', $id)
->first();
if($bind) {
// 已关联 → 同步更新设备名称
DB::table('s_devices')
->where('id', $bind->device_id)
->update(['device_name' => $info['department_resources_name']]);
} else {
// 未关联 → 新建设备并建立关联
$deviceId = DB::table('s_devices')->insertGetId([
'device_name' => $info['department_resources_name'],
'status' => 1,
'adduser' => $userid,
'is_del' => 0,
]);
DB::table('s_department_resources_device')->insert([
'department_id' => $department_id,
'rsourece_id' => $id,
'device_id' => $deviceId,
]);
}
} else {
// 新增诊室
$info['is_del'] = 0;
$info['adduser'] = $userid;
$info['time_range'] = json_encode($info['time_range']);
$resourceId = DB::table('s_department_resources')->insertGetId($info);
if(!$resourceId) {
DB::rollBack();
return \Yz::Return(false, '保存失败', []);
}
// 新建设备并建立关联
$deviceId = DB::table('s_devices')->insertGetId([
'device_name' => $info['department_resources_name'],
'status' => 1,
'adduser' => $userid,
'is_del' => 0,
]);
DB::table('s_department_resources_device')->insert([
'department_id' => $department_id,
'rsourece_id' => $resourceId,
'device_id' => $deviceId,
]);
}
DB::commit();
return \Yz::Return(true, '保存成功', []);
} catch (\Exception $e) {
DB::rollBack();
return \Yz::Return(false, '保存失败:' . $e->getMessage());
}
}
public function GetList($searchInfo,$page,$pageSize){
$list=DB::table('s_department_resources as a')->join('s_department','a.department_id','=','s_department.id')
->where('a.is_del',0);
if(isset($searchInfo['department_id'])){
$list= $list->where('a.department_id',$searchInfo['department_id']);
}
if(isset($searchInfo['status'])){
$list= $list->where('a.department_resources_status',$searchInfo['status']);
}
if(!empty($searchInfo['name'])){
$list= $list->where('a.department_resources_name','like','%'.$searchInfo['name'].'%');
}
$c=$list->count();
$list=$list->select(['a.*','s_department.department_name'])->skip(($page-1)*$pageSize)
->take($pageSize)->get() ;
foreach ($list as $k=>$v){
$list[$k]->time_range=json_decode($v->time_range,true);
}
return \Yz::Return(true, '查询成功', ['list'=>$list,'count'=>$c]);
}
public function Del($id){
$c= DB::table('s_department_resources')->where('id',$id)->update(['is_del'=>1]);
if(!$c){
return \Yz::Return(false, '删除失败', []);
}else{
return \Yz::Return(true, '删除成功', []);
}
}
//绑定设备
public function BindDevice($department_id,$resource_id,$device_ids){
$data=[];
foreach ($device_ids as $k=>$v){
$data[$k]['department_id']=$department_id;
$data[$k]['rsourece_id']=$resource_id;
$data[$k]['device_id']=$v;
}
DB::beginTransaction();
DB::table('s_department_resources_device')->where('rsourece_id',$resource_id)->delete();
$i= DB::table('s_department_resources_device')->insert($data);
if($i>0){
DB::commit();
return \Yz::Return(true, '绑定成功', []);
}else{
DB::rollBack();
return \Yz::Return(false, '绑定失败');
}
}
public function GetBindDeviceList($resource_id)
{
$list =DB::table('s_department_resources_device as a')
->join('s_devices as b', 'a.device_id', '=', 'b.id')
->select('b.*')
->where(['a.rsourece_id'=>$resource_id,'b.status'=>1,'b.is_del'=>0])->get();
return \Yz::Return(true, '查询成功', $list);
}
}