188 lines
9.7 KiB
PHP
188 lines
9.7 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
/**
|
||
* 平台订单列表页工具区:前端治理阻断口径(减少误点)。
|
||
*
|
||
* 说明:后端各批量动作/清理动作本身仍有安全阀阻断;这里仅用于 UI 层“按钮禁用 + 提示原因”。
|
||
*/
|
||
class PlatformOrderToolsGuard
|
||
{
|
||
/**
|
||
* @param array<string, mixed> $filters
|
||
* @return array<string, array{blocked:bool, reason:string}>
|
||
*/
|
||
public static function forIndex(array $filters): array
|
||
{
|
||
$batchActivateReason = self::batchActivateSubscriptionsReason($filters);
|
||
$batchBmpaReason = self::batchBmpaReason($filters);
|
||
$batchMarkActivatedReason = self::batchMarkActivatedReason($filters);
|
||
$clearSyncReason = self::clearSyncErrorsReason($filters);
|
||
$clearBmpaReason = self::clearBmpaErrorsReason($filters);
|
||
|
||
return [
|
||
'batch_activate_subscriptions' => [
|
||
'blocked' => $batchActivateReason !== '',
|
||
'reason' => $batchActivateReason,
|
||
],
|
||
'batch_bmpa' => [
|
||
'blocked' => $batchBmpaReason !== '',
|
||
'reason' => $batchBmpaReason,
|
||
],
|
||
'batch_mark_activated' => [
|
||
'blocked' => $batchMarkActivatedReason !== '',
|
||
'reason' => $batchMarkActivatedReason,
|
||
],
|
||
'clear_sync_errors' => [
|
||
'blocked' => $clearSyncReason !== '',
|
||
'reason' => $clearSyncReason,
|
||
],
|
||
'clear_bmpa_errors' => [
|
||
'blocked' => $clearBmpaReason !== '',
|
||
'reason' => $clearBmpaReason,
|
||
],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $filters
|
||
*/
|
||
public static function batchActivateSubscriptionsReason(array $filters): string
|
||
{
|
||
if ((string) ($filters['syncable_only'] ?? '') !== '1') {
|
||
return '请先勾选「只看可同步」再执行批量同步。';
|
||
}
|
||
// 口径一致:可同步集合语义为 unsynced(未同步且非失败)。
|
||
// 若用户手工叠加了 sync_status=failed/synced 等互斥筛选,按钮应直接禁用,避免误解“当前会同步哪些单”。
|
||
$syncStatus = (string) ($filters['sync_status'] ?? '');
|
||
if ($syncStatus !== '' && $syncStatus !== 'unsynced') {
|
||
return '当前已勾选「只看可同步」,但同步状态筛选不是「未同步」。请先切回 sync_status=unsynced(或清空同步状态筛选)后再批量同步。';
|
||
}
|
||
if ((string) ($filters['synced_only'] ?? '') === '1') {
|
||
return '当前已勾选「只看可同步」,但又勾选了「只看已同步」:两者语义互斥。请先取消只看已同步后再批量同步。';
|
||
}
|
||
if ((string) ($filters['fail_only'] ?? '') === '1' || trim((string) ($filters['sync_error_keyword'] ?? '')) !== '') {
|
||
return '当前筛选包含「同步失败/失败原因」。请先治理失败原因或切回未同步集合,再批量同步。';
|
||
}
|
||
if ((string) ($filters['receipt_status'] ?? '') === 'none') {
|
||
return '当前集合为「无回执」:建议先补齐支付回执留痕后再批量同步。';
|
||
}
|
||
if ((string) ($filters['refund_status'] ?? '') === 'has') {
|
||
return '当前集合为「有退款」:为避免带退款订单直接同步订阅,请先完成退款治理(核对退款回执/修正状态)后再批量同步。';
|
||
}
|
||
if (((string) ($filters['reconcile_mismatch'] ?? '') === '1') || ((string) ($filters['refund_inconsistent'] ?? '') === '1')) {
|
||
return '当前集合包含「对账不一致/退款不一致」:建议先完成金额/状态治理后再批量同步。';
|
||
}
|
||
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $filters
|
||
*/
|
||
public static function batchBmpaReason(array $filters): string
|
||
{
|
||
if ((string) ($filters['status'] ?? '') !== 'pending' || (string) ($filters['payment_status'] ?? '') !== 'unpaid') {
|
||
return '请先筛选「订单状态=待处理」且「支付状态=未支付」再执行批量 BMPA。';
|
||
}
|
||
|
||
// 互斥筛选阻断:BMPA 面向“待处理+未支付”推进链路;若叠加同步治理筛选,语义会混乱,且可能误伤治理集合。
|
||
if ((string) ($filters['syncable_only'] ?? '') === '1') {
|
||
return '当前已勾选「只看可同步」:该集合语义为“已生效+未同步”,与 BMPA(待处理+未支付)互斥。请先取消只看可同步后再执行。';
|
||
}
|
||
|
||
$hasSyncGovernanceFilters = ((string) ($filters['synced_only'] ?? '') === '1')
|
||
|| (trim((string) ($filters['sync_status'] ?? '')) !== '')
|
||
|| ((string) ($filters['fail_only'] ?? '') === '1')
|
||
|| (trim((string) ($filters['sync_error_keyword'] ?? '')) !== '');
|
||
|
||
if ($hasSyncGovernanceFilters) {
|
||
return '当前筛选包含「订阅同步治理」相关条件(同步状态/同步失败/失败原因等)。BMPA 动作仅用于推进待处理未支付订单,请先清空同步治理筛选后再执行。';
|
||
}
|
||
|
||
if (((string) ($filters['reconcile_mismatch'] ?? '') === '1') || ((string) ($filters['refund_inconsistent'] ?? '') === '1')) {
|
||
return '当前集合包含「对账不一致/退款不一致」治理集合:建议先完成回执/退款治理后再批量推进。';
|
||
}
|
||
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $filters
|
||
*/
|
||
public static function batchMarkActivatedReason(array $filters): string
|
||
{
|
||
if ((string) ($filters['payment_status'] ?? '') !== 'paid' || (string) ($filters['status'] ?? '') !== 'pending') {
|
||
return '请先筛选「支付状态=已支付」且「订单状态=待处理」再执行批量生效。';
|
||
}
|
||
|
||
// 待生效口径一致:paid + pending + unsynced(排除同步失败等异常单),避免误把失败单“批量生效”导致治理链路混乱。
|
||
if ((string) ($filters['sync_status'] ?? '') !== 'unsynced') {
|
||
return '为避免把同步失败等异常单混入,请先锁定「同步状态=未同步(sync_status=unsynced)」(建议用快捷筛选「待生效」)再执行批量生效。';
|
||
}
|
||
|
||
// 互斥筛选阻断:避免运营在其它治理集合上误触“批量生效”导致误解。
|
||
if ((string) ($filters['fail_only'] ?? '') === '1' || trim((string) ($filters['sync_error_keyword'] ?? '')) !== '') {
|
||
return '当前筛选包含「同步失败/失败原因」治理集合:与“待生效(unsynced)”互斥。请先切回待生效集合后再执行批量生效。';
|
||
}
|
||
if ((string) ($filters['synced_only'] ?? '') === '1') {
|
||
return '当前已勾选「只看已同步」:该集合与“待生效(unsynced)”互斥。请先取消该筛选后再执行批量生效。';
|
||
}
|
||
if ((string) ($filters['syncable_only'] ?? '') === '1') {
|
||
return '当前已勾选「只看可同步」:该集合语义为“已生效(activated)+未同步”,与本动作处理的“待处理(pending)”互斥。请先取消只看可同步后再执行。';
|
||
}
|
||
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $filters
|
||
*/
|
||
public static function clearSyncErrorsReason(array $filters): string
|
||
{
|
||
// 与后端 clearSyncErrors() 阻断口径对齐:避免运营在互斥集合上误清理。
|
||
$syncStatus = (string) ($filters['sync_status'] ?? '');
|
||
if ($syncStatus !== '' && $syncStatus !== 'failed') {
|
||
return '当前为「清理同步失败标记」动作:若需要筛选同步状态,请使用 sync_status=failed(或清空该筛选)后再执行。';
|
||
}
|
||
|
||
if ((string) ($filters['syncable_only'] ?? '') === '1') {
|
||
return '当前已勾选「只看可同步」:该集合与「同步失败」互斥,请先取消只看可同步或切到失败集合后再清理。';
|
||
}
|
||
|
||
$hasAnyFailedScope = ($syncStatus === 'failed')
|
||
|| ((string) ($filters['fail_only'] ?? '') === '1')
|
||
|| (trim((string) ($filters['sync_error_keyword'] ?? '')) !== '');
|
||
|
||
if (! $hasAnyFailedScope) {
|
||
return '建议先筛选「同步失败」集合(sync_status=failed / fail_only=1 / 失败原因关键词)后再执行清理,避免误清理。';
|
||
}
|
||
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $filters
|
||
*/
|
||
public static function clearBmpaErrorsReason(array $filters): string
|
||
{
|
||
// 口径对齐后端安全阀:清理 BMPA 失败标记必须先锁定 BMPA 治理集合,且不得叠加与 BMPA 治理无关/互斥的筛选。
|
||
if ((string) ($filters['syncable_only'] ?? '') === '1') {
|
||
return '当前已勾选「只看可同步」:该集合与「BMPA失败」治理集合无关。请先取消只看可同步或切到 BMPA 失败集合后再清理。';
|
||
}
|
||
|
||
if ((string) ($filters['fail_only'] ?? '') === '1' || trim((string) ($filters['sync_error_keyword'] ?? '')) !== '') {
|
||
return '当前筛选包含「同步失败/失败原因」:该集合用于订阅同步治理,请切到 BMPA 失败集合后再清理 BMPA 失败标记。';
|
||
}
|
||
|
||
if ((string) ($filters['bmpa_failed_only'] ?? '') === ''
|
||
&& trim((string) ($filters['bmpa_error_keyword'] ?? '')) === ''
|
||
&& (string) ($filters['batch_mark_paid_and_activate_24h'] ?? '') === '') {
|
||
return '建议先筛选「BMPA失败」集合(bmpa_failed_only=1 或失败原因关键词,或勾选 batch_mark_paid_and_activate_24h)后再执行清理,避免误清理。';
|
||
}
|
||
|
||
return '';
|
||
}
|
||
}
|