|
|
|
|
@ -393,6 +393,185 @@ FROM
|
|
|
|
|
return \Yz::Return(true, '查询完成', $planInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//批量生成所有有效模板的排班明细
|
|
|
|
|
public function BatchGenerateByDateRange(Request $request, RosterService $rosterService)
|
|
|
|
|
{
|
|
|
|
|
$dateRange = request('dateRange');
|
|
|
|
|
$HolidayEnable = request('HolidayEnable', 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allModels = $templatesQuery->get();
|
|
|
|
|
|
|
|
|
|
if ($allModels->isEmpty()) {
|
|
|
|
|
continue; // 该 time_mode 下没有模板,跳过
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$currentDate->modify('+1 day');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allModels = $validTemplates;
|
|
|
|
|
|
|
|
|
|
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'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return \Yz::echoError1($e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 时令日期匹配:判断日期是否在时令范围内
|
|
|
|
|
* - 如果资源 time_mode != 1 或模板 type == 0,不需要时令校验,返回 true
|
|
|
|
|
* - 否则检查日期是否在对应季节的周期范围内
|
|
|
|
|
*/
|
|
|
|
|
private function isDateInSeasonalRange($currentDate, $modelType, $resource)
|
|
|
|
|
{
|
|
|
|
|
// 不需要时令校验
|
|
|
|
|
if ($resource->time_mode != 1 || $modelType == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$currentMMDD = $currentDate->format('m-d');
|
|
|
|
|
|
|
|
|
|
// 解析 time_range
|
|
|
|
|
$time_range = json_decode($resource->time_range, true);
|
|
|
|
|
|
|
|
|
|
if (!$time_range || !is_array($time_range)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 遍历所有季节周期
|
|
|
|
|
foreach ($time_range as $season) {
|
|
|
|
|
// 转换类型进行匹配(字符串 "1"/"2" vs 整数 1/2)
|
|
|
|
|
if ((string)($season['type'] ?? '') === (string)$modelType) {
|
|
|
|
|
// 检查是否在任意周期范围内
|
|
|
|
|
if (isset($season['periods']) && is_array($season['periods'])) {
|
|
|
|
|
foreach ($season['periods'] as $period) {
|
|
|
|
|
$start = $period['start'] ?? '';
|
|
|
|
|
$end = $period['end'] ?? '';
|
|
|
|
|
|
|
|
|
|
if (!empty($start) && !empty($end) &&
|
|
|
|
|
$currentMMDD >= $start && $currentMMDD <= $end) {
|
|
|
|
|
return true; // 匹配成功
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false; // 不在时令范围内
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//保存占位数量
|
|
|
|
|
public function SaveLockedCount(Request $request)
|
|
|
|
|
{
|
|
|
|
|
|