hasOne(Merchant::class,'mer_id','mer_id')->field('is_trader,type_id,mer_id,mer_name,mer_avatar,product_score,service_score,postage_score,service_phone,care_count'); } public function storeCategory() { return $this->hasOne(StoreCategory::class,'store_category_id','cate_id')->field('store_category_id,cate_name'); } public function merCateId() { return $this->hasMany(ProductCate::class,'product_id','product_id')->field('product_id, mer_cate_id'); } public function brand() { return $this->hasOne(StoreBrand::class,'brand_id','brand_id')->field('brand_id,brand_name'); } /** * TODO 商户商品列表 * @Author:Qinii * @Date: 2020/5/11 * @param int $merId * @param array $where * @param int $page * @param int $limit * @return array */ public function getList(?int $mer_id, array $where, int $page, int $limit) { $query = self::search($mer_id, $where) ->with(['merCateId.category', 'storeCategory', 'brand', 'Merchant']); $count = $query->count(); $data = $query->field($this->filed)->page($page, $limit) ->order('Product.product_id desc') // ->fetchSql() ->select(); // echo $data;exit('--'); $data->append(['us_status']); $list = $this->searchList( $data, 'mer_labels', ProductLabel::class, 'product_label_id', 'mer_labels', ['status' => 1], 'product_label_id,product_label_id id,label_name name' ); return compact('count', 'list'); } // public function StoreSpu() // {} /** * @Author:Qinii * @Date: 2020/5/11 * @param int $merId * @param array $where * @return mixed */ protected function search(?int $merId, array $where) { $keyArray = $whereArr = []; unset($where['type']); // 以下字段为要搜索的字段 $out = ['soft', 'us_status', 'mer_labels', 'sys_labels', 'order', 'hot_type']; foreach ($where as $key => $item) { if ($item !== '' && !in_array($key, $out)) { $keyArray[] = $key; $whereArr[$key] = $item; } } // $query = isset($where['soft']) ? model::onlyTrashed()->alias('Product') : model::alias('Product'); $query = self::alias('Product'); if (isset($where['is_trader']) && $where['is_trader'] !== '') { // hasWhere添加别名不生效 alias, 改为Join // $query->hasWhere('merchant', // function ($query) use ($where) { // $query->where('is_trader', $where['is_trader']); // } // ); $query->Join('Merchant', 'Merchant.mer_id = Product.mer_id') ->where('Merchant.is_trader', $where['is_trader']); } $query->withSearch($keyArray, $whereArr) ->Join('StoreSpu U', 'Product.product_id = U.product_id') ->where('U.product_type', $where['product_type'] ?? 0) ->when(($merId !== null), function ($query) use ($merId) { $query->where('Product.mer_id', $merId); } ) ->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) { if ($where['hot_type'] == 'new') $query->where('is_new', 1); else if ($where['hot_type'] == 'hot') $query->where('is_hot', 1); else if ($where['hot_type'] == 'best') $query->where('is_best', 1); else if ($where['hot_type'] == 'good') $query->where('is_benefit', 1); } ) ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) { $ids = array_merge(self::findChildrenId((int)$where['pid']), [(int)$where['pid']]); if (count($ids)) $query->whereIn('cate_id', $ids); } ) ->when(isset($where['us_status']) && $where['us_status'] !== '', function ($query) use ($where) { if ($where['us_status'] == 0) { $query->where('Product.is_show', 0)->where('Product.is_used', 1)->where('Product.status', 1); } if ($where['us_status'] == 1) { $query->where('Product.is_show', 1)->where('Product.is_used', 1)->where('Product.status', 1); } if ($where['us_status'] == -1) { $query->where(function ($query) { $query->where('Product.is_used', 0)->whereOr('Product.status', '<>', 1); }); } }) ->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) { $query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%"); }) ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) { $query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%"); }) ->when(isset($where['order']), function ($query) use ($where, $merId) { if (in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sales'])) { if ($where['order'] == 'price_asc') { $where['order'] = 'price ASC'; } else if ($where['order'] == 'price_desc') { $where['order'] = 'price DESC'; } else { $where['order'] = $where['order'] . ' DESC'; } $query->order($where['order'] . ',rank DESC ,create_time DESC '); } else if ($where['order'] !== '') { $query->order('U.' . $where['order'] . ' DESC,U.create_time DESC'); } else { $query->order('U.create_time DESC'); } }) ->when(isset($where['star']), function ($query) use ($where) { $query->when($where['star'] !== '', function ($query) use ($where) { $query->where('U.star', $where['star']); }); $query->order('U.star DESC,U.rank DESC,Product.create_time DESC'); }); return $query; } /** * */ public function findChildrenId($id) { return StoreCategory::whereLike('path', '%/' . $id . '/%')->column('store_category_id'); } protected function searchList($collection, $field, $model, $searchKey, $insertKey, $where = [] ,$select = '*') { $ids = []; $link = []; if (!$collection) return []; $collection = $collection->toArray(); foreach ($collection as $k => $item) { if (is_array($item[$field])) { $link[$k] = array_unique($item[$field]); $ids = array_merge($item[$field], $ids); } else { $link[$k] = array_unique(explode(',', $item[$field])); } $ids = array_merge($link[$k], $ids); if (isset($collection[$k][$insertKey])) unset($collection[$k][$insertKey]); } $ids = array_filter(array_unique($ids)); if (!count($ids)) { return $collection; } $many = $model::whereIn($searchKey, array_unique($ids))->where($where)->field($select)->select(); if (!$many) return $collection; $many = $many->toArray(); foreach ($link as $k => $val) { foreach ($many as $item) { if (in_array($item[$searchKey], $val)) { if (!isset($collection[$k][$insertKey])) $collection[$k][$insertKey] = []; $collection[$k][$insertKey][] = $item; } } } return $collection; } }