AttrLogic.php
1.6 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
<?php
namespace App\Http\Logic\Bside\Product;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Attr;
use App\Models\Product\AttrValue;
use Illuminate\Support\Facades\DB;
/**
* Class AttrLogic
* @package App\Http\Logic\Bside\Product
* @author zbj
* @date 2023/4/15
*/
class AttrLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new Attr();
}
public function getInfo($id){
$info = parent::getCacheInfo($id);
if(!$info){
$this->fail('数据不存在或者已经删除');
}
$info->values;
return $this->success($info->toArray());
}
public function save($param){
$param['values'] = array_unique($param['values']);
DB::beginTransaction();
try {
//删除之前的参数值
if(!empty($param['id'])){
AttrValue::where('attr_id', $param['id'])->delete();
}
//保存参数名称
$data = $param;
unset($data['values']);
$data['value_num'] = count($param['values']);
$res = parent::save($data);
$attr_id = $res['id'];
//保存参数值
$values = [];
foreach ($param['values'] as $value){
$values[] = [
'attr_id' => $attr_id,
'value' => $value
];
}
AttrValue::insert($values);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('保存失败');
}
return $this->success();
}
}