Goods.php
4.1 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
<?php
namespace app\admin\model;
use think\Model;
class Goods extends Model
{
// 表名
protected $name = 'goods';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'issale_text',
'ismember_text',
'ishot_text',
'ismake_text',
'spec_type_text'
];
/**
* 关联商品规格表
*/
public function spec()
{
return $this->hasMany('GoodsSpec');
}
/**
* 关联商品规格关系表
*/
public function specRel()
{
return $this->belongsToMany('SpecValue', 'GoodsSpecRel');
}
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
public function getIssaleList()
{
return ['0' => __('Issale 0'), '1' => __('Issale 1')];
}
public function getIsmemberList()
{
return ['0' => __('Ismember 0'), '1' => __('Ismember 1')];
}
public function getIshotList()
{
return ['0' => __('Ishot 0'), '1' => __('Ishot 1')];
}
public function getIsmakeList()
{
return ['0' => __('Ismake 0'), '1' => __('Ismake 1')];
}
public function getSpecTypeList()
{
return ['1' => __('Spec_type 1'), '2' => __('Spec_type 2')];
}
public function getIssaleTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['issale']) ? $data['issale'] : '');
$list = $this->getIssaleList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsmemberTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['ismember']) ? $data['ismember'] : '');
$list = $this->getIsmemberList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIshotTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['ishot']) ? $data['ishot'] : '');
$list = $this->getIshotList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsmakeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['ismake']) ? $data['ismake'] : '');
$list = $this->getIsmakeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSpecTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
$list = $this->getSpecTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
/**
* 获取规格信息
*/
public function getManySpecData($spec_rel, $skuData)
{
// spec_attr
$specAttrData = [];
foreach ($spec_rel as $item) {
if (!isset($specAttrData[$item['spec_id']])) {
$specAttrData[$item['spec_id']] = [
'spec_id' => $item['spec']['id'],
'spec_name' => $item['spec']['spec_name'],
'spec_value_list' => [],
];
}
$specAttrData[$item['spec_id']]['spec_value_list'][] = [
'spec_value_id' => $item['id'],
'spec_value_name' => $item['spec_value_name'],
];
}
// spec_list
$goods_spec_list = [];
foreach ($skuData as $item) {
$goods_spec_list[] = [
'goods_spec_id' => $item['id'],
'spec_sku_id' => $item['spec_sku_id'],
'rows' => [],
'form' => [
'goods_price' => $item['goods_price'],
'goods_weight' => $item['goods_weight'],
'stock_num' => $item['stock_num'],
],
];
}
return ['spec_attr' => array_values($specAttrData), 'goods_spec_list' => $goods_spec_list];
}
}