采购单优化
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Modules\SystemTool\Models\SysFileModel;
|
||||
|
||||
/**
|
||||
* 商品快照首图解析:从明细 image_ids 批量解析文件 URL(门店订单列表/详情、采购单元格下钻共用)
|
||||
*/
|
||||
class ItemImageResolver
|
||||
{
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $items 明细数组(引用修改:写入 image,移除 image_ids)
|
||||
*/
|
||||
public function resolve(array &$items): void
|
||||
{
|
||||
$fileIds = [];
|
||||
foreach ($items as $line) {
|
||||
foreach ((array) ($line['image_ids'] ?? []) as $fileId) {
|
||||
if ($fileId !== '' && $fileId !== null) {
|
||||
$fileIds[] = (int) $fileId;
|
||||
}
|
||||
}
|
||||
}
|
||||
$fileUrls = [];
|
||||
if ($fileIds !== []) {
|
||||
foreach (SysFileModel::query()->whereIn('id', array_unique($fileIds))->get() as $file) {
|
||||
$fileUrls[$file->id] = $file->preview_url;
|
||||
}
|
||||
}
|
||||
foreach ($items as &$product) {
|
||||
$product['image'] = '';
|
||||
foreach ((array) ($product['image_ids'] ?? []) as $fileId) {
|
||||
if ($fileId !== '' && $fileId !== null && isset($fileUrls[(int) $fileId])) {
|
||||
$product['image'] = $fileUrls[(int) $fileId];
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($product['image_ids']);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user