TaskHelper.class.php
49 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 2017/7/26
* Time: 15:21
*/
namespace Common\Common;
use Think\Log;
use VcySDK\Service;
use VcySDK\Cron;
use Common\Service\CustomtaskService;
use Common\Service\CustomtaskRightService;
use Common\Service\CustomtaskContentService;
use Common\Service\CustomtaskCronService;
use Common\Service\UserTaskService;
use Common\Service\UserContentService;
use Common\Service\UserActionService;
use Common\Service\UserRewardService;
use Common\Service\UserNoticeService;
class TaskHelper
{
/**
* 实例化
* @author zhonglei
* @return TaskHelper
*/
public static function &instance()
{
static $instance;
if (is_null($instance)) {
$instance = new self();
}
return $instance;
}
/**
* 将常规任务数据格式化为消息通知所需格式
* @author zhonglei
* @param array $customtask 常规任务数据
* @return array
*/
public function formatCustomtaskForMsg($customtask)
{
if (!is_array($customtask) || empty($customtask)) {
return [];
}
// 标题取前20个字符
if (mb_strlen($customtask['task_name']) > 20) {
$customtask['task_name'] = mb_substr($customtask['task_name'], 0, 20) . '...';
}
// 格式化任务开始、结束时间、终止时间
$customtask['start_time'] = rgmdate($customtask['start_time']);
$customtask['end_time'] = rgmdate($customtask['end_time']);
$customtask['stop_time'] = rgmdate($customtask['stop_time']);
return $customtask;
}
/**
* 预发送新常规任务消息
* @author zhonglei
* @param array $customtask 常规任务数据
* @param array $rights 权限数据
* @return bool
*/
public function preSendNewCustomtaskMsg($customtask, $rights)
{
if (!is_array($customtask) || empty($customtask) || !is_array($rights) || empty($rights)) {
return false;
}
// 获取权限范围内的用户ID数组
$rightServ = new CustomtaskRightService();
$uids = $rightServ->getUidsByRight($rights);
if (empty($uids)) {
return false;
}
// 用户ID分组,优先发送第一组
$uid_groups = array_chunk($uids, Constant::MSG_USER_MAX_COUNT);
$uid_group = array_shift($uid_groups);
$this->sendNewCustomtaskMsg($customtask, $uid_group);
// 第二组发送时间(按当前时间推迟)
$send_time = MILLI_TIME + 1000 * Constant::MSG_INTERVAL_TIME;
$callback_url = oaUrl('Frontend/Callback/SendNewCustomtaskMsg/Index', ['customtask_id' => $customtask['customtask_id']]);
$cronSdk = new Cron(Service::instance());
foreach ($uid_groups as $uid_group) {
$params = [
'crRemark' => sprintf('SendNewCustomtaskMsg/%s', $customtask['customtask_id']),
'crDescription' => '定时发送新常规任务消息',
'crReqUrl' => $callback_url,
'crMethod' => 'POST',
'crParams' => json_encode(['uids' => $uid_group]),
'crCron' => rgmdate($send_time, '0 i H d m ? Y'),
'crTimes' => 1,
];
try {
// 创建计划任务
$cronSdk->add($params);
} catch (\Exception $e) {
Log::record('create cron fail: ' . $e->getMessage());
}
$send_time += 1000 * Constant::MSG_INTERVAL_TIME;
}
return true;
}
/**
* 发送新常规任务消息
* @author zhonglei
* @param array $customtask 常规任务数据
* @param array $uids 用户ID数组
* @return bool
*/
public function sendNewCustomtaskMsg($customtask, $uids)
{
if (!is_array($customtask) || empty($customtask) || !is_array($uids) || empty($uids)) {
return false;
}
// 消息通知前缀
$prefix = cfg('NOTICE_PREFIX', null, '');
$customtask = $this->formatCustomtaskForMsg($customtask);
$msgServ = &Msg::instance();
$msg_data = [
[
'title' => "{$prefix}您收到一个任务安排,请尽快完成噢",
'description' => "任务名称:{$customtask['task_name']}\n任务时间:{$customtask['start_time']}~{$customtask['end_time']}",
'url' => oaUrl('Frontend/Index/Detail/Index', ['customtask_id' => $customtask['customtask_id']]),
],
];
$msgServ->sendNews($uids, null, null, $msg_data);
return true;
}
/**
* 获取常规任务可参与、已完成、执行中、未参与人员ID数组
* @author zhonglei
* @param int $customtask_id 常规任务ID
* @return array [0 => 可参与人员ID, 1 => 已完成人员ID, 2 => 执行中人员ID, 3 => 未参与人员ID]
*/
public function getCustomtaskUserData($customtask_id)
{
$uids_all = [];
$uids_complete = [];
$uids_process = [];
$uids_unbegin = [];
// 查询条件
$conds = ['customtask_id' => $customtask_id];
// 获取权限数据
$rightServ = new CustomtaskRightService();
$right_list = $rightServ->list_by_conds($conds);
$rights = $rightServ->formatDBData($right_list);
if (empty($rights)) {
return [$uids_all, $uids_complete, $uids_process, $uids_unbegin];
}
// 获取可参与人员ID
$uids_all = $rightServ->getUidsByRight($rights);
// 获取已参与人员数据
$userTaskServ = new UserTaskService();
$join_list = $userTaskServ->list_by_conds($conds);
// 获取已完成人员ID
$complete_list = array_filter($join_list, function ($v) {
return $v['complete_status'] == Constant::USER_TASK_COMPLETE_STATUS_COMPLETE;
});
$uids_complete = array_column($complete_list, 'uid');
// 获取执行中人员ID
$uids_join = array_column($join_list, 'uid');
$uids_process = array_values(array_diff($uids_join, $uids_complete));
// 获取未参与人员ID
$uids_unbegin = array_values(array_diff($uids_all, $uids_join));
return [$uids_all, $uids_complete, $uids_process, $uids_unbegin];
}
/**
* 预发送常规任务未完成消息
* @author zhonglei
* @param array $customtask 常规任务数据
* @return bool
*/
public function preSendCustomtaskIncompleteMsg($customtask)
{
if (!is_array($customtask) || empty($customtask)) {
return false;
}
// 获取任务执行中、未参与的人员ID数组
list(,, $uids_process, $uids_unbegin) = $this->getCustomtaskUserData($customtask['customtask_id']);
$uids = array_merge($uids_process, $uids_unbegin);
if (empty($uids)) {
return false;
}
// 用户ID分组,优先发送第一组
$uid_groups = array_chunk($uids, Constant::MSG_USER_MAX_COUNT);
$uid_group = array_shift($uid_groups);
$this->sendCustomtaskIncompleteMsg($customtask, $uid_group);
// 第二组发送时间(按当前时间推迟)
$send_time = MILLI_TIME + 1000 * Constant::MSG_INTERVAL_TIME;
$callback_url = oaUrl('Frontend/Callback/SendCustomtaskIncompleteMsg/Index', ['customtask_id' => $customtask['customtask_id']]);
$cronSdk = new Cron(Service::instance());
foreach ($uid_groups as $uid_group) {
$params = [
'crRemark' => sprintf('SendCustomtaskIncompleteMsg/%s', $customtask['customtask_id']),
'crDescription' => '定时发送常规任务未完成消息',
'crReqUrl' => $callback_url,
'crMethod' => 'POST',
'crParams' => json_encode(['uids' => $uid_group]),
'crCron' => rgmdate($send_time, '0 i H d m ? Y'),
'crTimes' => 1,
];
try {
// 创建计划任务
$cronSdk->add($params);
} catch (\Exception $e) {
Log::record('create cron fail: ' . $e->getMessage());
}
$send_time += 1000 * Constant::MSG_INTERVAL_TIME;
}
return true;
}
/**
* 发送常规任务未完成消息
* @author zhonglei
* @param array $customtask 常规任务数据
* @param array $uids 用户ID数组
* @return bool
*/
public function sendCustomtaskIncompleteMsg($customtask, $uids)
{
if (!is_array($customtask) || empty($customtask) || !is_array($uids) || empty($uids)) {
return false;
}
// 消息通知前缀
$prefix = cfg('NOTICE_PREFIX', null, '');
$customtask = $this->formatCustomtaskForMsg($customtask);
$msgServ = &Msg::instance();
$msg_data = [
[
'title' => "{$prefix}您还有一个任务未完成,赶紧处理下吧",
'description' => "任务名称:{$customtask['task_name']}\n任务时间:{$customtask['start_time']}~{$customtask['end_time']}",
'url' => oaUrl('Frontend/Index/Detail/Index', ['customtask_id' => $customtask['customtask_id']]),
],
];
$msgServ->sendNews($uids, null, null, $msg_data);
return true;
}
/**
* 预发送常规任务终止消息
* @author zhonglei
* @param array $customtask 常规任务数据
* @return bool
*/
public function preSendCustomtaskStopMsg($customtask)
{
if (!is_array($customtask) || empty($customtask)) {
return false;
}
// 获取任务执行中、未参与的人员ID数组
list(,, $uids_process, $uids_unbegin) = $this->getCustomtaskUserData($customtask['customtask_id']);
$uids = array_merge($uids_process, $uids_unbegin);
if (empty($uids)) {
return true;
}
// 用户ID分组,优先发送第一组
$uid_groups = array_chunk($uids, Constant::MSG_USER_MAX_COUNT);
$uid_group = array_shift($uid_groups);
$this->sendCustomtaskStopMsg($customtask, $uid_group);
// 第二组发送时间(按当前时间推迟)
$send_time = MILLI_TIME + 1000 * Constant::MSG_INTERVAL_TIME;
$callback_url = oaUrl('Frontend/Callback/SendCustomtaskStopMsg/Index', ['customtask_id' => $customtask['customtask_id']]);
$cronSdk = new Cron(Service::instance());
foreach ($uid_groups as $uid_group) {
$params = [
'crRemark' => sprintf('SendCustomtaskStopMsg/%s', $customtask['customtask_id']),
'crDescription' => '定时发送常规任务终止消息',
'crReqUrl' => $callback_url,
'crMethod' => 'POST',
'crParams' => json_encode(['uids' => $uid_group]),
'crCron' => rgmdate($send_time, '0 i H d m ? Y'),
'crTimes' => 1,
];
try {
// 创建计划任务
$cronSdk->add($params);
} catch (\Exception $e) {
Log::record('create cron fail: ' . $e->getMessage());
}
$send_time += 1000 * Constant::MSG_INTERVAL_TIME;
}
return true;
}
/**
* 发送常规任务终止消息
* @author zhonglei
* @param array $customtask 常规任务数据
* @param array $uids 用户ID数组
* @return bool
*/
public function sendCustomtaskStopMsg($customtask, $uids)
{
if (!is_array($customtask) || empty($customtask) || !is_array($uids) || empty($uids)) {
return false;
}
// 消息通知前缀
$prefix = cfg('NOTICE_PREFIX', null, '');
$customtask = $this->formatCustomtaskForMsg($customtask);
$msgServ = &Msg::instance();
$msg_data = [
[
'title' => "{$prefix}您有一个未完成的任务已提前终止",
'description' => "任务名称:{$customtask['task_name']}\n终止原因:{$customtask['stop_desc']}\n操作时间:{$customtask['stop_time']}",
'url' => oaUrl('Frontend/Index/Detail/Index', ['customtask_id' => $customtask['customtask_id']]),
],
];
$msgServ->sendNews($uids, null, null, $msg_data);
return true;
}
/**
* @desc 发布常规任务
* @author tangxingguo
* @param int $customtaskId 常规任务ID
* @return bool
*/
public function sendCustomtask($customtaskId)
{
// 任务信息
$customtaskServ = new CustomtaskService();
$customtaskInfo = $customtaskServ->get($customtaskId);
if (empty($customtaskInfo) && !in_array($customtaskInfo['task_status'], [Constant::CUSTOMTASK_STATUS_DRAFT, Constant::CUSTOMTASK_STATUS_UNSTART])) {
return false;
}
// 检查任务内容是否被删除
$ContentServ = new CustomtaskContentService();
list($dels) = $ContentServ->contentStatus($customtaskId);
if (!empty($dels)) {
$this->stopCustomtask($customtaskInfo);
return false;
}
// 任务状态改为进行中
$customtaskServ->update($customtaskId, ['task_status' => Constant::CUSTOMTASK_STATUS_PROCESS]);
// 发送消息
if ($customtaskInfo['is_notice'] == Constant::NOTICE_SEND_STATUS_OPEN) {
$rightServ = new CustomtaskRightService();
$right = $rightServ->list_by_conds(['customtask_id' => $customtaskId]);
if (empty($right)) {
return false;
}
$right = $rightServ->formatDBData($right);
$this->preSendNewCustomtaskMsg($customtaskInfo, $right);
}
// 更新参与人数
list($userTotal) = $this->getCustomtaskUserData($customtaskId);
$customtaskServ->update($customtaskId, ['user_total' => count($userTotal), 'send_time' => MILLI_TIME]);
return true;
}
/**
* 根据搜索条件获取常规任务人员列表(调用前先检查常规任务是否存在)
* @author zhonglei
* @param array $conds 搜索条件
* + int customtask_id 常规任务ID
* + array uids 人员ID数组
* + array dp_ids 部门ID数组
* + array tag_ids 标签ID数组
* + array job_ids 岗位ID数组
* + array role_ids 角色ID数组
* + string username 姓名(模糊搜索)
* + int status 任务完成情况(1=未参与;2=执行中;3=已完成)
* @param int $page 当前页(需配合limit使用,默认为1)
* @param int $limit 每页数据总数(配合page使用,默认为0,则不分页)
* @return array
* + int page 当前页
* + int limit 每页数据总数
* + int total 数据总数
* + array list 列表数据
* + string uid 用户ID
* + string username 用户姓名
* + array dp_names 部门名称数组
* + string job 岗位
* + string role 角色
* + int progress 任务进度
* + int complete_status 任务状态(1=未参与;2=执行中;3=已完成;)
* + int update_time 更新时间
*/
public function listCustomtaskUser($conds, $page = 1, $limit = 0)
{
$result = [
'page' => $page,
'limit' => $limit,
'total' => 0,
'list' => [],
];
if (!is_array($conds) || empty($conds) || !isset($conds['customtask_id'], $conds['status'])) {
return $result;
}
// 取出权限数据
$right = array_intersect_key_reserved($conds, ['uids', 'dp_ids', 'tag_ids', 'job_ids', 'role_ids'], true);
$right_uids = [];
// 根据权限数据取出 uid 数组
if (!empty($right)) {
$rightServ = new CustomtaskRightService();
$rights = $rightServ->formatPostData($right);
$right_uids = $rightServ->getUidsByRight($rights);
// 权限数据没有对应的人员
if (empty($right_uids)) {
return $result;
}
}
$userServ = &User::instance();
// 根据权限数据、姓名取出 uid 数组
if (isset($conds['username'])) {
$user_conds = ['memUsername' => $conds['username']];
if (!empty($right_uids)) {
$user_conds['memUids'] = $right_uids;
}
$list = $userServ->listAll($user_conds);
// 权限数据、姓名没有对应的人员
if (empty($list)) {
return $result;
}
$right_uids = array_column($list, 'memUid');
}
list($uids_all, $uids_complete, $uids_process, $uids_unbegin) = $this->getCustomtaskUserData($conds['customtask_id']);
// 没有参与人员
if (empty($uids_all)) {
return $result;
}
$search_conds = [];
// 任务完成情况
switch ($conds['status']) {
// 未参与
case Constant::USER_TASK_COMPLETE_STATUS_UNSTART:
if (!empty($right_uids)) {
$search_conds['memUids'] = array_values(array_intersect($right_uids, $uids_unbegin));
} else {
$search_conds['memUids'] = $uids_unbegin;
}
// 没有符合条件的用户
if (empty($search_conds['memUids'])) {
return $result;
}
break;
// 执行中
case Constant::USER_TASK_COMPLETE_STATUS_PROCESS:
if (!empty($right_uids)) {
$search_conds['uid'] = array_values(array_intersect($right_uids, $uids_process));
// 没有符合条件的用户
if (empty($search_conds['uid'])) {
return $result;
}
}
$search_conds['complete_status'] = Constant::USER_TASK_COMPLETE_STATUS_PROCESS;
break;
// 已完成
case Constant::USER_TASK_COMPLETE_STATUS_COMPLETE:
if (!empty($right_uids)) {
$search_conds['uid'] = array_values(array_intersect($right_uids, $uids_complete));
// 没有符合条件的用户
if (empty($search_conds['uid'])) {
return $result;
}
}
$search_conds['complete_status'] = Constant::USER_TASK_COMPLETE_STATUS_COMPLETE;
break;
}
// 获取未参与人员数据
if ($conds['status'] == Constant::USER_TASK_COMPLETE_STATUS_UNSTART) {
// 不分页
if ($limit == 0) {
$user_list = $userServ->listAll($search_conds);
$result['total'] = count($user_list);
// 分页
} else {
$list = $userServ->listByConds($search_conds, $page, $limit);
$user_list = $list['list'];
$result['total'] = $list['total'];
}
foreach ($user_list as $k => $v) {
$result['list'][] = [
'uid' => $v['memUid'],
'mobile' => $v['memMobile'],
'username' => $v['memUsername'],
'dp_names' => array_column($v['dpName'], 'dpName'),
'job' => $v['memJob'],
'role' => $v['memRole'],
'progress' => 0,
'complete_status' => Constant::USER_TASK_COMPLETE_STATUS_UNSTART,
'update_time' => 0,
// 序列号
'serial_number' => ($k + 1) + ($page - 1) * $limit
];
}
// 获取已完成或执行中人员数据
} else {
$userTaskServ = new UserTaskService();
$search_conds['customtask_id'] = $conds['customtask_id'];
$order_option = ['progress' => 'desc', 'update_time' => 'asc'];
// 不分页
if ($limit == 0) {
$result['list'] = $userTaskServ->list_by_conds($search_conds, null, $order_option);
$result['total'] = count($result['list']);
// 分页
} else {
$result['total'] = $userTaskServ->count_by_conds($search_conds);
if ($result['total'] > 0) {
list($start, $perpage) = page_limit($page, $limit);
$result['list'] = $userTaskServ->list_by_conds($search_conds, [$start, $perpage], $order_option);
}
}
if (!empty($result['list'])) {
$uids = array_column($result['list'], 'uid');
$user_list = $userServ->listAll(['memUids' => $uids]);
$users = array_combine_by_key($user_list, 'memUid');
// 取人员姓名、部门、岗位、角色
foreach ($result['list'] as $k => $v) {
$uid = $v['uid'];
if (isset($users[$uid])) {
$user = $users[$uid];
$result['list'][$k]['username'] = $user['memUsername'];
$result['list'][$k]['dp_names'] = array_column($user['dpName'], 'dpName');
$result['list'][$k]['job'] = $user['memJob'];
$result['list'][$k]['role'] = $user['memRole'];
} else {
$result['list'][$k]['dp_names'] = [];
$result['list'][$k]['job'] = '';
$result['list'][$k]['role'] = '';
}
$result['list'][$k]['mobile'] = $user['memMobile'];
// 序列号
$result['list'][$k]['serial_number'] = ($k + 1) + ($page - 1) * $limit;
}
}
}
$result['total'] = intval($result['total']);
return $result;
}
/**
* 终止常规任务(人工终止请提前设置好常规任务数据)
* @author zhonglei
* @param array $customtask 常规任务数据
* @return bool
*/
public function stopCustomtask($customtask)
{
if (!is_array($customtask) || empty($customtask)) {
return false;
}
// 允许的状态
$allow_status = [
// 未开始
Constant::CUSTOMTASK_STATUS_UNSTART,
// 进行中
Constant::CUSTOMTASK_STATUS_PROCESS,
];
if (!in_array($customtask['task_status'], $allow_status)) {
return false;
}
// 终止原因
if (empty($customtask['stop_desc'])) {
$customtask['stop_ea_name'] = '系统';
$customtask['stop_desc'] = '任务下的内容已被管理员删除,系统自动终止任务';
}
// 终止时间
if ($customtask['stop_time'] == 0) {
$customtask['stop_time'] = MILLI_TIME;
}
$customtask_id = $customtask['customtask_id'];
$keys = ['stop_ea_id', 'stop_ea_name', 'stop_desc', 'stop_time'];
$data = array_intersect_key_reserved($customtask, $keys, true);
$data['task_status'] = Constant::CUSTOMTASK_STATUS_STOP;
// 更新任务数据
$taskServ = new CustomtaskService();
$taskServ->update($customtask_id, $data);
// 删除计划任务
$taskCronServ = new CustomtaskCronService();
$taskCronServ->deleteCron($customtask_id);
// 任务进行中
if ($customtask['task_status'] == Constant::CUSTOMTASK_STATUS_PROCESS) {
// 更新用户任务状态
$userTaskServ = new UserTaskService();
$userTaskServ->update_by_conds([
'customtask_id' => $customtask_id,
'task_status' => Constant::CUSTOMTASK_STATUS_PROCESS,
], [
'task_status' => Constant::CUSTOMTASK_STATUS_STOP
]);
// 发送任务终止消息
$this->preSendCustomtaskStopMsg($customtask);
}
return true;
}
/**
* 获取常规任务人员完成进度排名列表(不包含进度为0的人员,调用前先检查常规任务是否存在)
* @author zhonglei
* @param int customtask_id 常规任务ID
* @param int $page 当前页(需配合limit使用)
* @param int $limit 每页数据总数(配合page使用)
* @return array
* + int page 当前页
* + int limit 每页数据总数
* + int total 数据总数
* + array list 列表数据
* + string uid 用户ID
* + string username 用户姓名
* + string face 用户头像
* + int progress 任务进度
* + int update_time 更新时间
*/
public function listCustomtaskUserRank($customtask_id, $page, $limit)
{
$result = [
'page' => $page,
'limit' => $limit,
'total' => 0,
'list' => [],
];
$conds = [
'customtask_id' => $customtask_id,
'progress > ?' => 0,
'complete_status' => [
Constant::USER_TASK_COMPLETE_STATUS_PROCESS,
Constant::USER_TASK_COMPLETE_STATUS_COMPLETE,
],
];
list($start, $perpage) = page_limit($page, $limit);
$page_option = [$start, $perpage];
$order_option = ['progress' => 'desc', 'complete_time' => 'asc'];
$userTaskServ = new UserTaskService();
$list = $userTaskServ->list_by_conds($conds, $page_option, $order_option);
if (empty($list)) {
return $result;
}
$uids = array_column($list, 'uid');
$userServ = &User::instance();
// 可能有坑,User->listByUid()强行分页,单页最大数据30
$users = $userServ->listByUid($uids);
// 格式化列表数据
foreach ($list as $k => $v) {
$uid = $v['uid'];
$data = [
'uid' => $uid,
'username' => $v['username'],
'face' => '',
'progress' => $v['progress'],
'update_time' => $v['complete_time'],
];
if (isset($users[$uid])) {
$user = $users[$uid];
$data['username'] = $user['memUsername'];
$data['face'] = $user['memFace'];
}
$result['list'][] = $data;
}
$result['total'] = (int)$userTaskServ->count_by_conds($conds);
return $result;
}
/**
* 为用户构建新的常规任务
* @author zhonglei
* @param array $user 用户信息
* @param array $new_customtask_ids 新常规任务ID数组(默认为null,则构建所有常规任务)
* @return bool
*/
public function buildNewCustomtask($user, $new_customtask_ids = null)
{
if (!is_array($user) || empty($user)) {
return false;
}
$userTaskServ = new UserTaskService();
// 构建所有常规任务
if (is_null($new_customtask_ids)) {
// 获取任务权限列表
$rightServ = new CustomtaskRightService();
$right_list = $rightServ->listByUser($user);
if (empty($right_list)) {
return false;
}
// 获取可参与的进行中、已结束、已终止任务列表
$customtask_ids = array_column($right_list, 'customtask_id');
$customtaskServ = new CustomtaskService();
$customtask_list = $customtaskServ->list_by_conds([
'customtask_id' => $customtask_ids,
'task_status' => [
Constant::CUSTOMTASK_STATUS_PROCESS,
Constant::CUSTOMTASK_STATUS_END,
Constant::CUSTOMTASK_STATUS_STOP,
],
]);
if (empty($customtask_list)) {
return false;
}
// 可参与的任务ID数组
$ids_all = array_column($customtask_list, 'customtask_id');
// 获取已参与的任务列表
$usertask_list = $userTaskServ->list_by_conds(['uid' => $user['memUid']]);
// 已参与的任务ID数组
$ids_join = array_column($usertask_list, 'customtask_id');
// 计算未参与的任务
$ids_unbegin = array_diff($ids_all, $ids_join);
if (empty($ids_unbegin)) {
return false;
}
// 构建指定常规任务
} else {
$ids_unbegin = $new_customtask_ids;
}
// 获取任务内容列表
$taskcontentServ = new CustomtaskContentService();
$content_list = $taskcontentServ->list_by_conds(['customtask_id' => $ids_unbegin]);
$taskcontent_list = [];
// 格式化任务内容列表数组结构
foreach ($content_list as $v) {
$taskcontent_list[$v['customtask_id']][] = $v;
}
// 构建用户任务、任务内容数据
$usertask_data = [];
$usercontent_data = [];
foreach ($ids_unbegin as $customtask_id) {
$customtask_contents = isset($taskcontent_list[$customtask_id]) ? $taskcontent_list[$customtask_id] : [];
if (empty($customtask_contents)) {
continue;
}
$usertask_data[] = [
'uid' => $user['memUid'],
'username' => $user['memUsername'],
'customtask_id' => $customtask_id,
'update_time' => MILLI_TIME,
];
foreach ($customtask_contents as $customtask_content) {
$usercontent_data[] = [
'uid' => $user['memUid'],
'username' => $user['memUsername'],
'customtask_id' => $customtask_id,
'customtask_content_id' => $customtask_content['customtask_content_id'],
'app' => $customtask_content['app'],
'app_data_id' => $customtask_content['app_data_id'],
];
}
}
// 保存数据
if (!empty($usertask_data) && !empty($usercontent_data)) {
$userTaskServ->insert_all($usertask_data);
$usercontentServ = new UserContentService();
$usercontentServ->insert_all($usercontent_data);
return true;
}
return false;
}
/**
* 发放常规任务完成激励
* @author zhonglei
* @param array $customtask 常规任务数据
* @param array $user 用户信息
* @return bool
*/
public function sendCustomtaskReward($customtask, $user)
{
if (!is_array($customtask) || empty($customtask) || !is_array($user) || empty($user)) {
return false;
}
// 未开启激励
if ($customtask['is_reward'] == Constant::CUSTOMTASK_REWARD_CLOSE) {
return false;
}
$reward_setting = unserialize($customtask['reward_setting']);
$integralServ = &Integral::instance();
switch ($reward_setting['type']) {
// 勋章
case Constant::REWARD_TYPE_MEDAL:
// 获取勋章数据
$medal = $integralServ->getMedal($reward_setting['medal_id']);
if (empty($medal)) {
break;
}
// 发放勋章
$result = $integralServ->endowMedal($reward_setting['medal_id'], $user['memUid'], $user['memUsername']);
if ($result) {
$msgServ = &Msg::instance();
$msg_data = [
[
'title' => "恭喜您,获得【{$medal['name']}】勋章",
'description' => "获取渠道:员圈任务-{$customtask['task_name']}\n获取时间:" . rgmdate(MILLI_TIME),
'url' => oaUrl('Frontend/Index/Medal/Index'),
],
];
$msgServ->sendNews($user['memUid'], null, null, $msg_data);
}
break;
// 积分
case Constant::REWARD_TYPE_INTEGRAL:
$integralServ->asynUpdateIntegral([
'memUid' => $user['memUid'],
'irKey' => 'taskcenter_customtask_complete',
'msgIdentifier' => APP_IDENTIFIER,
'integral' => $reward_setting['integral'],
'remark' => '完成常规任务获得激励',
'businessKey' => 'task_center',
'businessAct' => 'normal_task',
]);
break;
}
return true;
}
/**
* 更新用户常规任务进度
* @author zhonglei
* @param array $user 用户信息
* @return bool
*/
public function updateCustomtaskProgress($user)
{
if (!is_array($user) || empty($user)) {
return false;
}
// 获取进行中未完成的任务列表
$userTaskServ = new UserTaskService();
$usertask_list = $userTaskServ->list_by_conds([
'uid' => $user['memUid'],
'complete_status' => Constant::USER_TASK_COMPLETE_STATUS_PROCESS,
'task_status' => Constant::CUSTOMTASK_STATUS_PROCESS,
]);
if (empty($usertask_list)) {
return false;
}
$usertasks = array_combine_by_key($usertask_list, 'customtask_id');
$customtask_ids = array_keys($usertasks);
// 获取任务内容列表
$userContentServ = new UserContentService();
$usercontent_list = $userContentServ->list_by_conds(['uid' => $user['memUid'], 'customtask_id' => $customtask_ids]);
// 格式化任务内容数据
foreach ($usercontent_list as $v) {
$customtask_id = $v['customtask_id'];
$usertasks[$customtask_id]['contents'][] = $v;
}
// 获取用户动作列表
$useractionServ = new UserActionService();
$useraction_list = $useractionServ->list_by_conds([
'uid' => $user['memUid'],
'customtask_id' => $customtask_ids,
]);
if (empty($useraction_list)) {
return false;
}
$useractions = [];
$ids_complete = [];
// 格式化动作数据
foreach ($useraction_list as $v) {
$customtask_id = $v['customtask_id'];
$app = $v['app'];
$app_data_id = $v['app_data_id'];
$useractions[$customtask_id][$app][$app_data_id] = $v['created'];
}
// 更新任务进度
foreach ($usertasks as $customtask_id => $usertask) {
$complete_count = 0;
foreach ($usertask['contents'] as $k => $content) {
if ($content['content_status'] == Constant::USER_CONTENT_STATUS_COMPLETE) {
$complete_count++;
continue;
}
$app = $content['app'];
$app_data_id = $content['app_data_id'];
// 用户动作已存在
if (isset($useractions[$customtask_id][$app][$app_data_id])) {
$complete_time = $useractions[$customtask_id][$app][$app_data_id];
// 更新内容完成状态为:已完成
$userContentServ->update($content['user_content_id'], [
'content_status' => Constant::USER_CONTENT_STATUS_COMPLETE,
'complete_time' => $complete_time,
]);
$usertask['contents'][$k]['complete_time'] = $complete_time;
$complete_count++;
}
}
// 计算任务进度,精确到小数点后两位
$progress = round($complete_count / count($usertask['contents']), 2) * 100;
if ($usertask['progress'] != $progress) {
$data = ['progress' => $progress];
if ($progress == 100) {
$data['complete_status'] = Constant::USER_TASK_COMPLETE_STATUS_COMPLETE;
$data['complete_time'] = max(array_column($usertask['contents'], 'complete_time'));
$ids_complete[] = $customtask_id;
}
$userTaskServ->update($usertask['user_task_id'], $data);
}
}
// 更新完成人数并且发放激励
if (!empty($ids_complete)) {
$customtaskServ = new CustomtaskService();
$customtask_list = $customtaskServ->list_by_conds(['customtask_id' => $ids_complete]);
foreach ($customtask_list as $customtask) {
// 更新完成人数(此出更新人数是兼容计划任务更新人数,因为有学习时长时计划任务不会更新已完成人数)
$customtaskServ->update_by_conds(
['customtask_id' => $customtask['customtask_id']],
['complete_total = complete_total + ?' => 1]
);
// 发放激励
$this->sendCustomtaskReward($customtask, $user);
}
}
return true;
}
/**
* 获取应用手机端Url
* @author zhonglei
* @param string $app 应用
* @return string
*/
public function getAppUrl($app)
{
switch ($app) {
case Constant::APP_SIGN:
$url = frontUrl('/app/page/task/sign/sign');
break;
case Constant::APP_WORKMATE:
$url = frontUrl('/app/page/workmate/workmate-home', [], 'Workmate');
break;
case Constant::APP_ANSWER:
$url = frontUrl('/app/page/answer/list/list', [], 'Answer');
break;
default:
$url = '';
}
return $url;
}
/**
* 构建每日任务列表
* @author zhonglei
* @param array $user 用户信息
* @return array
* + string app 应用
* + string app_url 应用Url
* + string rule_name 规则名称
* + string task_title 任务名称
* + int integral 积分
* + int require_total 需要完成总数
* + int complete_total 已完成总数
* + int is_get 是否已领取激励(1=未领取;2=已领取)
*/
public function buildDailytaskList($user)
{
if (!is_array($user) || empty($user)) {
return [];
}
// 获取每日任务设置
$dailytaskConfig = &DailytaskConfig::instance();
$configs = [
$dailytaskConfig->getCacheData(Constant::APP_WORKMATE),
$dailytaskConfig->getCacheData(Constant::APP_ANSWER),
];
$rule_list = [];
$action_keys = [];
// 获取已启用的任务规则、动作名称
foreach ($configs as $config) {
// 任务未开启
if ($config['is_open'] != Constant::DAILYTASK_IS_OPEN_TRUE) {
continue;
}
// 格式化任务名称
foreach ($config['rules'] as $name => $rule) {
$task_title = '';
switch ($name) {
// 员圈发表话题
case Constant::RULE_NAME_WORKMATE_CIRCLE:
$task_title = '在员圈发表%s个帖子';
break;
// 员圈添加评论
case Constant::RULE_NAME_WORKMATE_COMMENT:
$unique = isset($rule['config']['unique']) ? $rule['config']['unique'] : false;
$task_title = $unique ? '在员圈不同话题下添加%s条评论' : '在员圈添加%s条评论';
break;
// 员圈点赞
case Constant::RULE_NAME_WORKMATE_LIKE:
$unique = isset($rule['config']['unique']) ? $rule['config']['unique'] : false;
$task_title = $unique ? '在员圈对话题进行%s个点赞' : '在员圈进行%s个点赞';
break;
// 问答中心发起提问
case Constant::RULE_NAME_ANSWER_QUESTION:
$task_title = '在问答中心发起%s个提问';
break;
// 问答中心添加回答
case Constant::RULE_NAME_ANSWER_ANSWER:
$unique = isset($rule['config']['unique']) ? $rule['config']['unique'] : false;
$task_title = $unique ? '在问答中心不同问题下添加%s个回答' : '在问答中心添加%s个回答';
break;
}
$rule['app'] = $config['app'];
$rule['task_title'] = sprintf($task_title, $rule['count']);
$rule_list[$name] = $rule;
$action_keys = array_merge($action_keys, $rule['action_keys']);
}
}
if (empty($rule_list)) {
return [];
}
// 获取用户今日动作总数
$actionServ = new UserActionService();
$action_list = $actionServ->countTodayActionKeyByUid($action_keys, $user['memUid']);
$actions = array_combine_by_key($action_list, 'action_key');
$rule_names = array_keys($rule_list);
$time = rstrtotime(rgmdate(MILLI_TIME, 'Y-m-d'), 1);
// 获取用户今日激励领取数据
$rewardServ = new UserRewardService();
$reward_list = $rewardServ->list_by_conds([
'uid' => $user['memUid'],
'rule_name' => $rule_names,
'created > ?' => $time,
]);
$rewards = array_column($reward_list, 'rule_name');
$task_list = [];
// 计算任务完成、领取情况
foreach ($rule_list as $name => $rule) {
$unique = isset($rule['config']['unique']) ? $rule['config']['unique'] : false;
$complete_total = 0;
foreach ($rule['action_keys'] as $action_key) {
if (isset($actions[$action_key])) {
$complete_total += $unique ? (int)$actions[$action_key]['data_total'] : (int)$actions[$action_key]['total'];
}
}
// 格式化任务数据
$task_list[$rule['app']][] = [
'app' => $rule['app'],
'app_url' => $this->getAppUrl($rule['app']),
'rule_name' => $name,
'task_title' => $rule['task_title'],
'integral' => $rule['integral'],
'require_total' => $rule['count'],
'complete_total' => $complete_total,
'is_get' => in_array($name, $rewards) ? Constant::DAILYTASK_REWARD_GET : Constant::DAILYTASK_REWARD_UNGET,
];
}
return $task_list;
}
/**
* 领取每日任务激励
* @author zhonglei
* @param string $app 应用
* @param string $rule_name 规则名称
* @param array $user 用户信息
* @return bool
*/
public function getDailytaskReward($app, $rule_name, $user)
{
if (empty($app) || empty($rule_name) || !is_array($user) || empty($user)) {
return false;
}
$app = strtolower($app);
$rule_name = strtolower($rule_name);
// 获取每日任务设置
$dailytaskConfig = &DailytaskConfig::instance();
$configs = [
Constant::APP_WORKMATE => $dailytaskConfig->getCacheData(Constant::APP_WORKMATE),
Constant::APP_ANSWER => $dailytaskConfig->getCacheData(Constant::APP_ANSWER),
];
if (!isset($configs[$app]) || $configs[$app]['is_open'] != Constant::DAILYTASK_IS_OPEN_TRUE) {
E(L('_ERR_PLS_OPEN_DAILY_TASK', ['name' => Constant::APP_NAME[$app]]));
return false;
}
if (!isset($configs[$app]['rules'][$rule_name]) || $configs[$app]['rules'][$rule_name]['integral'] <= 0) {
return false;
}
$rule = $configs[$app]['rules'][$rule_name];
$time = rstrtotime(rgmdate(MILLI_TIME, 'Y-m-d'), 1);
// 获取用户今日激励领取数据
$rewardServ = new UserRewardService();
$count = $rewardServ->count_by_conds([
'uid' => $user['memUid'],
'rule_name' => $rule_name,
'created > ?' => $time,
]);
// 用户已领取激励
if ($count > 0) {
return false;
}
// 获取用户今日动作总数
$actionServ = new UserActionService();
$action_list = $actionServ->countTodayActionKeyByUid($rule['action_keys'], $user['memUid']);
$complete_total = 0;
foreach ($action_list as $v) {
$complete_total += $v['total'];
}
if ($complete_total < $rule['count']) {
return false;
}
$integralServ = &Integral::instance();
$integralServ->asynUpdateIntegral([
'memUid' => $user['memUid'],
'irKey' => 'taskcenter_dailytask_complete',
'msgIdentifier' => APP_IDENTIFIER,
'integral' => $rule['integral'],
'remark' => sprintf(
'完成%s%s任务',
Constant::APP_NAME[$app],
$app == 'sign' ? '签到每日' : '每日' . Constant::ACTION_NAME[$rule['action_keys'][0]]
),
'businessKey' => 'task_center',
'businessAct' => Constant::ACTION_UC_IDENTIFIER[$rule['action_keys'][0]],
]);
$rewardServ->insert([
'uid' => $user['memUid'],
'username' => $user['memUsername'],
'app' => $app,
'rule_name' => $rule_name,
'integral' => $rule['integral'],
]);
return true;
}
/**
* 发送每日任务完成消息
* @author zhonglei
* @param array $dailytask 每日任务数据
* @param string $uid 用户ID
* @return bool
*/
public function sendDailytaskCompleteMsg($dailytask, $uid)
{
if (!is_array($dailytask) || empty($dailytask) || empty($uid)) {
return false;
}
// 消息通知前缀
$prefix = cfg('NOTICE_PREFIX', null, '');
// 应用名称
$app_names = [
Constant::APP_WORKMATE => '员圈',
Constant::APP_ANSWER => '问答中心',
];
$app = $dailytask['app'];
$app_name = isset($app_names[$app]) ? $app_names[$app] : '';
$reward = "+{$dailytask['integral']}积分";
$complete_time = rgmdate(MILLI_TIME);
$msgServ = &Msg::instance();
$msg_data = [
[
'title' => "{$prefix}您已完成了{$app_name}的日常任务,快来领取奖励吧",
'description' => "任务名称:{$dailytask['task_title']}\n积分奖励:{$reward}\n完成时间:{$complete_time}",
'url' => oaUrl('Frontend/Index/TaskIndex'),
],
];
$msgServ->sendNews($uid, null, null, $msg_data);
return true;
}
/**
* 检查每日任务进度,如果完成则通知用户
* @author zhonglei
* @param array $user 用户信息
* @return bool
*/
public function checkDailytaskProgress($user)
{
if (!is_array($user) || empty($user)) {
return false;
}
// 获取任务列表
$dailytask_list = $this->buildDailytaskList($user);
if (empty($dailytask_list)) {
return false;
}
$starttime = rstrtotime(rgmdate(MILLI_TIME, 'Y-m-d'));
$notices = [];
// 获取通知列表
$noticeServ = new UserNoticeService();
$notice_list = $noticeServ->list_by_conds([
'uid' => $user['memUid'],
'created >= ?' => $starttime,
]);
// 格式化通知数据
foreach ($notice_list as $v) {
$notices[$v['app']][] = $v['rule_name'];
}
foreach ($dailytask_list as $app => $list) {
foreach ($list as $v) {
// 已领取任务奖励
if ($v['is_get'] == Constant::DAILYTASK_REWARD_GET) {
continue;
}
// 未完成任务
if ($v['complete_total'] < $v['require_total']) {
continue;
}
// 未通知用户
if (!isset($notices[$app]) || !in_array($v['rule_name'], $notices[$app])) {
$noticeServ->insert([
'uid' => $user['memUid'],
'username' => $user['memUsername'],
'app' => $app,
'rule_name' => $v['rule_name'],
]);
$this->sendDailytaskCompleteMsg($v, $user['memUid']);
}
}
}
}
}