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.

258 lines
13 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\Http\Request;
use Illuminate\Support\Facades\DB;
use DateTime;
use Tools;
class PlanListService
{
public function GetEnablePlan($regnum, $entrustid, $episodeid, $appointment_type, $appointment_date)
{
date_default_timezone_set('PRC');
// $regnum = request('regnum');
// $entrustid = request('entrustid');
// $episodeid = request('episodeid');
// $appointment_type = request('appointment_type'); //预约类型
// $appointment_date = request('date'); //预约日期
$info = DB::table('s_list')->where(['reg_num' => $regnum, 'entrust_id' => $entrustid, 'episodeid' => $episodeid])->first();
if (!$info) return \Yz::echoError1('没有找到对应医嘱信息');
$itemInfo = DB::table('s_check_item')->where(['item_name' => $info->entrust, 'status' => 1, "is_del" => 0])->get();
if (count($itemInfo) == 0) return \Yz::echoError1('没有找到可用的检查项目信息');
$itemInfo = $itemInfo[0];
$qudaos = explode(',', $itemInfo->reservation_method);
if (!in_array($appointment_type, $qudaos)) return \Yz::echoError1('此检查项目不支持在当前渠道预约');
$entrust_time = $info->entrust_date . ' ' . $info->entrust_time; //医嘱时间
$date = new DateTime($entrust_time);
$date->modify("+" . $itemInfo->check_begin_time . " minutes");
$enableCheckTime = $date;//到此时间后可进行预约
$current_time = new DateTime();
if ($current_time < $enableCheckTime) return \Yz::echoError1("请于" . $enableCheckTime->format("Y-m-d H:i:s") . "后进行预约");
//获取检查项目绑定的服务组(设备),判断状态正常的
$devices = DB::table('s_check_item_device')
->leftJoin("s_devices", "s_check_item_device.device_id", "=", "s_devices.id")
->where(['s_check_item_device.item_id' => $itemInfo->id])
->where(['s_devices.status' => 1, 'is_del' => 0])->pluck('s_devices.id')->toArray();
//获取主表检查项目绑定的科室id
$department_id = DB::table('s_department')->where(['department_number' => $info->RISRAcceptDeptCode])->first();
if (!$department_id) return \Yz::echoError1('获取医嘱检查项目科室信息失败');
//获取对应日期的计划明细
$plan = DB::table('s_source_roster_detail as a')
->select('a.*', 'b.department_resources_name', 'c.roster_detail_id', 'c.count', 'c.used_count')
->leftJoin('s_department_resources as b', 'a.resources_id', '=', 'b.id')
->leftJoin('s_source_roster_detail_count as c', 'a.id', '=', 'c.roster_detail_id')
->where(['a.department_id' => $department_id->id, 'a.date' => $appointment_date, 'a.status' => 1, 'a.is_del' => 0, 'b.is_del' => 0, 'c.appointment_type_id' => $appointment_type])->get();
$plan_enable = [];
foreach ($plan as $key => $value) {
if (array_intersect($devices, array_map('intval', explode(',', $value->device_id)))) {
$plan_enable[] = $value;
}
}
//匹配设备(服务组)
$devices = DB::table('s_devices')->get();
foreach ($plan_enable as $key => $value){
// dd($value);
$plan_enable[$key]->devices = [];
$array_device_id = explode(",", $value->device_id);
$names='';
foreach ($devices as $k => $v) {
if (in_array($v->id, $array_device_id)) {
$names=$names. $v->device_name.' ';
}
}
$plan_enable[$key]->devices =$names;
}
return \Yz::Return(true, '查询完成', ['today_date' => date("Y-m-d"), 'appointment_date' => $appointment_date, 'weekname' => Tools::GetWeekName($appointment_date), 'mainInfo' => $info, 'plan_list' => $plan_enable]);
}
//开始预约占用名额
public function YuYue($planid, $appointment_type, $mainlistid, $do_type)
{
date_default_timezone_set('PRC');
$nowdatetime = date("Y-m-d H:i:s");
// $planid = request('planid');
// $appointment_type = request('appointment_type');//渠道id
// $mainlistid = request('mainlistid');//主表id
// $do_type = request('dotype');//操作类型,1预约2改约
// if (!isset($do_type)) return \Yz::echoError1('参数:操作类型 不能为空');
$mainInfo = DB::table('s_list')->where(['id' => $mainlistid])->first();
if (!$mainInfo) return \Yz::echoError1('医嘱不存在');
//判断状态
if ($do_type == 1 && $mainInfo->list_status <> 0) return \Yz::echoError1('该信息状态不允许预约,当前状态:' . $mainInfo->list_status);
if ($do_type == 2 && $mainInfo->list_status <> 1) return \Yz::echoError1('该信息状态不允许改约操作,当前状态:' . $mainInfo->list_status);
$planInfo = DB::table('s_source_roster_detail')->where(['id' => $planid, 'status' => 1, 'is_del' => 0])->first();
if (!$planInfo) return \Yz::echoError1('当前时段不可用');
if ($nowdatetime > $planInfo->date . ' ' . $planInfo->end_reservation_time) return \Yz::echoError1('已经超过预约截止时间');
$planCount = DB::table('s_source_roster_detail_count')->where(['roster_detail_id' => $planid,
'appointment_type_id' => $appointment_type])->first();
if ($planCount->count <= $planCount->used_count) return \Yz::echoError1('当前预约时间已无名额');
//判断互斥暂时根据reg_num判断身份
//查询想要预约的项目 其自身code
$item = DB::table('s_check_item')->where(['item_name' => $mainInfo->entrust, 'status' => 1, 'is_del' => 0])->first();
if (!$item) return \Yz::echoError1('此检查项目不可用');
//查询当前检查项目是否存在互斥
$cha_hc = DB::table('s_huchi')->where('is_del', 0)
->where(function ($q) use ($item) {
$q->where(['code1' => $item->item_code])->orWhere('code2', $item->item_code);
})->get();
if (count($cha_hc) > 0) {
//查询用户预约中的医嘱
$status_1 = DB::table('s_list')
->select('s_check_item.item_code', 's_list.entrust', 's_list.reservation_date', 's_list.reservation_time')
->leftJoin('s_check_item', 's_list.entrust', '=', 's_check_item.item_name')
->where(['s_list.reg_num' => $mainInfo->reg_num, 's_list.list_status' => 1, 's_list.is_del' => 0, 's_list.is_nullify' => 0])->get();
if (count($status_1) > 0) {
foreach ($status_1 as $key => $value) {
foreach ($cha_hc as $k => $v) {
if ($v->code1 == $value->item_code or $v->code2 == $value->item_code) {
if ($v->time == 0) {
//如果是永久互斥,直接拒绝
return \Yz::Return(false, '当前预约项目与' . $value->entrust . '互斥,暂不可预约', ['name' => $value->entrust]);
}
if ($v->time > 0) {
//如果设置互斥时间,则判断预约时间是否超过 正在预约的最后时间段+互斥时间
$period = DB::table('s_period')->where(['id' => $value->reservation_time])->first();
$endTime = $period->period_end_time;
$periodEndDateTime = $value->reservation_date . ' ' . $endTime;
$date = new DateTime($periodEndDateTime);
// 添加互斥时间/小时
$date->add(new \DateInterval('PT' . $v->time . 'H')); // PTXH 表示X小时的时间间隔
$HuChi_EndDateTime = $date->format('Y-m-d H:i:s');//已经预约的项目结束互斥时间
$YuYueDateTime = substr($planInfo->date, 0, 10) . ' ' . $planInfo->begin_time;
if ($HuChi_EndDateTime > $YuYueDateTime) {
return \Yz::Return(false, '当前预约项目与' . $value->entrust . '互斥,暂不可预约,请预约' . $HuChi_EndDateTime . '后的日期', ['name' => $value->entrust]);
}
}
}
}
}
}
}
DB::beginTransaction();
try {
//更新计划明细表使用数量
$u = DB::table('s_source_roster_detail_count')->where(['id' => $planCount->id])->where('count', '>', 'used_count')->increment('used_count');
if ($u) {
$cha = DB::table('s_source_roster_detail_count')->where(['id' => $planCount->id])->first();
if ($cha->count >= $cha->used_count) {
//更新主表信息
$u_data=[
'list_status' => 1,
'reservation_date' => $planInfo->date,
'reservation_time' => $planInfo->period_id,
'reservation_sources' => $planInfo->resources_id,
'services_group' => $planInfo->device_id,
'roster_id' => $planInfo->id,
'department_id'=>$planInfo->department_id,
'xuhao' => 0,
'appointment_type_id' => $appointment_type,
];
$u_mainList = DB::table('s_list')->where(['id' => $mainlistid])->update($u_data);
$note="预约";
if ($do_type == 2) {
$note="改约";
//如果是改约,则恢复原来的数量
$u_old = DB::table('s_source_roster_detail_count')->where(['roster_detail_id' => $mainInfo->roster_id, 'appointment_type_id' => $mainInfo->appointment_type_id])->decrement('used_count');
}
$i_log=DB::table('s_list_log')->insert([
'list_id'=>$mainlistid,
'reg_num'=>$mainInfo->reg_num,
'old_status'=>$mainInfo->list_status,
'new_status'=>1,
'create_user'=>null,
'note'=>$note,
'data'=>json_encode($u_data)
]);
if ($u_mainList) {
DB::commit();
return \Yz::Return(true, '预约成功', []);
} else {
DB::rollBack();
return \Yz::echoError1('预约失败');
}
} else {
DB::rollBack();
return \Yz::echoError1('当前预约时间已无名额');
}
} else {
return \Yz::echoError1('操作失败');
}
} catch (\Exception $e) {
DB::rollBack();
return \Yz::echoError1('预约异常');
}
}
public function CancelYuYue($MainListId, $reg_num)
{
date_default_timezone_set('PRC');
$nowdatetime = date("Y-m-d H:i:s");
$mainInfo = DB::table('s_list')->where(['id' => $MainListId,'reg_num'=>$reg_num])->first();
if (!$mainInfo) return \Yz::echoError1('医嘱不存在');
//判断状态
if ($mainInfo->list_status <> 1) return \Yz::echoError1('该记录无法取消,当前状态:' . $mainInfo->list_status);
DB::beginTransaction();
try {
$u_data=[
'list_status' => 0,
'reservation_date' => null,
'reservation_time' => null,
'reservation_sources' => null,
'services_group' => null,
'roster_id' => null,
'xuhao' => null,
'department_id'=>null,
'appointment_type_id' => null,
'canel_time' => $nowdatetime,
];
$u_mainList = DB::table('s_list')->where(['id' => $MainListId])->update($u_data);
$i_log=DB::table('s_list_log')->insert([
'list_id'=>$mainInfo->id,
'reg_num'=>$mainInfo->reg_num,
'old_status'=>$mainInfo->list_status,
'new_status'=>0,
'create_user'=>null,
'note'=>'取消预约',
'data'=>json_encode($u_data)
]);
$u_count = DB::table('s_source_roster_detail_count')->where(['roster_detail_id' => $mainInfo->roster_id, 'appointment_type_id' => $mainInfo->appointment_type_id])->decrement('used_count');
if ($u_mainList && $u_count) {
DB::commit();
return \Yz::Return(true, '取消成功', []);
} else {
DB::rollBack();
return \Yz::echoError1('取消失败');
}
} catch (\Exception $e) {
DB::rollBack();
return \Yz::echoError1('取消异常');
}
}
}