<?php namespace App\Http\Services; use App\Model\SpuBrandModel; use App\Model\RedisModel; use App\Model\StandardBrandMappingModel; use App\Model\StandardBrandModel; use Illuminate\Support\Facades\DB; class StandardBrandService { public function getStandardBrandList($params) { $limit = array_get($params, 'limit', 30); $searchBrandName = array_get($params, 'brand_name'); $standardBrandModel = new StandardBrandModel(); $query = $standardBrandModel->selectRaw('brand_name,standard_brand_id as brand_id') ->where('status', 1); $flag = false; //这里搜索的普通品牌名字,要根据普通品牌名字去搜索出相关的标准品牌名称 if (!empty($searchBrandName)) { $brandIds = SpuBrandModel::where('brand_name', 'like', "%$searchBrandName%")->limit(100)->pluck('brand_id'); if (!empty($brandIds)) { $standardBrandIds = StandardBrandMappingModel::whereIn('brand_id', $brandIds)->pluck('standard_brand_id'); if (!empty($standardBrandIds)) { $query->whereIn('standard_brand_id', $standardBrandIds)->orWhere('brand_name', 'like', "%$searchBrandName%")->orderByRaw("CASE WHEN brand_name LIKE '${searchBrandName}' THEN 1 WHEN brand_name LIKE '${searchBrandName}%' THEN 2 WHEN brand_name LIKE '%${searchBrandName}' THEN 4 ELSE 3 END"); $flag = true; } } } $list = $query->paginate($limit)->toArray(); //如果有根据普通品牌搜出标准品牌,那么还要补充数据显示 if ($flag) { $list['data'] = array_map(function ($value) use ($searchBrandName) { $brandIds = StandardBrandMappingModel::where('standard_brand_id', $value['brand_id'])->pluck('brand_id')->toArray(); $brandNames = SpuBrandModel::whereIn('brand_id', $brandIds)->where('brand_name', 'like', "%${searchBrandName}%")->limit(10)->pluck('brand_name')->toArray(); //去除原始数据那些换行或者tab符号,原始数据是很乱的 $brandNames = array_map(function ($value) { return $value ? str_replace("\t", '', $value) : $value; }, $brandNames); $value['mapping_brand_names'] = $brandNames ? implode($brandNames, ' | ') : '-'; $value['show_name'] = $value['brand_name']; return $value; }, $list['data']); } else { $list['data'] = array_map(function ($value) { $value['mapping_brand_names'] = '-'; $value['show_name'] = $value['brand_name']; return $value; }, $list['data']); } return $list; } //获取供应商一开始给xm-select用的保存的品牌数据 public function getBrandInitValue($brandIds) { if (!trim($brandIds, ',')) { return []; } $brandIds = explode(',', $brandIds); $brands = StandardBrandModel::whereIn('standard_brand_id', $brandIds)->select([ 'brand_name', 'standard_brand_id as brand_id', ])->get(); $brands = !empty($brands) ? $brands->toArray() : []; return array_map(function ($brand) { $brand['show_name'] = $brand['brand_name']; $brand['mapping_brand_names'] = '-'; return $brand; }, $brands); } }