KeywordLogic.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace App\Http\Logic\Bside\Product;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;
/**
* Class KeywordLogic
* @package App\Http\Logic\Bside\APublicModel
* @author zbj
* @date 2023/4/15
*/
class KeywordLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new Keyword();
}
/**
* @remark :获取数据详情
* @name :getInfo
* @author :lyh
* @method :post
* @time :2023/8/23 16:50
*/
public function getInfo($id)
{
$info = parent::getInfo($id);
$info['url'] = $this->user['domain'] . $info['route'];
return $this->success($info);
}
/**
* @remark :保存
* @name :keywordSave
* @author :lyh
* @method :post
* @time :2023/8/23 16:50
*/
public function keywordSave(){
DB::beginTransaction();
try {
if(isset($this->param['id']) && !empty($this->param['id'])){
$route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $this->param['id'], $this->user['project_id']);
$id = $this->editCategoryRoute($this->param['id'],$route);
$this->model->edit($this->param,['id'=>$this->param['id']]);
}else{
$this->param['project_id'] = $this->user['project_id'];
$this->param['created_at'] = date('Y-m-d H:i:s');
$this->param['updated_at'] = $this->param['created_at'];
$id = $this->model->insertGetId($this->param);
//路由映射
$route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
$this->model->edit(['route'=>$route],['id'=>$id]);
}
//清除缓存
Common::del_user_cache('product_keyword',$this->user['project_id']);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('保存失败');
}
//通知更新
$this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_PRODUCT_KEYWORD, 'route'=>$route]);
return $this->success();
}
/**
* @remark :编辑路由时生成路由记录
* @name :editCategoryRoute
* @author :lyh
* @method :post
* @time :2023/9/7 10:51
*/
public function editCategoryRoute($id,$route){
//生成一条删除路由记录
$info = $this->model->read(['id'=>$id],['id','route']);
if($info['route'] != $route){
$data = [
'source'=>RouteMap::SOURCE_PRODUCT_KEYWORD,
'route'=>$info['route'],
];
$this->setRouteDeleteSave($data);
}
return $id;
}
/**
* @remark :批量添加数据
* @name :batchAdd
* @author :lyh
* @method :post
* @time :2023/8/28 14:03
*/
public function batchAdd(){
if(!empty($this->param['title']) && is_array($this->param['title'])){
foreach ($this->param['title'] as $v){
$param['project_id'] = $this->user['project_id'];
$param['created_at'] = date('Y-m-d H:i:s');
$param['updated_at'] = $param['created_at'];
$param['title'] = $v;
$id = $this->model->insertGetId($param);
RouteMap::setRoute($v, RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
}
}
return $this->success();
}
/**
* @remark :删除标签
* @name :keywordDelete
* @author :lyh
* @method :post
* @time :2023/8/28 11:28
*/
public function keywordDelete(){
$ids = $this->param['ids'];
$productModel = new Product();
foreach ($ids as $id){
$product_info = $productModel->read(['keyword_id'=>['like','%,'.$id.',%']]);
if($product_info !== false){
$this->fail('当前关键词拥有产品不允许删除');
}
$this->delRoute($id);
$this->model->del(['id'=>$id]);
}
//清除缓存
Common::del_user_cache('product_keyword',$this->user['project_id']);
return $this->success();
}
/**
* @remark :删除路由
* @name :delRoute
* @author :lyh
* @method :post
* @time :2023/9/7 10:50
*/
public function delRoute($id){
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
//生成一条删除路由记录
$info = $this->model->read(['id'=>$id],['id','route']);
$data = [
'source'=>RouteMap::SOURCE_PRODUCT_KEYWORD,
'route'=>$info['route'],
];
$this->setRouteDeleteSave($data);
return $this->success();
}
}