作者 赵彬吉

update

@@ -25,7 +25,7 @@ class AttrController extends BaseController @@ -25,7 +25,7 @@ class AttrController extends BaseController
25 $map[] = ['title', 'like', "%{$this->param['search']}%"]; 25 $map[] = ['title', 'like', "%{$this->param['search']}%"];
26 } 26 }
27 $sort = ['id' => 'desc']; 27 $sort = ['id' => 'desc'];
28 - $data = $logic->getList($map, $sort, ['id', 'title', 'remark', 'value_num']); 28 + $data = $logic->getList($map, $sort, ['id', 'title', 'attrs']);
29 return $this->success($data); 29 return $this->success($data);
30 } 30 }
31 31
@@ -36,7 +36,7 @@ class AttrController extends BaseController @@ -36,7 +36,7 @@ class AttrController extends BaseController
36 'id.required' => 'ID不能为空' 36 'id.required' => 'ID不能为空'
37 ]); 37 ]);
38 $data = $logic->getInfo($this->param['id']); 38 $data = $logic->getInfo($this->param['id']);
39 - return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'remark', 'values'])); 39 + return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'attrs']));
40 } 40 }
41 41
42 public function save(AttrRequest $request, AttrLogic $logic) 42 public function save(AttrRequest $request, AttrLogic $logic)
@@ -28,7 +28,7 @@ class KeywordController extends BaseController @@ -28,7 +28,7 @@ class KeywordController extends BaseController
28 $sort = ['id' => 'desc']; 28 $sort = ['id' => 'desc'];
29 $data = $logic->getList($map, $sort, ['id', 'title', 'seo_title', 'seo_keywords', 'seo_description', 'status', 'created_at']); 29 $data = $logic->getList($map, $sort, ['id', 'title', 'seo_title', 'seo_keywords', 'seo_description', 'status', 'created_at']);
30 foreach ($data['list'] as &$v){ 30 foreach ($data['list'] as &$v){
31 - $v['product_num'] = $logic->getProductNum($v['title']); 31 + $v['product_num'] = $logic->getProductNum($v['id']);
32 $v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']); 32 $v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']);
33 //todo 获取域名 拼接链接 33 //todo 获取域名 拼接链接
34 $v['url'] = $v['route']; 34 $v['url'] = $v['route'];
@@ -9,6 +9,7 @@ use App\Http\Controllers\Bside\BaseController; @@ -9,6 +9,7 @@ use App\Http\Controllers\Bside\BaseController;
9 use App\Http\Logic\Bside\Product\ProductLogic; 9 use App\Http\Logic\Bside\Product\ProductLogic;
10 use App\Http\Requests\Bside\Product\ProductRequest; 10 use App\Http\Requests\Bside\Product\ProductRequest;
11 use App\Models\Product\CategoryRelated; 11 use App\Models\Product\CategoryRelated;
  12 +use App\Models\Product\KeywordRelated;
12 use App\Rules\Ids; 13 use App\Rules\Ids;
13 use Illuminate\Http\Request; 14 use Illuminate\Http\Request;
14 15
@@ -34,6 +35,10 @@ class ProductController extends BaseController @@ -34,6 +35,10 @@ class ProductController extends BaseController
34 $ids = CategoryRelated::where('cate_id', $this->param['category_id'])->pluck('product_id')->toArray(); 35 $ids = CategoryRelated::where('cate_id', $this->param['category_id'])->pluck('product_id')->toArray();
35 $map[] = ['id', 'in', $ids]; 36 $map[] = ['id', 'in', $ids];
36 } 37 }
  38 + if(!empty($this->param['keyword_id'])){
  39 + $ids = KeywordRelated::where('keyword_id', $this->param['keyword_id'])->pluck('product_id')->toArray();
  40 + $map[] = ['id', 'in', $ids];
  41 + }
37 if(isset($this->param['status'])){ 42 if(isset($this->param['status'])){
38 $map[] = ['status', $this->param['status']]; 43 $map[] = ['status', $this->param['status']];
39 } 44 }
@@ -36,10 +36,11 @@ class InquiryLogic extends BaseLogic @@ -36,10 +36,11 @@ class InquiryLogic extends BaseLogic
36 36
37 public function save($param) 37 public function save($param)
38 { 38 {
  39 + //todo 根据域名 判断 project_id
39 $param['ip_info'] = Arr::s2a($param['ip_info']); 40 $param['ip_info'] = Arr::s2a($param['ip_info']);
40 $param['ip'] = $param['ip_info']['ip'] ?? ''; 41 $param['ip'] = $param['ip_info']['ip'] ?? '';
41 $param['ip_country'] = $param['ip_info']['country'] ?? ''; 42 $param['ip_country'] = $param['ip_info']['country'] ?? '';
42 - return parent::save($param); 43 + Logic::save($param);
43 } 44 }
44 45
45 } 46 }
@@ -4,8 +4,6 @@ namespace App\Http\Logic\Bside\Product; @@ -4,8 +4,6 @@ namespace App\Http\Logic\Bside\Product;
4 4
5 use App\Http\Logic\Bside\BaseLogic; 5 use App\Http\Logic\Bside\BaseLogic;
6 use App\Models\Product\Attr; 6 use App\Models\Product\Attr;
7 -use App\Models\Product\AttrValue;  
8 -use Illuminate\Support\Facades\DB;  
9 7
10 /** 8 /**
11 * Class AttrLogic 9 * Class AttrLogic
@@ -22,47 +20,5 @@ class AttrLogic extends BaseLogic @@ -22,47 +20,5 @@ class AttrLogic extends BaseLogic
22 $this->model = new Attr(); 20 $this->model = new Attr();
23 } 21 }
24 22
25 - public function getInfo($id){  
26 - $info = parent::getCacheInfo($id);  
27 - if(!$info){  
28 - $this->fail('数据不存在或者已经删除');  
29 - }  
30 - $info->values;  
31 - return $this->success($info->toArray());  
32 - }  
33 -  
34 - public function save($param){  
35 - $param['values'] = array_unique($param['values']);  
36 - DB::beginTransaction();  
37 - try {  
38 - //删除之前的参数值  
39 - if(!empty($param['id'])){  
40 - AttrValue::where('attr_id', $param['id'])->delete();  
41 - }  
42 -  
43 - //保存参数名称  
44 - $data = $param;  
45 - unset($data['values']);  
46 - $data['value_num'] = count($param['values']);  
47 - $res = parent::save($data);  
48 - $attr_id = $res['id'];  
49 23
50 - //保存参数值  
51 - $values = [];  
52 - foreach ($param['values'] as $value){  
53 - $values[] = [  
54 - 'attr_id' => $attr_id,  
55 - 'value' => $value  
56 - ];  
57 - }  
58 - AttrValue::insert($values);  
59 -  
60 - DB::commit();  
61 - }catch (\Exception $e){  
62 - DB::rollBack();  
63 - errorLog('产品参数保存失败', $param, $e);  
64 - $this->fail('保存失败');  
65 - }  
66 - return $this->success();  
67 - }  
68 } 24 }
@@ -65,7 +65,6 @@ class CategoryLogic extends BaseLogic @@ -65,7 +65,6 @@ class CategoryLogic extends BaseLogic
65 */ 65 */
66 public function getProductNum($cate_id){ 66 public function getProductNum($cate_id){
67 $cate_ids = $this->model->getChildIdsArr($cate_id); 67 $cate_ids = $this->model->getChildIdsArr($cate_id);
68 - $product_ids = CategoryRelated::whereIn('cate_id', $cate_ids)->pluck('id');  
69 - return Product::whereIn('id', $product_ids)->count(); 68 + return CategoryRelated::whereIn('cate_id', $cate_ids)->count();
70 } 69 }
71 } 70 }
@@ -4,7 +4,7 @@ namespace App\Http\Logic\Bside\Product; @@ -4,7 +4,7 @@ namespace App\Http\Logic\Bside\Product;
4 4
5 use App\Helper\Arr; 5 use App\Helper\Arr;
6 use App\Http\Logic\Bside\BaseLogic; 6 use App\Http\Logic\Bside\BaseLogic;
7 -use App\Models\Product\Product; 7 +use App\Models\Product\KeywordRelated;
8 use App\Models\RouteMap; 8 use App\Models\RouteMap;
9 use App\Models\Product\Keyword; 9 use App\Models\Product\Keyword;
10 use Illuminate\Support\Facades\DB; 10 use Illuminate\Support\Facades\DB;
@@ -47,6 +47,16 @@ class KeywordLogic extends BaseLogic @@ -47,6 +47,16 @@ class KeywordLogic extends BaseLogic
47 parent::delete($ids); 47 parent::delete($ids);
48 48
49 foreach ($ids as $id){ 49 foreach ($ids as $id){
  50 + $info = $this->getCacheInfo($id);
  51 + if(!$info){
  52 + continue;
  53 + }
  54 +
  55 + //是否有对应商品
  56 + if(KeywordRelated::where('keyword_id', $id)->count()){
  57 + $this->fail("关键词{$info['title']}存在产品,不能删除");
  58 + }
  59 +
50 //删除路由映射 60 //删除路由映射
51 RouteMap::delRoute(RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']); 61 RouteMap::delRoute(RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
52 } 62 }
@@ -60,7 +70,7 @@ class KeywordLogic extends BaseLogic @@ -60,7 +70,7 @@ class KeywordLogic extends BaseLogic
60 return $this->success(); 70 return $this->success();
61 } 71 }
62 72
63 - public function getProductNum($title){  
64 - return Product::whereRaw("find_in_set('{$title}',`keywords`)")->count(); 73 + public function getProductNum($keyword_id){
  74 + return KeywordRelated::where('keyword_id', $keyword_id)->count();
65 } 75 }
66 } 76 }
@@ -6,6 +6,7 @@ use App\Helper\Arr; @@ -6,6 +6,7 @@ use App\Helper\Arr;
6 use App\Http\Logic\Bside\BaseLogic; 6 use App\Http\Logic\Bside\BaseLogic;
7 use App\Http\Logic\Bside\User\UserLogic; 7 use App\Http\Logic\Bside\User\UserLogic;
8 use App\Models\Product\CategoryRelated; 8 use App\Models\Product\CategoryRelated;
  9 +use App\Models\Product\KeywordRelated;
9 use App\Models\Product\Product; 10 use App\Models\Product\Product;
10 use App\Models\RouteMap; 11 use App\Models\RouteMap;
11 use Illuminate\Support\Facades\DB; 12 use Illuminate\Support\Facades\DB;
@@ -62,6 +63,8 @@ class ProductLogic extends BaseLogic @@ -62,6 +63,8 @@ class ProductLogic extends BaseLogic
62 $res = parent::save($data); 63 $res = parent::save($data);
63 //关联分类 64 //关联分类
64 CategoryRelated::saveRelated($res['id'], $data['category_id']); 65 CategoryRelated::saveRelated($res['id'], $data['category_id']);
  66 + //关联关键词
  67 + KeywordRelated::saveRelated($res['id'], $data['keyword_id']);
65 //路由映射 68 //路由映射
66 RouteMap::setRoute($param['route'], RouteMap::SOURCE_PRODUCT, $res['id'], $this->user['project_id']); 69 RouteMap::setRoute($param['route'], RouteMap::SOURCE_PRODUCT, $res['id'], $this->user['project_id']);
67 DB::commit(); 70 DB::commit();
@@ -86,6 +89,9 @@ class ProductLogic extends BaseLogic @@ -86,6 +89,9 @@ class ProductLogic extends BaseLogic
86 89
87 //删除分类关联 90 //删除分类关联
88 CategoryRelated::where('product_id', $id)->delete(); 91 CategoryRelated::where('product_id', $id)->delete();
  92 +
  93 + //删除关键词关联
  94 + KeywordRelated::where('product_id', $id)->delete();
89 } 95 }
90 96
91 DB::commit(); 97 DB::commit();
@@ -31,8 +31,16 @@ class AttrRequest extends FormRequest @@ -31,8 +31,16 @@ class AttrRequest extends FormRequest
31 { 31 {
32 return [ 32 return [
33 'title' => 'required|max:50', 33 'title' => 'required|max:50',
34 - 'remark' => 'max:200',  
35 - 'values' => 'required|array' 34 + 'attrs' => ['required', 'array', function ($attribute, $value, $fail) {
  35 + foreach ($value as $v) {
  36 + if (empty($v['key'])) {
  37 + $fail('属性名不能为空');
  38 + }
  39 + if (empty($v['value'])) {
  40 + $fail('属性值不能为空');
  41 + }
  42 + }
  43 + }]
36 ]; 44 ];
37 } 45 }
38 46
@@ -41,9 +49,8 @@ class AttrRequest extends FormRequest @@ -41,9 +49,8 @@ class AttrRequest extends FormRequest
41 return [ 49 return [
42 'title.required' => '请输入参数名称', 50 'title.required' => '请输入参数名称',
43 'title.max' => '参数名称不能超过50个字符', 51 'title.max' => '参数名称不能超过50个字符',
44 - 'remark.max' => '备注不能超过200个字符',  
45 - 'values.required' => '请添加参数值',  
46 - 'values.array' => '参数值格式异常', 52 + 'attrs.required' => '请添加属性参数',
  53 + 'attrs.array' => '属性参数格式异常',
47 ]; 54 ];
48 } 55 }
49 56
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace App\Models\Product; 3 namespace App\Models\Product;
4 4
  5 +use App\Helper\Arr;
5 use App\Models\Base; 6 use App\Models\Base;
6 use Illuminate\Database\Eloquent\SoftDeletes; 7 use Illuminate\Database\Eloquent\SoftDeletes;
7 8
@@ -9,12 +10,24 @@ class Attr extends Base @@ -9,12 +10,24 @@ class Attr extends Base
9 { 10 {
10 use SoftDeletes; 11 use SoftDeletes;
11 12
  13 + protected $appends = ['attr_num'];
  14 +
12 //设置关联表名 15 //设置关联表名
13 protected $table = 'gl_product_attr'; 16 protected $table = 'gl_product_attr';
14 17
15 - public function values() 18 + public function setAttrsAttribute($value)
  19 + {
  20 + $this->attributes['attrs'] = Arr::a2s($value);
  21 + }
  22 +
  23 + public function getAttrsAttribute($value)
  24 + {
  25 + return Arr::s2a($value);
  26 + }
  27 +
  28 + public function getAttrNumAttribute()
16 { 29 {
17 - return $this->hasMany(AttrValue::class, 'attr_id', 'id')->orderBy('id'); 30 + return count($this->attrs);
18 } 31 }
19 32
20 } 33 }
1 -<?php  
2 -  
3 -namespace App\Models\Product;  
4 -  
5 -use App\Models\Base;  
6 -  
7 -class AttrValue extends Base  
8 -{  
9 -  
10 - //设置关联表名  
11 - protected $table = 'gl_product_attr_value';  
12 -  
13 -}  
  1 +<?php
  2 +
  3 +namespace App\Models\Product;
  4 +
  5 +
  6 +use App\Helper\Arr;
  7 +use App\Models\Base;
  8 +
  9 +class KeywordRelated extends Base
  10 +{
  11 +
  12 + //设置关联表名
  13 + protected $table = 'gl_product_keyword_related';
  14 +
  15 + const CREATED_AT = null;
  16 + const UPDATED_AT = null;
  17 +
  18 +
  19 + /**
  20 + * 关联产品关键词
  21 + * @param $product_id
  22 + * @param $keyword_ids
  23 + * @author zbj
  24 + * @date 2023/5/4
  25 + */
  26 + public static function saveRelated($product_id, $keyword_ids)
  27 + {
  28 + if(!is_array($keyword_ids)){
  29 + $keyword_ids = array_filter(Arr::splitFilterToArray($keyword_ids), 'intval');
  30 + }
  31 + //先删除
  32 + self::where('product_id', $product_id)->delete();
  33 +
  34 + //批量保存
  35 + $data = [];
  36 + foreach ($keyword_ids as $keyword_id){
  37 + $data[] = [
  38 + 'product_id' => $product_id,
  39 + 'keyword_id' => $keyword_id
  40 + ];
  41 + }
  42 + self::insert($data);
  43 + }
  44 +}