fix getusertorrentlistajax hr

This commit is contained in:
xiaomlove
2022-03-10 21:45:23 +08:00
parent 40e36b715a
commit f0ced9cdf9
9 changed files with 113 additions and 39 deletions

View File

@@ -40,7 +40,7 @@ class AttendanceCleanup extends Command
{
$query = Attendance::query()->groupBy('uid')->selectRaw('uid, max(id) as max_id');
$page = 1;
$size = 1000;
$size = 10000;
while (true) {
$rows = $query->forPage($page, $size)->get();
$log = "sql: " . last_query() . ", count: " . $rows->count();

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Console\Commands;
use App\Models\Attendance;
use App\Repositories\AttendanceRepository;
use Illuminate\Console\Command;
class AttendanceMigrate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'attendance:migrate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrate attendance from one time one record to one user one record.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$rep = new AttendanceRepository();
$result = $rep->migrateAttendance();
$log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query());
$this->info($log);
do_log($log);
return 0;
}
}

View File

@@ -63,7 +63,9 @@ class Test extends Command
*/
public function handle()
{
$rep = new AttendanceRepository();
$r = $rep->migrateAttendance();
dd($r);
}
}

View File

@@ -5,6 +5,7 @@ use App\Models\Attendance;
use App\Models\Setting;
use App\Models\User;
use Carbon\Carbon;
use Nexus\Database\NexusDB;
class AttendanceRepository extends BaseRepository
{
@@ -90,4 +91,51 @@ class AttendanceRepository extends BaseRepository
}
return $points;
}
public function migrateAttendance(): int
{
$page = 1;
$size = 10000;
$caseWhens = [];
$idArr = [];
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) {
//use case when instead.
$caseWhens[] = sprintf('when %s then %s', $row->id, $row->counts);
$idArr[] = $row->id;
// $update = [
// 'total_days' => $row->counts,
// ];
// $updateResult = $row->update($update);
do_log(sprintf(
"$logPrefix, update user: %s(ID: %s) => %s",
$row->uid, $row->id, $row->counts
));
}
$page++;
}
if (empty($caseWhens)) {
do_log("no data to update...");
return 0;
}
$caseWhenStr = sprintf('case id %s end', implode(' ', $caseWhens));
$result = Attendance::query()
->whereIn('id', $idArr)
->update(['total_days' => NexusDB::raw($caseWhenStr)]);
do_log("[MIGRATE_ATTENDANCE] DONE! $caseWhenStr, result: " . var_export($result, true));
return count($idArr);
}
}