Litestoregoods.php
6.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
<?php
namespace app\admin\model;
use think\Model;
class Litestoregoods extends Model
{
// 表名
protected $name = 'litestore_goods';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'spec_type_text',
'deduct_stock_type_text',
'goods_status_text',
'is_recommend_text',
'is_delete_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['goods_sort' => $row[$pk]]);
});
}
public function getSpecTypeList()
{
return ['10' => __('Spec_type 10'),'20' => __('Spec_type 20')];
}
public function getDeductStockTypeList()
{
return ['10' => __('Deduct_stock_type 10'),'20' => __('Deduct_stock_type 20')];
}
public function getGoodsStatusList()
{
return ['10' => __('Goods_status 10'),'20' => __('Goods_status 20')];
}
public function getIsRecommendList()
{
return ['0' => __('Is_recommend 0'),'1' => __('Is_recommend 1')];
}
public function getIsDeleteList()
{
return ['0' => __('Is_delete 0'),'1' => __('Is_delete 1')];
}
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 getDeductStockTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['deduct_stock_type']) ? $data['deduct_stock_type'] : '');
$list = $this->getDeductStockTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getGoodsStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['goods_status']) ? $data['goods_status'] : '');
$list = $this->getGoodsStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsDeleteTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_delete']) ? $data['is_delete'] : '');
$list = $this->getIsDeleteList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsRecommendTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
$list = $this->getIsRecommendList();
return isset($list[$value]) ? $list[$value] : '';
}
public function category()
{
return $this->belongsTo('litestorecategory', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function freight()
{
return $this->belongsTo('Litestorefreight', 'delivery_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 关联商品规格表
*/
public function spec()
{
return $this->hasMany('Litestoregoodsspec','goods_id','goods_id');
}
/**
* 关联商品规格关系表
*/
public function specRel()
{
return $this->belongsToMany('Litestorespecvalue', 'litestore_goods_spec_rel','spec_value_id','goods_id');
}
/**
* 计算显示销量 (初始销量 + 实际销量)
* @param $value
* @param $data
* @return mixed
*/
public function getGoodsSalesAttr($value, $data)
{
return $data['sales_initial'] + $data['sales_actual'];
}
/**
* 添加商品规格
* @param $data
* @param $isUpdate
* @throws \Exception
*/
public function addGoodsSpec(&$data,$params,$specparams,$isUpdate = false)
{
// 更新模式: 先删除所有规格
$model = new Litestoregoodsspec;
$isUpdate && $model->removeAll($this['goods_id']);
// 添加规格数据
if ($data['spec_type'] === '10') {
// 单规格
$this->spec()->save($specparams);
} else if ($data['spec_type'] === '20') {
// 添加商品与规格关系记录
$model->addGoodsSpecRel($this['goods_id'],$params['spec_attr']);
// 添加商品sku
$model->addSkuList($this['goods_id'],$params['spec_list']);
}
}
public function removesku(){
// 删除商品sku
(new Litestoregoodsspec)->removeAll($this['goods_id']);
}
/**
* 获取规格信息
*/
public function getManySpecData($spec_rel, $skuData)
{
// spec_attr
$specAttrData = [];
foreach ($spec_rel as $item) {
if (!isset($specAttrData[$item['spec_id']])) {
$specAttrData[$item['spec_id']] = [
'group_id' => $item['spec']['id'],
'group_name' => $item['spec']['spec_name'],
'spec_items' => [],
];
}
$specAttrData[$item['spec_id']]['spec_items'][] = [
'item_id' => $item['pivot']['spec_value_id'],
'spec_value' => $item['spec_value'],
];
}
// spec_list
$specListData = [];
foreach ($skuData as $item) {
$specListData[] = [
'goods_spec_id' => $item['goods_spec_id'],
'spec_sku_id' => $item['spec_sku_id'],
'rows' => [],
'form' => [
'goods_no' => $item['goods_no'],
'goods_price' => $item['goods_price'],
'goods_weight' => $item['goods_weight'],
'line_price' => $item['line_price'],
'stock_num' => $item['stock_num'],
'spec_image' => $item['spec_image'],
],
];
}
return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
}
}