预约号源BUG

main
鹿和sa0ChunLuyu 2 days ago
parent b68389244b
commit 260b608e83

@ -313,7 +313,7 @@ public function GetEnablePlan($regnum, $entrustids, $episodeid, $appointment_typ
$existingSharedAtPlan = []; $existingSharedAtPlan = [];
if (!empty($regnum)) { if (!empty($regnum)) {
$existingSharedItems = DB::table('s_list') $existingSharedItems = DB::table('s_list')
->join('s_check_item', 's_list.entrust', '=', 's_check_item.item_name') ->join('s_check_item', 's_list.entrust_code', '=', 's_check_item.item_code')
->where('s_list.reg_num', $regnum) ->where('s_list.reg_num', $regnum)
->where('s_list.list_status', 1) ->where('s_list.list_status', 1)
->where('s_list.roster_id', '>', 0) ->where('s_list.roster_id', '>', 0)
@ -557,7 +557,7 @@ public function GetOptimalPlan($type, $regnum, $entrustids, $episodeid, $appoint
$shareSlotMap = []; $shareSlotMap = [];
if (!empty($entrustIds)) { if (!empty($entrustIds)) {
$itemData = DB::table('s_list') $itemData = DB::table('s_list')
->join('s_check_item', 's_list.entrust', '=', 's_check_item.item_name') ->join('s_check_item', 's_list.entrust_code', '=', 's_check_item.item_code')
->whereIn('s_list.entrust_id', $entrustIds) ->whereIn('s_list.entrust_id', $entrustIds)
->select('s_list.entrust_id', 's_check_item.use_seats', 's_check_item.is_share_slot') ->select('s_list.entrust_id', 's_check_item.use_seats', 's_check_item.is_share_slot')
->get(); ->get();
@ -602,7 +602,7 @@ public function GetOptimalPlan($type, $regnum, $entrustids, $episodeid, $appoint
// 查询该患者已有共享项目在各排班下的 max // 查询该患者已有共享项目在各排班下的 max
$existingSharedAtPlan = []; $existingSharedAtPlan = [];
$existingSharedItems = DB::table('s_list') $existingSharedItems = DB::table('s_list')
->join('s_check_item', 's_list.entrust', '=', 's_check_item.item_name') ->join('s_check_item', 's_list.entrust_code', '=', 's_check_item.item_code')
->where('s_list.reg_num', $regnum) ->where('s_list.reg_num', $regnum)
->where('s_list.list_status', 1) ->where('s_list.list_status', 1)
->where('s_list.is_nullify', 0) ->where('s_list.is_nullify', 0)
@ -1108,10 +1108,10 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
//查询用户预约中的医嘱 //查询用户预约中的医嘱
$status_1 = DB::table('s_list') $status_1 = DB::table('s_list')
->select('s_check_item.item_code', 's_list.entrust', 's_list.reservation_date', 's_list.reservation_time') ->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') ->leftJoin('s_check_item', 's_list.entrust_code', '=', 's_check_item.item_code')
->where(['s_list.reg_num' => $mainInfo->reg_num, 's_list.list_status' => 1, 's_list.is_del' => 0, 's_list.is_nullify' => 0]) ->where(['s_list.reg_num' => $mainInfo->reg_num, 's_list.list_status' => 1, 's_list.is_del' => 0, 's_list.is_nullify' => 0])
//排除自身,自己不能互斥自己 //排除自身,自己不能互斥自己
->whereNotIn('item_name', [$mainInfo->entrust]) ->whereNotIn('s_list.entrust_code', [$mainInfo->entrust_code])
->get(); ->get();
if (count($status_1) > 0) { if (count($status_1) > 0) {
foreach ($status_1 as $key => $value) { foreach ($status_1 as $key => $value) {
@ -1196,7 +1196,7 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
} }
// 共享号源项目合并计算只保留最大值其余设为0 // 共享号源预计算(不依赖排班数据的部分)
$shareSlotIds = []; $shareSlotIds = [];
$shareSlotMax = 0; $shareSlotMax = 0;
foreach ($itemSeatsData as $id => $data) { foreach ($itemSeatsData as $id => $data) {
@ -1205,14 +1205,61 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
$shareSlotMax = max($shareSlotMax, $data['use_seats']); $shareSlotMax = max($shareSlotMax, $data['use_seats']);
} }
} }
$maxShareSlotId = null;
// 查询该排班下已有共享项目的 max $maxShareSeats = 0;
foreach ($itemSeatsData as $id => $data) {
if (!empty($data['is_share_slot']) && $data['use_seats'] > $maxShareSeats) {
$maxShareSeats = $data['use_seats'];
$maxShareSlotId = $id;
}
}
// 以下变量在事务内计算
$existingSharedMax = 0; $existingSharedMax = 0;
$additionalNeeded = 0; $additionalNeeded = 0;
$zhanweiCount = 0;
DB::beginTransaction();
try {
// 改约时提前记录旧排班 ID在 s_list 更新后校正
$oldRosterIds = [];
if ($do_type == 2) {
foreach ($oldMainInfos as $oldMainInfo) {
$this->releaseSourceFromDetail($oldMainInfo->id, $oldMainInfo);
if ($oldMainInfo->roster_id) {
$oldRosterIds[] = $oldMainInfo->roster_id;
}
}
$oldRosterIds = array_unique($oldRosterIds);
// 锁定旧排班渠道行,防止并发竞态
if (!empty($oldRosterIds)) {
DB::table('s_source_roster_detail_count')
->whereIn('roster_detail_id', $oldRosterIds)
->lockForUpdate()
->get();
}
}
//查询号源渠道是否有合并
$appointment_types=[$appointment_type];
$appointment_type_link = DB::table('s_appointment_type_ratio')->where(['department_id'=>$planInfo->department_id,'appointment_type_id' => $appointment_type])->first();
if (!empty($appointment_type_link->link)) {
$appointment_types=json_decode($appointment_type_link->link, true);
$appointment_types=array_merge($appointment_types, [$appointment_type]);
sort($appointment_types);
}
$roster_detail_counts = DB::table('s_source_roster_detail_count')
->where(['roster_detail_id' => $planid])
->whereIn('appointment_type_id', $appointment_types)
->orderBy('appointment_type_id','asc')
->lockForUpdate() // 添加行级锁 ★ 关键
->get();
// === 共享号源计算(在锁保护下查询已有共享占位)===
$regNum = $oldMainInfos[0]->reg_num ?? null; $regNum = $oldMainInfos[0]->reg_num ?? null;
if (!empty($shareSlotIds) && $regNum) { if (!empty($shareSlotIds) && $regNum) {
$existingSharedItems = DB::table('s_list') $existingSharedItems = DB::table('s_list')
->join('s_check_item', 's_list.entrust', '=', 's_check_item.item_name') ->join('s_check_item', 's_list.entrust_code', '=', 's_check_item.item_code')
->where('s_list.reg_num', $regNum) ->where('s_list.reg_num', $regNum)
->where('s_list.roster_id', $planid) ->where('s_list.roster_id', $planid)
->where('s_list.list_status', 1) ->where('s_list.list_status', 1)
@ -1225,18 +1272,6 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
$overallMax = max($existingSharedMax, $shareSlotMax); $overallMax = max($existingSharedMax, $shareSlotMax);
$additionalNeeded = max(0, $overallMax - $existingSharedMax); $additionalNeeded = max(0, $overallMax - $existingSharedMax);
} }
// 找到 use_seats 最高的共享项目,将 max 分配给它
$maxShareSlotId = null;
$maxShareSeats = 0;
foreach ($itemSeatsData as $id => $data) {
if (!empty($data['is_share_slot']) && $data['use_seats'] > $maxShareSeats) {
$maxShareSeats = $data['use_seats'];
$maxShareSlotId = $id;
}
}
$zhanweiCount = 0;
foreach ($itemSeatsData as $id => $data) { foreach ($itemSeatsData as $id => $data) {
if (!empty($data['is_share_slot'])) { if (!empty($data['is_share_slot'])) {
if ($id === $maxShareSlotId) { if ($id === $maxShareSlotId) {
@ -1250,7 +1285,6 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
$zhanweiCount += $data['use_seats']; $zhanweiCount += $data['use_seats'];
} }
} }
Log::info('[YuYueDebug] YuYue 预约参数', [ Log::info('[YuYueDebug] YuYue 预约参数', [
'planid' => $planid, 'planid' => $planid,
'mainlistids' => $mainlistids, 'mainlistids' => $mainlistids,
@ -1263,35 +1297,6 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
'zhanweiCount' => $zhanweiCount, 'zhanweiCount' => $zhanweiCount,
]); ]);
DB::beginTransaction();
try {
// 改约时提前记录旧排班 ID在 s_list 更新后校正
$oldRosterIds = [];
if ($do_type == 2) {
foreach ($oldMainInfos as $oldMainInfo) {
$this->releaseSourceFromDetail($oldMainInfo->id, $oldMainInfo);
if ($oldMainInfo->roster_id) {
$oldRosterIds[] = $oldMainInfo->roster_id;
}
}
$oldRosterIds = array_unique($oldRosterIds);
}
//查询号源渠道是否有合并
$appointment_types=[$appointment_type];
$appointment_type_link = DB::table('s_appointment_type_ratio')->where(['department_id'=>$planInfo->department_id,'appointment_type_id' => $appointment_type])->first();
if (!empty($appointment_type_link->link)) {
$appointment_types=json_decode($appointment_type_link->link, true);
$appointment_types=array_merge($appointment_types, [$appointment_type]);
sort($appointment_types);
}
$roster_detail_counts = DB::table('s_source_roster_detail_count')
->where(['roster_detail_id' => $planid])
->whereIn('appointment_type_id', $appointment_types)
->orderBy('appointment_type_id','asc')
->lockForUpdate() // 添加行级锁 ★ 关键
->get();
$planZongCount=0;//合并后的各个渠道总名额 $planZongCount=0;//合并后的各个渠道总名额
$planZongUsedCount=0;//合并后的各个渠道已预约的名额 $planZongUsedCount=0;//合并后的各个渠道已预约的名额
$planZongLockedCount=0;//合并后的各个渠道占位名额 $planZongLockedCount=0;//合并后的各个渠道占位名额
@ -1579,6 +1584,7 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
// === 号源总量校验结束 === // === 号源总量校验结束 ===
$list_all_success = true; $list_all_success = true;
$savedDataMap = []; // 记录每条项目的实际写入数据
foreach ($mainlistids as $id) { foreach ($mainlistids as $id) {
$details = []; $details = [];
if (isset($itemChannelMap[$id])) { if (isset($itemChannelMap[$id])) {
@ -1636,6 +1642,7 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
$list_all_success = false; $list_all_success = false;
break; break;
} }
$savedDataMap[$id] = $u_data;
// 更新后读回确认 // 更新后读回确认
$afterUpdate = DB::table('s_list')->where('id', $id)->value('appointment_use_plan_detail'); $afterUpdate = DB::table('s_list')->where('id', $id)->value('appointment_use_plan_detail');
@ -1660,7 +1667,7 @@ public function YuYue($planid, $appointment_type, $mainlistids, $do_type,$is_eme
'new_status' => 1, 'new_status' => 1,
'create_user' => $do_user, 'create_user' => $do_user,
'note' => $note, 'note' => $note,
'data' => json_encode($u_data) 'data' => json_encode($savedDataMap[$oldMainInfo->id] ?? [])
]); ]);
} }

Loading…
Cancel
Save