-
Notifications
You must be signed in to change notification settings - Fork 3
/
activity.php
360 lines (327 loc) · 11.8 KB
/
activity.php
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
<?php
class syspromotion_activity {
/**
* @brief 获取活动状态
* @author gjp
* @param $params array
*
* @return
*/
public function getActivityStatus($params)
{
$objMdlActivityItem = app::get('syspromotion')->model('activity_item');
$objMdlActivity = app::get('syspromotion')->model('activity_register');
$list = $objMdlActivityItem->getList('activity_id',array('item_id'=>$params['item_id']));
if($list)
{
foreach($list as $key=>$value)
{
$data = $objMdlActivity->getRow('*',array('activity_id'=>$value['activity_id']));
//echo '<pre>';print_r($data);exit();
if($data['apply_begin_time'] < time() && time() < $data['end_time'] && $value['verify_status']!='refuse')
{
$result = 1;
}
else
{
$result = 0;
}
}
}
else
{
$result = 0;
}
return $result;
#code
}
/**
* @brief 删除活动
* @author lujy
* @param $params array
*
* @return
*/
public function deleteActivity($params)
{
$objMdlActivity = app::get('syspromotion')->model('activity');
$list = $objMdlActivity->getList('activity_id,apply_begin_time',array('activity_id'=>$params));
if($list)
{
$result = true;
foreach($list as $key=>$value)
{
if($value['apply_begin_time'] < time())
{
$result = false;
$msg = "活动报名已经开始,不可删除";
}
else
{
$return = $objMdlActivity->delete(array('activity_id'=>$value['activity_id']));
if(!$return)
{
$result = false;
$msg = "删除失败";
}
}
}
if(!$result)
{
throw new LogicException($msg);
}
}
return true;
#code
}
/**
* 保存活动
* @param array $data 活动传入数据
* @return bool 是否保存成功
*/
public function saveActivity($data)
{
$activityData = $this->__preareData($data);
$objMdlActivity = app::get('syspromotion')->model('activity');
$db = app::get('syspromotion')->database();
$db->beginTransaction();
try
{
if( !$objMdlActivity->save($activityData) )
{
throw \LogicException('活动保存失败');
}
$db->commit();
}
catch (Exception $e)
{
$db->rollback();
throw $e;
}
return true;
}
private function __preareData($data) {
$aResult = array();
$aResult = $data;
if($data['activity_id'])
{
$objMdlActivity = app::get('syspromotion')->model('activity');
$activityInfo = $objMdlActivity->getRow('*',array('activity_id'=>$data['activity_id']));
if( time() >= $activityInfo['start_time'] )
{
throw new \LogicException('活动生效开始时间后则不可进行编辑!');
}
}
else
{
$aResult['created_time'] = time();
}
/* if( $data['buy_limit'] <= 0 )
{
throw new \LogicException('用户限购数量要大于0!');
}
if( $data['discount_max'] <= $data['discount_min'])
{
throw new \LogicException('折扣范围必须由小到大!');
}
if($data['apply_begin_time'] < time())
{
throw new \LogicException('活动报名的开始时间必须大于当前时间!');
}
if( $data['apply_end_time'] <= $data['apply_begin_time'] )
{
throw new \LogicException('活动报名结束时间必须大于报名的开始时间!');
}
if( $data['release_time'] <= $data['apply_end_time'] )
{
throw new \LogicException('发布时间必须大于报名结束时间!');
}
if( $data['start_time'] <= $data['release_time'] )
{
throw new \LogicException('活动生效时间必须大于活动发布时间!');
}
if( $data['end_time'] <= $data['start_time'] )
{
throw new \LogicException('活动生效结束时间必须大于活动开始时间!');
}
if( !$data['shoptype'])
{
throw new \LogicException('至少选择一种店铺类型!');
}
if( !$data['limit_cat'])
{
throw new \LogicException('至少选择一种平台商品类目!');
}*/
$aResult['activity_name'] = strip_tags($data['activity_name']);
$aResult['activity_desc'] = strip_tags($data['activity_desc']);
$aResult['shoptype'] = implode(',',$data['shoptype']);
// $forPlatform = intval($data['used_platform']);
// $aResult['used_platform'] = $forPlatform ? $forPlatform : '0';
return $aResult;
}
public function getList($row,$filter,$offset=0, $limit=200, $orderBy=null)
{
$objMdlActivity = app::get('syspromotion')->model('activity');
$activity = $objMdlActivity->getList($row,$filter,$offset,$limit,$orderBy);
return $activity;
}
public function countActivity($filter)
{
$objMdlActivity = app::get('syspromotion')->model('activity');
return $objMdlActivity->count($filter);
}
public function countActivityItem($filter)
{
$objMdlActivityItem = app::get('syspromotion')->model('activity_item');
return $objMdlActivityItem->count($filter);
}
public function getInfo($row,$filter)
{
$objMdlActivity = app::get('syspromotion')->model('activity');
$activity = $objMdlActivity->getRow($row,$filter);
//如果查询的字段中有店铺类型,需要显示店铺中文描述
if(strpos($row,'shoptype') || $row == "*" || $row == "shoptype")
{
$shoptype = $activity['shoptype'];
$activity['shoptype'] = $this->__getShopType($shoptype);
}
//如果查询的字段中包含类目,需要查询类目相关的所有内容
if(strpos($row,'limit_cat') || $row == "*" || $row == "limit_cat")
{
$cat = $activity['limit_cat'];
$activity['limit_cat'] = $this->__getCat($cat);
}
return $activity;
}
private function __getShopType($params)
{
// 获取店铺类型
$shopType = app::get('syspromotion')->rpcCall('shop.type.get',array('shop_type'=>$params));
foreach($shopType as $value)
{
$type[$value['shop_type']] = $value['name'];
}
return $type;
}
private function __getCat($params)
{
$params = implode(',',$params);
//获取类目
$cat = app::get('syspromotion')->rpcCall('category.cat.get.info',array('cat_id' => $params,'level' =>1));
foreach($cat as $value)
{
$data[$value['cat_id']] = $value['cat_name'];
}
return $data;
}
public function getItemList($row,$filter,$offset=0, $limit=200, $orderBy=null)
{
$objMdlItemActivity = app::get('syspromotion')->model('activity_item');
$activityItem = $objMdlItemActivity->getList($row,$filter,$offset,$limit,$orderBy);
return $activityItem;
}
public function getItemInfo($row,$filter)
{
$objMdlItemActivity = app::get('syspromotion')->model('activity_item');
$activityItem = $objMdlItemActivity->getRow($row,$filter);
return $activityItem;
}
/**
* 保存活动报名
* @param array $data 活动报名数据
* @return bool 是否保存成功
*/
public function saveRegisterActivity($data)
{
$activityItem = array();
$itemIds = array();
foreach($data['item_info'] as $v)
{
$apiParams['fields'] = 'item_id,cat_id,title,image_default_id,price';
$apiParams['item_id'] = $v['item_id'];
$itemsInfo = app::get('topshop')->rpcCall('item.get', $apiParams);
$itemIds[]=$v['item_id'];
$activityItem[] = array(
'activity_id' => $data['activity_id'],
'shop_id' => $data['shop_id'],
'item_id' => $v['item_id'],
'cat_id' => $itemsInfo['cat_id'],
'title' => $itemsInfo['title'],
'item_default_image' => $itemsInfo['image_default_id'],
'price' => $itemsInfo['price'],
'activity_price' => $v['activity_price'],
'start_time' => $data['activity_info']['start_time'],
'end_time' => $data['activity_info']['end_time'],
'activity_tag' => $data['activity_info']['activity_tag'],
);
}
$registerData = array(
'activity_id' => $data['activity_id'],
'shop_id' => $data['shop_id'],
'modified_time' => time(),
);
//商品参加团购判断重复添加
$objMdlActivityItem = app::get('syspromotion')->model('activity_item');
$itemList = $objMdlActivityItem->getList('activity_id, title,verify_status', array('item_id'=>$itemIds, 'end_time|than'=>time() ) );
foreach($itemList as $v)
{
if($data['activity_id'] && $v['verify_status']!='refuse')
{
if($v['activity_id'] != $data['activity_id'])
{
throw new \LogicException("商品 {$v['title']} 已经参加别的团购,同一个商品只能应用于一个有效的团购促销中!");
}
}
/*else
{
throw new \LogicException("商品 {$v['title']} 已经参加别的团购,同一个商品只能应用于一个有效的团购促销中!");
}*/
}
$objMdlActivityRegister = app::get('syspromotion')->model('activity_register');
$objMdlActivityItem = app::get('syspromotion')->model('activity_item');
$db = app::get('syspromotion')->database();
$db->beginTransaction();
try
{
// 保存报名的基础信息
$objMdlActivityRegister->delete(array('activity_id'=>$registerData['activity_id'], 'shop_id'=>$registerData['shop_id']));
if( !$objMdlActivityRegister->save($registerData) )
{
throw \LogicException('活动报名保存失败');
}
// 先删除报名关联的商品
$objMdlActivityItem->delete(array('activity_id'=>$registerData['activity_id'], 'shop_id'=>$registerData['shop_id']));
// 保存报名的商品信息
foreach ($activityItem as $vItem)
{
if( !$objMdlActivityItem->save($vItem) )
{
throw \LogicException("活动报名商品保存失败");
}
}
$db->commit();
}
catch (Exception $e)
{
$db->rollback();
throw $e;
}
return true;
}
public function setMainpush($params)
{
$objMdlActivity = app::get('syspromotion')->model('activity');
$result = $objMdlActivity->update(array( 'mainpush' => 0));
if(!$result)
{
throw \LogicException("取消原有主推活动失败");
}
$params['mainpush'] = 1;
$result = $objMdlActivity->save($params);
if(!$result)
{
throw \LogicException("设置主推活动失败");
}
return true;
}
}