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.

402 lines
18 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services\Admin\YeWu;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Table;
class CheckItemService
{
//获取检查项目类别列表
public function GetClassList($searchInfo){
$bigClass=DB::table('s_check_item_class')->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){
$small_id=[];
$list=DB::table('s_check_item')->where('s_check_item.is_del', 0)->whereNotNull('sheetType')->where('sheetType', '!=', '');
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) {
$q->select('item_id')->from('s_check_item_device');
});
} elseif ($searchInfo['deviceBind'] == 0) {
$list->whereNotIn('s_check_item.id', function($q) {
$q->select('item_id')->from('s_check_item_device');
});
}
}
//执行科室筛选
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', '');
});
}
}
//只显示参与预约科室的项目
if (!empty($searchInfo['appointmentDeptOnly'])) {
$deptNumbers = DB::table('s_department')
->where('appointment_enabled', 1)
->where('is_del', 0)
->where('department_status', 1)
->pluck('department_number')
->toArray();
if (!empty($deptNumbers)) {
$list->where(function($q) use ($deptNumbers) {
foreach ($deptNumbers as $num) {
$q->orWhere('hisExecDepts', 'like', '%"' . $num . '"%');
}
});
}
}
$count = $list->count();
$list= $list
->leftJoin('s_appointment_type', 's_check_item.reservation_method', '=', 's_appointment_type.id')
->select('s_check_item.*','s_appointment_type.name as reservation_method_name');
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)->get();
// 解析原始科室
$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 ExportItemList($searchInfo)
{
$small_id=[];
$list=DB::table('s_check_item')->where('s_check_item.is_del', 0)->whereNotNull('sheetType')->where('sheetType', '!=', '');
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) {
$q->select('item_id')->from('s_check_item_device');
});
} elseif ($searchInfo['deviceBind'] == 0) {
$list->whereNotIn('s_check_item.id', function($q) {
$q->select('item_id')->from('s_check_item_device');
});
}
}
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', '');
});
}
}
$list = $list->orderBy('s_check_item.id')->get();
// 构建 HTML 表格
$html = '<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%;font-size:12px;font-family:微软雅黑;">';
$html .= '<thead><tr style="background:#4472C4;color:#fff;font-weight:bold;text-align:center;">';
$html .= '<th style="padding:8px;">检查项目代码</th><th style="padding:8px;">检查项目名称</th><th style="padding:8px;">HIS执行科室</th><th style="padding:8px;">关联排班</th></tr></thead><tbody>';
$index = 0;
foreach ($list as $v) {
$index++;
$bgColor = ($index % 2 == 1) ? '#d9e2f3' : '#ffffff';
// 解析原始科室名称
$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();
}
}
// 获取关联排班名称
$deviceNames = DB::table('s_check_item_device')
->join('s_devices', 's_check_item_device.device_id', '=', 's_devices.id')
->where('item_id', $v->id)
->pluck('s_devices.device_name')
->toArray();
$deptCount = count($deptNames);
$deviceCount = count($deviceNames);
$maxRows = max($deptCount, $deviceCount, 1);
// 第一行,带合并单元格和背景色
$html .= '<tr style="background:'.$bgColor.';">';
$html .= '<td rowspan="'.$maxRows.'" style="vertical-align:middle;text-align:center;padding:4px;">'.$v->item_code.'</td>';
$html .= '<td rowspan="'.$maxRows.'" style="vertical-align:middle;text-align:center;padding:4px;">'.$v->item_name.'</td>';
$html .= '<td style="padding:4px;">'.($deptNames[0] ?? '').'</td>';
$html .= '<td style="padding:4px;">'.($deviceNames[0] ?? '').'</td>';
$html .= '</tr>';
// 后续行
for ($i = 1; $i < $maxRows; $i++) {
$html .= '<tr style="background:'.$bgColor.';">';
$html .= '<td style="padding:4px;">'.($deptNames[$i] ?? '').'</td>';
$html .= '<td style="padding:4px;">'.($deviceNames[$i] ?? '').'</td>';
$html .= '</tr>';
}
}
$html .= '</tbody></table>';
return $html;
}
//绑定设备
public function BindDevice($item_id,$device_ids){
$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)->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 Save($Info){
if(!empty($Info['id'])){
$Info['reservation_method']=implode(',', $Info['reservation_method']);
$res=DB::table('s_check_item')->where('id',$Info['id'])->update($Info);
if($res){
return \Yz::Return(true, '修改成功');
}else{
return \Yz::Return(false, '修改失败');
}
}else{
return \Yz::Return(false, 'id不能为空');
}
}
public function BatchUpdateNotice($item_ids, $field, $value)
{
if (empty($item_ids) || !in_array($field, ['check_notice', 'warm_tips'])) {
return \Yz::Return(false, '参数错误');
}
$updated = DB::table('s_check_item')
->whereIn('id', $item_ids)
->where('is_del', 0)
->update([$field => $value]);
return \Yz::Return(true, '批量更新成功,共更新' . $updated . '条');
}
public function GetRules($item_id, $userid)
{
$user = DB::table('users')->where('id', $userid)->first();
$rules = DB::table('s_check_item_rule')
->where(['target_type' => 'ITEM', 'target_id' => $item_id, 'deleted' => 0, 'status' => 1])
->get();
if ($user && $user->department_id) {
// 有科室归属:获取用户科室下的所有排班资源 ID
$deptResourceIds = DB::table('s_department_resources')
->where('department_id', $user->department_id)
->where('is_del', 0)
->pluck('id')
->toArray();
$filtered = [];
foreach ($rules as $rule) {
$rule->rule_value = json_decode($rule->rule_value, true);
$resourcesId = $rule->rule_value['resources_id'] ?? null;
// 全局规则resources_id 为空/0 → 保留
if (empty($resourcesId)) {
$filtered[] = $rule;
continue;
}
// 排班资源属于当前用户科室 → 保留
if (in_array($resourcesId, $deptResourceIds)) {
$filtered[] = $rule;
}
// 其他科室的规则 → 过滤掉
}
return \Yz::Return(true, '查询成功', $filtered);
}
// admin 或无科室:返回全部规则
foreach ($rules as $rule) {
$rule->rule_value = json_decode($rule->rule_value, true);
}
return \Yz::Return(true, '查询成功', $rules);
}
public function SaveRules($item_id, $rules, $userid)
{
$user = DB::table('users')->where('id', $userid)->first();
$now = date('Y-m-d H:i:s');
DB::beginTransaction();
try {
if ($user && $user->department_id) {
// 科室用户:获取当前科室的排班资源 ID 列表
$deptResourceIds = DB::table('s_department_resources')
->where('department_id', $user->department_id)
->where('is_del', 0)
->pluck('id')
->toArray();
// 软删除:只删除全局规则 + 当前科室的规则,不碰其他科室
DB::table('s_check_item_rule')
->where(['target_type' => 'ITEM', 'target_id' => $item_id])
->where('deleted', 0)
->where(function($q) use ($deptResourceIds) {
$q->whereRaw('JSON_UNQUOTE(JSON_EXTRACT(rule_value, \'$.resources_id\')) IS NULL')
->orWhereRaw('JSON_UNQUOTE(JSON_EXTRACT(rule_value, \'$.resources_id\')) = \'\'')
->orWhereRaw('JSON_UNQUOTE(JSON_EXTRACT(rule_value, \'$.resources_id\')) = \'0\'');
foreach ($deptResourceIds as $id) {
$q->orWhereRaw('JSON_UNQUOTE(JSON_EXTRACT(rule_value, \'$.resources_id\')) = ?', [(string)$id]);
}
})
->update(['deleted' => 1, 'updated_at' => $now]);
// 过滤传入的规则:忽略其他科室的
if (!empty($rules)) {
$rules = array_values(array_filter($rules, function($rule) use ($deptResourceIds) {
$resourcesId = $rule['rule_value']['resources_id'] ?? null;
return empty($resourcesId) || in_array($resourcesId, $deptResourceIds);
}));
}
} else {
// admin软删除该项目的全部规则
DB::table('s_check_item_rule')
->where(['target_type' => 'ITEM', 'target_id' => $item_id])
->where('deleted', 0)
->update(['deleted' => 1, 'updated_at' => $now]);
}
// 插入新规则
if (!empty($rules)) {
$insertData = [];
foreach ($rules as $rule) {
$rule_value = $rule['rule_value'];
if (is_array($rule_value)) {
$rule_value = json_encode($rule_value, JSON_UNESCAPED_UNICODE);
}
$insertData[] = [
'target_type' => 'ITEM',
'target_id' => $item_id,
'rule_type' => $rule['rule_type'],
'rule_value' => $rule_value,
'status' => 1,
'deleted' => 0,
'created_at' => $now,
'updated_at' => $now,
];
}
DB::table('s_check_item_rule')->insert($insertData);
}
DB::commit();
return \Yz::Return(true, '保存成功');
} catch (\Exception $e) {
DB::rollBack();
return \Yz::Return(false, '保存失败:' . $e->getMessage());
}
}
//获取检查项目关联的排班资源列表
public function GetItemRelatedResources($item_id, $userid)
{
$user = DB::table('users')->where('id', $userid)->first();
$query = DB::table('s_check_item_device')
->join('s_devices', 's_check_item_device.device_id', '=', 's_devices.id')
->join('s_department_resources_device', 's_devices.id', '=', 's_department_resources_device.device_id')
->join('s_department_resources', 's_department_resources_device.rsourece_id', '=', 's_department_resources.id')
->leftJoin('s_department', 's_department_resources.department_id', '=', 's_department.id')
->select(
's_department_resources.id',
's_department_resources.department_resources_name',
's_department.department_name'
)
->where('s_check_item_device.item_id', $item_id)
->where('s_department_resources.is_del', 0)
->where('s_department_resources.department_resources_status', 1);
if ($user && $user->department_id) {
$query->where('s_department_resources.department_id', $user->department_id);
}
$resources = $query->distinct()->get();
return \Yz::Return(true, '查询成功', $resources);
}
}