SignController.class.php
2.97 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
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 2018-5-18
* Time: 15:06:45
*/
namespace Api\Controller\Course;
use Common\Common\Constant;
use Common\Service\CourseArticleService;
use Common\Service\PlanDoService;
use Common\Service\RightUsersService;
use Common\Service\SignService;
class SignController extends \Api\Controller\AbstractController
{
/**
* Sign
* @author zhonglei
* @desc 线下课程签到接口
* @param Int article_id:true 课程ID
* @return Array
*/
public function Index_post()
{
$article_id = I('post.article_id', 0, 'intval');
if (empty($article_id)) {
E('_ERR_ARTICLE_ID_INVALID');
}
$articleServ = new CourseArticleService();
$article = $articleServ->get($article_id);
// 未找到课程
if (empty($article)) {
E('_ERR_ARTICLE_DATA_NOT_FOUND');
}
// 课程未启用签到
if ($article['is_sign'] != Constant::COURSE_IS_SIGN_IS_TRUE) {
E('_ERR_ARTICLE_SIGN_DISABLED');
}
// 课程已授课结束
if (MILLI_TIME > $article['end_time']) {
E('_ERR_ARTICLE_TIMEEND');
}
$rightUsersServ = new RightUsersService();
$uid = $this->_login->user['memUid'];
$conds = ['ru_uid' => $uid, 'ed_id' => $article['ed_id']];
$right_users = $rightUsersServ->get_by_conds($conds);
// 没有权限学习课程
if (empty($right_users)) {
E('_ERR_ARTICLE_PERMISSION_DEND');
}
// 未报名
if ($right_users['ru_sign_status'] != RightUsersService::SIGN_STATUS_SIGNED) {
E('_ERR_USER_NOT_SIGNED');
}
$signServ = new SignService();
$conds = [
'sign_uid' => $uid,
'ed_id' => $article['ed_id'],
'plan_id' => $article['plan_id'],
'article_id' => $article_id,
];
// 已经签到
if (!empty($signServ->get_by_conds($conds))) {
E('_ERR_ARTICLE_ALREADY_SIGN');
}
$data = array_merge($conds, ['sign_on_time' => MILLI_TIME]);
try {
// 开启事务
$signServ->start_trans();
$signServ->insert($data);
$plan_do_serv = new PlanDoService();
// plan_do表待加数据
$plan_do_data = [
'pd_uid' => $this->uid,
'plan_id' => $article['plan_id'],
'ed_id' => $article['ed_id']
];
// plan_do表中添加数据,标识已完成
$plan_do_serv->insert($plan_do_data);
// 提交事务
$signServ->commit();
} catch (\Think\Exception $e) {
// 事务回滚
$signServ->rollback();
E('_ERR_SIGN_FAIL');
return false;
}
$this->_result = [
'start_time' => $article['start_time'],
'end_time' => $article['end_time'],
];
}
}