ProductController.php
5.9 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
<?php
namespace App\Http\Controllers\Bside\Product;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Product\ProductLogic;
use App\Http\Requests\Bside\Product\ProductRequest;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\User\User;
use App\Rules\Ids;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* Class ProductController
* @package App\Http\Controllers\Bside
* @author zbj
* @date 2023/4/17
*/
class ProductController extends BaseController
{
public function index(Product $product)
{
$this->order = 'sort';
if(isset($this->map['title']) && !empty($this->map['title'])){
$this->map['title'] = ['like','%'.$this->map['title'].'%'];
}
if(isset($this->map['category_id']) && !empty($this->map['category_id'])){
$this->map['category_id'] = ['like','%'.$this->map['category_id'].'%'];
}
$this->map['project_id'] = $this->user['project_id'];
$filed = ['id', 'project_id', 'title', 'sort' ,'thumb', 'product_type' , 'route' ,'category_id', 'keyword_id', 'status', 'created_uid', 'created_at', 'updated_at'];
$lists = $product->lists($this->map,$this->page,$this->row,$this->order,$filed);
if(!empty($lists['list'])){
foreach ($lists['list'] as $k=>$v){
//处理参数
$v = $this->handleParam($v);
$lists['list'][$k] = $v;
}
}
return $this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :列表
* @name :info
* @author :lyh
* @method :post
* @time :2023/8/21 18:12
*/
public function info(Product $product){
$this->request->validate([
'id'=>'required'
],[
'id.required' => 'ID不能为空'
]);
$info = $product->read(['id'=>$this->param['id']]);
$info = $this->handleParam($info);
return $this->response('success',Code::SUCCESS,$info);
}
/**
* @remark :处理列表参数
* @name :handleParam
* @author :lyh
* @method :post
* @time :2023/8/17 9:15
*/
public function handleParam($v){
$v['category_id_text'] = '';
$v['keyword_id_text'] = '';
if(!empty($v['category_id'])){
$categoryModel = new Category();
$category_data = $categoryModel->list(['id'=>['in',$v['category_id']]],'id',['title']);
foreach ($category_data as $v1){
$v['category_id_text'] .= $v1['title'].',';
}
$v['category_id_text'] = trim($v['category_id_text'],',');
}
if(!empty($v['keyword_id'])){
$keywordModel = new Keyword();
$keyword_data = $keywordModel->list(['id'=>['in',$v['keyword_id']]]);
foreach ($keyword_data as $v1){
$v['keyword_id_text'] .= $v1['title'].',';
}
$v['keyword_id_text'] = trim($v['keyword_id_text'],',');
}
$v['status_text'] = Product::statusMap()[$v['status']] ?? '';
$v['created_uid_text'] = (new User())->read(['id'=>$v['created_uid']])['name'] ?? '';
$v['url'] = $this->user['domain'].$v['route'];
return $v;
}
/**
* @remark :保存产品数据
* @name :save
* @author :lyh
* @method :post
* @time :2023/8/17 15:01
*/
public function save(ProductRequest $request, ProductLogic $logic)
{
$request->validated();
$data = $logic->productSave();
return $this->success($data);
}
/**
* @remark :删除
* @name :delete
* @author :lyh
* @method :post
* @time :2023/8/22 13:45
*/
public function delete(ProductLogic $logic)
{
$this->request->validate([
'ids'=>['required', new Ids()]
],[
'ids.required' => 'ID不能为空'
]);
$logic->productDelete();
$this->response('success');
}
/**
* @remark :根据状态获取数量
* @name :getStatusNumber
* @author :lyh
* @method :post
* @time :2023/8/21 18:33
*/
public function getStatusNumber(ProductLogic $logic){
$data = $logic->getStatusNumber();
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :复制产品
* @name :copyProduct
* @author :lyh
* @method :post
* @time :2023/7/29 14:59
*/
public function copyProduct(ProductLogic $logic){
$rs = $logic->setCopyProduct();
$this->response('success',Code::SUCCESS,$rs);
}
/**
* @remark :批量设置产品分类及状态
* @name :batchSetCategory
* @author :lyh
* @method :post
* @time :2023/8/15 17:51
*/
public function batchSetCategory(ProductLogic $logic){
$this->request->validate([
'id'=>'required',
'category_id'=>'required',
'status'=>'required'
],[
'id.required' => '产品ID不能为空',
'category_id.required' => '分类ID不能为空',
'status.required'=>'状态不能为空'
]);
$logic->batchSetCategory();
$this->response('success');
}
/**
* @remark :修改排序
* @name :sort
* @author :lyh
* @method :post
* @time :2023/8/19 11:13
*/
public function sort(ProductLogic $logic){
$this->request->validate([
'id'=>'required',
'sort'=>'required'
],[
'id.required' => '产品ID不能为空',
'sort.required'=>'排序字段不能为空'
]);
$logic->setSort();
$this->response('success');
}
}