Commit f3662775 by 杨树贤

优化主营品牌选择

parent 89b06eaf
...@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api; ...@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Services\AdminUserService; use App\Http\Services\AdminUserService;
use App\Http\Services\CompanyService; use App\Http\Services\CompanyService;
use App\Http\Services\StandardBrandService;
use App\Model\BrandModel; use App\Model\BrandModel;
use App\Model\StandardBrandModel; use App\Model\StandardBrandModel;
use App\Model\SupplierChannelModel; use App\Model\SupplierChannelModel;
...@@ -35,9 +36,19 @@ class CommonApiController extends Controller ...@@ -35,9 +36,19 @@ class CommonApiController extends Controller
//获取标准品牌 //获取标准品牌
public function getStandardBrandList($request) public function getStandardBrandList($request)
{ {
$model = new BrandModel(); $list = (new StandardBrandService())->getStandardBrandList($request->all());
$brandList = $model->getStandardBrandList(); $data = array_get($list, 'data');
$this->response(0, 'ok', $brandList); $lastPage = array_get($list, 'last_page');
$total = array_get($list, 'total');
echo json_encode([
'err_code' => 0,
'err_msg' => 'ok',
'total' => $total,
'count' => $total,
'data' => $data,
'last_page' => $lastPage
]);
exit();
} }
private function SearchBrand($request) private function SearchBrand($request)
......
...@@ -5,6 +5,7 @@ namespace App\Http\Controllers; ...@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Http\Services\LogService; use App\Http\Services\LogService;
use App\Http\Services\RegionService; use App\Http\Services\RegionService;
use App\Http\Services\SkuUploadLogService; use App\Http\Services\SkuUploadLogService;
use App\Http\Services\StandardBrandService;
use App\Http\Services\SupplierAttachmentService; use App\Http\Services\SupplierAttachmentService;
use App\Http\Services\SupplierService; use App\Http\Services\SupplierService;
use App\Http\Services\SupplierShareApplyService; use App\Http\Services\SupplierShareApplyService;
...@@ -76,7 +77,7 @@ class SupplierController extends Controller ...@@ -76,7 +77,7 @@ class SupplierController extends Controller
} }
$this->data['statusData'] = $statusData; $this->data['statusData'] = $statusData;
$stockupTypeData = []; $stockupTypeData = [];
foreach (config('fixed.StockupType') as $key=>$value) { foreach (config('fixed.StockupType') as $key => $value) {
$stockupTypeData[] = [ $stockupTypeData[] = [
'name' => $value, 'name' => $value,
'value' => $key, 'value' => $key,
...@@ -84,7 +85,7 @@ class SupplierController extends Controller ...@@ -84,7 +85,7 @@ class SupplierController extends Controller
} }
$this->data['stockupTypeData'] = $stockupTypeData; $this->data['stockupTypeData'] = $stockupTypeData;
$levelData = []; $levelData = [];
foreach (config('field.LevelMap') as $key=>$value) { foreach (config('field.LevelMap') as $key => $value) {
$levelData[] = [ $levelData[] = [
'name' => $value, 'name' => $value,
'value' => $key, 'value' => $key,
...@@ -92,7 +93,7 @@ class SupplierController extends Controller ...@@ -92,7 +93,7 @@ class SupplierController extends Controller
} }
$this->data['levelData'] = $levelData; $this->data['levelData'] = $levelData;
$supplierTypeData = []; $supplierTypeData = [];
foreach (config('field.SupplierType') as $key=>$value) { foreach (config('field.SupplierType') as $key => $value) {
$supplierTypeData[] = [ $supplierTypeData[] = [
'name' => $value, 'name' => $value,
'value' => $key, 'value' => $key,
...@@ -155,6 +156,7 @@ class SupplierController extends Controller ...@@ -155,6 +156,7 @@ class SupplierController extends Controller
//省市区数据放到script模板 //省市区数据放到script模板
$regionService = new RegionService(); $regionService = new RegionService();
$this->data['region_data'] = $regionService->getCityRegionData(); $this->data['region_data'] = $regionService->getCityRegionData();
$this->data['brand_init_value'] = [];
//编辑 //编辑
if (!empty($supplierId)) { if (!empty($supplierId)) {
$this->data['title'] = '编辑供应商'; $this->data['title'] = '编辑供应商';
...@@ -215,6 +217,7 @@ class SupplierController extends Controller ...@@ -215,6 +217,7 @@ class SupplierController extends Controller
$this->data['ignore_supplier_type_change_tips'] = in_array($supplier['supplier_name'], $this->data['ignore_supplier_type_change_tips'] = in_array($supplier['supplier_name'],
config('field.SkipChangeSupplierTypeNames')); config('field.SkipChangeSupplierTypeNames'));
$this->data['sku_upload_log_count'] = (new SkuUploadLogService())->getSkuUploadLogCount($supplierId); $this->data['sku_upload_log_count'] = (new SkuUploadLogService())->getSkuUploadLogCount($supplierId);
$this->data['brand_init_value'] = (new StandardBrandService())->getBrandInitValue($supplier['main_brands']);
return $this->view('编辑供应商'); return $this->view('编辑供应商');
} }
......
<?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();
return !empty($brands) ? $brands->toArray() : [];
}
}
\ No newline at end of file
...@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model; ...@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
//自营分类模型
class BrandModel extends Model class BrandModel extends Model
{ {
protected $connection = 'self'; protected $connection = 'self';
...@@ -63,19 +64,4 @@ class BrandModel extends Model ...@@ -63,19 +64,4 @@ class BrandModel extends Model
} }
return $brandList; return $brandList;
} }
public function getStandardBrandList()
{
$redis = new RedisModel();
$standardBrands = json_decode($redis->get('standard_brands_for_supplier'),true);
if (empty($standardBrands)) {
$standardBrandModel = new StandardBrandModel();
$standardBrands = $standardBrandModel->selectRaw('brand_name,standard_brand_id as brand_id')
->where('status', 1)->get()->toArray();
$redis->set('standard_brands_for_supplier', json_encode($standardBrands));
$redis->expire('standard_brands_for_supplier', 600);
}
return $standardBrands;
}
} }
\ No newline at end of file
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
//联营分类模型
class SpuBrandModel extends Model
{
protected $connection = 'spu';
protected $table='brand';
protected $primaryKey = 'brand_id';
public $timestamps = false;
}
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class StandardBrandMappingModel extends Model
{
protected $connection='spu';
protected $table='brand_standard_mapping';
public $timestamps = false;
}
...@@ -192,9 +192,6 @@ ...@@ -192,9 +192,6 @@
//渲染主营品牌的多选 //渲染主营品牌的多选
function getBrandOption(element, brandType) { function getBrandOption(element, brandType) {
let brandUrl = '/api/common/getStandardBrandList';
let brandRes = ajax(brandUrl);
let brandList = brandRes.data;
return { return {
el: '#' + element, el: '#' + element,
filterable: true, filterable: true,
...@@ -207,17 +204,41 @@ ...@@ -207,17 +204,41 @@
name: 'brand_name', name: 'brand_name',
value: 'brand_id', value: 'brand_id',
}, },
remoteSearch: true,
pageRemote: true,
template({ item, sels, name, value }){
return item.brand_name + '<span style="position: absolute; right: 10px; color: #8799a3">'+ item.mapping_brand_names +'</span>'
},
filterMethod: function (val, item, index, prop) { filterMethod: function (val, item, index, prop) {
if (val.toLowerCase() === item.brand_name.toLowerCase()) {//把brand_name相同的搜索出来
return true;
}
return item.brand_name.toLowerCase().indexOf(val.toLowerCase()) === 0;
//不知道的就不管了
}, },
pageSize: 30, pageSize: 30,
data: brandList, remoteMethod: function (val, cb, show, pageIndex) {
on: function (brandList) { //val: 搜索框的内容, 不开启搜索默认为空, cb: 回调函数, show: 当前下拉框是否展开, pageIndex: 当前第几页
let arr = brandList.arr; $.ajax({
url: '/api/common/getStandardBrandList',
type: 'post',
data: {
brand_name: val,
page: pageIndex
},
dataType: 'json',
timeout: 10000,
success: function (res) {
console.log(res)
if (!res) return layer.msg('网络错误,请重试', {icon: 5});
if (res.err_code === 0) {
cb(res.data, res.last_page);
} else {
layer.msg(res.err_msg, {icon: 6});
}
},
error: function () {
return layer.msg('网络错误,请重试', {icon: 5});
}
});
},
on: function (data) {
let arr = data.arr;
let brandIds = ''; let brandIds = '';
for (let i in arr) { for (let i in arr) {
brandIds += arr[i].brand_id + ','; brandIds += arr[i].brand_id + ',';
...@@ -231,7 +252,9 @@ ...@@ -231,7 +252,9 @@
let brandOption = getBrandOption('brand_selector', 2); let brandOption = getBrandOption('brand_selector', 2);
let brandSelector = xmSelect.render(brandOption); let brandSelector = xmSelect.render(brandOption);
let brandIds = $('#main_brands').attr('value'); let brandIds = $('#main_brands').attr('value');
brandSelector.setValue(brandIds.split(',')); let brandInitValue = {!! json_encode($brand_init_value?:[])!!};
console.log(brandInitValue);
brandSelector.setValue(brandInitValue);
//供应商标签的多选 //供应商标签的多选
function getTagOption(element) { function getTagOption(element) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment