mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-19 08:10:49 +08:00
add exam index init value
This commit is contained in:
@@ -62,4 +62,4 @@ GOOGLE_DRIVE_REFRESH_TOKEN=
|
||||
GOOGLE_DRIVE_FOLDER_ID=
|
||||
|
||||
GEOIP2_DATABASE=
|
||||
EXAM_PROGRESS_UPDATE_PROBABILITY=60
|
||||
EXAM_PROGRESS_UPDATE_PROBABILITY=20
|
||||
|
||||
3
admin/src/utils/api.js
vendored
3
admin/src/utils/api.js
vendored
@@ -84,6 +84,9 @@ const api = {
|
||||
avoidExamUser: (id) => {
|
||||
return axios.put('exam-users-avoid', {id});
|
||||
},
|
||||
recoverExamUser: (id) => {
|
||||
return axios.put('exam-users-recover', {id});
|
||||
},
|
||||
storeExamUser: (params) => {
|
||||
return axios.post('exam-users', params);
|
||||
},
|
||||
|
||||
@@ -125,6 +125,15 @@
|
||||
<el-button type="info" size="small">Avoid</el-button>
|
||||
</template>
|
||||
</el-popconfirm>
|
||||
<el-popconfirm
|
||||
v-if="examInfo.status === -1"
|
||||
title="Confirm Recover ?"
|
||||
@confirm="handleRecoverExam(examInfo.id)"
|
||||
>
|
||||
<template #reference>
|
||||
<el-button type="primary" size="small">Recover</el-button>
|
||||
</template>
|
||||
</el-popconfirm>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
@@ -205,6 +214,12 @@ export default {
|
||||
await fetchPageData()
|
||||
}
|
||||
|
||||
const handleRecoverExam = async (id) => {
|
||||
let res = await api.recoverExamUser(id)
|
||||
ElMessage.success(res.msg)
|
||||
await fetchPageData()
|
||||
}
|
||||
|
||||
const handleAssignExam = async () => {
|
||||
assignExam.value.open(id)
|
||||
}
|
||||
@@ -230,6 +245,7 @@ export default {
|
||||
handleRemoveExam,
|
||||
handleAvoidExam,
|
||||
handleAssignExam,
|
||||
handleRecoverExam,
|
||||
handleEnableUser,
|
||||
handleViewInviteInfo,
|
||||
handleDisableUser,
|
||||
|
||||
@@ -99,4 +99,11 @@ class ExamUserController extends Controller
|
||||
return $this->success($result, 'Avoid user exam success!');
|
||||
}
|
||||
|
||||
public function recover(Request $request)
|
||||
{
|
||||
$request->validate(['id' => 'required']);
|
||||
$result = $this->repository->recoverExamUser($request->id);
|
||||
return $this->success($result, 'Recover user exam success!');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
10
app/Models/ExamIndexInitValue.php
Normal file
10
app/Models/ExamIndexInitValue.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class ExamIndexInitValue extends NexusModel
|
||||
{
|
||||
protected $fillable = ['uid', 'exam_id', 'index', 'value',];
|
||||
|
||||
public $timestamps = true;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ namespace App\Repositories;
|
||||
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Models\Exam;
|
||||
use App\Models\ExamIndexInitValue;
|
||||
use App\Models\ExamProgress;
|
||||
use App\Models\ExamUser;
|
||||
use App\Models\Message;
|
||||
@@ -91,12 +92,22 @@ class ExamRepository extends BaseRepository
|
||||
return $exam;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete an exam task, also will delete all exam user and progress.
|
||||
*
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$exam = Exam::query()->findOrFail($id);
|
||||
DB::transaction(function () use ($exam) {
|
||||
ExamUser::query()->where('exam_id', $exam->id)->delete();
|
||||
ExamProgress::query()->where('exam_id', $exam->id)->delete();
|
||||
do {
|
||||
$deleted = ExamUser::query()->where('exam_id', $exam->id)->limit(10000)->delete();
|
||||
} while ($deleted > 0);
|
||||
do {
|
||||
$deleted = ExamProgress::query()->where('exam_id', $exam->id)->limit(10000)->delete();
|
||||
} while ($deleted > 0);
|
||||
$exam->delete();
|
||||
});
|
||||
return true;
|
||||
@@ -232,8 +243,9 @@ class ExamRepository extends BaseRepository
|
||||
$data['end'] = $end;
|
||||
}
|
||||
do_log("$logPrefix, data: " . nexus_json_encode($data));
|
||||
$result = $user->exams()->create($data);
|
||||
return $result;
|
||||
$examUser = $user->exams()->create($data);
|
||||
$this->initProgress($examUser, $user);
|
||||
return $examUser;
|
||||
}
|
||||
|
||||
public function listUser(array $params)
|
||||
@@ -343,6 +355,41 @@ class ExamRepository extends BaseRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public function initProgress($examUser, $user = null)
|
||||
{
|
||||
if (!$examUser instanceof ExamUser) {
|
||||
$examUser = ExamUser::query()->findOrFail((int)$examUser);
|
||||
}
|
||||
$exam = $examUser->exam;
|
||||
if (!$user instanceof User) {
|
||||
$user = $examUser->user()->select(['id', 'uploaded', 'downloaded', 'seedtime', 'leechtime', 'seedbonus'])->first();
|
||||
}
|
||||
$insert = [
|
||||
'uid' => $user->id,
|
||||
'exam_id' => $exam->id,
|
||||
];
|
||||
foreach ($exam->indexes as $key => $index) {
|
||||
if (!isset($index['checked']) || !$index['checked']) {
|
||||
continue;
|
||||
}
|
||||
$insert['index'] = $index['index'];
|
||||
if ($index['index'] == Exam::INDEX_UPLOADED) {
|
||||
$insert['value'] = $user->uploaded;
|
||||
} elseif ($index['index'] == Exam::INDEX_DOWNLOADED) {
|
||||
$insert['value'] = $user->downloaded;
|
||||
} elseif ($index['index'] == Exam::INDEX_SEED_BONUS) {
|
||||
$insert['value'] = $user->seedbonus;
|
||||
} elseif ($index['index'] == Exam::INDEX_SEED_TIME_AVERAGE) {
|
||||
$insert['value'] = 0;
|
||||
} else {
|
||||
throw new \RuntimeException("Unknown index: {$index['index']}");
|
||||
}
|
||||
ExamIndexInitValue::query()->insert($insert);
|
||||
do_log("insert: " . json_encode($insert));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getUserExamProgress($uid, $status = null, $with = ['exam', 'user'])
|
||||
{
|
||||
$logPrefix = "uid: $uid";
|
||||
@@ -449,7 +496,9 @@ class ExamRepository extends BaseRepository
|
||||
{
|
||||
$examUser = ExamUser::query()->findOrFail($examUserId);
|
||||
$result = DB::transaction(function () use ($examUser) {
|
||||
$examUser->progresses()->delete();
|
||||
do {
|
||||
$deleted = $examUser->progresses()->limit(10000)->delete();
|
||||
} while ($deleted > 0);
|
||||
return $examUser->delete();
|
||||
});
|
||||
return $result;
|
||||
@@ -457,11 +506,18 @@ class ExamRepository extends BaseRepository
|
||||
|
||||
public function avoidExamUser(int $examUserId)
|
||||
{
|
||||
$examUser = ExamUser::query()->findOrFail($examUserId);
|
||||
$examUser = ExamUser::query()->where('status',ExamUser::STATUS_NORMAL)->findOrFail($examUserId);
|
||||
$result = $examUser->update(['status' => ExamUser::STATUS_AVOIDED]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function recoverExamUser(int $examUserId)
|
||||
{
|
||||
$examUser = ExamUser::query()->where('status',ExamUser::STATUS_AVOIDED)->findOrFail($examUserId);
|
||||
$result = $examUser->update(['status' => ExamUser::STATUS_NORMAL]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function cronjonAssign()
|
||||
{
|
||||
$exams = $this->listValid(null, Exam::DISCOVERED_YES);
|
||||
@@ -523,17 +579,16 @@ class ExamRepository extends BaseRepository
|
||||
// do_log("$currentLogPrefix, exam not match this user.");
|
||||
// continue;
|
||||
// }
|
||||
$insert[] = [
|
||||
$insert = [
|
||||
'uid' => $user->id,
|
||||
'exam_id' => $exam->id,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
];
|
||||
do_log("$currentLogPrefix, exam will be assigned to this user.");
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$result += count($insert);
|
||||
ExamUser::query()->insert($insert);
|
||||
$examUser = ExamUser::query()->create($insert);
|
||||
$this->initProgress($examUser, $user);
|
||||
$result++;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateExamIndexInitValuesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('exam_index_init_values', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('uid')->index();
|
||||
$table->integer('exam_user_id');
|
||||
$table->integer('exam_id')->index();
|
||||
$table->integer('index')->index();
|
||||
$table->bigInteger('value');
|
||||
$table->timestamps();
|
||||
$table->unique(['exam_user_id', 'exam_id', 'index']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('exam_index_init_values');
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ Route::group(['middleware' => ['auth:sanctum', 'permission', 'locale']], functio
|
||||
|
||||
Route::resource('exam-users', \App\Http\Controllers\ExamUserController::class);
|
||||
Route::put('exam-users-avoid', [\App\Http\Controllers\ExamUserController::class, 'avoid']);
|
||||
Route::put('exam-users-recover', [\App\Http\Controllers\ExamUserController::class, 'recover']);
|
||||
|
||||
Route::get('dashboard/system-info', [\App\Http\Controllers\DashboardController::class, 'systemInfo']);
|
||||
Route::get('dashboard/stat-data', [\App\Http\Controllers\DashboardController::class, 'statData']);
|
||||
|
||||
Reference in New Issue
Block a user