platform orders: add created_at range filter (created_from/to)

This commit is contained in:
萝卜
2026-03-16 00:30:09 +08:00
parent 223ea1d950
commit 0a58204799
3 changed files with 126 additions and 3 deletions

View File

@@ -273,6 +273,9 @@ class PlatformOrderController extends Controller
'refund_inconsistent' => (string) $request->query('refund_inconsistent', ''), 'refund_inconsistent' => (string) $request->query('refund_inconsistent', ''),
// 线索联动:用于从开通线索跳转查看关联订单 // 线索联动:用于从开通线索跳转查看关联订单
'lead_id' => trim((string) $request->query('lead_id', '')), 'lead_id' => trim((string) $request->query('lead_id', '')),
// 创建时间范围(用于“趋势→集合”跳转与运营筛选)
'created_from' => trim((string) $request->query('created_from', '')),
'created_to' => trim((string) $request->query('created_to', '')),
]; ];
@@ -1235,6 +1238,9 @@ class PlatformOrderController extends Controller
'refund_inconsistent' => (string) $request->query('refund_inconsistent', ''), 'refund_inconsistent' => (string) $request->query('refund_inconsistent', ''),
// 线索联动:用于从开通线索跳转查看关联订单 // 线索联动:用于从开通线索跳转查看关联订单
'lead_id' => trim((string) $request->query('lead_id', '')), 'lead_id' => trim((string) $request->query('lead_id', '')),
// 创建时间范围(用于“趋势→集合”跳转与运营筛选)
'created_from' => trim((string) $request->query('created_from', '')),
'created_to' => trim((string) $request->query('created_to', '')),
]; ];
@@ -2404,6 +2410,32 @@ class PlatformOrderController extends Controller
} else { } else {
$builder->whereRaw("CAST(JSON_UNQUOTE(JSON_EXTRACT(meta, '$.platform_lead_id')) AS UNSIGNED) = ?", [$leadId]); $builder->whereRaw("CAST(JSON_UNQUOTE(JSON_EXTRACT(meta, '$.platform_lead_id')) AS UNSIGNED) = ?", [$leadId]);
} }
})
->when(($filters['created_from'] ?? '') !== '' || ($filters['created_to'] ?? '') !== '', function (Builder $builder) use ($filters) {
// 创建时间范围筛选:用于“趋势→集合”跳转与运营快速定位
// 口径:基于 created_at。
$from = trim((string) ($filters['created_from'] ?? ''));
$to = trim((string) ($filters['created_to'] ?? ''));
// 容错:仅接受 YYYY-MM-DD 格式;不合法则忽略,避免异常输入污染查询
if ($from !== '' && ! preg_match('/^\d{4}-\d{2}-\d{2}$/', $from)) {
$from = '';
}
if ($to !== '' && ! preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) {
$to = '';
}
if ($from !== '' && $to !== '') {
// [from 00:00:00, to 23:59:59]
$builder->whereBetween('created_at', [
$from . ' 00:00:00',
$to . ' 23:59:59',
]);
} elseif ($from !== '') {
$builder->where('created_at', '>=', $from . ' 00:00:00');
} elseif ($to !== '') {
$builder->where('created_at', '<=', $to . ' 23:59:59');
}
}); });
} }

View File

@@ -249,13 +249,13 @@
</div> </div>
@php @php
// 快捷筛选:尽量保留当前筛选上下文(站点/套餐/订阅ID/keyword/lead_id/back 等),仅覆盖目标筛选字段,并清空 page。 // 快捷筛选:尽量保留当前筛选上下文(站点/套餐/订阅ID/keyword/lead_id/back/时间范围等),仅覆盖目标筛选字段,并清空 page。
// 注意:不保留 syncable_only/fail_only 等“工具型开关”,避免用户从一个集合切到另一个集合时被残留开关影响(导致误判/空结果)。 // 注意:不保留 syncable_only/fail_only 等“工具型开关”,避免用户从一个集合切到另一个集合时被残留开关影响(导致误判/空结果)。
$buildQuickFilterUrl = function (array $overrides) use ($safeBackForLinks) { $buildQuickFilterUrl = function (array $overrides) use ($safeBackForLinks) {
// 快捷筛选:仅保留上下文字段(站点/套餐/订阅ID/keyword/lead_id/安全 back避免把其它筛选条件叠加导致空结果。 // 快捷筛选:仅保留上下文字段(站点/套餐/订阅ID/keyword/lead_id/时间范围/安全 back避免把其它筛选条件叠加导致空结果。
// 该构造器内部会强制清空 page并且不会继承 syncable_only/fail_only 等“工具型开关”。 // 该构造器内部会强制清空 page并且不会继承 syncable_only/fail_only 等“工具型开关”。
return \App\Support\BackUrl::currentPathQuickFilter( return \App\Support\BackUrl::currentPathQuickFilter(
['merchant_id', 'plan_id', 'site_subscription_id', 'keyword', 'lead_id'], ['merchant_id', 'plan_id', 'site_subscription_id', 'keyword', 'lead_id', 'created_from', 'created_to'],
$overrides, $overrides,
$safeBackForLinks $safeBackForLinks
); );
@@ -349,6 +349,8 @@
</select> </select>
<input type="number" name="site_subscription_id" placeholder="订阅ID可选" value="{{ $filters['site_subscription_id'] ?? '' }}" class="w-140"> <input type="number" name="site_subscription_id" placeholder="订阅ID可选" value="{{ $filters['site_subscription_id'] ?? '' }}" class="w-140">
<input type="text" name="created_from" placeholder="创建时间从YYYY-MM-DD" value="{{ $filters['created_from'] ?? '' }}" class="w-180">
<input type="text" name="created_to" placeholder="创建时间到YYYY-MM-DD" value="{{ $filters['created_to'] ?? '' }}" class="w-180">
<label class="form-inline-row"> <label class="form-inline-row">
<input type="checkbox" name="fail_only" value="1" @checked(($filters['fail_only'] ?? '') === '1')> <input type="checkbox" name="fail_only" value="1" @checked(($filters['fail_only'] ?? '') === '1')>
<span>只看同步失败</span> <span>只看同步失败</span>

View File

@@ -0,0 +1,89 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderCreatedAtRangeFilterShouldWorkTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->seed();
$this->post('/admin/login', [
'email' => 'platform.admin@demo.local',
'password' => 'Platform@123456',
])->assertRedirect('/admin');
}
public function test_platform_orders_page_can_filter_by_created_at_range(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'created_at_range_test',
'name' => '创建时间范围筛选测试套餐',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$in = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_CREATED_RANGE_IN',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 10,
'paid_amount' => 0,
]);
$out = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_CREATED_RANGE_OUT',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 10,
'paid_amount' => 0,
]);
// 强制调整 created_at 到不同日期
PlatformOrder::query()->where('id', $in->id)->update([
'created_at' => now()->subDays(2)->startOfDay(),
'updated_at' => now()->subDays(2)->startOfDay(),
]);
PlatformOrder::query()->where('id', $out->id)->update([
'created_at' => now()->subDays(10)->startOfDay(),
'updated_at' => now()->subDays(10)->startOfDay(),
]);
$from = now()->subDays(3)->format('Y-m-d');
$to = now()->subDays(1)->format('Y-m-d');
$this->get('/admin/platform-orders?created_from=' . $from . '&created_to=' . $to)
->assertOk()
->assertSee('PO_CREATED_RANGE_IN')
->assertDontSee('PO_CREATED_RANGE_OUT');
}
}