|
|
|
|
@ -402,135 +402,190 @@ public function BatchGenerateByDateRange(Request $request, RosterService $roster
|
|
|
|
|
{
|
|
|
|
|
$dateRange = request('dateRange');
|
|
|
|
|
$HolidayEnable = request('HolidayEnable', 1);
|
|
|
|
|
$autoMode = request('autoMode', 1);
|
|
|
|
|
$userid = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 1. 查询所有资源,按 time_mode 分组
|
|
|
|
|
$resources = DB::table('s_department_resources')
|
|
|
|
|
->where(['is_del' => 0])
|
|
|
|
|
->get()
|
|
|
|
|
->groupBy('time_mode');
|
|
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
|
$totalCount = 0;
|
|
|
|
|
|
|
|
|
|
// 2. 遍历不同 time_mode 的资源
|
|
|
|
|
foreach ($resources as $timeMode => $resourceGroup) {
|
|
|
|
|
$resourceIds = $resourceGroup->pluck('id')->toArray();
|
|
|
|
|
|
|
|
|
|
// 3. 根据 time_mode 查询对应的模板
|
|
|
|
|
$templatesQuery = DB::table('s_source_roster')
|
|
|
|
|
->leftJoin('s_department_resources', 's_source_roster.resources_id', '=', 's_department_resources.id')
|
|
|
|
|
->select(
|
|
|
|
|
's_source_roster.*',
|
|
|
|
|
's_department_resources.department_resources_name',
|
|
|
|
|
's_department_resources.time_mode',
|
|
|
|
|
's_department_resources.time_range'
|
|
|
|
|
)
|
|
|
|
|
->whereIn('s_source_roster.resources_id', $resourceIds)
|
|
|
|
|
->where(['s_source_roster.status' => 1, 's_source_roster.is_del' => 0]);
|
|
|
|
|
|
|
|
|
|
if ($timeMode == 1) {
|
|
|
|
|
// 时令资源: 只查询 type=1 或 2 的模板
|
|
|
|
|
$templatesQuery->whereIn('s_source_roster.type', [1, 2]);
|
|
|
|
|
} else {
|
|
|
|
|
// 普通资源: 只查询 type=0 的模板
|
|
|
|
|
$templatesQuery->where('s_source_roster.type', 0);
|
|
|
|
|
}
|
|
|
|
|
if ($autoMode == 1) {
|
|
|
|
|
// 自动模式:按顶级科室的 auto_gen_days 生成
|
|
|
|
|
$depts = DB::table('s_department')
|
|
|
|
|
->where('pid', 0)
|
|
|
|
|
->where('is_del', 0)
|
|
|
|
|
->where('appointment_enabled', 1)
|
|
|
|
|
->where('auto_gen_days', '>', 0)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
$allResults = [];
|
|
|
|
|
$allTotal = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($depts as $dept) {
|
|
|
|
|
$today = date('Y-m-d');
|
|
|
|
|
$endDate = date('Y-m-d', strtotime("+{$dept->auto_gen_days} days"));
|
|
|
|
|
$deptDateRange = [$today, $endDate];
|
|
|
|
|
|
|
|
|
|
$resources = DB::table('s_department_resources')
|
|
|
|
|
->where('department_id', $dept->id)
|
|
|
|
|
->where('is_del', 0)
|
|
|
|
|
->get()
|
|
|
|
|
->groupBy('time_mode');
|
|
|
|
|
|
|
|
|
|
if ($resources->isEmpty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allModels = $templatesQuery->get();
|
|
|
|
|
$res = $this->generateBatchForResources(
|
|
|
|
|
$resources, $deptDateRange, $HolidayEnable, $userid, $rosterService
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($allModels->isEmpty()) {
|
|
|
|
|
continue; // 该 time_mode 下没有模板,跳过
|
|
|
|
|
$allResults = array_merge($allResults, $res['details']);
|
|
|
|
|
$allTotal += $res['total_count'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. 时令模板根据资源的 time_range 进行匹配
|
|
|
|
|
if ($timeMode == 1) {
|
|
|
|
|
$validTemplates = collect();
|
|
|
|
|
$startDate = new DateTime($dateRange[0]);
|
|
|
|
|
$endDate = new DateTime($dateRange[1]);
|
|
|
|
|
$currentDate = clone $startDate;
|
|
|
|
|
|
|
|
|
|
while ($currentDate <= $endDate) {
|
|
|
|
|
foreach ($allModels as $model) {
|
|
|
|
|
$resource = $resourceGroup->where('id', $model->resources_id)->first();
|
|
|
|
|
|
|
|
|
|
// 检查日期是否在时令范围内
|
|
|
|
|
if ($this->isDateInSeasonalRange($currentDate, $model->type, $resource)) {
|
|
|
|
|
if (!$validTemplates->contains('id', $model->id)) {
|
|
|
|
|
$validTemplates->push($model);
|
|
|
|
|
}
|
|
|
|
|
$successCount = collect($allResults)->where('status', 'success')->count();
|
|
|
|
|
$failedCount = collect($allResults)->where('status', 'failed')->count();
|
|
|
|
|
|
|
|
|
|
return \Yz::Return(
|
|
|
|
|
true,
|
|
|
|
|
"自动生成完成,共 {$allTotal} 条 (成功: {$successCount} 资源, 失败: {$failedCount} 资源)",
|
|
|
|
|
[
|
|
|
|
|
'total_count' => $allTotal,
|
|
|
|
|
'success_count' => $successCount,
|
|
|
|
|
'failed_count' => $failedCount,
|
|
|
|
|
'details' => $allResults
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// 手动模式:使用入参 dateRange,走原有逻辑
|
|
|
|
|
$resources = DB::table('s_department_resources')
|
|
|
|
|
->where(['is_del' => 0])
|
|
|
|
|
->get()
|
|
|
|
|
->groupBy('time_mode');
|
|
|
|
|
|
|
|
|
|
$res = $this->generateBatchForResources(
|
|
|
|
|
$resources, $dateRange, $HolidayEnable, $userid, $rosterService
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return \Yz::Return(
|
|
|
|
|
true,
|
|
|
|
|
"批量生成完成,共 {$res['total_count']} 条 (成功: {$res['success_count']} 资源, 失败: {$res['failed_count']} 资源)",
|
|
|
|
|
$res
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return \Yz::echoError1($e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量生成号源(核心逻辑,按资源分组遍历)
|
|
|
|
|
*/
|
|
|
|
|
private function generateBatchForResources($resources, $dateRange, $HolidayEnable, $userid, RosterService $rosterService)
|
|
|
|
|
{
|
|
|
|
|
$results = [];
|
|
|
|
|
$totalCount = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($resources as $timeMode => $resourceGroup) {
|
|
|
|
|
$resourceIds = $resourceGroup->pluck('id')->toArray();
|
|
|
|
|
|
|
|
|
|
$templatesQuery = DB::table('s_source_roster')
|
|
|
|
|
->leftJoin('s_department_resources', 's_source_roster.resources_id', '=', 's_department_resources.id')
|
|
|
|
|
->select(
|
|
|
|
|
's_source_roster.*',
|
|
|
|
|
's_department_resources.department_resources_name',
|
|
|
|
|
's_department_resources.time_mode',
|
|
|
|
|
's_department_resources.time_range'
|
|
|
|
|
)
|
|
|
|
|
->whereIn('s_source_roster.resources_id', $resourceIds)
|
|
|
|
|
->where(['s_source_roster.status' => 1, 's_source_roster.is_del' => 0]);
|
|
|
|
|
|
|
|
|
|
if ($timeMode == 1) {
|
|
|
|
|
$templatesQuery->whereIn('s_source_roster.type', [1, 2]);
|
|
|
|
|
} else {
|
|
|
|
|
$templatesQuery->where('s_source_roster.type', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allModels = $templatesQuery->get();
|
|
|
|
|
|
|
|
|
|
if ($allModels->isEmpty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($timeMode == 1) {
|
|
|
|
|
$validTemplates = collect();
|
|
|
|
|
$startDate = new DateTime($dateRange[0]);
|
|
|
|
|
$endDate = new DateTime($dateRange[1]);
|
|
|
|
|
$currentDate = clone $startDate;
|
|
|
|
|
|
|
|
|
|
while ($currentDate <= $endDate) {
|
|
|
|
|
foreach ($allModels as $model) {
|
|
|
|
|
$resource = $resourceGroup->where('id', $model->resources_id)->first();
|
|
|
|
|
|
|
|
|
|
if ($this->isDateInSeasonalRange($currentDate, $model->type, $resource)) {
|
|
|
|
|
if (!$validTemplates->contains('id', $model->id)) {
|
|
|
|
|
$validTemplates->push($model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$currentDate->modify('+1 day');
|
|
|
|
|
}
|
|
|
|
|
$currentDate->modify('+1 day');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allModels = $validTemplates;
|
|
|
|
|
$allModels = $validTemplates;
|
|
|
|
|
|
|
|
|
|
if ($allModels->isEmpty()) {
|
|
|
|
|
continue; // 没有匹配的时令模板,跳过
|
|
|
|
|
}
|
|
|
|
|
if ($allModels->isEmpty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. 按资源分组,按 date_type 细分,循环调用 generatePlans
|
|
|
|
|
$groupedByResource = $allModels->groupBy('resources_id');
|
|
|
|
|
|
|
|
|
|
foreach ($groupedByResource as $resourcesId => $models) {
|
|
|
|
|
$groupedByDateType = $models->groupBy('date_type');
|
|
|
|
|
$resourceCount = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
foreach ($groupedByDateType as $dateType => $typeModels) {
|
|
|
|
|
$typeModelIds = $typeModels->pluck('id')->toArray();
|
|
|
|
|
|
|
|
|
|
$result = $rosterService->generatePlans(
|
|
|
|
|
$dateRange,
|
|
|
|
|
$typeModelIds,
|
|
|
|
|
$userid,
|
|
|
|
|
$dateType,
|
|
|
|
|
$HolidayEnable
|
|
|
|
|
);
|
|
|
|
|
$resourceCount += $result['count'];
|
|
|
|
|
}
|
|
|
|
|
$groupedByResource = $allModels->groupBy('resources_id');
|
|
|
|
|
|
|
|
|
|
foreach ($groupedByResource as $resourcesId => $models) {
|
|
|
|
|
$groupedByDateType = $models->groupBy('date_type');
|
|
|
|
|
$resourceCount = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
foreach ($groupedByDateType as $dateType => $typeModels) {
|
|
|
|
|
$typeModelIds = $typeModels->pluck('id')->toArray();
|
|
|
|
|
|
|
|
|
|
$results[] = [
|
|
|
|
|
'resources_id' => $resourcesId,
|
|
|
|
|
'resources_name' => $models->first()->department_resources_name ?? '',
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'count' => $resourceCount
|
|
|
|
|
];
|
|
|
|
|
$totalCount += $resourceCount;
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$results[] = [
|
|
|
|
|
'resources_id' => $resourcesId,
|
|
|
|
|
'resources_name' => $models->first()->department_resources_name ?? '',
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'count' => 0
|
|
|
|
|
];
|
|
|
|
|
$result = $rosterService->generatePlans(
|
|
|
|
|
$dateRange,
|
|
|
|
|
$typeModelIds,
|
|
|
|
|
$userid,
|
|
|
|
|
$dateType,
|
|
|
|
|
$HolidayEnable
|
|
|
|
|
);
|
|
|
|
|
$resourceCount += $result['count'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$results[] = [
|
|
|
|
|
'resources_id' => $resourcesId,
|
|
|
|
|
'resources_name' => $models->first()->department_resources_name ?? '',
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'count' => $resourceCount
|
|
|
|
|
];
|
|
|
|
|
$totalCount += $resourceCount;
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$results[] = [
|
|
|
|
|
'resources_id' => $resourcesId,
|
|
|
|
|
'resources_name' => $models->first()->department_resources_name ?? '',
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'count' => 0
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. 统计结果
|
|
|
|
|
$successCount = collect($results)->where('status', 'success')->count();
|
|
|
|
|
$failedCount = collect($results)->where('status', 'failed')->count();
|
|
|
|
|
|
|
|
|
|
return \Yz::Return(
|
|
|
|
|
true,
|
|
|
|
|
"批量生成完成,共 {$totalCount} 条 (成功: {$successCount} 资源, 失败: {$failedCount} 资源)",
|
|
|
|
|
[
|
|
|
|
|
'total_count' => $totalCount,
|
|
|
|
|
'success_count' => $successCount,
|
|
|
|
|
'failed_count' => $failedCount,
|
|
|
|
|
'details' => $results
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$successCount = collect($results)->where('status', 'success')->count();
|
|
|
|
|
$failedCount = collect($results)->where('status', 'failed')->count();
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return \Yz::echoError1($e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'total_count' => $totalCount,
|
|
|
|
|
'success_count' => $successCount,
|
|
|
|
|
'failed_count' => $failedCount,
|
|
|
|
|
'details' => $results
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|