ActivityGrouponAutoOper.php
2.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
<?php
namespace addons\shopro\job;
use addons\shopro\library\traits\ActivityCache;
use addons\shopro\library\traits\Groupon;
use addons\shopro\model\Activity;
use addons\shopro\model\ActivityGroupon;
use think\queue\Job;
/**
* 订单自动操作
*/
class ActivityGrouponAutoOper extends BaseJob
{
use ActivityCache, Groupon;
/**
* 拼团判断,将团结束,
*/
public function expire(Job $job, $data){
try {
$activity = $data['activity'];
$activity_groupon_id = $data['activity_groupon_id'];
$activityGroupon = ActivityGroupon::where('id', $activity_groupon_id)->find();
// 活动正在进行中, 走这里的说明人数 都没满
if ($activityGroupon && $activityGroupon['status'] == 'ing') {
\think\Db::transaction(function () use ($activity, $activityGroupon) {
$rules = $activity['rules'];
// 是否虚拟成团
$is_fictitious = $rules['is_fictitious'] ?? 0;
// 最大虚拟人数 ,不填或者 "" 不限制人数,都允许虚拟成团, 0相当于不允许虚拟成团
$fictitious_num = (!isset($rules['fictitious_num']) || $rules['fictitious_num'] == '') ? 'no-limit' : $rules['fictitious_num'];
// 拼团剩余人数
$surplus_num = $activityGroupon['num'] - $activityGroupon['current_num'];
if ($is_fictitious && ($fictitious_num == 'no-limit' || $fictitious_num >= $surplus_num)) {
// 虚拟成团,如果虚拟用户不够,则自动解散
$this->finishFictitiousGroupon($activityGroupon);
} else {
// 解散退款
$this->invalidRefundGroupon($activityGroupon);
}
});
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
\think\Log::write('queue-' . get_class() . '-expire' . ':执行失败,错误信息:' . $e->getMessage());
}
}
}