doGenerate($data['month']); }); } catch (\Throwable $e) { return [false, $e->getMessage()]; } return [true, '']; } protected function doGenerate(string $month) { $monthStart = Carbon::createFromFormat('Y-m', $month)->startOfMonth()->timestamp; $monthEnd = Carbon::createFromFormat('Y-m', $month)->endOfMonth()->timestamp; $calendar = DB::table('calendar') ->where('del_time', 0) ->where('time', $monthStart) ->first(); if (!$calendar) throw new \Exception('当月日历未配置'); $workDays = DB::table('calendar_details') ->where('del_time', 0) ->where('calendar_id', $calendar->id) ->where('is_work', 1) ->orderBy('time') ->pluck('time') ->toArray(); if (empty($workDays)) throw new \Exception('当月无工作日'); $items = DB::table('item') ->where('del_time', 0) ->where('is_use', 1) ->where('start_time', '<=', $monthEnd) ->where('end_time', '>=', $monthStart) ->get(); $itemIds = $items->pluck('id')->toArray(); if (!empty($itemIds)) { $rds = DB::table('rd') ->where('del_time', 0) ->where('type',RD::type_one) ->whereIn('item_id', $itemIds) ->whereBetween('order_time', [$monthStart, $monthEnd]) ->pluck('id') ->toArray(); if (!empty($rds)) { DB::table('rd_details')->whereIn('rd_id', $rds)->delete(); DB::table('rd')->whereIn('id', $rds)->delete(); } } $employeeIds = DB::table('item_details') ->where('del_time', 0) ->whereIn('item_id', $itemIds) ->where('type', 1) ->pluck('data_id') ->unique() ->toArray(); foreach ($employeeIds as $employeeId) { $this->generateEmployeeMonth($employeeId, $items, $workDays, $calendar); } } protected function generateEmployeeMonth(int $employeeId, $items, array $workDays, $calendar) { $employeeMonth = DB::table('employee_details') ->where('del_time', 0) ->where('employee_id', $employeeId) ->where('time', $calendar->time) ->first(); if (!$employeeMonth || $employeeMonth->total_hours_2 <= 0) return; // 1. 月总工时取整为满小时 (60的倍数) $totalMinutes = intval(round($employeeMonth->total_hours_2 * 60) / 60) * 60; $employeeItems = []; foreach ($items as $item) { $exists = DB::table('item_details') ->where('del_time', 0) ->where('item_id', $item->id) ->where('type', 1) ->where('data_id', $employeeId) ->exists(); if ($exists) $employeeItems[] = $item; } if (empty($employeeItems)) return; $validDays = []; foreach ($workDays as $day) { $dayItems = []; foreach ($employeeItems as $item) { if ($day >= $item->start_time && $day <= $item->end_time) { $dayItems[] = $item; } } if (!empty($dayItems)) $validDays[$day] = $dayItems; } if (empty($validDays)) return; // 2. 随机化每日总工时分布 $daysCount = count($validDays); $avgDailyMinutes = intval(floor($totalMinutes / $daysCount) / 60) * 60; $remainderMinutes = $totalMinutes - ($avgDailyMinutes * $daysCount); $dailyMinutesMap = []; foreach (array_keys($validDays) as $day) { $dailyMinutesMap[$day] = $avgDailyMinutes; } if ($remainderMinutes > 0) { $extraHours = $remainderMinutes / 60; $dayKeys = array_keys($validDays); shuffle($dayKeys); for ($i = 0; $i < $extraHours; $i++) { $targetDay = $dayKeys[$i % $daysCount]; if ($dailyMinutesMap[$targetDay] < 480) { // 最高8小时 (480min) $dailyMinutesMap[$targetDay] += 60; } else { $extraHours++; } } } // 3. 遍历日期并生成明细 foreach ($validDays as $day => $dayItems) { $dailyMinutes = $dailyMinutesMap[$day]; if ($dailyMinutes <= 0) continue; $slots = []; $remaining = $dailyMinutes; $numItems = count($dayItems); for ($i = 0; $i < $numItems; $i++) { if ($i === $numItems - 1) { $slotMinutes = $remaining; } else { // 随机分配项目工时 $maxSlot = intval(floor($remaining * 0.7) / 60) * 60; $slotMinutes = ($maxSlot >= 60) ? mt_rand(1, $maxSlot / 60) * 60 : 0; } $slots[] = $slotMinutes; $remaining -= $slotMinutes; } foreach ($dayItems as $idx => $item) { if (isset($slots[$idx]) && $slots[$idx] > 0) { $this->createRd($item->id, $day, $slots[$idx], $employeeId); } } } } protected function createRd(int $itemId, int $dayTime, int $minutes, int $employeeId) { $dayStart = Carbon::createFromTimestamp($dayTime)->setTime(8, 0); $lunchStart = Carbon::createFromTimestamp($dayTime)->setTime(11, 30); $lunchEnd = Carbon::createFromTimestamp($dayTime)->setTime(12, 0); $dayEnd = Carbon::createFromTimestamp($dayTime)->setTime(16, 30); $availableMinutes = 480; // 8:00-16:30 扣除 30min 午休 $minutes = min($minutes, $availableMinutes); // 计算最大可偏移量(单位:分钟),确保不会超出 16:30 $maxOffset = $availableMinutes - $minutes; // 随机偏移:以 30 分钟为步长进行随机 (0, 30, 60...) $randomOffset = (intval($maxOffset / 30) > 0) ? mt_rand(0, intval($maxOffset / 30)) * 30 : 0; $start = $dayStart->copy()->addMinutes($randomOffset); $end = $start->copy(); $remaining = $minutes; // 逻辑:如果开始时间在午休前 if ($start->lt($lunchStart)) { $morningLeft = $lunchStart->diffInMinutes($start); if ($remaining <= $morningLeft) { $end->addMinutes($remaining); } else { // 跨午休逻辑 $remaining -= $morningLeft; $end = $lunchEnd->copy()->addMinutes($remaining); } } else { // 开始时间就在午休后 $end->addMinutes($remaining); } // 最终校验防止越界(虽然 offset 已做限制) if ($end->gt($dayEnd)) { $end = $dayEnd->copy(); } $crtTime = Carbon::createFromTimestamp($dayTime)->setTime(16, 30)->addSeconds(mt_rand(0, 1800))->timestamp; $orderNumber = 'RD' . date('Ymd', $dayTime) . mt_rand(1000, 9999) . substr(uniqid(), -3); $rdId = DB::table('rd')->insertGetId([ 'crt_time' => $crtTime, 'order_time' => $dayTime, 'item_id' => $itemId, 'start_time_hour' => $start->hour, 'start_time_min' => $start->minute, 'end_time_hour' => $end->hour, 'end_time_min' => $end->minute, 'total_hours' => $minutes, 'order_number' => $orderNumber, 'type' => 1, 'del_time' => 0 ]); DB::table('rd_details')->insert([ 'rd_id' => $rdId, 'data_id' => $employeeId, 'type' => 1, 'crt_time' => $crtTime, 'upd_time' => $crtTime, 'del_time' => 0 ]); } }