GrantCreditController.class.php
4.93 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 18/5/17
* Time: 21:58
*/
namespace Frontend\Controller\Callback;
use Think\Log;
use VcySDK\Adminer;
use VcySDK\Service;
use Common\Common\Constant;
use Common\Common\Msg;
use Common\Common\Integral;
use Common\Service\SignService;
use Common\Service\CronService;
use Common\Service\RewardService;
use Common\Service\CourseArticleService;
class GrantCreditController extends AbstractController
{
/**
* 线下课程授课结束后,为签到的人员发放学分
* @author liyifei
* @return mixed
*/
public function Index()
{
// UC计划任务ID
$cron_id = I('get.cron_id', '', 'trim');
$article_id = I('get.article_id', 0, 'intval');
// 线下课程详情
$cronServ = new CronService();
$articleServ = new CourseArticleService();
$article = $articleServ->get($article_id);
if (empty($article)) {
$cronServ->delCron($cron_id);
Log::record('article not found: ' . var_export($_GET, true), Log::ERR);
return false;
}
// 未开启签到
if ($article['is_sign'] != Constant::COURSE_IS_SIGN_IS_TRUE) {
$cronServ->delCron($cron_id);
Log::record('article sign is close: ' . var_export($_GET, true), Log::ERR);
return false;
}
// 学分为0
if ($article['credit'] == 0) {
$cronServ->delCron($cron_id);
Log::record('article credit is zero: ' . var_export($_GET, true), Log::ERR);
return false;
}
// 所有已签到人员
$signServ = new SignService();
$sign_list = $signServ->list_by_conds(['article_id' => $article_id]);
if (empty($sign_list)) {
$cronServ->delCron($cron_id);
Log::record('no one sign in: ' . var_export($_GET, true), Log::ERR);
return false;
}
$sign_uids = array_column($sign_list, 'sign_uid');
// 已发放学分的人员
$rewardServ = new RewardService();
$reward_list = $rewardServ->list_by_conds([
'article_id' => $article_id,
'reward_type' => Constant::REWARD_TYPE_CREDIT,
]);
$reward_uids = array_column($reward_list, 'uid');
// 未发放学分的人员
$uids = array_diff($sign_uids, $reward_uids);
if (empty($uids)) {
$cronServ->delCron($cron_id);
exit('SUCCESS');
}
// 管理员列表
$adminSdk = new Adminer(Service::instance());
$result = $adminSdk->listAll([], 1, 1);
// 批量发放学分
$app_name = cfg('APPLICATION_NAME', null, '');
$params = [
// 用户id(多个用户之间逗号分隔)
'uids' => implode(',', $uids),
'milOptType' => 2,
'miType' => 'mi_type1',
'integral' => $article['credit'],
'remark' => '【' . $app_name . '】线下课程:' . $article['article_title'],
'milCreateMemUid' => $result['list'][0]['eaId'],
'milCreateMemUsername' => $result['list'][0]['eaRealname'],
];
$integralServ = &Integral::instance();
$integralServ->integralUpdate($params);
// 保存已发学分的人员
$insert_all = [];
foreach ($uids as $uid) {
$insert_all[] = [
'uid' => $uid,
'article_id' => $article_id,
'reward_type' => Constant::REWARD_TYPE_CREDIT,
'amount' => $article['credit'],
];
}
$rewardServ->insert_all($insert_all);
// 推送获取学分消息
$this->_sendNotice($uids, $article);
// 删除计划任务(本地+UC)
$cronServ->delCron($cron_id);
exit('SUCCESS');
}
/**
* 发送新课程消息通知
* @param array $uids 人员数据
* @param array $article 线下课程数据
* @return bool
*/
private function _sendNotice($uids, $article)
{
$msgServ = &Msg::instance();
$app_name = cfg('APPLICATION_NAME', null, '');
// 给用户推送消息(分批发送)
$uid_groups = array_chunk($uids, Msg::USER_MAX_COUNT);
foreach ($uid_groups as $uid_group) {
$times = rgmdate(MILLI_TIME, 'Y-m-d H:i');
$msg_data = [
[
"title" => "学分变动提醒",
"description" => "您的学时最新变动信息\n变动时间:{$times}\n变动行为:{$app_name}-线下课程\n学时:+{$article['credit']}学分\n备注:【线下课程】{$article['article_title']}\n",
"url" => oaUrl("Frontend/Index/Index/Index", ["miType" => "mi_type1"], "", "Integral"),
],
];
$uid_str = implode('|', $uid_group);
$msgServ->sendNews($uid_str, null, null, $msg_data);
}
return true;
}
}