- 新增违规字典表 store_violation_rules 与推送日志表 store_violation_push_logs;统一表增加预警派生字段(迁移,全部可空带索引) - 预警计算:按平台+违规类型匹配字典(去括号后 相等/前缀/分词/宽松/关键词),匹配不到=不提醒;责任人取店铺账号镜像运营负责人并回退店铺档案;处理截止=申诉截止优先,否则级别天数(P0+1/P1+2/P2+3,当天23:59:59) - 页面:新增预警级别(红/橙/绿标签)、责任人、处理截止、是否超时、推送状态5列;级别/责任人(含我的待办)/超时筛选;默认视图(需提醒&未处理)与全部数据切换;行内 认领/完成/忽略(忽略必填原因) - 处理状态扩为 未处理/处理中/处理完成/已忽略,导入兼容旧值(已处理/无需处理) - 新增「预览统计」页面:平台Tab联动 + 4张KPI卡(今日新增/待处理/P0未处理/已超时)+ 需提醒未处理预览列表与认领 - 群推送先留接口:P0/P1 每天09:00起每5分钟即时推送(责任人、处理建议、去认领签名链接);P2每日10:00汇总(含超时未认领点名);防重与推送日志;新命令 push-instant/push-digest - 同步与导入自动计算派生字段;新增 store-violations:recalc-alerts 重算命令;生产 cron 已登记推送任务 - 测试:StoreViolation 系列 + 部署调度 + 导航 共42项/453断言通过
129 lines
4.6 KiB
PHP
129 lines
4.6 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Repositories;
|
||
|
||
use Illuminate\Database\Connection;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Collection;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* 店铺违规信息预警:推送取数、状态回写与推送日志(business_mirror)。
|
||
*/
|
||
class StoreViolationPushRepository
|
||
{
|
||
public const LOG_TABLE = 'store_violation_push_logs';
|
||
|
||
private Connection $connection;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
|
||
}
|
||
|
||
/**
|
||
* 即时推送候选:需提醒 + 未处理 + 未推送 + 指定级别。
|
||
*
|
||
* @param list<string> $levels
|
||
* @return Collection<int,object>
|
||
*/
|
||
public function instantCandidates(array $levels, bool $retryFailed, int $limit): Collection
|
||
{
|
||
return $this->connection->table(StoreViolationRecordRepository::TABLE)
|
||
->where('needs_alert', true)
|
||
->where('handling_status', (string) config('store_violations.default_handling_status', '未处理'))
|
||
->whereIn('alert_level', $levels)
|
||
->where(function ($query) use ($retryFailed): void {
|
||
$query->where('push_status', '未推送');
|
||
if ($retryFailed) {
|
||
$query->orWhere('push_status', '推送失败');
|
||
}
|
||
})
|
||
->orderByRaw("CASE alert_level WHEN 'P0' THEN 0 WHEN 'P1' THEN 1 ELSE 2 END")
|
||
->orderBy('id')
|
||
->limit(max($limit, 1))
|
||
->get();
|
||
}
|
||
|
||
/**
|
||
* 每日汇总取数:各级别待处理数量与超时未认领记录。
|
||
*
|
||
* @return array{pending:array<string,int>,overdue:Collection<int,object>,p2_pending:Collection<int,object>}
|
||
*/
|
||
public function digestData(int $overdueLimit = 20): array
|
||
{
|
||
$defaultStatus = (string) config('store_violations.default_handling_status', '未处理');
|
||
|
||
$pending = [];
|
||
foreach ($this->connection->table(StoreViolationRecordRepository::TABLE)
|
||
->selectRaw('alert_level, COUNT(*) AS aggregate')
|
||
->where('needs_alert', true)
|
||
->where('handling_status', $defaultStatus)
|
||
->groupBy('alert_level')
|
||
->get() as $row) {
|
||
$pending[(string) $row->alert_level] = (int) $row->aggregate;
|
||
}
|
||
|
||
$overdue = $this->connection->table(StoreViolationRecordRepository::TABLE)
|
||
->whereIn('alert_level', array_keys((array) config('store_violations.alert_levels', [])))
|
||
->whereNotIn('handling_status', StoreViolationRecordRepository::FINAL_HANDLING_STATUSES)
|
||
->whereNotNull('handling_deadline')
|
||
->where('handling_deadline', '<', Carbon::now())
|
||
->where('needs_alert', true)
|
||
->orderBy('handling_deadline')
|
||
->limit(max($overdueLimit, 1))
|
||
->get(['id', 'platform', 'shop_name', 'owner_name', 'alert_level', 'violation_type', 'handling_deadline']);
|
||
|
||
$p2 = $this->connection->table(StoreViolationRecordRepository::TABLE)
|
||
->where('needs_alert', true)
|
||
->where('alert_level', 'P2')
|
||
->where('handling_status', $defaultStatus)
|
||
->where('push_status', '未推送')
|
||
->orderBy('id')
|
||
->get(['id']);
|
||
|
||
return ['pending' => $pending, 'overdue' => $overdue, 'p2_pending' => $p2];
|
||
}
|
||
|
||
/** @param list<int> $ids */
|
||
public function markPushed(array $ids, Carbon $at): void
|
||
{
|
||
if ($ids === []) {
|
||
return;
|
||
}
|
||
|
||
foreach (array_chunk($ids, 500) as $chunk) {
|
||
$this->connection->table(StoreViolationRecordRepository::TABLE)
|
||
->whereIn('id', $chunk)
|
||
->update(['push_status' => '已推送', 'last_pushed_at' => $at, 'updated_at' => $at]);
|
||
}
|
||
}
|
||
|
||
public function markPushFailed(int $id, Carbon $at): void
|
||
{
|
||
$this->connection->table(StoreViolationRecordRepository::TABLE)
|
||
->where('id', $id)
|
||
->update(['push_status' => '推送失败', 'last_pushed_at' => $at, 'updated_at' => $at]);
|
||
}
|
||
|
||
/** @param array<string,mixed> $entry */
|
||
public function log(array $entry): void
|
||
{
|
||
if (! $this->available()) {
|
||
return;
|
||
}
|
||
|
||
$now = now();
|
||
$this->connection->table(self::LOG_TABLE)->insert($entry + [
|
||
'created_at' => $now,
|
||
'updated_at' => $now,
|
||
]);
|
||
}
|
||
|
||
public function available(): bool
|
||
{
|
||
return \Illuminate\Support\Facades\Schema::connection($this->connection->getName())
|
||
->hasTable(self::LOG_TABLE);
|
||
}
|
||
}
|