mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
H&R + exam user support bulk action
This commit is contained in:
@@ -106,4 +106,16 @@ class ExamUserController extends Controller
|
||||
return $this->success($result, 'Recover user exam success!');
|
||||
}
|
||||
|
||||
public function bulkAvoid(Request $request): array
|
||||
{
|
||||
$result = $this->repository->avoidExamUserBulk($request->all(), Auth::user());
|
||||
return $this->success(['result' => $result],"Affected: " . intval($result));
|
||||
}
|
||||
|
||||
public function bulkDelete(Request $request): array
|
||||
{
|
||||
$result = $this->repository->removeExamUserBulk($request->all(), Auth::user());
|
||||
return $this->success(['result' => $result],"Affected: " . intval($result));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -107,4 +107,16 @@ class HitAndRunController extends Controller
|
||||
$result = $this->repository->pardon($id, Auth::user());
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
public function bulkPardon(Request $request): array
|
||||
{
|
||||
$result = $this->repository->bulkPardon($request->all(), Auth::user());
|
||||
return $this->success(['result' => $result],"Affected: " . intval($result));
|
||||
}
|
||||
|
||||
public function bulkDelete(Request $request): array
|
||||
{
|
||||
$result = $this->repository->bulkDelete($request->all(), Auth::user());
|
||||
return $this->success(['result' => $result],"Affected: " . intval($result));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class ExamUser extends NexusModel
|
||||
const STATUS_FINISHED = 1;
|
||||
const STATUS_AVOIDED = -1;
|
||||
|
||||
public static $status = [
|
||||
public static array $status = [
|
||||
self::STATUS_NORMAL => ['text' => 'Normal'],
|
||||
self::STATUS_FINISHED => ['text' => 'Finished'],
|
||||
self::STATUS_AVOIDED => ['text' => 'Avoided'],
|
||||
@@ -21,7 +21,7 @@ class ExamUser extends NexusModel
|
||||
const IS_DONE_YES = 1;
|
||||
const IS_DONE_NO = 0;
|
||||
|
||||
public static $isDoneInfo = [
|
||||
public static array $isDoneInfo = [
|
||||
self::IS_DONE_YES => ['text' => 'Yes'],
|
||||
self::IS_DONE_NO => ['text' => 'No'],
|
||||
];
|
||||
|
||||
@@ -11,6 +11,8 @@ class NexusModel extends Model
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $perPage = 50;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \DateTimeInterface $date
|
||||
|
||||
@@ -17,6 +17,8 @@ class User extends Authenticatable
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $perPage = 50;
|
||||
|
||||
const STATUS_CONFIRMED = 'confirmed';
|
||||
const STATUS_PENDING = 'pending';
|
||||
|
||||
|
||||
@@ -740,6 +740,47 @@ class ExamRepository extends BaseRepository
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function removeExamUserBulk(array $params, User $user)
|
||||
{
|
||||
$result = $this->getExamUserBulkQuery($params)->delete();
|
||||
do_log(sprintf(
|
||||
'user: %s bulk delete by filter: %s, result: %s',
|
||||
$user->id, json_encode($params), json_encode($result)
|
||||
), 'alert');
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function avoidExamUserBulk(array $params, User $user): int
|
||||
{
|
||||
$query = $this->getExamUserBulkQuery($params)->where('status', ExamUser::STATUS_NORMAL);
|
||||
$update = [
|
||||
'status' => ExamUser::STATUS_AVOIDED,
|
||||
];
|
||||
$affected = $query->update($update);
|
||||
do_log(sprintf(
|
||||
'user: %s bulk avoid by filter: %s, affected: %s',
|
||||
$user->id, json_encode($params), $affected
|
||||
), 'alert');
|
||||
return $affected;
|
||||
}
|
||||
|
||||
private function getExamUserBulkQuery(array $params): Builder
|
||||
{
|
||||
$query = ExamUser::query();
|
||||
$hasWhere = false;
|
||||
$validFilter = ['uid', 'id', 'exam_id'];
|
||||
foreach ($validFilter as $item) {
|
||||
if (!empty($params[$item])) {
|
||||
$hasWhere = true;
|
||||
$query->whereIn($item, Arr::wrap($params[$item]));
|
||||
}
|
||||
}
|
||||
if (!$hasWhere) {
|
||||
throw new \InvalidArgumentException("No filter.");
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function recoverExamUser(int $examUserId)
|
||||
{
|
||||
$examUser = ExamUser::query()->where('status',ExamUser::STATUS_AVOIDED)->findOrFail($examUserId);
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Models\User;
|
||||
use App\Models\UserBanLog;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class HitAndRunRepository extends BaseRepository
|
||||
@@ -59,6 +60,33 @@ class HitAndRunRepository extends BaseRepository
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function bulkDelete(array $params, User $user)
|
||||
{
|
||||
$result = $this->getBulkQuery($params)->delete();
|
||||
do_log(sprintf(
|
||||
'user: %s bulk delete by filter: %s, result: %s',
|
||||
$user->id, json_encode($params), json_encode($result)
|
||||
), 'alert');
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getBulkQuery(array $params): Builder
|
||||
{
|
||||
$query = HitAndRun::query();
|
||||
$hasWhere = false;
|
||||
$validFilter = ['uid', 'id'];
|
||||
foreach ($validFilter as $item) {
|
||||
if (!empty($params[$item])) {
|
||||
$hasWhere = true;
|
||||
$query->whereIn($item, Arr::wrap($params[$item]));
|
||||
}
|
||||
}
|
||||
if (!$hasWhere) {
|
||||
throw new \InvalidArgumentException("No filter.");
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function cronjobUpdateStatus($uid = null, $torrentId = null, $ignoreTime = false): bool|int
|
||||
{
|
||||
do_log("uid: $uid, torrentId: $torrentId, ignoreTime: " . var_export($ignoreTime, true));
|
||||
@@ -330,7 +358,7 @@ class HitAndRunRepository extends BaseRepository
|
||||
|
||||
}
|
||||
|
||||
public function listStatus()
|
||||
public function listStatus(): array
|
||||
{
|
||||
$results = [];
|
||||
foreach (HitAndRun::$status as $key => $value) {
|
||||
@@ -346,8 +374,28 @@ class HitAndRunRepository extends BaseRepository
|
||||
throw new \LogicException("Can't be pardoned due to status is: " . $model->status_text . " !");
|
||||
}
|
||||
$model->status = HitAndRun::STATUS_PARDONED;
|
||||
$model->comment = DB::raw(sprintf("concat_ws('\n', comment, '%s')", addslashes('Pardon by ' . $user->username)));
|
||||
$model->comment = $this->getCommentUpdateRaw(addslashes('Pardon by ' . $user->username));
|
||||
$model->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function bulkPardon(array $params, User $user): int
|
||||
{
|
||||
$query = $this->getBulkQuery($params)->where('status', HitAndRun::STATUS_INSPECTING);
|
||||
$update = [
|
||||
'status' => HitAndRun::STATUS_PARDONED,
|
||||
'comment' => $this->getCommentUpdateRaw(addslashes('Pardon by ' . $user->username)),
|
||||
];
|
||||
$affected = $query->update($update);
|
||||
do_log(sprintf(
|
||||
'user: %s bulk pardon by filter: %s, affected: %s',
|
||||
$user->id, json_encode($params), $affected
|
||||
), 'alert');
|
||||
return $affected;
|
||||
}
|
||||
|
||||
private function getCommentUpdateRaw($comment): \Illuminate\Database\Query\Expression
|
||||
{
|
||||
return DB::raw(sprintf("if (comment = '', '%s', concat('\n', '%s', comment))", $comment, $comment));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user