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\DistributionLevel;
use app\common\model\goods\GoodsBrand;
use app\common\model\goods\GoodsCategory;
use app\common\model\shop\ShopFollow;
use app\common\model\user\User;
@ -428,16 +429,27 @@ class GoodsLogic extends Logic
$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';
$list = Goods::field($field)
->where($where)
->order($order)
->page($get['page_no'], $get['page_size'])
->select();
$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)
->where($where)
->order($order)
->page($get['page_no'], $get['page_size'])
->select();
$brandIds = $list->column('brand_id');
$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() : [];
$more = is_more($count, $get['page_no'], $get['page_size']);