where(['pid'=>0,'is_del'=>0])->get(); $smallClass=[]; if(!empty($searchInfo['bigClass'])) { $smallClass = DB::table('s_check_item_class')->where(['pid'=>$searchInfo['bigClass'],'is_del'=>0])->get(); } return \Yz::Return(true, '查询成功', ['bigClass'=>$bigClass,'smallClass'=>$smallClass]); } //获取当前科室权限范围内的检查项目列表 public function GetItemList($searchInfo,$page,$pageSize,$userid){ $user = DB::table('users')->where('id', $userid)->first(); if (!$user || !$user->department_id) { return \Yz::Return(true, '查询成功', ['list' => [], 'count' => 0]); } //获取当前科室及子科室的 department_number $deptNumbers = DB::table('s_department') ->where('is_del', 0) ->where(function($q) use ($user) { $q->where('id', $user->department_id) ->orWhere('pid', $user->department_id); }) ->pluck('department_number') ->toArray(); //获取当前科室下的设备 IDs $deptDeviceIds = DB::table('s_department_resources_device') ->where('department_id', $user->department_id) ->pluck('device_id') ->toArray(); $small_id=[]; $list=DB::table('s_check_item')->where('s_check_item.is_del', 0)->whereNotNull('sheetType')->where('sheetType', '!=', ''); //科室过滤:hisExecDepts 包含当前科室及子科室编号 $list->whereNotNull('hisExecDepts')->where('hisExecDepts', '!=', ''); if (!empty($deptNumbers)) { $list->where(function($q) use ($deptNumbers) { foreach ($deptNumbers as $num) { $q->orWhere('hisExecDepts', 'like', '%"' . $num . '"%'); } }); } else { return \Yz::Return(true, '查询成功', ['list' => [], 'count' => 0]); } if(empty($searchInfo['bigClass'])!==true and empty($searchInfo['smallClass'])===true){ $small_id= DB::table('s_check_item_class')->where(['pid'=>$searchInfo['bigClass'],'is_del'=>0])->pluck('id')->toArray(); $list= $list->whereIn('item_class_id',$small_id); } if(!empty($searchInfo['smallClass'])){ $small_id[0]=$searchInfo['smallClass']; $list= $list->whereIn('item_class_id',$small_id); } if(!empty($searchInfo['name'])){ $list->where(function($query) use ($searchInfo) { $query->where('item_name', 'like', '%'.$searchInfo['name'].'%') ->orWhere('item_code', 'like', '%'.$searchInfo['name'].'%'); }); } if (isset($searchInfo['deviceBind']) && $searchInfo['deviceBind'] !== '') { if ($searchInfo['deviceBind'] == 1) { $list->whereIn('s_check_item.id', function($q) use ($deptDeviceIds) { $q->select('item_id')->from('s_check_item_device')->whereIn('device_id', $deptDeviceIds); }); } elseif ($searchInfo['deviceBind'] == 0) { $list->whereNotIn('s_check_item.id', function($q) use ($deptDeviceIds) { $q->select('item_id')->from('s_check_item_device')->whereIn('device_id', $deptDeviceIds); }); } } //执行科室筛选 if (isset($searchInfo['hisExecDepts']) && $searchInfo['hisExecDepts'] !== '') { if ($searchInfo['hisExecDepts'] == 1) { $list->whereNotNull('hisExecDepts')->where('hisExecDepts', '!=', ''); } elseif ($searchInfo['hisExecDepts'] == 0) { $list->where(function($q) { $q->whereNull('hisExecDepts')->orWhere('hisExecDepts', ''); }); } } $count = $list->count(); $list= $list ->leftJoin('s_dept_check_item', function($join) use ($user) { $join->on('s_dept_check_item.check_item_id', '=', 's_check_item.id') ->where('s_dept_check_item.department_id', '=', $user->department_id); }) ->select( 's_check_item.*', DB::raw('COALESCE(s_dept_check_item.reservation_method, s_check_item.reservation_method) as reservation_method'), DB::raw('COALESCE(s_dept_check_item.limosis, s_check_item.limosis) as limosis'), DB::raw('COALESCE(s_dept_check_item.check_time, s_check_item.check_time) as check_time'), DB::raw('COALESCE(s_dept_check_item.check_begin_time, s_check_item.check_begin_time) as check_begin_time'), DB::raw('COALESCE(s_dept_check_item.check_notice, s_check_item.check_notice) as check_notice'), DB::raw('COALESCE(s_dept_check_item.use_seats, s_check_item.use_seats, 1) as use_seats'), DB::raw('COALESCE(s_dept_check_item.warm_tips, s_check_item.warm_tips) as warm_tips'), DB::raw('COALESCE(s_dept_check_item.is_share_slot, s_check_item.is_share_slot, 0) as is_share_slot') ); if (!empty($searchInfo['orderField']) && !empty($searchInfo['orderDirection'])) { $direction = $searchInfo['orderDirection'] == 'ascending' ? 'asc' : 'desc'; $list->orderBy($searchInfo['orderField'], $direction); } $list = $list->skip(($page-1)*$pageSize)->take($pageSize)->get(); //查询出所有的预约渠道 $qudaoList=DB::table('s_appointment_type')->get(); foreach ($list as $k=>$v){ $qudao_names=[]; $qudao_ids = explode(",", $v->reservation_method); foreach ($qudaoList as $k1=>$v1){ foreach ($qudao_ids as $k2=>$v2){ if($v1->id==$v2){ $qudao_names[]=$v1->name; } } } $list[$k]->reservation_method= array_map('intval', $qudao_ids); $list[$k]->reservation_method_name=$qudao_names; $list[$k]->devicesInfo=DB::table('s_check_item_device') ->join('s_devices', 's_check_item_device.device_id', '=', 's_devices.id') ->select('s_devices.*') ->where('item_id',$v->id) ->whereIn('s_check_item_device.device_id', $deptDeviceIds) ->get(); // 解析HIS绑定科室 $deptNames = []; if (!empty($v->hisExecDepts)) { $deptCodes = json_decode($v->hisExecDepts, true); if (!empty($deptCodes) && is_array($deptCodes)) { $deptNames = DB::table('s_department') ->whereIn('department_number', $deptCodes) ->pluck('department_name') ->toArray(); } } $list[$k]->his_exec_dept_names = $deptNames; } return \Yz::Return(true, '查询成功', ['list'=>$list,'count'=>$count]); } //绑定设备(只影响当前科室下的排班关联) public function BindDevice($item_id,$device_ids,$userid){ $user = DB::table('users')->where('id', $userid)->first(); if (!$user || !$user->department_id) { return \Yz::Return(false, '用户科室信息不存在'); } //获取当前科室下的设备 IDs $deptDeviceIds = DB::table('s_department_resources_device') ->where('department_id', $user->department_id) ->pluck('device_id') ->toArray(); $data=[]; foreach ($device_ids as $k=>$v){ $data[$k]['item_id']=$item_id; $data[$k]['device_id']=$v; } DB::beginTransaction(); //只删除当前科室下的关联 DB::table('s_check_item_device') ->where('item_id',$item_id) ->whereIn('device_id', $deptDeviceIds) ->delete(); $i= DB::table('s_check_item_device')->insert($data); if($i>0){ DB::commit(); return \Yz::Return(true, '绑定成功', []); }else{ DB::rollBack(); return \Yz::Return(false, '绑定失败'); } } public function BatchUpdateNotice($item_ids, $field, $value, $userid) { if (empty($item_ids) || !in_array($field, ['check_notice', 'warm_tips'])) { return \Yz::Return(false, '参数错误'); } $user = DB::table('users')->where('id', $userid)->first(); if (!$user || !$user->department_id) { return \Yz::Return(false, '用户科室信息不存在'); } $deptId = $user->department_id; foreach ($item_ids as $item_id) { // 查覆盖表是否已有记录 $existing = DB::table('s_dept_check_item') ->where('check_item_id', $item_id) ->where('department_id', $deptId) ->first(); if ($existing) { // 已有记录,直接更新目标字段 DB::table('s_dept_check_item') ->where('id', $existing->id) ->update([$field => $value]); } else { // 不存在,从主表读取基线数据回填 $base = DB::table('s_check_item') ->where('id', $item_id) ->first(); $data = [ 'check_item_id' => $item_id, 'department_id' => $deptId, 'reservation_method' => $base->reservation_method ?? null, 'limosis' => $base->limosis ?? null, 'check_time' => $base->check_time ?? null, 'check_begin_time' => $base->check_begin_time ?? null, 'check_notice' => $base->check_notice ?? null, 'warm_tips' => null, 'use_seats' => $base->use_seats ?? 1, 'is_share_slot' => 0, ]; // 覆盖目标字段 $data[$field] = $value; DB::table('s_dept_check_item')->insert($data); } } return \Yz::Return(true, '批量更新成功,共更新' . count($item_ids) . '条'); } public function Save($Info, $userid){ if(empty($Info['id'])){ return \Yz::Return(false, 'id不能为空'); } $user = DB::table('users')->where('id', $userid)->first(); if(!$user || !$user->department_id){ return \Yz::Return(false, '用户科室信息不存在'); } // 预约方式数组转逗号分隔串 $reservationMethod = $Info['reservation_method']; if(is_array($reservationMethod)){ $reservationMethod = implode(',', $reservationMethod); } $data = [ 'reservation_method' => $reservationMethod, 'limosis' => $Info['limosis'] ?? null, 'check_time' => $Info['check_time'] ?? null, 'check_begin_time' => $Info['check_begin_time'] ?? null, 'check_notice' => $Info['check_notice'] ?? null, 'warm_tips' => $Info['warm_tips'] ?? null, 'use_seats' => $Info['use_seats'] ?? 1, 'is_share_slot' => $Info['is_share_slot'] ?? 0, ]; $res = DB::table('s_dept_check_item')->updateOrInsert( ['check_item_id' => $Info['id'], 'department_id' => $user->department_id], $data ); if($res){ return \Yz::Return(true, '修改成功'); }else{ return \Yz::Return(false, '修改失败'); } } //同步检查项目分类(全量,复用 HIS 逻辑) public function SyncItemClass() { $hisController = app(\App\Http\Controllers\API\His\CheckItemController::class); return $hisController->UpdateItemClass(); } //同步检查项目(全量) + 返回当前科室相关统计 public function SyncCheckItem($userid) { // 1. 同步前快照:当前科室所有符合条件的项目 code + 关键字段摘要 $beforeSnapshot = $this->getDeptItemSnapshot($userid); // 2. 调用 HIS 同步项目(全量) request()->merge(['termClassId' => 155]); $hisController = app(\App\Http\Controllers\API\His\CheckItemController::class); $hisController->UpdateCheckItem(); // 3. 同步后快照 $afterSnapshot = $this->getDeptItemSnapshot($userid); // 4. 对比统计 $beforeCodes = array_keys($beforeSnapshot); $afterCodes = array_keys($afterSnapshot); $beforeSet = array_flip($beforeCodes); $afterSet = array_flip($afterCodes); $addedCodes = array_diff_key($afterSet, $beforeSet); $removedCodes = array_diff_key($beforeSet, $afterSet); $commonCodes = array_intersect_key($beforeSet, $afterSet); // 字段级对比:只有真正变化的才算更新 $updatedCount = 0; foreach ($commonCodes as $code => $v) { if ($beforeSnapshot[$code] !== $afterSnapshot[$code]) { $updatedCount++; } } return \Yz::Return(true, '同步完成', [ 'added' => count($addedCodes), 'removed' => count($removedCodes), 'updated' => $updatedCount, ]); } // ========== 公共方法:department_id 获取 ========== /** * 根据 userid 查 users.department_id */ public static function getDeptIdByUserid($userid) { if (empty($userid)) return 0; $user = DB::table('users')->where('id', $userid)->first(['department_id']); return $user->department_id ?? 0; } /** * 根据排班明细 ID 查 s_source_roster_detail.department_id */ public static function getDeptIdByRosterId($rosterId) { if (empty($rosterId)) return 0; $roster = DB::table('s_source_roster_detail')->where('id', $rosterId)->first(['department_id']); return $roster->department_id ?? 0; } /** * 根据预约资源 ID 查 s_department_resources.department_id */ public static function getDeptIdByResourceId($resourceId) { if (empty($resourceId)) return 0; $resource = DB::table('s_department_resources')->where('id', $resourceId)->first(['department_id']); return $resource->department_id ?? 0; } //获取当前科室所有符合条件的项目快照(code => 字段摘要) private function getDeptItemSnapshot($userid) { $user = DB::table('users')->where('id', $userid)->first(); if (!$user || !$user->department_id) return []; $deptNumbers = DB::table('s_department') ->where('is_del', 0) ->where(function($q) use ($user) { $q->where('id', $user->department_id) ->orWhere('pid', $user->department_id); }) ->pluck('department_number') ->toArray(); if (empty($deptNumbers)) return []; $items = DB::table('s_check_item') ->where('is_del', 0) ->whereNotNull('sheetType')->where('sheetType', '!=', '') ->where(function($q) use ($deptNumbers) { foreach ($deptNumbers as $num) { $q->orWhere('hisExecDepts', 'like', '%"' . $num . '"%'); } }) ->select('item_code', 'item_name', 'hisExecDepts', 'check_notice', 'sheetType', 'item_class_id') ->get(); $snapshot = []; foreach ($items as $item) { $snapshot[$item->item_code] = md5(json_encode([ 'item_name' => $item->item_name, 'hisExecDepts' => $item->hisExecDepts, 'check_notice' => $item->check_notice, 'sheetType' => $item->sheetType, 'item_class_id'=> $item->item_class_id, ])); } return $snapshot; } }