feat: 系统化待办提醒使用首次完成时间 (#1000)

This commit is contained in:
chenxi 2026-11-09 09:36:09 +08:00
parent 783da70ba2
commit 3b2a0086e6
4 changed files with 149 additions and 19 deletions

View File

@ -191,8 +191,8 @@ class SystemizationTodos extends Controller
if ($filters['status'] === self::OVERDUE_FILTER) {
$query->where('status', '<>', SystemizationTodo::STATUS_COMPLETED)
->whereNotNull('due_date')
->whereDate('due_date', '<', today());
->whereNotNull('reminder_date')
->whereDate('reminder_date', '<', today());
} elseif ($filters['status'] === SystemizationTodo::STATUS_INCOMPLETE) {
$query->where('status', '<>', SystemizationTodo::STATUS_COMPLETED);
} elseif ($filters['status'] === SystemizationTodo::STATUS_COMPLETED) {
@ -201,17 +201,17 @@ class SystemizationTodos extends Controller
if ($filters['reminder'] !== '') {
if ($filters['reminder'] === 'overdue') {
$query->where('status', '<>', SystemizationTodo::STATUS_COMPLETED)
->whereNotNull('due_date')
->whereDate('due_date', '<', today());
->whereNotNull('reminder_date')
->whereDate('reminder_date', '<', today());
} elseif ($filters['reminder'] === 'today') {
$query->where('status', '<>', SystemizationTodo::STATUS_COMPLETED)
->whereDate('due_date', today());
->whereDate('reminder_date', today());
} elseif ($filters['reminder'] === 'upcoming') {
$query->where('status', '<>', SystemizationTodo::STATUS_COMPLETED)
->whereDate('due_date', '>', today());
->whereDate('reminder_date', '>', today());
} elseif ($filters['reminder'] === 'pending') {
$query->where('status', '<>', SystemizationTodo::STATUS_COMPLETED)
->whereNull('due_date');
->whereNull('reminder_date');
} elseif ($filters['reminder'] === 'none') {
$query->where('status', SystemizationTodo::STATUS_COMPLETED);
}
@ -304,7 +304,7 @@ class SystemizationTodos extends Controller
->selectRaw(
'COUNT(*) AS total, '
. 'COALESCE(SUM(CASE WHEN status = ? THEN 1 ELSE 0 END), 0) AS completed, '
. 'COALESCE(SUM(CASE WHEN status <> ? AND due_date IS NOT NULL AND due_date < ? THEN 1 ELSE 0 END), 0) AS overdue',
. 'COALESCE(SUM(CASE WHEN status <> ? AND reminder_date IS NOT NULL AND reminder_date < ? THEN 1 ELSE 0 END), 0) AS overdue',
[
SystemizationTodo::STATUS_COMPLETED,
SystemizationTodo::STATUS_COMPLETED,
@ -1344,9 +1344,10 @@ class SystemizationTodos extends Controller
private function todoMatchesFilters(SystemizationTodo $todo, array $filters): bool
{
$completed = $todo->statusLabel() === SystemizationTodo::STATUS_COMPLETED;
$reminderDate = $todo->reminder_date ?: $todo->due_date;
$overdue = ! $completed
&& $todo->due_date !== null
&& $todo->due_date->lt(today()->startOfDay());
&& $reminderDate !== null
&& $reminderDate->lt(today()->startOfDay());
if ($filters['status'] === self::OVERDUE_FILTER && ! $overdue) {
return false;
@ -1363,9 +1364,9 @@ class SystemizationTodos extends Controller
$reminderMatches = match ($filters['reminder']) {
'overdue' => $overdue,
'today' => ! $completed && $todo->due_date?->isSameDay(today()) === true,
'upcoming' => ! $completed && $todo->due_date?->gt(today()->startOfDay()) === true,
'pending' => ! $completed && $todo->due_date === null,
'today' => ! $completed && $reminderDate?->isSameDay(today()) === true,
'upcoming' => ! $completed && $reminderDate?->gt(today()->startOfDay()) === true,
'pending' => ! $completed && $reminderDate === null,
'none' => $completed,
default => true,
};

View File

@ -40,6 +40,7 @@ class SystemizationTodo extends Model
'start_note',
'due_date',
'due_note',
'reminder_date',
'status',
'completed_at',
'progress',
@ -55,6 +56,7 @@ class SystemizationTodo extends Model
'company_id' => 'integer',
'start_date' => 'date',
'due_date' => 'date',
'reminder_date' => 'date',
'completed_at' => 'date',
'sort_order' => 'integer',
'imported_at' => 'datetime',
@ -62,7 +64,20 @@ class SystemizationTodo extends Model
protected static function booted(): void
{
static::creating(function (self $todo): void {
if ($todo->reminder_date === null && $todo->due_date !== null) {
$todo->reminder_date = $todo->due_date->copy();
}
});
static::updating(function (self $todo): void {
$originalReminderDate = $todo->getOriginal('reminder_date');
if ($originalReminderDate !== null) {
$todo->reminder_date = $originalReminderDate;
} elseif ($todo->isDirty('due_date') && $todo->due_date !== null) {
$todo->reminder_date = $todo->due_date->copy();
}
if ($todo->getOriginal('created_by') !== null && $todo->isDirty('created_by')) {
$todo->created_by = $todo->getOriginal('created_by');
}
@ -127,12 +142,13 @@ class SystemizationTodo extends Model
if ($this->isCompleted()) {
return ['label' => '—', 'tone' => 'muted'];
}
if ($this->due_date === null) {
$reminderDate = $this->reminder_date ?: $this->due_date;
if ($reminderDate === null) {
return ['label' => '时间待定', 'tone' => 'muted'];
}
$today = today()->startOfDay();
$dueDate = $this->due_date->copy()->startOfDay();
$dueDate = $reminderDate->copy()->startOfDay();
if ($dueDate->lt($today)) {
return [
'label' => '逾期 ' . $dueDate->diffInDays($today) . ' 天',

View File

@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('business_systemization_todos')) {
return;
}
if (! Schema::hasColumn('business_systemization_todos', 'reminder_date')) {
Schema::table('business_systemization_todos', function (Blueprint $table): void {
$table->date('reminder_date')
->nullable()
->after('due_date')
->comment('首次填写完成时间,作为提醒基准');
$table->index(
['company_id', 'reminder_date'],
'systemization_todos_company_reminder_index',
);
});
}
DB::table('business_systemization_todos')
->whereNull('reminder_date')
->whereNotNull('due_date')
->update(['reminder_date' => DB::raw('due_date')]);
}
public function down(): void
{
if (! Schema::hasTable('business_systemization_todos')) {
return;
}
if (Schema::hasColumn('business_systemization_todos', 'reminder_date')) {
Schema::table('business_systemization_todos', function (Blueprint $table): void {
$table->dropIndex('systemization_todos_company_reminder_index');
$table->dropColumn('reminder_date');
});
}
}
};

View File

@ -1458,8 +1458,10 @@ class SystemizationTodosTest extends FeatureTestCase
]);
$legacy = $this->createTodo('收入', '提醒测试', '历史进行中事项', '2026-11-01', 5);
$legacy->update(['status' => '进行中']);
$pendingDate = $this->createTodo('收入', '提醒测试', '时间待定事项', '2026-10-30', 6);
$pendingDate->update(['due_date' => null, 'due_note' => '待定']);
$clearedDate = $this->createTodo('收入', '提醒测试', '已清空完成时间事项', '2026-10-30', 6);
$clearedDate->update(['due_date' => null, 'due_note' => '待定']);
$pendingDate = $this->createTodo('收入', '提醒测试', '时间待定事项', null, 7);
$pendingDate->update(['due_note' => '待定']);
$pageResponse = $this->get(route('business.pages.show', [
'section' => 'other',
@ -1476,6 +1478,7 @@ class SystemizationTodosTest extends FeatureTestCase
$this->assertSame('今天到期', $items['今天到期事项']->reminder_label);
$this->assertSame('剩余 4 天', $items['未来事项']->reminder_label);
$this->assertSame('—', $items['已完成事项']->reminder_label);
$this->assertSame('剩余 4 天', $items['已清空完成时间事项']->reminder_label);
$this->assertSame('时间待定', $items['时间待定事项']->reminder_label);
$this->assertSame('', $items['已完成事项']->displayProgress());
$this->assertSame(SystemizationTodo::STATUS_INCOMPLETE, $items['历史进行中事项']->status_label);
@ -1498,7 +1501,7 @@ class SystemizationTodosTest extends FeatureTestCase
foreach ([
'overdue' => ['逾期事项'],
'today' => ['今天到期事项'],
'upcoming' => ['未来事项', '历史进行中事项'],
'upcoming' => ['未来事项', '历史进行中事项', '已清空完成时间事项'],
'pending' => ['时间待定事项'],
'none' => ['已完成事项'],
] as $reminder => $expectedTitles) {
@ -1516,6 +1519,68 @@ class SystemizationTodosTest extends FeatureTestCase
}
}
public function test_reminder_keeps_the_first_saved_completion_date(): void
{
Carbon::setTestNow('2026-10-26 12:00:00');
try {
$this->loginAs();
$todo = $this->createTodo('收入', '首次完成时间', '完成时间延后事项', '2026-10-30', 1);
$todo->update(['due_date' => '2026-11-10']);
$todo->refresh();
$this->assertSame('2026-11-10', $todo->due_date?->format('Y-m-d'));
$this->assertSame('2026-10-30', $todo->reminder_date?->format('Y-m-d'));
$this->assertSame('剩余 4 天', $todo->reminder()['label']);
$content = $this->get(route('business.pages.show', [
'section' => 'other',
'page' => 'systemization-todos',
'reminder' => 'upcoming',
]))->assertOk()->viewData('content');
$this->assertSame(['完成时间延后事项'], $content['items']->pluck('title')->all());
$dueDateContent = $this->get(route('business.pages.show', [
'section' => 'other',
'page' => 'systemization-todos',
'due_date' => '2026-11-10',
]))->assertOk()->viewData('content');
$this->assertSame(['完成时间延后事项'], $dueDateContent['items']->pluck('title')->all());
$todo->update(['due_date' => null, 'due_note' => '待定']);
$todo->refresh();
$this->assertNull($todo->due_date);
$this->assertSame('2026-10-30', $todo->reminder_date?->format('Y-m-d'));
$this->assertSame('剩余 4 天', $todo->reminder()['label']);
} finally {
Carbon::setTestNow();
}
}
public function test_reminder_captures_the_first_completion_date_added_to_a_pending_todo(): void
{
Carbon::setTestNow('2026-10-26 12:00:00');
try {
$this->loginAs();
$todo = $this->createTodo('收入', '首次完成时间', '先待定后填写事项', null, 1);
$this->assertNull($todo->reminder_date);
$this->assertSame('时间待定', $todo->reminder()['label']);
$todo->update(['due_date' => '2026-10-28', 'due_note' => null]);
$todo->refresh();
$this->assertSame('2026-10-28', $todo->reminder_date?->format('Y-m-d'));
$todo->update(['due_date' => '2026-11-20']);
$todo->refresh();
$this->assertSame('2026-11-20', $todo->due_date?->format('Y-m-d'));
$this->assertSame('2026-10-28', $todo->reminder_date?->format('Y-m-d'));
$this->assertSame('剩余 2 天', $todo->reminder()['label']);
} finally {
Carbon::setTestNow();
}
}
public function test_page_filters_by_date_and_merges_adjacent_category_and_section_cells(): void
{
$this->travelTo(Carbon::parse('2026-10-26 12:00:00'));
@ -2157,7 +2222,7 @@ class SystemizationTodosTest extends FeatureTestCase
string $category,
string $section,
string $title,
string $dueDate,
?string $dueDate,
int $sortOrder,
string $owner = '晨曦',
string $assignee = '栎阳',