TaskReminderController.class.php
11.8 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
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
<?php
/**
* Created by PhpStorm.
* 自动提醒 未完成任务人员
* User: zhoutao
* Date: 2017/12/2
* Time: 下午2:53
*/
namespace Frontend\Controller\Crontab;
use Common\Model\CustomtaskModel;
use Common\Model\CustomtaskRightModel;
use Common\Model\UserTaskModel;
use Common\Service\CustomtaskRightService;
use Common\Service\CustomtaskService;
use Common\Service\UserTaskService;
use Think\Log;
use VcySDK\Member;
use VcySDK\Message;
use VcySDK\Service;
/**
* Class TaskReminderController
* @property CustomtaskService $customtaskServ
* @property CustomtaskRightService $customtaskRightServ
* @property Message $messageSdk
* @property UserTaskService $userTaskServ
* @property Member $userServ
*/
class TaskReminderController extends AbstractController
{
/**
* 任务翻页 单次处理个数
*/
const DEAL_TASK_PAGE_LIMIT = 5;
/**
* Crontab 执行时间区间 (单位: 分钟)
*/
const CRONTAB_CYCLE_TIMES = 5;
/**
* 常规任务权限表: 全公司
*/
const RIGHT_ALL = 1;
/**
* 人员处理单次处理个数
*/
const DEAL_MEMBER_LIMIT = 500;
/**
* customtaskServ
* @var null
*/
protected $customtaskServ = null;
/**
* $customtaskRightServ
* @var null
*/
protected $customtaskRightServ = null;
/**
* 消息
* @var null
*/
protected $messageSdk = null;
/**
* 用户常规任务进度表
* @var null
*/
protected $userTaskServ = null;
/**
* 用户
* @var null
*/
protected $userServ = null;
public function index()
{
set_time_limit(0);
\Think\Log::record('开始自动提醒' . "\n");
$this->customtaskServ = new CustomtaskService();
$this->customtaskRightServ = new CustomtaskRightService();
$this->userTaskServ = new UserTaskService();
// 查询在 任务时间范围内 的任务
$taskTotal = $this->customtaskServ->countReminder([
// 在任务时间范围内
'start_time <= ?' => MILLI_TIME,
'end_time >= ?' => MILLI_TIME,
// 任务进行中
'task_status' => CustomtaskModel::TASK_STATUS_ING,
// 开启了定时提醒
'is_auto_remind' => CustomtaskModel::IS_AUTO_REMIND_TRUE
]);
\Think\Log::record("\n共有 {$taskTotal} 任务\n", Log::INFO);
// 翻页处理 防止单次查询出来过多 内存溢出
$time = ceil($taskTotal / self::DEAL_TASK_PAGE_LIMIT);
for ($page = 1; $page <= $time; $page ++) {
\Think\Log::record("\n处理第 {$page} 页\n", Log::INFO);
try {
$this->dealPageTask($page);
} catch (\Exception $e) {
\Think\Log::record("\n处理第 {$page} 页时发生错误\n" . $e->getMessage());
}
\Think\Log::record("\n结束 处理第 {$page} 页\n", Log::INFO);
}
return true;
}
/**
* 批量处理任务提醒
* @param $page
* @return bool
*/
private function dealPageTask($page)
{
// 查询任务
list($page, $limit) = page_limit($page, self::DEAL_TASK_PAGE_LIMIT);
$taskList = $this->customtaskServ->listReminder([
// 在任务时间范围内
'start_time <= ?' => MILLI_TIME,
'end_time >= ?' => MILLI_TIME,
// 任务进行中
'task_status' => CustomtaskModel::TASK_STATUS_ING,
// 开启了定时提醒
'is_auto_remind' => CustomtaskModel::IS_AUTO_REMIND_TRUE
], [$page, $limit], [],
// 取用到的字段
implode(
',',
[
'customtask_id',
'interval_time',
'reminder_time',
'start_time',
'end_time',
'domain',
'task_name',
]
)
);
// 遍历每个任务
foreach ($taskList as $item) {
// 提醒周期时间长度
$intervalTime = $item['interval_time'] * 3600 * 1000;
// 提醒时间点
$reminderTime =
// 如果没有最近一次提醒时间 那就从任务开始时间加上周期时间 (小时 -> 毫秒)
(empty($item['reminder_time']) ? $item['start_time'] : $item['reminder_time'])
+ $intervalTime;
// 如果当前时间 和 上一次提醒时间 相差超过一个提醒周期
if (MILLI_TIME > $reminderTime) {
// 把周期时间补上
$reminderTime += ceil((MILLI_TIME - $reminderTime) / ($intervalTime)) * $intervalTime;
}
// 在 Crontab 执行时间区间 (当前时间 到 当前时间加上 Crontab 执行区间时间 为范围)
$inReminderRange = ($reminderTime >= MILLI_TIME) &&
// (分钟 -> 毫秒)
($reminderTime <= (MILLI_TIME + self::CRONTAB_CYCLE_TIMES * 60 * 1000));
// 提醒时间点不在 此次提醒点
if (!$inReminderRange) {
continue;
}
// 初始化 SDK
$config = array(
'apiUrl' => cfg('UC_APIURL'),
'enumber' => $item['domain'],
'pluginIdentifier' => 'yuanquan',
'thirdIdentifier' => cfg('SDK_THIRD_IDENTIFIER'),
'logPath' => RUNTIME_PATH . '/Logs/VcySDK/',
'apiSecret' => cfg('API_SECRET'),
'apiSigExpire' => cfg('API_SIG_EXPIRE'),
'fileConvertApiUrl' => cfg('FILE_CONVERT_API_URL')
);
$service = &Service::instance();
$service->initSdk($config);
$this->messageSdk = new Message($service);
$this->userServ = new Member($service);
// 处理单个任务提醒
try {
$this->dealSingleTask($item);
} catch (\Exception $e) {
\Think\Log::record("\n处理ID: {$item['customtask_id']} 任务提醒时发生错误\n" . $e->getMessage());
}
// 更新提醒时间
$this->customtaskServ->updateWithOutDomain($item['customtask_id'], ['reminder_time' => $reminderTime]);
}
return true;
}
/**
* 处理单个任务提醒
* @param $task
* @return bool
*/
private function dealSingleTask($task)
{
// 格式化发送的信息
if (mb_strlen($task['task_name']) > 20) {
$task['task_name'] = mb_substr($task['task_name'], 0, 20) . '...';
}
$task['start_time'] = rgmdate($task['start_time'], 'Y-m-d H:i');
$task['end_time'] = rgmdate($task['end_time'], 'Y-m-d H:i');
$msg = [
'articles' => [[
'title' => cfg('NOTICE_PREFIX', null, '') . '您还有一个任务未完成,赶紧处理下吧',
'description' => "任务名称:{$task['task_name']}\n任务时间:{$task['start_time']}~{$task['end_time']}",
'url' => oaUrl(
'Frontend/Index/Detail/Index',
['customtask_id' => $task['customtask_id']],
$task['domain']
),
]]
];
// 权限对象
$rightList = $this->customtaskRightServ->listWithOutDomian(['customtask_id' => $task['customtask_id']]);
// 完成人员
$completeList = $this->userTaskServ->listWithOutDomian([
'customtask_id' => $task['customtask_id'],
'complete_status' => UserTaskModel::COMPLETE_STATUS_OVER
], [], [], 'uid');
$completeList = empty($completeList) ? [] : array_column($completeList, 'uid');
// 如果全公司
if (in_array(self::RIGHT_ALL, array_column($rightList, 'obj_type'))) {
// 如果有完成的人员
if (!empty($completeList)) {
// 则过滤
$userResult = $this->userServ->listAll([], 1, self::DEAL_MEMBER_LIMIT);
$userList = array_column($userResult['list'], 'memUid');
// 去掉已经完成的人员
$msg['toUser'] = array_diff($userList, $completeList);
$this->sendNews($msg);
// 继续剩下的页数
$times = ceil($userResult['total'] / self::DEAL_MEMBER_LIMIT);
for ($i = 2; $i <= $times; $i ++) {
$userResult = $this->userServ->listAll([], $i, self::DEAL_MEMBER_LIMIT);
$userList = array_column($userResult['list'], 'memUid');
// 去掉已经完成的人员
$msg['toUser'] = array_diff($userList, $completeList);
$this->sendNews($msg);
}
} else {
// 没有就发送
$msg['toUser'] = '@all';
$this->sendNews($msg);
}
return true;
} else {
$targeList = [];
// 目标
$targeConds = [];
foreach ($rightList as $item) {
switch ($item['obj_type']) {
// 部门
case CustomtaskRightModel::OBJ_TYPE_DEP:
$targeConds['dpIdList'][] = $item['obj_id'];
// 获取子部门
$targeConds['departmentChildrenFlag'] = 1;
break;
// 人员
case CustomtaskRightModel::OBJ_TYPE_MEMBER:
$targeConds['memUids'][] = $item['obj_id'];
break;
// 职位
case CustomtaskRightModel::OBJ_TYPE_JOB:
$targeConds['jobIdList'][] = $item['obj_id'];
break;
// 角色
case CustomtaskRightModel::OBJ_TYPE_ROLE:
$targeConds['roleIdList'][] = $item['obj_id'];
break;
default:
break;
}
}
if (empty($targeConds)) {
return true;
}
// 获取人员列表 (第一页查询)
$userResult = $this->userServ->listAll($targeConds, 1, self::DEAL_MEMBER_LIMIT);
$userList = array_column($userResult['list'], 'memUid');
// 合并上 设置里的 人员
$userList = array_merge($userList, $targeList);
// 去掉已经完成的人员
$msg['toUser'] = array_diff($userList, $completeList);
$this->sendNews($msg);
// 发完了
if ($userResult['total'] <= self::DEAL_MEMBER_LIMIT) {
return true;
}
// 已经发送过的人员
$completeList = array_merge($completeList, $targeList);
// 继续剩下的页数
$times = ceil($userResult['total'] / self::DEAL_MEMBER_LIMIT);
for ($i = 2; $i <= $times; $i ++) {
$userResult = $this->userServ->listAll($targeConds, $i, self::DEAL_MEMBER_LIMIT);
$userList = array_column($userResult['list'], 'memUid');
// 去掉已经完成的人员
$msg['toUser'] = array_diff($userList, $completeList);
$this->sendNews($msg);
}
}
return true;
}
/**
* 发送消息
* @param $msg
* @return bool
*/
private function sendNews($msg)
{
$msg['toUser'] = implode('|', $msg['toUser']);
try {
$this->messageSdk->sendNews($msg);
} catch (\Exception $e) {
\Think\Log::record('发送消息出错:::' . $e->getMessage());
}
return true;
}
}