From 2733bf9761c1d8eb83d4b559ff67fc24c5a3cc03 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sun, 13 Jun 2021 22:21:42 +0800 Subject: [PATCH] attendance remove total_points --- app/Console/Commands/Test.php | 30 +++++++++++-- app/Models/Attendance.php | 2 + app/Repositories/ExamRepository.php | 4 +- classes/class_attendance.php | 8 ++-- ...40_add_total_days_to_attendance_table.php} | 8 +--- include/constants.php | 4 +- nexus/Install/Update.php | 43 +++++++++++++++++++ 7 files changed, 81 insertions(+), 18 deletions(-) rename database/migrations/{2021_06_10_150523_add_total_days_and_total_points_to_attendance_table.php => 2021_06_13_215440_add_total_days_to_attendance_table.php} (65%) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 7f9e4baf..dee01084 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -53,10 +53,32 @@ class Test extends Command */ public function handle() { - $examRep = new ExamRepository(); - $examuser = ExamUser::query()->findOrFail(2); - $r = $examRep->updateProgress($examuser); - dd($r); + $page = 1; + $size = 1000; + while (true) { + $logPrefix = "[MIGRATE_ATTENDANCE], page: $page, size: $size"; + $result = Attendance::query() + ->groupBy(['uid']) + ->selectRaw('uid, max(id) as id, count(*) as counts') + ->forPage($page, $size) + ->get(); + do_log("$logPrefix, " . last_query() . ", count: " . $result->count()); + if ($result->isEmpty()) { + do_log("$logPrefix, no more data..."); + break; + } + foreach ($result as $row) { + $update = [ + 'total_days' => $row->counts, + ]; + $updateResult = $row->update($update); + do_log(sprintf( + "$logPrefix, update user: %s(ID: %s) => %s, result: %s", + $row->uid, $row->id, json_encode($update), var_export($updateResult, true) + )); + } + $page++; + } } } diff --git a/app/Models/Attendance.php b/app/Models/Attendance.php index 95d09349..50715cf9 100644 --- a/app/Models/Attendance.php +++ b/app/Models/Attendance.php @@ -6,6 +6,8 @@ class Attendance extends NexusModel { protected $table = 'attendance'; + protected $fillable = ['uid', 'added', 'points', 'days', 'total_days']; + protected $casts = [ 'added' => 'datetime', ]; diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index 5478a744..8b13683f 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -736,8 +736,8 @@ class ExamRepository extends BaseRepository if ($donateStatus == User::DONATE_YES) { $baseQuery->where("$userTable.donoruntil", ">=", Carbon::now()->toDateTimeString()); } elseif ($donateStatus == User::DONATE_NO) { - $baseQuery->where(function (Builder $query) { - $query->whereNull('donoruntil')->orWhere('donoruntil', '<', Carbon::now()->toDateTimeString()); + $baseQuery->where(function (Builder $query) use ($userTable) { + $query->whereNull("$userTable.donoruntil")->orWhere("$userTable.donoruntil", '<', Carbon::now()->toDateTimeString()); }); } else { do_log("{$exam->id} filter $filter: $donateStatus invalid.", "error"); diff --git a/classes/class_attendance.php b/classes/class_attendance.php index 84aa83ba..47abb885 100644 --- a/classes/class_attendance.php +++ b/classes/class_attendance.php @@ -46,13 +46,13 @@ class Attendance // sql_query(sprintf('INSERT INTO `attendance` (`uid`,`added`,`points`,`days`) VALUES (%u, %s, %u, %u)', $this->userid, sqlesc(date('Y-m-d H:i:s')), $points, $cdays)) or sqlerr(__FILE__, __LINE__); if ($doUpdate) { $sql = sprintf( - 'UPDATE `attendance` set added = %s, points = %s, days = %s, total_days= %s, total_points = %s where uid = %s order by id desc limit 1', - sqlesc(date('Y-m-d H:i:s')), $points, $cdays, $totalDays + 1, $totalPoints + $points, $this->userid + 'UPDATE `attendance` set added = %s, points = %s, days = %s, total_days= %s where uid = %s order by id desc limit 1', + sqlesc(date('Y-m-d H:i:s')), $points, $cdays, $totalDays + 1, $this->userid ); } else { $sql = sprintf( - 'INSERT INTO `attendance` (`uid`, `added`, `points`, `days`, `total_days`, `total_points`) VALUES (%u, %s, %u, %u, %u, %u)', - $this->userid, sqlesc(date('Y-m-d H:i:s')), $points, $cdays, $totalDays + 1, $totalPoints + $points + 'INSERT INTO `attendance` (`uid`, `added`, `points`, `days`, `total_days`) VALUES (%u, %s, %u, %u, %u, %u)', + $this->userid, sqlesc(date('Y-m-d H:i:s')), $points, $cdays, $totalDays + 1 ); } do_log(sprintf('uid: %s, date: %s, doUpdate: %s, sql: %s', $this->userid, $this->curdate, $doUpdate, $sql), 'notice'); diff --git a/database/migrations/2021_06_10_150523_add_total_days_and_total_points_to_attendance_table.php b/database/migrations/2021_06_13_215440_add_total_days_to_attendance_table.php similarity index 65% rename from database/migrations/2021_06_10_150523_add_total_days_and_total_points_to_attendance_table.php rename to database/migrations/2021_06_13_215440_add_total_days_to_attendance_table.php index eef06b57..a045cc02 100644 --- a/database/migrations/2021_06_10_150523_add_total_days_and_total_points_to_attendance_table.php +++ b/database/migrations/2021_06_13_215440_add_total_days_to_attendance_table.php @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class AddTotalDaysAndTotalPointsToAttendanceTable extends Migration +class AddTotalDaysToAttendanceTable extends Migration { /** * Run the migrations. @@ -13,12 +13,8 @@ class AddTotalDaysAndTotalPointsToAttendanceTable extends Migration */ public function up() { - if (Schema::hasColumns('attendance',['total_days', 'total_points'])) { - return; - } Schema::table('attendance', function (Blueprint $table) { $table->integer('total_days')->default(0); - $table->integer('total_points')->default(0); }); } @@ -30,7 +26,7 @@ class AddTotalDaysAndTotalPointsToAttendanceTable extends Migration public function down() { Schema::table('attendance', function (Blueprint $table) { - $table->dropColumn(['total_days', 'total_points']); + $table->dropColumn('total_days'); }); } } diff --git a/include/constants.php b/include/constants.php index 16362d54..aaf62526 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ hasColumn('attendance', 'total_points')) { + + } + } + + private function migrateAttendance() + { + $page = 1; + $size = 1000; + $sub = Attendance::query()->orderBy('id', 'desc'); + while (true) { + $logPrefix = "[MIGRATE_ATTENDANCE], page: $page, size: $size"; + $result = Attendance::query() + ->fromSub($sub, 'a') + ->groupBy('id, uid') + ->selectRaw('id, uid, count(*) as counts') + ->forPage($page, $size) + ->get(); + $this->doLog("$logPrefix, " . last_query() . ", count: " . $result->count()); + if ($result->isEmpty()) { + $this->doLog("$logPrefix, no more data..."); + break; + } + foreach ($result as $row) { + $update = [ + 'total_days' => $row->counts, + ]; + $updateResult = $row->update($update); + $this->doLog(sprintf( + "$logPrefix, update user: %s(%s) => %s, result: %s", + $row->uid, $row->id, json_encode($update), var_export($updateResult, true) + )); + } + $page++; + } + } }