SaveController.class.php
13.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 17/7/25
* Time: 11:57
*/
namespace Apicp\Controller\Customtask;
use Com\PackageValidate;
use Common\Common\AttachOperation;
use Common\Common\Constant;
use Common\Common\TaskHelper;
use Common\Model\CustomtaskModel;
use Common\Service\CustomtaskContentService;
use Common\Service\CustomtaskCronService;
use Common\Service\CustomtaskRightService;
use Common\Service\CustomtaskService;
class SaveController extends \Apicp\Controller\AbstractController
{
/**
* Save
* @author tangxingguo
* @desc 保存常规任务接口
* @param Int customtask_id 常规任务ID(新增为0)
* @param String task_name:true 常规任务名(max:20)
* @param Int start_time:true 任务开始时间
* @param Int end_time:true 任务结束时间
* @param Array right:true 权限数据
* @param Int right.is_all 是否全公司(1=否;2=是)
* @param Array right.uids 人员ID
* @param Array right.dp_ids 部门ID
* @param Array right.tag_ids 标签ID
* @param Array right.job_ids 岗位ID
* @param Array right.role_ids 角色ID
* @param String description 任务描述
* @param Array content:true 任务内容
* @param String content[].app 应用(news=新闻;course=课程;exam=考试;activity=活动;questionnaire=调研)
* @param Int content[].app_data_id 数据ID
* @param String content[].title 标题
* @param Int content[].order 顺序
* @param Int is_reward:true 开启激励(1=不开启;2=开启)
* @param Array reward 激励数据(开启激励时必要)
* @param Array reward.type 激励类型(1=勋章;2=积分)
* @param Array reward.medal_id 勋章ID
* @param Array reward.integral 积分值
* @param Int is_notice:true 发布消息提醒(1=不提醒;2=提醒)
* @param Int task_type:true 任务状态(1=草稿;2=发布)
* @param Int is_auto_remind:true 是否自动提醒(1=否;2=是)
* @param Int interval_time 提醒间隔时间(单位:小时)
*/
public function Index_post()
{
// 验证规则
$rules = [
'customtask_id' => 'require|integer',
'task_name' => 'require|max:20',
'start_time' => 'require|integer',
'end_time' => 'require|integer',
'right' => 'require|array',
'content' => 'require|array',
'reward' => 'array',
'is_reward' => 'require|integer|in:1,2',
'is_notice' => 'require|integer|in:1,2',
'task_type' => 'require|integer|in:1,2',
'is_auto_remind' => 'require|integer|in:1,2',
'interval_time' => 'integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
$postData['description'] = I('post.description', '', 'trim');
// 编辑任务,必须任务状态为草稿
$customtaskServ = new CustomtaskService();
if ($postData['customtask_id'] > 0) {
$customtaskInfo = $customtaskServ->get($postData['customtask_id']);
if (empty($customtaskInfo)) {
E('_ERR_CUSTOMTASK_NOT_FOUND');
}
if (!in_array($customtaskInfo['task_status'], [Constant::CUSTOMTASK_STATUS_DRAFT, Constant::CUSTOMTASK_STATUS_UNSTART])) {
$errorMsg = [
Constant::CUSTOMTASK_STATUS_PROCESS => '任务已发布!',
Constant::CUSTOMTASK_STATUS_END => '任务已结束!',
Constant::CUSTOMTASK_STATUS_STOP => '任务已终止!',
];
E($errorMsg[$customtaskInfo['task_status']]);
}
}
// 数据格式化
list($sqlData, $right, $contents) = $this->_formatPostData($postData);
// 获取任务描述中的附件
$attach_serv = new AttachOperation();
$attach_ids = $attach_serv->match_at_ids($sqlData['description']);
// 保存任务草稿
if ($postData['customtask_id'] > 0) {
$customtaskId = $postData['customtask_id'];
$customtaskServ->update($customtaskId, $sqlData);
// 更新附件操作
$attach_serv->update_attach(
APP_DIR,
'customtask',
$customtaskId,
['attach_ids' => $attach_ids]
);
} else {
$customtaskId = $customtaskServ->insert($sqlData);
// 新增附件操作
$attach_serv->insert_attach(
APP_DIR,
'customtask',
$customtaskId,
['attach_ids' => $attach_ids]
);
}
// 保存任务内容
$this->_saveContents($contents, $customtaskId);
// 保存权限
$rightServ = new CustomtaskRightService();
$rightServ->saveData(['customtask_id' => $customtaskId], $right);
// 检查任务内容是否被删除
$ContentServ = new CustomtaskContentService();
list($dels) = $ContentServ->contentStatus($customtaskId);
if (!empty($dels)) {
// 修改状态为草稿
$customtaskServ->update($customtaskId, ['task_status' => Constant::CUSTOMTASK_STATUS_DRAFT]);
// 返回被删除项
$titles = implode(',', array_column($dels, 'title'));
E($titles . '被删除');
}
$cronServ = new CustomtaskCronService();
$customtask = $customtaskServ->get($customtaskId);
if ($postData['task_type'] == Constant::CUSTOMTASK_SAVE_SEND) {
$startTime = rgmdate($postData['start_time'], 'Y-m-d H:i');
$nowTime = rgmdate(MILLI_TIME, 'Y-m-d H:i');
if ($startTime <= $nowTime) {
// 任任务开始时间小于当前时间=立即发布
$taskHelper = new TaskHelper();
$taskHelper->sendCustomtask($customtaskId);
} elseif ($startTime > $nowTime) {
// 任务开始时间大于当前时间=创建发布计划任务
$cronServ->sendCustomtask($customtask);
}
}
// 终止任务
$cronServ->stopCustomtask($customtask);
}
/**
* @desc 格式化数据
* @author tangxingguo
* @param array $postData 用户提交的数据
* @return array
*/
private function _formatPostData($postData)
{
$user = $this->_login->user;
$sqlData = [
'task_name' => $postData['task_name'],
'start_time' => $postData['start_time'],
'end_time' => $postData['end_time'],
'description' => $postData['description'],
'is_notice' => $postData['is_notice'],
'is_reward' => $postData['is_reward'],
'ea_id' => $user['eaId'],
'ea_name' => $user['eaRealname'],
'update_time' => MILLI_TIME,
'is_auto_remind' => isset($postData['is_auto_remind']) ? $postData['is_auto_remind'] :
CustomtaskModel::IS_AUTO_REMIND_FALSE,
'interval_time' => isset($postData['interval_time']) ? $postData['interval_time'] : 0,
];
// 结束时间需大于当前时间,结束时间需大于开始时间
if ($postData['end_time'] <= MILLI_TIME || $postData['start_time'] >= $postData['end_time']) {
E('_ERR_DAILYTASK_TIME_SCOPE_ERROR');
}
// 权限数据校验
$rightServ = new CustomtaskRightService();
$right = $rightServ->formatPostData($postData['right']);
if (empty($right)) {
E('_ERR_DAILYTASK_RIGHT_IS_EMPTY');
}
// 参与人数
$uids = $rightServ->getUidsByRight($right);
$sqlData['user_total'] = count($uids);
$right = $postData['right'];
// 任务内容校验
foreach ($postData['content'] as $k => $v) {
// 检查字段是否缺失
if (!isset($v['app'], $v['app_data_id'], $v['title'], $v['order'])) {
E('_ERR_DAILYTASK_CONTENT_KEY_DEFECT');
}
}
// 验证间隔时间
if ($postData['is_auto_remind'] == Constant::CUSTOMTASK_AUTO_REMIND_TRUE && $postData['interval_time'] <= 0) {
E('_ERR_INTERVAL_TIME_ERROR');
}
// 过滤字段
$keys = ['app', 'app_data_id', 'title', 'order'];
$contents = array_intersect_key_reserved($postData['content'], $keys);
// 激励校验
if ($postData['is_reward'] == Constant::CUSTOMTASK_REWARD_OPEN) {
$types = [Constant::REWARD_TYPE_MEDAL, Constant::REWARD_TYPE_INTEGRAL];
if (!isset($postData['reward']['type']) && !in_array($postData['reward']['type'], $types)) {
// 缺少数据
E('_ERR_DAILYTASK_REWARD_FORMAT_ERROR');
} elseif ($postData['reward']['type'] == Constant::REWARD_TYPE_INTEGRAL) {
// 积分值校验
if (!isset($postData['reward']['integral']) || strlen($postData['reward']['integral']) > 6 || $postData['reward']['integral'] < 1) {
E('_ERR_DAILYTASK_REWARD_INTEGRAL_ERROR');
}
} elseif ($postData['reward']['type'] == Constant::REWARD_TYPE_MEDAL && !isset($postData['reward']['medal_id'])) {
// 勋章
E('_ERR_DAILYTASK_MEDAL_ID_IS_EMPTY');
}
$sqlData['reward_setting'] = serialize($postData['reward']);
} else {
// 编辑的时候,如果激励关闭,清空激励设置
$sqlData['reward_setting'] = '';
}
// 任务状态
if ($postData['task_type'] == Constant::CUSTOMTASK_SAVE_DRAFT) {
// 草稿
$sqlData['task_status'] = Constant::CUSTOMTASK_STATUS_DRAFT;
} elseif ($postData['task_type'] == Constant::CUSTOMTASK_SAVE_SEND) {
// 未开始
$sqlData['task_status'] = Constant::CUSTOMTASK_STATUS_UNSTART;
}
return [$sqlData, $right, $contents];
}
/**
* @desc 保存任务内容
* @author tangxingguo
* @param array $postContents 用户提交的任务内容
* @param $customtaskId 任务ID
*/
private function _saveContents($postContents, $customtaskId)
{
// 添加任务ID字段
foreach ($postContents as $k => $v) {
$postContents[$k]['customtask_id'] = $customtaskId;
}
// 数据库数据
$contentServ = new CustomtaskContentService();
$dbContents = $contentServ->list_by_conds(['customtask_id' => $customtaskId]);
if (empty($dbContents)) {
// 数据库内没有历史数据,全部添加
$contentServ->insert_all($postContents);
return;
}
// 数据分组
$dbGroup = $this->groupDataId($dbContents);
$postGroup = $this->groupDataId($postContents);
// 应用集合
$apps = [
Constant::APP_NEWS,
Constant::APP_COURSE,
Constant::APP_EXAM,
Constant::APP_ACTIVITY,
Constant::APP_QUESTIONNAIRE,
];
// 数据对比
foreach ($apps as $v) {
$dbGroup[$v] = isset($dbGroup[$v]) ? $dbGroup[$v] : [];
$postGroup[$v] = isset($postGroup[$v]) ? $postGroup[$v] : [];
// 要删除的数据
if (!empty(array_diff($dbGroup[$v], $postGroup[$v]))) {
$delDataIds[$v] = array_diff($dbGroup[$v], $postGroup[$v]);
}
// 要新增的数据
if (!empty(array_diff($postGroup[$v], $dbGroup[$v]))) {
$addDataIds[$v] = array_diff($postGroup[$v], $dbGroup[$v]);
}
}
// 删除数据
if (!empty($delDataIds)) {
foreach ($delDataIds as $app => $ids) {
foreach ($ids as $id) {
$contentServ->delete_by_conds([
'app_data_id' => $id,
'app' => $app,
'customtask_id' => $customtaskId,
]);
}
}
}
// 插入数据
if (!empty($addDataIds)) {
$addContents = [];
foreach ($addDataIds as $app => $ids) {
foreach ($ids as $id) {
foreach ($postContents as $v) {
if ($v['app'] == $app && $v['app_data_id'] == $id) {
$addContents[] = $v;
break;
}
}
}
}
if (!empty($addContents)) {
$contentServ->insert_all($addContents);
}
}
}
/**
* @desc 将任务内容数据按app分组
* @author tangxingguo
* @param array $contentList 任务内容数据
* @return array|bool
*/
private function groupDataId($contentList)
{
if (empty($contentList) || !is_array($contentList)) {
return false;
}
$dataIds = [];
// 分组
foreach ($contentList as $v) {
switch ($v['app']) {
case Constant::APP_NEWS:
$dataIds[Constant::APP_NEWS][] = $v['app_data_id'];
break;
case Constant::APP_COURSE:
$dataIds[Constant::APP_COURSE][] = $v['app_data_id'];
break;
case Constant::APP_EXAM:
$dataIds[Constant::APP_EXAM][] = $v['app_data_id'];
break;
case Constant::APP_ACTIVITY:
$dataIds[Constant::APP_ACTIVITY][] = $v['app_data_id'];
break;
case Constant::APP_QUESTIONNAIRE:
$dataIds[Constant::APP_QUESTIONNAIRE][] = $v['app_data_id'];
break;
}
}
return $dataIds;
}
}