You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Controller的index方法添加开关条件控制自动签退逻辑

实现自动签退的开关控制

首先从数据库中读取复选框的状态值,假设你的设置存储在settings表中,用auto_clockout_status字段记录ON/OFF状态。接下来把自动签退逻辑包裹在状态判断里,仅当状态为OFF时执行。修改后的完整index方法如下:

public function index() 
{
    if (permission::permitted('dashboard')=='fail'){ return redirect()->route('denied'); }

    // 获取自动签退开关状态(根据实际表结构调整)
    $autoClockoutStatus = DB::table('settings')
        ->where('key', 'auto_clockout_status') // 替换为你实际的设置标识字段
        ->value('value'); // 替换为存储ON/OFF的字段

    // 开关为OFF时执行自动签退逻辑
    if ($autoClockoutStatus === 'OFF') {
        // Updated Code of Auto Clockout Start
        $clockedInUsers = DB::table('people_attendance')
            ->whereNull('timeout')
            ->get();

        foreach ($clockedInUsers as $user) {
            $timeIn = Carbon::parse($user->timein);
            $currentTime = Carbon::now();

            // 获取用户当日首次签到时间
            $firstClockIn = DB::table('people_attendance')
                ->where('employee', $user->employee)
                ->where('date', $timeIn->format('Y-m-d'))
                ->orderBy('timein')
                ->first();

            if (!$firstClockIn) {
                continue; // 跳过当日无签到记录的用户
            }

            $firstClockInTime = Carbon::parse($firstClockIn->timein);
            $clockInCount = DB::table('people_attendance')
                ->where('employee', $user->employee)
                ->where('date', $timeIn->format('Y-m-d'))
                ->count();

            // 根据签到次数定义自动签退时间和总时长
            $autoClockOutIntervals = [
                1 => ['minutes' => 330, 'totalhours' => '5.30'],
                2 => ['minutes' => 720, 'totalhours' => '12'],
                3 => ['minutes' => 720, 'totalhours' => '12'],
            ];

            if (isset($autoClockOutIntervals[$clockInCount])) {
                $intervalData = $autoClockOutIntervals[$clockInCount];
                $autoClockOutMinutes = $intervalData['minutes'];
                $autoClockOutTime = $firstClockInTime->copy()->addMinutes($autoClockOutMinutes);

                if ($currentTime >= $autoClockOutTime) {
                    $newTimeout = $firstClockInTime->addMinutes($autoClockOutMinutes)->format('Y-m-d H:i:s');

                    DB::table('people_attendance')
                        ->where('id', $user->id)
                        ->update([
                            'timeout' => $newTimeout,
                            'totalhours' => $intervalData['totalhours']
                        ]);
                }
            }
        }
        // Updated Code of Auto Clockout End
    }

    // 此处添加index方法原有逻辑,比如返回视图
    // return view('dashboard.index');
}

注意事项:

  • 替换settings表、key和value字段为你实际使用的表结构,确保能正确获取开关状态。
  • 如果开关用布尔值(0/1)存储,把判断条件改成if (!$autoClockoutStatus)即可。

内容的提问来源于stack exchange,提问作者Mohammed Kamar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 13:47:45