ProductLogic.php
16.0 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
namespace App\Http\Logic\Bside\Product;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Services\CosService;
use Illuminate\Support\Facades\DB;
/**
* Class ProductLogic
* @package App\Http\Logic\Bside\APublicModel
* @author zbj
* @date 2023/4/14
*/
class ProductLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new Product();
}
/**
* @remark :保存产品
* @name :productSave
* @author :lyh
* @method :post
* @time :2023/8/21 18:35
*/
public function productSave(){
//参数处理
$this->param = $this->handleSaveParam($this->param);
DB::beginTransaction();
try {
if(isset($this->param['id']) && !empty($this->param['id'])){
//查看路由是否更新
$id = $this->editProductRoute($this->param['id'],$this->param['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->addReturnId($this->param);
}
//路由映射
$route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_PRODUCT, $id, $this->user['project_id']);
$this->model->edit(['route'=>$route],['id'=>$id]);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('系统错误请联系管理员');
}
//通知更新
$this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_PRODUCT, 'route'=>$route]);
return $this->success();
}
/**
* @remark :不使用save处理参数
* @name :handleSaveParam
* @author :lyh
* @method :post
* @time :2023/8/21 17:03
*/
public function handleSaveParam(&$param){
//产品图
if(isset($param['gallery']) && !empty($param['gallery'])){
foreach ($param['gallery'] as $k => $v){
$v['url'] = str_replace_url($v['url']);
$param['gallery'][$k] = $v;
}
$param['thumb'] = Arr::a2s($param['gallery'][0] ?? '');
$param['gallery'] = Arr::a2s($param['gallery'] ?? '');
}
$param['attrs'] = Arr::a2s($param['attrs'] ?? '');
$param['attr_id'] = Arr::arrToSet($param['attr_id'] ?? '');
if(isset($param['category_id']) && !empty($param['category_id'])){
$param['category_id'] = ','.Arr::arrToSet($param['category_id']).',';
}
if(isset($param['keyword_id']) && !empty($param['keyword_id'])){
$param['keyword_id'] = ','.Arr::arrToSet($param['keyword_id']).',';
}
$param['describe'] = Arr::a2s($param['describe'] ?? '');
$param['describe_id'] = Arr::arrToSet($param['describe_id'] ?? '');
$param['seo_mate'] = Arr::a2s($param['seo_mate'] ?? '');
$param['related_product_id'] = Arr::arrToSet($param['related_product_id'] ?? '');
if(isset($param['icon']) && !empty($param['icon'])){
foreach ($param['icon'] as $k1 => $v1){
$param['icon'][$k1] = str_replace_url($v1);
}
$param['icon'] = Arr::a2s($param['icon'] ?? '');
}
$param['created_uid'] = $this->user['id'];
return $param;
}
/**
* @remark :编辑产品
* @name :editProduct
* @author :lyh
* @method :post
* @time :2023/9/7 10:02
*/
public function editProductRoute($id,$route){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info['route'] != $route){
//生成一条删除路由记录
$data = [
'source'=>RouteMap::SOURCE_PRODUCT,
'route'=>$route,
];
$this->setRouteDeleteSave($data);
}
return $id;
}
/**
* @remark :删除数据
* @name :delete
* @author :lyh
* @method :post
* @time :2023/8/21 17:11
*/
public function productDelete(){
DB::beginTransaction();
try {
foreach ($this->param['ids'] as $id) {
$info = $this->model->read(['id'=>$id],['id','status']);
if($info['status'] == Product::STATUS_RECYCLE){
$this->delRoute($id);
//删除当前产品模版
$this->delProductModule($id);
$this->model->del(['id'=>$id]);
}else{
//回收站
$this->model->edit(['status'=>Product::STATUS_RECYCLE],['id'=>$id]);
}
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('删除失败');
}
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, $id, $this->user['project_id']);
//生成一条删除路由记录
$info = $this->model->read(['id'=>$id],['id','route']);
$data = [
'source'=>RouteMap::SOURCE_PRODUCT,
'route'=>$info['route'],
];
$this->setRouteDeleteSave($data);
return $this->success();
}
/**
* @remark :统计数据
* @name :getStatusNumber
* @author :lyh
* @method :post
* @time :2023/8/9 10:17
*/
public function getStatusNumber(){
//三种状态 0:草稿 1:发布 2:回收站
$data = ['dra'=>0,'pub'=>1,'del'=>2,'tal'=>3];
foreach ($data as $k => $v){
if($v == 3){
$data[$k] = $this->model->where(['project_id'=>$this->user['project_id']])->count();
}else{
$data[$k] = $this->model->where(['status'=>$v,'project_id'=>$this->user['project_id']])->count();
}
}
return $this->success($data);
}
/**
* @remark :复制产品
* @name :setCopyProduct
* @author :lyh
* @method :post
* @time :2023/7/29 15:03
*/
public function setCopyProduct(){
$info = $this->model->read(['id'=>$this->param['id']]);
$param = $this->setProductParams($info);
$save_id = $this->model->insertGetId($param);
$route = RouteMap::setRoute($param['route'], RouteMap::SOURCE_PRODUCT, $save_id, $this->user['project_id']);
$this->model->edit(['route'=>$route],['id'=>$save_id]);
//同步可视化装修数据
$this->copyTemplate($this->param['id'],$info['project_id'],$save_id);
return $this->success();
}
/**
* @remark :同步模版数据
* @name :copyTemplate
* @author :lyh
* @method :post
* @time :2023/7/29 15:53
*/
public function copyTemplate($id,$project_id,$save_id){
$BTemplateModel = new BTemplate();
$list = $BTemplateModel->list(['source'=>2,'source_id'=>$id,'project_id'=>$project_id]);
if(!empty($list)){
$data = [];
foreach ($list as $k => $v){
$data[] = $this->setTemplateParams($v,$project_id,$save_id);
}
$rs = $BTemplateModel->insert($data);
if($rs === false){
$this->fail('error');
}
}
return $this->success();
}
/**
* @remark :组装模版数据
* @name :setTemplateParams
* @author :lyh
* @method :post
* @time :2023/7/29 15:54
*/
public function setTemplateParams($v,$project_id,$save_id){
$param = [
'html'=>$v['html'],
'project_id'=>$project_id,
'source'=>$v['source'],
'source_id'=>$save_id,
'template_id'=>$v['template_id'],
'section_list_id'=>$v['section_list_id'],
'head_html'=>$v['head_html'],
'main_html'=>$v['main_html'],
'footer_html'=>$v['footer_html'],
'head_css'=>$v['head_css'],
'main_css'=>$v['main_css'],
'footer_css'=>$v['footer_css'],
'created_at'=>$v['created_at'],
'updated_at'=>$v['updated_at']
];
return $this->success($param);
}
/**
* @remark :组装数据
* @name :setParams
* @author :lyh
* @method :post
* @time :2023/7/29 15:45
*/
public function setProductParams($info){
$param = [
'project_id'=>$info['project_id'],
'title'=>$info['title']."-copy",
'attrs'=>Arr::a2s($info['attrs']),
'attr_id'=>Arr::arrToSet($info['attr_id']),
'category_id'=>!empty($info['category_id']) ? ','.Arr::arrToSet($info['category_id']).',' : '',
'keyword_id'=>!empty($info['keyword_id']) ? ','.Arr::arrToSet($info['keyword_id']).',' : '',
'intro'=>$info['intro'],
'content'=>$info['content'],
'describe'=>Arr::a2s($info['describe']),
'describe_id'=>Arr::arrToSet($info['describe_id']),
'seo_mate'=>Arr::a2s($info['seo_mate']),
'related_product_id'=>Arr::arrToSet($info['related_product_id']),
'sort'=>$info['sort'],
'status'=>0,
'route'=>$info['route'],
'product_type'=>$info['product_type'],
'created_uid'=>$this->user['id'],
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
if(isset($info['icon']) && !empty($info['icon'])){
foreach ($info['icon'] as $k1 => $v1){
$info['icon'][$k1] = str_replace_url($v1);
}
$param['icon'] = Arr::a2s($info['icon'] ?? '');
}
//产品图
if(isset($info['gallery']) && !empty($info['gallery'])){
foreach ($info['gallery'] as $k => $v){
$v['url'] = str_replace_url($v['url']);
$info['gallery'][$k] = $v;
}
$param['thumb'] = Arr::a2s($info['gallery'][0] ?? '');
$param['gallery'] = Arr::a2s($info['gallery'] ?? '');
}
return $param;
}
/**
* @remark :删除当前产品所有模块
* @name :delProductModule
* @author :lyh
* @method :post
* @time :2023/7/31 10:31
*/
public function delProductModule($id){
$param = [
'source' => BTemplate::SOURCE_PRODUCT,
'source_id' => $id,
'project_id'=>$this->user['project_id'],
];
$bTemplateModel = new BTemplate();
$rs = $bTemplateModel->del($param);
if($rs === false){
$this->fail('error');
}
return $rs;
}
/**
* @remark :批量设置产品分类及状态
* @name :batchSetCategory
* @author :lyh
* @method :post
* @time :2023/8/15 17:53
*/
public function batchSetCategory(){
DB::beginTransaction();
try {
//批量
$param = [
'category_id'=>','.Arr::arrToSet($this->param['category_id']).',',
'status'=>$this->param['status']
];
$this->model->edit($param,['id'=>['in',$this->param['id']]]);
DB::commit();
//对应添加关联表
}catch (\Exception $e){
DB::rollBack();
$this->fail('系统错误,请联系管理员');
}
return $this->success();
}
/**
* @remark :排序
* @name :setSort
* @author :lyh
* @method :post
* @time :2023/8/19 11:16
*/
public function setSort(){
$rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* 产品导入
* @param $project_id
* @param $user_id
* @param $data
* @return bool
* @throws \Exception
* @author Akun
* @date 2023/09/21 14:55
*/
public function importProduct($project_id, $user_id, $data)
{
$product = $this->model->read(['title' => $data[0]]);
if (!$product) {
$category_id = '';
if ($data[2]) {
//处理分类
$categoryLogic = new CategoryLogic();
$category_id = $categoryLogic->importProductCategory($project_id, $data[2]);
}
$keyword_id = '';
if($data[3]){
//处理关键词
$keywordLogic = new KeywordLogic();
$keyword_id = $keywordLogic->importProductKeyword($project_id, $data[3]);
}
$gallery = [];
$thumb = '';
if($data[7]){
//处理图片
$img_arr = explode(',',$data[7]);
foreach ($img_arr as $v_img){
if($v_img){
$one_img = CosService::uploadRemote($project_id,'image_product',$v_img);
if($one_img){
$one_gallery = [
'alt' => '这是一张产品图',
'url' => $one_img
];
if(!$thumb){
$thumb = Arr::a2s($one_gallery);
}
$gallery[] = $one_gallery;
}
}
}
}
$intro = '';
if($data[5]){
//处理短描述中的图片
$pattern = '<img src="(.*?)">';
preg_match_all($pattern, $data[5], $result_intro);
if($result_intro[1]){
foreach ($result_intro[1] as $vi_img){
$data[5] = str_replace($vi_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vi_img)),$data[5]);
}
}
$intro = $data[5];
}
$content = '';
if($data[6]){
//处理内容中的图片
$pattern = '<img src="(.*?)">';
preg_match_all($pattern, $data[6], $result_content);
if($result_content[1]){
foreach ($result_content[1] as $vc_img){
$data[6] = str_replace($vc_img,getImageUrl(CosService::uploadRemote($project_id,'image_product',$vc_img)),$data[6]);
}
}
$content = $data[6];
}
//处理seo
$seo_mate = [
'title' => $data[8]??'',
'keyword' => $data[9]??'',
'description' => $data[10]??''
];
$id = $this->model->addReturnId(
[
'project_id' => $project_id,
'title' => $data[0],
'thumb' => $thumb,
'gallery' => Arr::a2s($gallery),
'attrs' => trim($data[4]) ? $data[4] : Arr::a2s([]),
'category_id' => $category_id,
'keyword_id' => $keyword_id,
'intro' => $intro,
'content' => $content,
'seo_mate' => Arr::a2s($seo_mate),
'created_uid' => $user_id,
'status' => Product::STATUS_ON
]
);
//更新路由
$route = RouteMap::setRoute((isset($data[1]) && $data[1]) ? $data[1] : $data[0], RouteMap::SOURCE_PRODUCT, $id, $project_id);
$this->edit(['route' => $route], ['id' => $id]);
return true;
}
return false;
}
}