feat: 商品列表添加品牌字段

master
yin5th 2023-10-13 14:29:13 +08:00
parent d9806a96cb
commit 1caff8fc70
1 changed files with 19 additions and 7 deletions

View File

@ -5,6 +5,7 @@ namespace app\api\logic;
use app\common\model\distribution\Distribution; use app\common\model\distribution\Distribution;
use app\common\model\distribution\DistributionLevel; use app\common\model\distribution\DistributionLevel;
use app\common\model\goods\GoodsBrand;
use app\common\model\goods\GoodsCategory; use app\common\model\goods\GoodsCategory;
use app\common\model\shop\ShopFollow; use app\common\model\shop\ShopFollow;
use app\common\model\user\User; use app\common\model\user\User;
@ -428,15 +429,26 @@ class GoodsLogic extends Logic
$order = array_merge($elt, $order); $order = array_merge($elt, $order);
} }
$field = 'id,image,name,min_price,market_price,sales_actual,first_cate_id, second_cate_id,third_cate_id,sort_weight,brand_id,shop_id,sales_virtual,(sales_actual + sales_virtual) as sales_total'; $field = 'id,image,name,min_price,market_price,sales_actual,first_cate_id, second_cate_id,third_cate_id,sort_weight,brand_id,shop_id,sales_virtual,(sales_actual + sales_virtual) as sales_total, "" as brand_name';
$count = Goods::where($where)->count();
$list = [];
if ($count > 0) {
$list = Goods::field($field) $list = Goods::field($field)
->where($where) ->where($where)
->order($order) ->order($order)
->page($get['page_no'], $get['page_size']) ->page($get['page_no'], $get['page_size'])
->select(); ->select();
$brandIds = $list->column('brand_id');
$count = Goods::where($where)->count(); $brandIds = array_filter(array_unique($brandIds));
if (!empty($brandIds)) {
$brandName = GoodsBrand::whereIn('id', $brandIds)->column('name', 'id');
$list->each(function ($item) use ($brandName){
$item->brand_name = $brandName[$item->brand_id] ?? '';
});
}
}
$list = $list ? $list->toArray() : []; $list = $list ? $list->toArray() : [];