|
|
|
@ -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')
|
|
|
|
DB::beginTransaction();
|
|
|
|
->where(['roster_detail_id' => $planid])
|
|
|
|
try {
|
|
|
|
->whereIn('appointment_type_id', $appointment_types)
|
|
|
|
|
|
|
|
->orderBy('appointment_type_id','asc')
|
|
|
|
//查询号源渠道是否有合并
|
|
|
|
->get();
|
|
|
|
$appointment_types=[$appointment_type];
|
|
|
|
$planZongCount=0;//合并后的各个渠道总名额
|
|
|
|
$appointment_type_link = DB::table('s_appointment_type_ratio')->where(['department_id'=>$planInfo->department_id,'appointment_type_id' => $appointment_type])->first();
|
|
|
|
$planZongUsedCount=0;//合并后的各个渠道已预约的名额
|
|
|
|
if (!empty($appointment_type_link->link)) {
|
|
|
|
$planZongLockedCount=0;//合并后的各个渠道占位名额
|
|
|
|
$appointment_types=json_decode($appointment_type_link->link, true);
|
|
|
|
$plan_qudao_tempCount=[];//拆分各个渠道需要占用的名额
|
|
|
|
$appointment_types=array_merge($appointment_types, [$appointment_type]);
|
|
|
|
$weifenpeiCount=$zhanweiCount;//未分配的名额
|
|
|
|
sort($appointment_types);
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
$roster_detail_counts = DB::table('s_source_roster_detail_count')
|
|
|
|
if ($planZongCount < ($planZongUsedCount + $planZongLockedCount + $zhanweiCount)){
|
|
|
|
->where(['roster_detail_id' => $planid])
|
|
|
|
if($is_emergency!==1){
|
|
|
|
->whereIn('appointment_type_id', $appointment_types)
|
|
|
|
return \Yz::echoError1('当前预约时间名额不足');
|
|
|
|
->orderBy('appointment_type_id','asc')
|
|
|
|
}else{ //如果是紧急预约,则把未分配的 强制加到当前预约渠道
|
|
|
|
->lockForUpdate() // 添加行级锁 ★ 关键
|
|
|
|
foreach ($roster_detail_counts as $key=> $roster_detail_count) {
|
|
|
|
->get();
|
|
|
|
if($roster_detail_count->appointment_type_id==$appointment_type){
|
|
|
|
$planZongCount=0;//合并后的各个渠道总名额
|
|
|
|
$plan_qudao_tempCount[$key]=$weifenpeiCount;
|
|
|
|
$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=[];
|
|
|
|
$userGroup=[];
|
|
|
|
foreach ($oldMainInfos as $key1 => $value1) {
|
|
|
|
foreach ($oldMainInfos as $key1 => $value1) {
|
|
|
|
$reg_num = $value1->reg_num;
|
|
|
|
$reg_num = $value1->reg_num;
|
|
|
|
if (!isset($userGroup[$reg_num])) {
|
|
|
|
if (!isset($userGroup[$reg_num])) {
|
|
|
|
$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][]=[
|
|
|
|
// $huchiList
|
|
|
|
'reg_num' => $value1->reg_num,
|
|
|
|
|
|
|
|
'user_name' => $value1->user_name,
|
|
|
|
|
|
|
|
'entrust_code' => $value1->entrust_code,
|
|
|
|
|
|
|
|
'entrust' => $value1->entrust,
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// $huchiList
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($userGroup as $personEntry) {
|
|
|
|
foreach ($userGroup as $personEntry) {
|
|
|
|
$ids = array_column($personEntry, 'entrust_code');
|
|
|
|
$ids = array_column($personEntry, 'entrust_code');
|
|
|
|
if ($this->isMutuallyExclusive($ids, $huchiList)) {
|
|
|
|
if ($this->isMutuallyExclusive($ids, $huchiList)) {
|
|
|
|
return \Yz::echoError1($personEntry[0]['user_name'].'勾选的项目存在互斥,不可同时预约');
|
|
|
|
return \Yz::echoError1($personEntry[0]['user_name'].'勾选的项目存在互斥,不可同时预约');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
//更新计划明细表使用数量
|
|
|
|
//更新计划明细表使用数量
|
|
|
|
$up_plan_count_all_success =true;
|
|
|
|
$up_plan_count_all_success =true;
|
|
|
|
$appointment_use_plan_detail_arr=[];//预约各个号源占用号源详情
|
|
|
|
$appointment_use_plan_detail_arr=[];//预约各个号源占用号源详情
|
|
|
|
foreach ($roster_detail_counts as $key => $planCount) {
|
|
|
|
foreach ($roster_detail_counts as $key => $planCount) {
|
|
|
|
if($plan_qudao_tempCount[$key]==0) continue;
|
|
|
|
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) {
|
|
|
|
if ($is_emergency!==1) {
|
|
|
|
$query->whereRaw('count >= (used_count + IFNULL(locked_count, 0) + ?)', [$plan_qudao_tempCount[$key]]);
|
|
|
|
$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){
|
|
|
|
if(!$u){
|
|
|
|
$up_plan_count_all_success=false;
|
|
|
|
$up_plan_count_all_success=false;
|
|
|
|
|