SignController.class.php
6.23 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
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
<?php
/**
* 【手机端】11-签到接口
* Created by PhpStorm.
* @author:wanghuan
* @date:2017-09-01
*/
namespace Api\Controller\Education;
use Common\Service\EducationService;
use Common\Service\RightUsersService;
use Common\Service\SignService;
use Common\Service\PlanService;
use Common\Service\PlanDoService;
class SignController extends \Api\Controller\AbstractController
{
/** @var SignService */
protected $sign_s;
/** @var PlanService */
protected $plan_s;
/** @var PlanDoService */
protected $plan_do_s;
/** @var RightUsersService */
protected $right_users_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化签到service
$this->sign_s = new SignService();
// 实例化计划service
$this->plan_s = new PlanService();
// 实例化计划完成记录service
$this->plan_do_s = new PlanDoService();
// 实例化员工名单service
$this->right_users_s = new RightUsersService();
return true;
}
/**
* 签到
*/
public function Index_post()
{
// 培训id
$ed_id = I('post.ed_id', 0, 'intval');
// 培训id为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 培训详情
$education = $this->get_education($ed_id);
// 验证当前用户是否有签到权限
$this->validate_sign_right($education);
// 培训签到场次查询条件
$plan_cond = [
'ed_id' => $ed_id,
'plan_type' => PlanService::PLAN_TYPE_SIGN,
'plan_use_status' => PlanService::PLAN_STATUS_OPEN
];
// 培训签到场次数据
$plans = $this->plan_s->list_by_conds($plan_cond);
// 培训没有设置签到,二维码已过期
if (empty($plans)) {
E('_ERR_QC_INVALID');
}
// 签到时间
$now_time = MILLI_TIME;
// 有效签到场次总数
$sign_enable_count = 0;
// 当前签到场次id
$plan_id = 0;
foreach ($plans as $value) {
// 判断统计有效签到场次
if ($value['plan_sign_end'] > $now_time) {
$sign_enable_count += 1;
}
// 获取当前签到场次id
if ($value['plan_sign_begin'] <= $now_time && $value['plan_sign_end'] >= $now_time) {
$plan_id = $value['plan_id'];
}
}
// 所有场次签到均已结束:签到时间已结束
if (!$sign_enable_count) {
E('_ERR_SIGN_TIME_END');
}
// 没有查询到当前签到场次:没有进行中签到场次
if (!$plan_id) {
E('_ERR_NO_SIGNING');
}
// 计划详情
$plan_info = $this->plan_s->get($plan_id);
// 计划状态为失效或者当前时间小于计划启用时间:计划未启用
if ($now_time < $plan_info['plan_use_time']) {
E('_ERR_SIGN_STATUS_CLOSE');
}
// 用户签到场次查询条件
$plan_sign_cond = [
'sign_uid' => $this->uid,
'ed_id' => $ed_id,
'plan_id' => $plan_id
];
// 用户签到场次信息
$user_plan_sign = $this->sign_s->get_by_conds($plan_sign_cond);
// 已签到
if (!empty($user_plan_sign)) {
E('_ERR_SIGN_EXIST');
}
// 签到表待新增数据
$plan_sign_cond['sign_on_time'] = $now_time;
try {
// 开始事务
$this->sign_s->start_trans();
// 签到表插入该用户的签到数据
$result = $this->sign_s->insert($plan_sign_cond);
// 签到成功
if ($result) {
// plan_do表待加数据
$plan_do_data = [
'pd_uid' => $this->uid,
'plan_id' => $plan_id,
'ed_id' => $ed_id
];
// plan_do表中添加数据
$this->plan_do_s->insert($plan_do_data);
} else { // 签到失败
$this->sign_s->rollback();
}
$this->sign_s->commit();
} catch (\Think\Exception $e) {
$this->sign_s->rollback();
E('_ERR_SIGN_FAIL');
return false;
}
// 签到失败
if (!$result) {
E('_ERR_SIGN_FAIL');
}
return true;
}
/**
* 验证用户是否有签到权限
*
* @param array $education
*/
protected function validate_sign_right($education = [])
{
// 是否收费
$ed_is_charge = $education['ed_is_charge'];
// 是否审核
$ed_is_check = $education['ed_is_check'];
// 报名查询条件
$right_cond = [
'ru_uid' => $this->uid,
'ed_id' => $education['ed_id']
];
// 用户报名数据
$right_users = $this->right_users_s->get_by_conds($right_cond);
// 用户不在适用范围
if (empty($right_users)) {
// 没有签到权限
E('_ERR_SIGN_NO_RIGHT');
}
// 开启报名
if (EducationService::EDUCATION_SIGN_OPEN == $education['ed_is_sign_up']) {
// 不收费、不审核、未报名,没有签到权限
if (EducationService::EDUCATION_UN_CHARGE == $ed_is_charge
&& EducationService::EDUCATION_UN_CHECK == $ed_is_check
&& RightUsersService::SIGN_STATUS_SIGNED != $right_users['ru_sign_status']) {
E('_ERR_SIGN_NO_RIGHT');
}
// 开启收费、未支付,没有签到权限
if (EducationService::EDUCATION_CHARGE == $ed_is_charge
&& RightUsersService::SIGN_CHARGE_PAY != $right_users['ru_pay_status']) {
E('_ERR_SIGN_NO_RIGHT');
}
// 开启审核、审核未通过,没有签到权限
if (EducationService::EDUCATION_CHECK == $ed_is_check
&& RightUsersService::SIGN_CHECK_PASS != $right_users['ru_check_status']) {
E('_ERR_SIGN_NO_RIGHT');
}
}
}
}