Sku.php
14.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
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* Steps 表单: Goods Step Form-分类
+-----------------------------------------------------------------------------------------------------------------------
* PHP version 7
*
* @category App\Admin\Forms\Steps\Goods
* @package App\Admin\Forms\Steps\Goods
* @author Richer <yangzi1028@163.com>
* @date 2022年5月7日15:14:47
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Forms\Steps\Goods;
use App\Admin\Traits\FormFieldTrait;
use App\Models\Goods\Goods;
use App\Models\Goods\GoodsCategory;
use App\Admin\Rewrite\Widgets\StepForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form\NestedForm;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
/**
* Class Sku
*
* @category App\Admin\Forms\Steps\Goods
* @package App\Admin\Forms\Steps\Goods
* @author Richer <yangzi1028@163.com>
* @date 2022年5月7日15:14:47
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class Sku extends StepForm
{
use FormFieldTrait;
/**
* The form title.
*
* @var string
*/
public $title = 'Sku信息';
/**
* The form.
*
* @var string
*/
public $form = null;
public $suffix = 'goods';
/**
* Other constructor.
* @param array $data
*/
public function __construct($data = [])
{
$this->model = new Goods();
$this->form = $this;
parent::__construct($data);
}
/**
* The form title.
*
* @var string
*/
public $model = null;
/**
* Handle the form request.
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function handle(Request $request)
{
session()->put("steps.goods.model", $this->model);
$basic = $this->save($request);
if ($basic === false) {
admin_toastr('请至少维护一个规格!', 'error');
return $this->prev();
} else {
admin_toastr('规格维护成功,请维护商品其他信息!');
return $this->next($basic);
}
}
/**
* Build a form here.
*/
public function form()
{
Admin::script(' $(".step-submit").on("click", function() {$(this).text("Loading...").addClass("disabled");});');
// dump(\request()->all());
$id = $this->getId() ;
if ($id) {
$this->hidden('id')->default($this->getId());
}
$model = $this->model->with(['skus','specification','specificationItems.attribute.values'])->find($id);
$skus = optional($model)->skus->toArray();
// update by Richer 调整规格为手动输入
$attributes = [];
$attributes = optional($model)->skus->each(function ($sku) {
return $sku->name;
});
dd($skus);
$skus = optional($model)->skus->toArray();
$attributes = optional($model)->specificationItems->map(function ($item) {
return [
'id' => optional($item->attribute)->id,
'name' => optional($item->attribute)->name,
'values' => optional($item->attribute)->values->each(function ($value) {
return [
'id' => optional($value)->id,
'value' => trim(optional($value)->value),
];
})->toArray(),
];
})->toArray();
dd($attributes);
$attribute_str =
$this->form->html(
view(
'admin::goods.skus',
compact('skus', 'id','attributes')
),
)->setWidth(10, 1)->setGroupClass('col-md-12');
// $this->form->hasMany('skus', '', function (NestedForm $form) {
// $form->select('attribute_value_id', __('goods_attribute_value'))->required();
// $form->currency('price', __('price')) ->symbol('¥')->required()->setWidth(6, 3)->setGroupClass('col-md-4');
// $form->currency('original_price', __('original_price')) ->symbol('¥')->required()->setWidth(6, 3)->setGroupClass('col-md-4');
// $form->number('stock', __('stock'))->required()->setWidth(6, 3)->setGroupClass('col-md-4');
// $form->textarea('description', ' '.__('description'))->setWidth(10, 1);
// })->setWidth(10, 1)->setGroupClass('col-md-12');
}
/**
* 渲染不同模块的特殊字段
*/
public function renderSpecialFields()
{
}
/**
* The data of the form.
*
* @return array $data
*/
public function data()
{
return optional($this->getBase())->toArray();
}
/**
* 获取 id
*
* @return int|null
*/
private function getId()
{
return optional($this->getBase())->id;
}
/**
* 获取基础信息模型对象
*
* @return array
*/
protected function getBase()
{
return Arr::get(session('steps'), 'base');
}
/**
* 保存方案:新增或者更新
*
* @param $request
* @return mixed
*/
public function save1($request)
{
$id = $request->id;
$model = $this->model->find($id);
$attribute_ids = $request->attribute_ids;
$attribute_names = $request->attribute_names;
$attribute_values = $request->attribute_values;
// $attribute_values = $request->attribute_value_names;
$names = $request->name;
$stocks = $request->stock;
$prices = $request->price;
$original_prices = $request->original_price;
$descriptions = $request->description;
$store_array = [];
$delete_array = [];
$update_array = [];
if ($attribute_values) {
foreach ($attribute_values as $key => $values) {
$name = Arr::get($names, $key);// 库存
$stock = Arr::get($stocks, $key);// 库存
$price = Arr::get($prices, $key);// 销售价格
$original_price = Arr::get($original_prices, $key);// 原始价格
$description = Arr::get($descriptions, $key);// 描述
$content = [];
foreach ($values as $key_value => $value) {
$content[] = [
'attribute_id' => $attribute_ids[$key_value],
'attribute_name' => $attribute_names[$key_value],
// 'attribute_value_id' => $value,
'attribute_value' => $value,
];
}
// 新增
if (Str::contains($key, 'new')) {
$store_array[] = [
'shop_id' => $model->shop_id,
'category_id' => $model->category_id,
'specification_id' => $model->specification_id,
'name' => $name,
'stock' => $stock,
'price' => $price,
'original_price'=> $original_price,
'description' => $description,
'content' => my_json_encode($content),
'created_at' => now()->toDateTimeString(),
];
} else {
$update_array[] = [
'id' => $key,
'name' => $name,
'stock' => $stock,
'price' => $price,
'original_price'=> $original_price,
'description' => $description,
'content' => my_json_encode($content),
'updated_at' => now()->toDateTimeString(),
];
}
}
}
if (!$update_array && !$store_array) {
return false;
}
if ($store_array) {
$model->skus()->createMany($store_array);
}
if ($update_array) {
$updateColumn = array_keys(Arr::first($update_array));
$referenceColumn = Arr::first($updateColumn);
unset($updateColumn[0]);
$whereIn = "";
$q = 'UPDATE goods_skus SET ';
foreach ($updateColumn as $uColumn) {
$q .= $uColumn . " = CASE ";
foreach ($update_array as $data) {
$q .= "WHEN " . $referenceColumn . " = " . $data[$referenceColumn] . " THEN '" . $data[$uColumn] . "' ";
}
$q .= "ELSE " . $uColumn . " END, ";
}
foreach ($update_array as $data) {
$whereIn .= "'" . $data[$referenceColumn] . "', ";
}
$q = rtrim($q, ", ") . " WHERE " . $referenceColumn . " IN (" . rtrim($whereIn, ', ') . ")";
DB::update(DB::raw($q));
}
// 设置价格,暂时设置最小的价格
$price = optional($model->skus()->orderBy('price')->first())->price;
$original_price = optional($model->skus()->orderBy('original_price')->first())->original_price;
$model->price = $price;
$model->original_price = $original_price;
$model->save();
return true;
}
/**
* 保存方案:新增或者更新
*
* @param $request
* @return mixed
*/
public function save($request)
{
$id = $request->id;
$model = $this->model->find($id);
$attribute_ids = $request->attribute_ids;
$attribute_names = $request->attribute_names;
$attribute_values = $request->attribute_values;
$attribute_value_names = $request->attribute_value_names;
$names = $request->name;
$stocks = $request->stock;
$prices = $request->price;
$original_prices = $request->original_price;
$descriptions = $request->description;
$store_array = [];
$delete_array = [];
$update_array = [];
if ($attribute_values) {
foreach ($attribute_values as $key => $values) {
$name = Arr::get($names, $key);// 库存
$stock = Arr::get($stocks, $key);// 库存
$price = Arr::get($prices, $key);// 销售价格
$original_price = Arr::get($original_prices, $key);// 原始价格
$description = Arr::get($descriptions, $key);// 描述
$value_names = Arr::get($attribute_value_names, $key);// 描述
// dump($stock, $price, $description);
$content = [];
foreach ($values as $key_value => $value) {
$content[] = [
'attribute_id' => $attribute_ids[$key_value],
'attribute_name' => $attribute_names[$key_value],
'attribute_value_id' => $value,
'attribute_value_name' => $value_names[$key_value],
];
}
// 新增
if (Str::contains($key, 'new')) {
$store_array[] = [
'shop_id' => $model->shop_id,
'category_id' => $model->category_id,
'specification_id' => $model->specification_id,
'name' => $name,
'stock' => $stock,
'price' => $price,
'original_price'=> $original_price,
'description' => $description,
'content' => my_json_encode($content),
'created_at' => now()->toDateTimeString(),
];
} else {
$update_array[] = [
'id' => $key,
'name' => $name,
'stock' => $stock,
'price' => $price,
'original_price'=> $original_price,
'description' => $description,
'content' => my_json_encode($content),
'updated_at' => now()->toDateTimeString(),
];
}
}
}
if (!$update_array && !$store_array) {
return false;
}
if ($store_array) {
$model->skus()->createMany($store_array);
}
if ($update_array) {
$updateColumn = array_keys(Arr::first($update_array));
$referenceColumn = Arr::first($updateColumn);
unset($updateColumn[0]);
$whereIn = "";
$q = 'UPDATE goods_skus SET ';
foreach ($updateColumn as $uColumn) {
$q .= $uColumn . " = CASE ";
foreach ($update_array as $data) {
$q .= "WHEN " . $referenceColumn . " = " . $data[$referenceColumn] . " THEN '" . $data[$uColumn] . "' ";
}
$q .= "ELSE " . $uColumn . " END, ";
}
foreach ($update_array as $data) {
$whereIn .= "'" . $data[$referenceColumn] . "', ";
}
$q = rtrim($q, ", ") . " WHERE " . $referenceColumn . " IN (" . rtrim($whereIn, ', ') . ")";
DB::update(DB::raw($q));
}
// 设置价格,暂时设置最小的价格
$price = optional($model->skus()->orderBy('price')->first())->price;
$original_price = optional($model->skus()->orderBy('original_price')->first())->original_price;
$model->price = $price;
$model->original_price = $original_price;
$model->save();
return true;
}
}