mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-07-15 06:27:28 +08:00
fix ExamRepository pg 兼容
This commit is contained in:
@@ -269,12 +269,24 @@ class ExamRepository extends BaseRepository
|
|||||||
$now = Carbon::now();
|
$now = Carbon::now();
|
||||||
$query = Exam::query()
|
$query = Exam::query()
|
||||||
->where('status', Exam::STATUS_ENABLED)
|
->where('status', Exam::STATUS_ENABLED)
|
||||||
->whereRaw('
|
->where(function ($q) use ($now) {
|
||||||
CASE
|
$q->where(function ($sub) use ($now) {
|
||||||
WHEN begin IS NOT NULL AND "end" IS NOT NULL
|
// 如果 begin 和 end 都不为空,则判断时间
|
||||||
THEN begin <= ? AND "end" >= ?
|
$sub->whereNotNull('begin')
|
||||||
ELSE duration > 0 OR recurring IS NOT NULL
|
->whereNotNull('end')
|
||||||
END', [$now, $now]);
|
->where('begin', '<=', $now)
|
||||||
|
->where('end', '>=', $now);
|
||||||
|
})->orWhere(function ($sub) {
|
||||||
|
// 如果不满足上面的条件(即 begin 或 end 任意一个为空)
|
||||||
|
$sub->where(function ($inner) {
|
||||||
|
$inner->whereNull('begin')
|
||||||
|
->orWhereNull('end');
|
||||||
|
})->where(function ($inner) {
|
||||||
|
$inner->where('duration', '>', 0)
|
||||||
|
->orWhereNotNull('recurring');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (!is_null($excludeId)) {
|
if (!is_null($excludeId)) {
|
||||||
$query->whereNotIn('id', Arr::wrap($excludeId));
|
$query->whereNotIn('id', Arr::wrap($excludeId));
|
||||||
@@ -285,6 +297,7 @@ class ExamRepository extends BaseRepository
|
|||||||
if (!is_null($type)) {
|
if (!is_null($type)) {
|
||||||
$query->where("type", $type);
|
$query->where("type", $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $query->orderBy('priority', 'desc')->orderBy('id', 'asc')->get();
|
return $query->orderBy('priority', 'desc')->orderBy('id', 'asc')->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1131,34 +1144,44 @@ class ExamRepository extends BaseRepository
|
|||||||
|
|
||||||
public function cronjobCheckout($ignoreTimeRange = false): int
|
public function cronjobCheckout($ignoreTimeRange = false): int
|
||||||
{
|
{
|
||||||
$now = Carbon::now()->toDateTimeString();
|
$now = Carbon::now(); // 保持 Carbon 对象即可,Laravel 会自动序列化
|
||||||
$examUserTable = (new ExamUser())->getTable();
|
$examUserTable = (new ExamUser())->getTable();
|
||||||
$examTable = (new Exam())->getTable();
|
$examTable = (new Exam())->getTable();
|
||||||
$userTable = (new User())->getTable();
|
$userTable = (new User())->getTable();
|
||||||
|
|
||||||
$baseQuery = ExamUser::query()
|
$baseQuery = ExamUser::query()
|
||||||
->join($examTable, "$examUserTable.exam_id", "=", "$examTable.id")
|
->join($examTable, "$examUserTable.exam_id", "=", "$examTable.id")
|
||||||
->where("$examUserTable.status", ExamUser::STATUS_NORMAL)
|
->where("$examUserTable.status", ExamUser::STATUS_NORMAL)
|
||||||
->selectRaw("$examUserTable.*")
|
->select("$examUserTable.*") // 替换 selectRaw
|
||||||
->with(['exam', 'user', 'user.language'])
|
->with(['exam', 'user', 'user.language'])
|
||||||
->orderBy("$examUserTable.id", "asc");
|
->orderBy("$examUserTable.id", "asc");
|
||||||
|
|
||||||
if (!$ignoreTimeRange) {
|
if (!$ignoreTimeRange) {
|
||||||
$whenThens = [];
|
$baseQuery->where(function ($query) use ($examUserTable, $examTable, $now) {
|
||||||
$params = [];
|
$query->where(function ($q) use ($examUserTable, $now) {
|
||||||
|
// 条件 1: exam_user.end 不为空且小于当前时间
|
||||||
|
$q->whereNotNull("$examUserTable.end")
|
||||||
|
->where("$examUserTable.end", '<', $now);
|
||||||
|
})
|
||||||
|
->orWhere(function ($q) use ($examTable, $now) {
|
||||||
|
// 条件 2: exam.end 不为空且小于当前时间
|
||||||
|
$q->whereNotNull("$examTable.end")
|
||||||
|
->where("$examTable.end", '<', $now);
|
||||||
|
})
|
||||||
|
->orWhere(function ($q) use ($examUserTable, $examTable, $now) {
|
||||||
|
// 条件 3: exam.duration > 0 且过期
|
||||||
|
// 因为涉及到列与列的计算,这里需要用 whereRaw,但我们可以针对多数据库做自适应
|
||||||
|
$q->where("$examTable.duration", '>', 0);
|
||||||
|
|
||||||
$whenThens[] = "WHEN $examUserTable.\"end\" IS NOT NULL THEN $examUserTable.\"end\" < ?";
|
if (NexusDB::isPgsql()) {
|
||||||
$params[] = $now;
|
// PG 写法:使用 || 拼接字符串再转为 INTERVAL
|
||||||
|
$q->whereRaw("$examUserTable.created_at + ($examTable.duration || ' day')::INTERVAL < ?", [$now]);
|
||||||
$whenThens[] = "WHEN $examTable.\"end\" IS NOT NULL THEN $examTable.\"end\" < ?";
|
} else {
|
||||||
$params[] = $now;
|
// MySQL 写法
|
||||||
|
$q->whereRaw("DATE_ADD($examUserTable.created_at, INTERVAL $examTable.duration DAY) < ?", [$now]);
|
||||||
if (NexusDB::isMysql()) {
|
}
|
||||||
$whenThens[] = "when $examTable.duration > 0 then date_add($examUserTable.created_at, interval $examTable.duration day) < ?";
|
});
|
||||||
} elseif (NexusDB::isPgsql()) {
|
});
|
||||||
$whenThens[] = "WHEN $examTable.duration > 0 THEN ($examUserTable.created_at + ($examTable.duration || ' day')::INTERVAL) < ?";
|
|
||||||
}
|
|
||||||
$params[] = $now;
|
|
||||||
|
|
||||||
$baseQuery->whereRaw(sprintf("CASE %s ELSE false END", implode(" ", $whenThens)), $params);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$size = 1000;
|
$size = 1000;
|
||||||
|
|||||||
Reference in New Issue
Block a user