ProductLogic.php
10.5 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
<?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 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->param['id'];
$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['route'], RouteMap::SOURCE_PRODUCT, $id, $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, 'route'=>$route]);
return $this->success();
}
/**
* @remark :不使用save处理参数
* @name :handleSaveParam
* @author :lyh
* @method :post
* @time :2023/8/21 17:03
*/
public function handleSaveParam(&$param){
$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'] ?? '');
$param['category_id'] = ','.Arr::arrToSet($param['category_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'] ?? '');
$param['icon'] = Arr::a2s($param['icon'] ?? '');
$param['created_uid'] = $this->user['id'];
return $param;
}
/**
* @remark :删除数据
* @name :delete
* @author :lyh
* @method :post
* @time :2023/8/21 17:11
*/
public function productDelete(){
DB::beginTransaction();
try {
foreach ($this->param['ids'] as $k => $id) {
$info = $this->model->read(['id'=>$id]);
if($info['status'] == Product::STATUS_RECYCLE){
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_PRODUCT, $id, $this->user['project_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 :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);
//同步路由信息
$this->copyRoute($save_id,$param['route'].'-'.$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 :copyRoute
* @author :lyh
* @method :post
* @time :2023/8/19 11:53
*/
public function copyRoute($news_id,$new_route){
$routeModel = new RouteMap();
$data = [
'source'=>$routeModel::SOURCE_PRODUCT,
'source_id'=>$news_id,
'project_id'=>$this->user['project_id'],
'route'=>$new_route,
];
return $routeModel->add($data);
}
/**
* @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",
'thumb'=>Arr::a2s($info['thumb']),
'gallery'=>Arr::a2s($info['gallery']),
'attrs'=>Arr::a2s($info['attrs']),
'attr_id'=>Arr::arrToSet($info['attr_id']),
'category_id'=>Arr::arrToSet(trim($info['category_id'],',')),
'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'=>$info['status'],
'product_type'=>$info['product_type'],
'created_uid'=>$this->user['id'],
'icon'=>Arr::a2s($info['icon']),
'route'=>$info['route']."-copy",
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
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 {
//删除分类关联表记录
$categoryRelatedModel = new CategoryRelated();
$categoryRelatedModel->del(['product_id'=>['in',$this->param['id']]]);
//批量
$param = [
'category_id'=>Arr::arrToSet($this->param['category_id']),
'status'=>$this->param['status']
];
$this->model->edit($param,['id'=>['in',$this->param['id']]]);
//新增关联分类
foreach ($this->param['id'] as $v1){
foreach ($this->param['category_id'] as $v2){
CategoryRelated::saveRelated($v1,$v2);
}
}
DB::commit();
//对应添加关联表
}catch (\Exception $e){
DB::rollBack();
$this->fail('error');
}
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();
}
}