calc(product-source-link): 计算商品溯源列表中的总数和仓库数

- 移除了字段 'total_nums', 'nums', 'warehouse_total_nums', 'warehouse_nums' 的直接查询
- 使用 each 函数遍历查询结果,计算每个商品的总数量和仓库数量
- 通过 ProductSourceLinkInfo 模型
This commit is contained in:
mkm 2025-03-12 14:34:21 +08:00
parent 47de656777
commit 8c4128ccd4

View File

@ -6,7 +6,8 @@ namespace app\admin\lists\product_source_link;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\product_source_link\ProductSourceLink;
use app\common\lists\ListsSearchInterface;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\warehouse_product\WarehouseProduct;
/**
* 商品溯源管理列表
@ -44,11 +45,14 @@ class ProductSourceLinkLists extends BaseAdminDataLists implements ListsSearchIn
{
return ProductSourceLink::where($this->searchWhere)
->with(['product','warehouse','purchase'])
// ->join('')
->field(['id', 'purchase_uid', 'product_id', 'total_nums', 'nums', 'warehouse_id', 'warehouse_total_nums', 'warehouse_nums'])
->field(['id', 'purchase_uid', 'product_id','warehouse_id',])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->each(function ($item) {
$item->total_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid',$item->id)->where('types',1)->sum('nums');
$item->warehouse_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid',$item->id)->where('types',2)->sum('nums');
})
->toArray();
}