KeywordLogic.php
12.8 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?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\Com\NoticeLog;
use App\Models\News\News;
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 getKeywordInfo()
{
$info = $this->model->read($this->param);
if($info !== false){
$info['url'] = $this->user['domain'] . $info['route'];
$info['related_news_info'] = News::whereIn('id', $info['related_news_ids'])->select(['id', 'name'])->get();
$product = $this->getProduct($info['id']);
$info['product_list'] = $product['product'] ?? [];
$info['product_video_list'] = $product['video_product'] ?? [];
}
return $this->success($info);
}
/**
* @remark :保存
* @name :keywordSave
* @author :lyh
* @method :post
* @time :2023/8/23 16:50
*/
public function keywordSave(){
$this->param = $this->handleSaveParam($this->param);
if(isset($this->param['id']) && !empty($this->param['id'])){
$info = $this->model->read(['title'=>$this->param['title'],'id'=>['!=',$this->param['id']]]);
if($info !== false){
$this->fail('当前title已存在');
}
$route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_PRODUCT_KEYWORD, $this->param['id'], $this->user['project_id']);
$this->param['route'] = $route;
//todo::通知C端生成
if(isset($this->param['is_video_keyword']) && ($this->param['is_video_keyword'] == 1)){
$info = $this->model->read(['id'=>$this->param['id']],['id','is_video_keyword','video']);
if(($info['is_video_keyword'] != $this->param['is_video_keyword']) && ($info['video'] != null || $info['video'] != '')){
$this->sendHttpC([$route]);
}
}
$this->model->edit($this->param,['id'=>$this->param['id']]);
$data = ['id'=>$this->param['id']];
}else{
$info = $this->model->read(['title'=>$this->param['title']]);
if($info !== false){
return $this->success(['id'=>$info['id']]);
}
$this->param = $this->addHandleParam($this->param);
$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]);
$data = ['id'=>$id];
if(isset($this->param['keyword_arr']) && isset($this->param['keyword_video_arr'])){
$this->delRelated($id,$this->param['keyword_arr'],$this->param['keyword_video_arr']);
}
}
Common::del_user_cache('product_keyword',$this->user['project_id']);
$this->addUpdateNotify(RouteMap::SOURCE_PRODUCT_KEYWORD,$route);
$this->curlDelRoute(['new_route'=>$route]);
return $this->success($data);
}
/**
* @remark :添加组装数据
* @name :addHandleParam
* @author :lyh
* @method :post
* @time :2023/11/30 15:00
*/
public function addHandleParam($param){
$param['project_id'] = $this->user['project_id'];
$param['created_at'] = date('Y-m-d H:i:s');
$param['updated_at'] = $param['created_at'];
return $this->success($param);
}
/**
* @remark :保存数据时参数处理
* @name :handleSaveParam
* @author :lyh
* @method :post
* @time :2023/10/23 14:47
*/
public function handleSaveParam($param){
if(isset($param['keyword_top_banner'])){
$param['keyword_top_banner'] = str_replace_url($param['keyword_top_banner'] ?? []);
}
if(isset($param['keyword_foot_banner'])){
$param['keyword_foot_banner'] = str_replace_url($param['keyword_foot_banner'] ?? []);
}
if(isset($param['keyword_video'])){
$param['keyword_video'] = Arr::a2s($param['keyword_video'] ?? []);
}
if(!empty($param['related_news_ids'])){
$param['related_news_ids'] = Arr::arrToSet($param['related_news_ids'] ?? []);
}
if(!empty($param['related_blog_ids'])){
$param['related_blog_ids'] = Arr::arrToSet($param['related_blog_ids'] ?? []);
}
if(!isset($param['is_video_keyword']) || $param['is_video_keyword'] == null){
$param['is_video_keyword'] = 0;
}
$param['first_word'] = $this->first_word($param['title']);
return $param;
}
/**
* @remark :获取字符串首字符
* @name :first_word
* @author :lyh
* @method :post
* @time :2024/10/28 10:47
*/
public function first_word($title){
$first_title = mb_substr($title, 0, 1);
//返回对应的键
$keywordModel = new Keyword();
$firstNumWord = $keywordModel->firstNumWord;
foreach($firstNumWord as $k => $v){
if(strtolower($v) == strtolower($first_title)){
return $k;
}
}
return 27;
}
/**
* @remark :批量添加关键词任务, 异步处理
* @name :batchAdd
* @author :lyh
* @method :post
* @time :2024/6/6 10:27
*/
public function batchAdd(){
try {
foreach ($this->param['title'] as $k=>$v){
if(empty($v)){
continue;
}
$this->model = new Keyword();
$info = $this->model->read(['title'=>$v],['id']);
if($info === false){
$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;
$param['first_word'] = $this->first_word($param['title']);
$this->model->insertGetId($param);
}
}
}catch (\Exception $e){
$this->fail('保存失败,请联系管理员');
}
Common::del_user_cache('product_keyword',$this->user['project_id']);
NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_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){
$replace_id = ','.$id.',';
$productModel->where('keyword_id', 'like', '%,' . $id . ',%')
->update([
'keyword_id' => DB::raw("REPLACE(keyword_id, '$replace_id' , ',')")
]);
$productModel->where('keyword_id', ',')
->update([
'keyword_id' => DB::raw("REPLACE(keyword_id, ',' , '')")
]);
$this->delRoute($id);
$this->model->del(['id'=>$id]);
}
(new KeywordRelated())->del(['keyword_id'=>['in',$ids]]);
//清除缓存
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']);
if($info !== false){
$this->curlDelRoute(['old_route'=>$info['route']]);
}
return $this->success();
}
/**
* 产品导入:关键词处理
* @param $project_id
* @param $keyword
* @return string
* @throws \Exception
* @author Akun
* @date 2023/09/21 14:55
*/
public function importProductKeyword($project_id,$keyword){
$return = [];
$keyword_arr = explode('^v6sp$',$keyword);
foreach ($keyword_arr as $v){
if($v){
$keyword_info = $this->model->read(['title'=>$v]);
if(!$keyword_info){
$k_id = $this->model->addReturnId(['title'=>$v,'first_word' => $this->first_word($v),'project_id'=>$project_id]);
$route = RouteMap::setRoute($v, RouteMap::SOURCE_PRODUCT_KEYWORD, $k_id, $project_id);
$this->model->edit(['route'=>$route],['id'=>$k_id]);
}else{
$k_id = $keyword_info['id'];
}
$return[] = $k_id;
}
}
//清除缓存
Common::del_user_cache('product_keyword',$project_id);
return ','.implode(',',$return).',';
}
/**
* 批量删除
* @return array
* @throws BsideGlobalException
* @throws \App\Exceptions\AsideGlobalException
* @author zbj
* @date 2023/11/22
*/
public function batchDel(){
try {
$productModel = new Product();
foreach ($this->param['title'] as $v){
$info = $this->model->read(['title'=>$v]);
if($info){
$this->delRoute($info['id']);
$this->model->del(['id'=>$info['id']]);
$id = $info['id'];
$replace_id = ','.$id.',';
$productModel->where('keyword_id', 'like', '%,' . $id . ',%')
->update([
'keyword_id' => DB::raw("REPLACE(keyword_id, '$replace_id' , ',')")
]);
$productModel->where('keyword_id', ',')
->update([
'keyword_id' => DB::raw("REPLACE(keyword_id, ',' , '')")
]);
(new KeywordRelated())->del(['keyword_id'=>$id]);
}
}
//清除缓存
Common::del_user_cache('product_keyword',$this->user['project_id']);
}catch (\Exception $e){
$this->fail('error');
}
return $this->success();
}
/**
* @remark :根据关键字获取产品
* @name :getProduct
* @author :lyh
* @method :post
* @time :2024/11/28 9:26
*/
public function getProduct($keyword_id){
$data_video_type_arr = $data_type_arr = [];
$keywordRelatedModel = new KeywordRelated();
$keywordRelateList = $keywordRelatedModel->list(['keyword_id'=>$keyword_id],'id',['id','product_id','type']);
if(!empty($keywordRelateList)){
foreach ($keywordRelateList as $val){
if($val['type'] == 1){
$data_type_arr[] = $val['product_id'];
}else{
$data_video_type_arr[] = $val['product_id'];
}
}
}
$productVideoList = $productList = [];
$productModel = new Product();
if(!empty($data_type_arr)){
$productList = $productModel->list(['id'=>['in',$data_type_arr]],'id',['id','title']);
}
if(!empty($data_video_type_arr)){
$productVideoList = $productModel->list(['id'=>['in',$data_video_type_arr]],'id',['id','title']);
}
return $this->success(['product'=>$productList,'video_product'=>$productVideoList]);
}
/**
* @remark :对应删除关联关系
* @name :delRelated
* @author :lyh
* @method :post
* @time :2024/11/28 9:46
*/
public function delRelated($product_id,$keyword_arr = [],$keyword_video_arr = []){
$productModel = new Product();
$keyword_str = !empty($keyword_arr) ? ','.implode(',',$keyword_arr).',' : '';
KeywordRelated::saveRelated($product_id,$keyword_arr);
KeywordRelated::saveRelated($product_id,$keyword_video_arr,2);
$keyword_video_str = !empty($keyword_video_arr) ? ','.implode(',',$keyword_arr).',' : '';
$productModel->edit(['keyword_id'=>$keyword_str,'keyword_video_id'=>$keyword_video_str],['id'=>$product_id]);
}
}