HouseActivity.php
4.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
<?php
namespace app\admin\model;
use think\Model;
class HouseActivity extends Model
{
// 表名
protected $name = 'house_activity';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'spec_type_text',
'start_time_text',
'end_time_text'
];
public function getSpecTypeList()
{
return ['0' => __('Spec_type 0'), '1' => __('Spec_type 1'), '2' => __('Spec_type 2')];
}
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 getStartTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getEndTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setStartTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setEndTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
/**
* 关联活动规格表
*/
public function spec()
{
return $this->hasMany('Houseactivityspec','house_activity_id','id');
}
/**
* 关联活动规格关系表
*/
public function specRel()
{
return $this->belongsToMany('Housespecvalue', 'house_activity_spec_rel','spec_value_id','house_activity_id');
}
/**
* 添加活动规格
* @param $data
* @param $isUpdate
* @throws \Exception
*/
public function addGoodsSpec(&$data,$params,$specparams,$isUpdate = false)
{
// 更新模式: 先删除所有规格
$model = new Houseactivityspec;
$isUpdate && $model->removeAll($this['id']);
// 添加规格数据
if ($data['spec_type'] === '1') {
// 单规格
$this->spec()->save($specparams);
} else if ($data['spec_type'] === '2') {
// 添加商品与规格关系记录
$model->addGoodsSpecRel($this['id'],$params['spec_attr']);
// 添加商品sku
$model->addSkuList($this['id'],$params['spec_list']);
}
}
public function removesku(){
// 删除活动sku
(new Houseactivityspec)->removeAll($this['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[] = [
'id' => $item['id'],
'spec_sku_id' => $item['spec_sku_id'],
'rows' => [],
'form' => [
'price' => $item['price'],
],
];
}
return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
}
}