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'); } /* * ----------------------------------------------------------------------------------------------------------------- * 搜索器 * ----------------------------------------------------------------------------------------------------------------- */ public function searchMerCateIdAttr($query, $value) { $cate_ids = (StoreCategory::where('path','like','%/'.$value.'/%'))->column('store_category_id'); $cate_ids[] = intval($value); $product_id = ProductCate::whereIn('mer_cate_id',$cate_ids)->column('product_id'); $query->whereIn('Product.product_id',$product_id); } public function searchKeywordAttr($query, $value) { if (!$value) return; if (is_numeric($value)) { $query->whereLike("Product.store_name|Product.keyword|bar_code|Product.product_id", "%{$value}%"); } // else { // // $word = app()->make(VicWordService::class)->getWord($value); // $word = $value; // $query->where(function ($query) use ($word, $value) { // foreach ($word as $item) { // $query->whereOr('Product.store_name|Product.keyword', 'LIKE', "%$item%"); // } // $query->order(Db::raw('REPLACE(Product.store_name,\'' . $value . '\',\'\')')); // }); // } } public function searchStatusAttr($query, $value) { if($value === -1){ $query->where('Product.status', 'in',[-1,-2]); }else { $query->where('Product.status',$value); } } public function searchCateIdAttr($query, $value) { $query->where('cate_id',$value); } public function searchCateIdsAttr($query, $value) { $query->whereIn('cate_id',$value); } public function searchIsShowAttr($query, $value) { $query->where('is_show',$value); } public function searchPidAttr($query, $value) { $cateId = app()->make(StoreCategoryRepository::class)->allChildren(intval($value)); $query->whereIn('cate_id', $cateId); } public function searchStockAttr($query, $value) { $value ? $query->where('stock','<=', $value) : $query->where('stock', $value); } public function searchIsNewAttr($query, $value) { $query->where('is_new',$value); } public function searchPriceAttr($query, $value) { if(empty($value[0]) && !empty($value[1])) $query->where('price','<',$value[1]); if(!empty($value[0]) && empty($value[1])) $query->where('price','>',$value[0]); if(!empty($value[0]) && !empty($value[1])) $query->whereBetween('price',[$value[0],$value[1]]); } public function searchBrandIdAttr($query, $value) { $query->whereIn('brand_id',$value); } public function searchIsGiftBagAttr($query, $value) { $query->where('is_gift_bag',$value); } public function searchIsGoodAttr($query, $value) { $query->where('is_good',$value); } public function searchIsUsedAttr($query, $value) { $query->where('is_used',$value); } public function searchProductTypeAttr($query, $value) { $query->where('Product.product_type',$value); } // public function searchSeckillStatusAttr($query, $value) // { // $product_id = (new StoreSeckillActiveDao())->getStatus($value)->column('product_id'); // $query->whereIn('Product.product_id',$product_id); // } public function searchStoreNameAttr($query, $value) { $query->where('Product.store_name','like','%'.$value.'%'); } public function searchMerStatusAttr($query, $value) { $query->where('mer_status',$value); } public function searchProductIdAttr($query, $value) { $query->where('Product.product_id',$value); } public function searchPriceOnAttr($query, $value) { $query->where('price','>=',$value); } public function searchPriceOffAttr($query, $value) { $query->where('price','<=',$value); } public function searchisFictiAttr($query, $value) { $query->where('type',$value); } public function searchGuaranteeTemplateIdAttr($query, $value) { $query->whereIn('guarantee_template_id',$value); } public function searchTempIdAttr($query, $value) { $query->whereIn('Product.temp_id',$value); } /** * 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; } }