fix: 退款待确认按售后单汇总多商品明细 (#934)
This commit is contained in:
parent
b4d3ec9d14
commit
fc313deb8a
@ -7309,7 +7309,7 @@ class Pages extends Controller
|
||||
|
||||
$platformKey = sha1($platform);
|
||||
$details = \Illuminate\Support\Facades\Cache::remember(
|
||||
"refund-pending.details.v2:{$monthKey}:{$end->toDateString()}:{$platformKey}:{$page}:{$limit}",
|
||||
"refund-pending.details.v3:{$monthKey}:{$end->toDateString()}:{$platformKey}:{$page}:{$limit}",
|
||||
now()->addMinutes(5),
|
||||
function () use ($filters, $page, $limit): array {
|
||||
$result = $this->nonJingxiRefunds->pagePendingConfirmation($filters, $page, $limit);
|
||||
|
||||
@ -238,12 +238,24 @@ class NonJingxiRefundMirrorRepository
|
||||
public function paginatePendingConfirmation(array $filters, int $page, int $perPage): array
|
||||
{
|
||||
$this->assertPendingStatusReady();
|
||||
$result = $this->paginate($this->pendingConfirmationFilters($filters), $page, $perPage);
|
||||
foreach ($result['data'] as $row) {
|
||||
if (! $this->ready() || ! $this->queryReady()) {
|
||||
throw new RuntimeException('business_mirror_dataset_not_ready');
|
||||
}
|
||||
|
||||
$page = max($page, 1);
|
||||
$perPage = min(max($perPage, 1), 100);
|
||||
$pendingFilters = $this->pendingConfirmationFilters($filters);
|
||||
$total = $this->sourceQuery($pendingFilters, false, false)->count()
|
||||
+ $this->sourceQuery($pendingFilters, true, false)->count();
|
||||
$keys = $this->orderedHeaderKeys($pendingFilters, null, $page * $perPage)
|
||||
->forPage($page, $perPage)
|
||||
->get();
|
||||
$rows = $this->loadHeaderRows($pendingFilters, $keys)->all();
|
||||
foreach ($rows as $row) {
|
||||
$this->normalizePendingConfirmationLabels($row);
|
||||
}
|
||||
|
||||
return $result;
|
||||
return ['data' => $rows, 'total' => $total];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -262,10 +274,10 @@ class NonJingxiRefundMirrorRepository
|
||||
$page = max($page, 1);
|
||||
$perPage = min(max($perPage, 1), 100);
|
||||
$pendingFilters = $this->pendingConfirmationFilters($filters);
|
||||
$keys = $this->orderedKeys($pendingFilters, null, ($page * $perPage) + 1)->get();
|
||||
$keys = $this->orderedHeaderKeys($pendingFilters, null, ($page * $perPage) + 1)->get();
|
||||
$hasMore = $keys->count() > $page * $perPage;
|
||||
$pageKeys = $keys->slice(($page - 1) * $perPage, $perPage)->values();
|
||||
$rows = $this->loadRows($pageKeys)->all();
|
||||
$rows = $this->loadHeaderRows($pendingFilters, $pageKeys)->all();
|
||||
foreach ($rows as $row) {
|
||||
$this->normalizePendingConfirmationLabels($row);
|
||||
}
|
||||
@ -457,10 +469,60 @@ class NonJingxiRefundMirrorRepository
|
||||
public function exportPendingConfirmationRows(array $filters, int $chunkSize = 1000): iterable
|
||||
{
|
||||
$this->assertPendingStatusReady();
|
||||
foreach ($this->exportRows($this->pendingConfirmationFilters($filters), $chunkSize) as $row) {
|
||||
$this->normalizePendingConfirmationLabels($row);
|
||||
yield $row;
|
||||
if (! $this->ready() || ! $this->queryReady()) {
|
||||
throw new RuntimeException('business_mirror_dataset_not_ready');
|
||||
}
|
||||
|
||||
$chunkSize = min(max($chunkSize, 1), 1000);
|
||||
$pendingFilters = $this->pendingConfirmationFilters($filters);
|
||||
$cursor = null;
|
||||
do {
|
||||
$keys = $this->orderedHeaderKeys($pendingFilters, $cursor, $chunkSize)->limit($chunkSize)->get();
|
||||
if ($keys->isEmpty()) {
|
||||
break;
|
||||
}
|
||||
foreach ($this->loadHeaderRows($pendingFilters, $keys) as $row) {
|
||||
$this->normalizePendingConfirmationLabels($row);
|
||||
yield $row;
|
||||
}
|
||||
$cursor = $keys->last();
|
||||
} while ($keys->count() === $chunkSize);
|
||||
}
|
||||
|
||||
private const HEADER_SORT_COLUMNS = ['refundDate', '_source_rank', '_head_id', '_source_id'];
|
||||
|
||||
private function orderedHeaderKeys(array $filters, ?object $cursor, int $limit): Builder
|
||||
{
|
||||
$branches = [];
|
||||
foreach ([false, true] as $supplement) {
|
||||
$query = $this->sourceQuery($filters, $supplement, false);
|
||||
$query->selectRaw($supplement
|
||||
? "r.as_date AS refundDate, 1 AS _source_rank, 0 AS _head_id, r.as_id AS _source_id"
|
||||
: "r.as_date AS refundDate, 0 AS _source_rank, r.Id AS _head_id, '' AS _source_id");
|
||||
if ($cursor !== null) {
|
||||
$query->where(function (Builder $after) use ($cursor): void {
|
||||
foreach (self::HEADER_SORT_COLUMNS as $position => $column) {
|
||||
$after->orWhere(function (Builder $part) use ($cursor, $position, $column): void {
|
||||
foreach (array_slice(self::HEADER_SORT_COLUMNS, 0, $position) as $equal) {
|
||||
$part->where($equal, '=', $cursor->$equal);
|
||||
}
|
||||
$part->where($column, '<', $cursor->$column);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
foreach (self::HEADER_SORT_COLUMNS as $column) {
|
||||
$query->orderByDesc($column);
|
||||
}
|
||||
$query->limit($limit);
|
||||
$branches[] = $this->connection->query()->fromSub($query, 'bounded_headers');
|
||||
}
|
||||
$rows = $this->connection->query()->fromSub($branches[0]->unionAll($branches[1]), 'refund_headers');
|
||||
foreach (self::HEADER_SORT_COLUMNS as $column) {
|
||||
$rows->orderByDesc($column);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
@ -667,6 +729,24 @@ class NonJingxiRefundMirrorRepository
|
||||
];
|
||||
}
|
||||
|
||||
private function selectSourceRows(Builder $query, bool $supplement): Builder
|
||||
{
|
||||
if ($supplement) {
|
||||
return $query->select([
|
||||
'r.as_date as refundDate', 'r.as_id', 'r.outer_as_id', 'r.l_id as waybillNo',
|
||||
'r.o_id', 'r.so_id', 'r.type', 'r.node', 'r.shop_type', 'r.shop_name',
|
||||
'r.refund as totalRefundMoney', 'i.outer_oi_id', 'i.shop_sku_id', 'i.i_id', 'i.sku_id',
|
||||
'i.qty', 'i.shop_amount as detailRefundMoney', 'i.type as refundType',
|
||||
'i.r_qty as refundQty', 'r.buyer_apply_refund as AfSalesMoney',
|
||||
'r.local_created as createTime', 'r.status', 'r.shop_status', 'r.created', 'r.confirm_date',
|
||||
'r.shop_id',
|
||||
])->selectRaw("COALESCE(i.asi_id, r.as_id) AS Id, NULL AS RefundMoneyIncome, '退款清单' AS dataSource, 1 AS _source_rank, 0 AS _head_id, COALESCE(i.asi_id, 0) AS _item_id, r.as_id AS _source_id");
|
||||
}
|
||||
|
||||
return $query->select($this->selectColumns())
|
||||
->selectRaw("COALESCE(i.Id, r.Id) AS Id, '售后资料' AS dataSource, 0 AS _source_rank, r.Id AS _head_id, COALESCE(i.Id, 0) AS _item_id, '' AS _source_id");
|
||||
}
|
||||
|
||||
private function loadRows($keys)
|
||||
{
|
||||
$loaded = collect();
|
||||
@ -687,24 +767,24 @@ class NonJingxiRefundMirrorRepository
|
||||
$items->orWhereNull($itemKey);
|
||||
}
|
||||
});
|
||||
if ($supplement) {
|
||||
$query->select([
|
||||
'r.as_date as refundDate', 'r.as_id', 'r.outer_as_id', 'r.l_id as waybillNo',
|
||||
'r.o_id', 'r.so_id', 'r.type', 'r.node', 'r.shop_type', 'r.shop_name',
|
||||
'r.refund as totalRefundMoney', 'i.outer_oi_id', 'i.shop_sku_id', 'i.i_id', 'i.sku_id',
|
||||
'i.qty', 'i.shop_amount as detailRefundMoney', 'i.type as refundType',
|
||||
'i.r_qty as refundQty', 'r.buyer_apply_refund as AfSalesMoney',
|
||||
'r.local_created as createTime', 'r.status', 'r.shop_status', 'r.created', 'r.confirm_date',
|
||||
'r.shop_id',
|
||||
])->selectRaw("COALESCE(i.asi_id, r.as_id) AS Id, NULL AS RefundMoneyIncome, '退款清单' AS dataSource, 1 AS _source_rank, 0 AS _head_id, COALESCE(i.asi_id, 0) AS _item_id, r.as_id AS _source_id");
|
||||
} else {
|
||||
$query->select($this->selectColumns())
|
||||
->selectRaw("COALESCE(i.Id, r.Id) AS Id, '售后资料' AS dataSource, 0 AS _source_rank, r.Id AS _head_id, COALESCE(i.Id, 0) AS _item_id, '' AS _source_id");
|
||||
}
|
||||
$this->selectSourceRows($query, $supplement);
|
||||
$loaded = $loaded->concat($query->get());
|
||||
}
|
||||
$byIdentity = $loaded->keyBy(fn (object $row): string => $this->identity($row));
|
||||
$rows = $keys->map(fn (object $key) => $byIdentity->get($this->identity($key)))->filter()->values();
|
||||
$this->enrichRows($rows);
|
||||
foreach ($rows as $row) {
|
||||
foreach (array_slice(self::SORT_COLUMNS, 1) as $column) {
|
||||
unset($row->$column);
|
||||
}
|
||||
}
|
||||
$this->hydratePage($rows);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function enrichRows($rows): void
|
||||
{
|
||||
$shopIds = $rows->where('_source_rank', 1)->pluck('shop_id')->unique();
|
||||
$platforms = $shopIds->isEmpty() ? collect() : $this->connection->table('ops_jst_shop_info')
|
||||
->whereIn('shop_id', $shopIds)->get(['shop_id', 'shop_site'])->groupBy('shop_id');
|
||||
@ -720,15 +800,128 @@ class NonJingxiRefundMirrorRepository
|
||||
$row->$money = null;
|
||||
}
|
||||
}
|
||||
foreach (array_slice(self::SORT_COLUMNS, 1) as $column) {
|
||||
}
|
||||
}
|
||||
|
||||
private function loadHeaderRows(array $filters, $keys)
|
||||
{
|
||||
if ($keys->isEmpty()) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$loaded = collect();
|
||||
foreach ([false, true] as $supplement) {
|
||||
$sourceKeys = $keys->filter(fn (object $key): bool => (bool) $key->_source_rank === $supplement);
|
||||
if ($sourceKeys->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$query = $this->sourceQuery($filters, $supplement)
|
||||
->whereIn(
|
||||
$supplement ? 'r.as_id' : 'r.Id',
|
||||
$sourceKeys->pluck($supplement ? '_source_id' : '_head_id'),
|
||||
);
|
||||
$this->selectSourceRows($query, $supplement);
|
||||
$loaded = $loaded->concat($query->get());
|
||||
}
|
||||
|
||||
$deduplicated = $loaded
|
||||
->groupBy(fn (object $row): string => $this->headerIdentity($row) . '|' . $this->detailBusinessKey($row))
|
||||
->map(fn ($rows) => $rows->sortByDesc(fn (object $row): int => (int) $row->_item_id)->first())
|
||||
->values();
|
||||
$this->enrichRows($deduplicated);
|
||||
$this->hydratePage($deduplicated);
|
||||
|
||||
$byHeader = $deduplicated
|
||||
->groupBy(fn (object $row): string => $this->headerIdentity($row))
|
||||
->map(fn ($rows) => $this->aggregateHeaderRows($rows));
|
||||
$rows = $keys
|
||||
->map(fn (object $key) => $byHeader->get($this->headerIdentity($key)))
|
||||
->filter()
|
||||
->values();
|
||||
foreach ($rows as $row) {
|
||||
foreach (['_source_rank', '_head_id', '_source_id', '_item_id'] as $column) {
|
||||
unset($row->$column);
|
||||
}
|
||||
}
|
||||
$this->hydratePage($rows);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function aggregateHeaderRows($rows): object
|
||||
{
|
||||
$row = $rows->first();
|
||||
if ($rows->count() === 1) {
|
||||
return $row;
|
||||
}
|
||||
|
||||
$row->Id = (int) $rows->max(fn (object $item): int => (int) $item->_item_id);
|
||||
$row->_item_id = $row->Id;
|
||||
foreach (['shop_sku_id', 'i_id', 'sku_id', 'refundType'] as $field) {
|
||||
$row->$field = $this->joinDistinctValues($rows->pluck($field));
|
||||
}
|
||||
foreach (['qty', 'refundQty', 'detailRefundMoney', 'refundCostGoods'] as $field) {
|
||||
$row->$field = $this->sumNumericValues($rows->pluck($field));
|
||||
}
|
||||
$row->buyer_paid_amount = null;
|
||||
$row->unitCostGoods = null;
|
||||
$row->detailStatus = '已关联(多商品)';
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
private function joinDistinctValues($values): ?string
|
||||
{
|
||||
$values = $values
|
||||
->map(static fn (mixed $value): string => trim((string) ($value ?? '')))
|
||||
->filter(static fn (string $value): bool => $value !== '')
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
return $values->isEmpty() ? null : $values->implode('、');
|
||||
}
|
||||
|
||||
private function sumNumericValues($values): int|float|null
|
||||
{
|
||||
$numbers = $values
|
||||
->filter(static fn (mixed $value): bool => is_numeric($value))
|
||||
->map(static fn (mixed $value): float => (float) $value);
|
||||
if ($numbers->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sum = round((float) $numbers->sum(), 4);
|
||||
|
||||
return abs($sum - round($sum)) < 0.00001 ? (int) round($sum) : $sum;
|
||||
}
|
||||
|
||||
private function headerIdentity(object $row): string
|
||||
{
|
||||
return implode('|', [$row->_source_rank, $row->_head_id, $row->_source_id]);
|
||||
}
|
||||
|
||||
private function detailBusinessKey(object $row): string
|
||||
{
|
||||
$values = [];
|
||||
foreach (
|
||||
[
|
||||
'outer_oi_id',
|
||||
'shop_sku_id',
|
||||
'i_id',
|
||||
'sku_id',
|
||||
'qty',
|
||||
'detailRefundMoney',
|
||||
'refundQty',
|
||||
'refundType',
|
||||
] as $field
|
||||
) {
|
||||
$value = $row->$field ?? null;
|
||||
$values[] = $value === null ? "\x02" : trim((string) $value);
|
||||
}
|
||||
|
||||
return implode("\x1f", $values);
|
||||
}
|
||||
|
||||
private function identity(object $row): string
|
||||
{
|
||||
return implode('|', [$row->_source_rank, $row->_head_id, $row->_item_id, $row->_source_id]);
|
||||
|
||||
@ -259,6 +259,63 @@ class NonJingxiRefundMirrorRepositoryTest extends TestCase
|
||||
$this->assertSame(12, (int) $export[0]->Id);
|
||||
}
|
||||
|
||||
public function test_pending_confirmation_combines_multiple_items_into_one_refund_row(): void
|
||||
{
|
||||
$db = DB::connection('business_mirror');
|
||||
$db->table('ops_jst_return_refund_info')->insert([
|
||||
array_replace($this->refundInfo(1, 101, '2026-10-31 16:57:49', '京东商城', '京东测试店'), [
|
||||
'status' => 'WaitConfirm',
|
||||
'shop_status' => 'SUCCESS',
|
||||
]),
|
||||
]);
|
||||
$first = array_replace($this->refundItem(11, 101, 'SKU-A', 'ITEM-A'), [
|
||||
'qty' => 1,
|
||||
'shop_amount' => 150.62,
|
||||
'r_qty' => 1,
|
||||
]);
|
||||
$second = array_replace($this->refundItem(12, 101, 'SKU-B', 'ITEM-B'), [
|
||||
'qty' => 1,
|
||||
'shop_amount' => 652.16,
|
||||
'r_qty' => 1,
|
||||
]);
|
||||
$db->table('ops_jst_return_refund_item')->insert([
|
||||
$first,
|
||||
$second,
|
||||
array_replace($first, ['Id' => 13]),
|
||||
array_replace($second, ['Id' => 14]),
|
||||
]);
|
||||
$db->table('ops_jst_refund_status')->insert([
|
||||
['as_id' => '101', 'status' => 'WaitConfirm', 'shop_status' => 'SUCCESS'],
|
||||
]);
|
||||
|
||||
$filters = [
|
||||
'from' => '2026-10-01',
|
||||
'to' => '2026-10-31',
|
||||
'order_no' => '',
|
||||
'waybill_no' => '',
|
||||
'shop_name' => '',
|
||||
'sku_id' => '',
|
||||
'platform' => '京东商城',
|
||||
];
|
||||
$page = $this->repository->pagePendingConfirmation($filters, 1, 25);
|
||||
$this->assertCount(1, $page['data']);
|
||||
$this->assertFalse($page['has_more']);
|
||||
$this->assertSame('SKU-A、SKU-B', $page['data'][0]->sku_id);
|
||||
$this->assertSame(2, $page['data'][0]->qty);
|
||||
$this->assertSame(2, $page['data'][0]->refundQty);
|
||||
$this->assertSame(802.78, $page['data'][0]->detailRefundMoney);
|
||||
$this->assertSame(14, (int) $page['data'][0]->Id);
|
||||
$this->assertSame('已关联(多商品)', $page['data'][0]->detailStatus);
|
||||
|
||||
$paged = $this->repository->paginatePendingConfirmation($filters, 1, 25);
|
||||
$this->assertSame(1, $paged['total']);
|
||||
$this->assertCount(1, $paged['data']);
|
||||
|
||||
$export = iterator_to_array($this->repository->exportPendingConfirmationRows($filters, 1));
|
||||
$this->assertCount(1, $export);
|
||||
$this->assertSame('SKU-A、SKU-B', $export[0]->sku_id);
|
||||
}
|
||||
|
||||
public function test_pending_confirmation_monthly_summary_combines_unique_source_counts(): void
|
||||
{
|
||||
$db = DB::connection('business_mirror');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user