占用号源加锁

main
岩仔88 4 days ago
parent 130ba4c3cd
commit a5fa190d4c

@ -319,89 +319,102 @@ WHERE
}
//查询号源渠道是否有合并
$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')
->get();
$planZongCount=0;//合并后的各个渠道总名额
$planZongUsedCount=0;//合并后的各个渠道已预约的名额
$planZongLockedCount=0;//合并后的各个渠道占位名额
$plan_qudao_tempCount=[];//拆分各个渠道需要占用的名额
$weifenpeiCount=$zhanweiCount;//未分配的名额
foreach ($roster_detail_counts as $roster_detail_count) {
$planZongCount+=$roster_detail_count->count;
$planZongUsedCount+=$roster_detail_count->used_count;
$planZongLockedCount+=($roster_detail_count->locked_count ?? 0);
$keyongCount=$roster_detail_count->count - $roster_detail_count->used_count - ($roster_detail_count->locked_count ?? 0);
if($weifenpeiCount-$keyongCount>=0){
$plan_qudao_tempCount[]=$keyongCount;
$weifenpeiCount-=$keyongCount;
}else{
$plan_qudao_tempCount[]=$weifenpeiCount;
$weifenpeiCount=0;
DB::beginTransaction();
try {
//查询号源渠道是否有合并
$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);
}
}
if ($planZongCount < ($planZongUsedCount + $planZongLockedCount + $zhanweiCount)){
if($is_emergency!==1){
return \Yz::echoError1('当前预约时间名额不足');
}else{ //如果是紧急预约,则把未分配的 强制加到当前预约渠道
foreach ($roster_detail_counts as $key=> $roster_detail_count) {
if($roster_detail_count->appointment_type_id==$appointment_type){
$plan_qudao_tempCount[$key]=$weifenpeiCount;
$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;//合并后的各个渠道总名额
$planZongUsedCount=0;//合并后的各个渠道已预约的名额
$planZongLockedCount=0;//合并后的各个渠道占位名额
$plan_qudao_tempCount=[];//拆分各个渠道需要占用的名额
$weifenpeiCount=$zhanweiCount;//未分配的名额
foreach ($roster_detail_counts as $roster_detail_count) {
$planZongCount+=$roster_detail_count->count;
$planZongUsedCount+=$roster_detail_count->used_count;
$planZongLockedCount+=($roster_detail_count->locked_count ?? 0);
$keyongCount=$roster_detail_count->count - $roster_detail_count->used_count - ($roster_detail_count->locked_count ?? 0);
if($weifenpeiCount-$keyongCount>=0){
$plan_qudao_tempCount[]=$keyongCount;
$weifenpeiCount-=$keyongCount;
}else{
$plan_qudao_tempCount[]=$weifenpeiCount;
$weifenpeiCount=0;
}
}
if ($planZongCount < ($planZongUsedCount + $planZongLockedCount + $zhanweiCount)){
if($is_emergency!==1){
return \Yz::echoError1('当前预约时间名额不足');
}else{ //如果是紧急预约,则把未分配的 强制加到当前预约渠道
foreach ($roster_detail_counts as $key=> $roster_detail_count) {
if($roster_detail_count->appointment_type_id==$appointment_type){
$plan_qudao_tempCount[$key]=$weifenpeiCount;
}
}
}
}
}
//判断某人这些待预约项目里,是否存在互斥
$userGroup=[];
foreach ($oldMainInfos as $key1 => $value1) {
$reg_num = $value1->reg_num;
if (!isset($userGroup[$reg_num])) {
$userGroup[$reg_num] = [];
//判断某人这些待预约项目里,是否存在互斥
$userGroup=[];
foreach ($oldMainInfos as $key1 => $value1) {
$reg_num = $value1->reg_num;
if (!isset($userGroup[$reg_num])) {
$userGroup[$reg_num] = [];
}
$userGroup[$reg_num][]=[
'reg_num' => $value1->reg_num,
'user_name' => $value1->user_name,
'entrust_code' => $value1->entrust_code,
'entrust' => $value1->entrust,
];
}
$userGroup[$reg_num][]=[
'reg_num' => $value1->reg_num,
'user_name' => $value1->user_name,
'entrust_code' => $value1->entrust_code,
'entrust' => $value1->entrust,
];
}
// $huchiList
// $huchiList
foreach ($userGroup as $personEntry) {
foreach ($userGroup as $personEntry) {
$ids = array_column($personEntry, 'entrust_code');
if ($this->isMutuallyExclusive($ids, $huchiList)) {
return \Yz::echoError1($personEntry[0]['user_name'].'勾选的项目存在互斥,不可同时预约');
}
}
DB::beginTransaction();
try {
}
//更新计划明细表使用数量
$up_plan_count_all_success =true;
$appointment_use_plan_detail_arr=[];//预约各个号源占用号源详情
foreach ($roster_detail_counts as $key => $planCount) {
if($plan_qudao_tempCount[$key]==0) continue;
$currentVersion = $planCount->version ?? 0;
$query = DB::table('s_source_roster_detail_count')->where(['id' => $planCount->id]);
$query = DB::table('s_source_roster_detail_count')
->where(['id' => $planCount->id])
->where('version', $currentVersion);
// 【核心修改】只有非紧急预约时,才检查名额是否充足
if ($is_emergency!==1) {
$query->whereRaw('count >= (used_count + IFNULL(locked_count, 0) + ?)', [$plan_qudao_tempCount[$key]]);
}
$u = $query->increment('used_count', $plan_qudao_tempCount[$key]);
// $u = $query->increment('used_count', $plan_qudao_tempCount[$key]);
$u = $query->update([
'used_count' => DB::raw("used_count + {$plan_qudao_tempCount[$key]}"),
'version' => DB::raw("version + 1"),
// 如果需要更新更新时间,可以在这里加 'updated_at' => now()
]);
if(!$u){
$up_plan_count_all_success=false;

Loading…
Cancel
Save