EducationService.class.php
37.5 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
<?php
/**
* 培训基础表Service
* Created by PhpStorm.
*/
namespace Common\Service;
use Com\Validate;
use Common\Common\Constant;
use Common\Common\AttachOperation;
use Common\Model\CategoryModel;
use Common\Model\EducationModel;
class EducationService extends AbstractService
{
// 培训发布状态:草稿
const EDUCATION_PUBLISH_STATUS_DRAFT = 1;
// 培训发布状态:已发布
const EDUCATION_PUBLISH_STATUS_PUBLISHED = 2;
// 培训发布状态:已终止
const EDUCATION_PUBLISH_STATUS_STOP = 3;
// 编辑第一步
const STEP_TYPE_ONE = 1;
// 编辑第二步
const STEP_TYPE_TWO = 2;
// 培训进行状态:未开始
const EDUCATION_PROCESS_STATUS_UN_BEGIN = 1;
// 培训进行状态:进行中
const EDUCATION_PROCESS_ON_GOING = 2;
// 培训进行状态:已结束
const EDUCATION_PROCESS_END = 3;
// 培训进行状态:已终止
const EDUCATION_PROCESS_STOP = 4;
// 培训进行状态:草稿
const EDUCATION_PROCESS_DRAFT = 5;
// 是否开启报名:否
const EDUCATION_SIGN_CLOSE = 0;
// 是否开启报名:是
const EDUCATION_SIGN_OPEN = 1;
// 是否审核:否
const EDUCATION_UN_CHECK = 0;
// 是否审核:是
const EDUCATION_CHECK = 1;
// 是否收费:否
const EDUCATION_UN_CHARGE = 0;
// 是否收费:是
const EDUCATION_CHARGE = 1;
// 关闭推送消息
const PUSH_MSG_CLOSE = 0;
// 开启推送消息
const PUSH_MSG_OPEN = 1;
// 不推荐到首页
const RECOMMEND_INDEX_FALSE = 0;
// 推荐到首页
const RECOMMEND_INDEX_TRUE = 1;
// 培训评价项目限制个数
const COMMENT_LIMIT = 9;
// 默认限制人数
const EDUCATION_USER_LIMIT = 0;
/** @var EducationModel */
protected $_d;
/** @var RightService */
protected $right_serv;
/** @var StageService */
protected $stage_serv;
/** @var PlanService */
protected $plan_serv;
/** @var CommentOptionsService */
protected $com_opt_serv;
/** @var RightUsersService */
protected $right_users_serv;
/** @var CategoryModel */
protected $category_d;
public function __construct()
{
// 实例化培训model
$this->_d = new EducationModel();
$this->right_serv = new RightService();
$this->stage_serv = new StageService();
$this->plan_serv = new PlanService();
$this->com_opt_serv = new CommentOptionsService();
$this->right_users_serv = new RightUsersService();
$this->category_d = new CategoryModel();
parent::__construct();
}
/**
* 更新已参加人数
* @author wanghuan
*
* @param int $ed_id 培训id
* @param int $type 更新类型:0=加1,1=减1
*
* @return bool
*/
public function update_joined($ed_id = 0, $type = 0)
{
return $this->_d->update_joined($ed_id, $type);
}
/**
* 根据条件获取用户权限范围的培训总数
* @author wanghuan
*
* @param array $cond 查询条件
*
* @return array|bool
*/
public function user_education_count($cond = [])
{
return $this->_d->user_education_count($cond);
}
/**
* 根据条件获取用户权限范围的培训列表
* @author wanghuan
*
* @param array $cond 查询条件
* @param null $page_option 分页
* @param array $order_option 排序
*
* @return array|bool
*/
public function user_education_list($cond = [], $page_option = null, $order_option = [])
{
return $this->_d->user_education_list($cond, $page_option, $order_option);
}
/**
* 新增培训
*
* @author houyingcai
* @param array $result 返回培训信息
* @param array $params 请求数据
*
* @return bool
*/
public function add_education(&$result, $params)
{
// 获取培训数据
$education_data = $this->fetch_education_data($params);
// 验证数据
$this->validate_for_add($education_data);
// 权限列表
$right_list = $education_data['right_list'];
// 培训计划列表
$plan_list = $education_data['plan_list'];
// 验证培训计划列表
$this->stage_serv->check_data($plan_list, $params['ed_status']);
// 评价项目列表
$comment_option_list = $education_data['comment_type_list'];
// 验证评价项目列表
if (!empty($comment_option_list)) {
$this->com_opt_serv->validate_com_opt_list($comment_option_list);
}
// 导入人员ID列表
$import_users = [];
if (!empty($education_data['import_users'])) {
$import_users = json_decode($education_data['import_users'], true);
}
// 格式化用户提交过来的权限
$post_right = [
'is_all' => $right_list['is_all'],
'uids' => array_column($right_list['user_list'], 'memID'),
'dp_ids' => array_column($right_list['dp_list'], 'dpID'),
'tag_ids' => array_column($right_list['tag_list'], 'tagID'),
'job_ids' => array_column($right_list['job_list'], 'jobID'),
'role_ids' => array_column($right_list['role_list'], 'roleID'),
];
// 获取权限范围内的人员UID列表
$uids = $this->right_serv->list_post_right_uids($post_right);
// 统计应参与人数
$count_uids = count(array_unique(array_merge($uids, $import_users)));
$education_data['ed_join_count'] = $count_uids;
unset(
$education_data['right_list'],
$education_data['plan_list'],
$education_data['comment_type_list'],
$education_data['import_users']
);
$right_uids = [];
try {
// 开始事务
$this->start_trans();
// 获取分类的状态
$category = $this->category_d->get($education_data['ca_id']);
// 分类不为空
if (!empty($category)) {
$education_data['ca_status'] = $category['ca_status'];
}
// 培训数据入库
$ed_id = $this->_d->insert($education_data);
if (!$ed_id) {
// 培训添加失败
E('_ERR_EDUCATION_FAIL');
}
// 权限数据入库
$row_right = $this->right_serv->save_data(['ed_id' => $ed_id], $right_list, $import_users);
if (!$row_right) {
// 权限保存失败
E('_ERR_RIGHT_SAVE');
}
// 班级人员入库(发布状态班级人员才入库,草稿不入库)
if (self::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education_data['ed_status']) {
$right_uids = $this->right_users_serv->save_right_users(
$ed_id,
$education_data['ed_is_sign_up']
);
}
// 培训计划入库
$this->stage_serv->save_stage($ed_id, $plan_list, [], [], $params['ed_status']);
// 评价项目入库
if (!empty($comment_option_list)) {
$this->com_opt_serv->save_comment_option($ed_id, $comment_option_list);
}
// 提交事务
$this->commit();
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->rollback();
return false;
}
// 推送消息和首页feed流推荐
$this->sen_msg_and_recommend(intval($ed_id), $education_data, $right_list, $import_users, $right_uids);
/**
* 附件操作处理 start
*/
$this->attach_operation(intval($ed_id), $education_data);
/**
* 附件操作处理 end
*/
//返回结果集
$result['ed_id'] = (int)$ed_id;
return true;
}
/**
* 获取培训数据
*
* @author houyingcai
* @param array $params 题目数据
*
* @return array|bool
*/
protected function fetch_education_data($params)
{
return [
'ca_id' => intval($params['ca_id']),
'ed_name' => raddslashes($params['ed_name']),
'ed_begin_time' => $params['ed_begin_time'],
'ed_end_time' => $params['ed_end_time'],
'ed_address' => raddslashes($params['ed_address']),
'ed_cover_id' => $params['ed_cover_id'],
'ed_introductions' => $params['ed_introductions'],
'ed_is_sign_up' => intval($params['ed_is_sign_up']),
'ed_sign_deadline' => intval($params['ed_sign_deadline']),
'ed_is_charge' => intval($params['ed_is_charge']),
'ed_charge_type' => intval($params['ed_charge_type']),
'ed_charge_score' => intval($params['ed_charge_score']),
'ed_is_check' => intval($params['ed_is_check']),
'ed_sign_up_number' => intval($params['ed_sign_up_number']),
'plan_list' => $params['plan_list'],
'right_list' => $params['right_list'],
'ed_is_recommend' => intval($params['ed_is_recommend']),
'ed_is_push_msg' => intval($params['ed_is_push_msg']),
'ed_tags' => raddslashes($params['ed_tags']),
'comment_type_list' => $params['comment_type_list'],
'ed_status' => intval($params['ed_status']),
'import_users' => $params['import_users'],
'ed_comment_show_self' => intval($params['ed_comment_show_self']),
];
}
/**
* 验证新增培训数据
*
* @author houyingcai
* @param array $params 培训数据
* @param bool $validate_time 是否验证开始时间
* @return bool
*/
public function validate_for_add(&$params, $validate_time = true)
{
// 验证规则
$rules = [
'ca_id' => 'require',
'ed_name' => 'require|max:64',
'ed_begin_time' => 'require',
'ed_end_time' => 'require',
'ed_is_sign_up' => 'require|in:0,1',
'right_list' => 'require',
'ed_is_recommend' => 'require|in:0,1',
'ed_is_push_msg' => 'require|in:0,1',
'ed_status' => 'require|in:1,2',
'ed_comment_show_self' => 'require|in:0,1',
];
// 错误提示
$message = [
'ca_id' => L('_EMPTY_CATEGORY_ID'),
'ed_name.require' => L('_EMPTY_ED_NAME'),
'ed_name.max' => L('_ERR_ED_NAME_LENGTH'),
'ed_begin_time' => L('_EMPTY_BEGIN_TIME'),
'ed_end_time' => L('_EMPTY_END_TIME'),
'ed_is_sign_up' => L('_ERR_SIGN_UP_INVALID'),
'right_list' => L('_EMPTY_RIGHT_LIST'),
'ed_is_recommend' => L('_ERR_IS_RECOMMEND_INVALID'),
'ed_is_push_msg' => L('_ERR_IS_PUSH_MSG_INVALID'),
'ed_status' => L('_ERR_ED_STATUS_INVALID'),
'ed_comment_show_self' => L('_ERR_COMMENT_SHOW_INVALID'),
];
// 开始验证
$validate = new Validate($rules, $message);
if (!$validate->check($params)) {
E($validate->getError());
}
// 如果需要验证开始时间
if ($validate_time) {
// 开始时间不能小于当前时间
if ($params['ed_begin_time'] <= MILLI_TIME) {
E('_ERR_BEGIN_TIME_LT_NOWTIME');
}
}
// 结束时间不能小于开始时间
if ($params['ed_end_time'] <= $params['ed_begin_time']) {
E('_ERR_END_TIME_LT_NOWTIME');
}
// 验证报名截止时间
if ($params['ed_is_sign_up'] && empty($params['ed_sign_deadline'])) {
// 报名截止时间不能为空
E('_EMPTY_ED_SIGN_DEADLINE');
}
// 报名截止时间不能大于培训开始时间
if ($params['ed_sign_deadline'] && $params['ed_sign_deadline'] >= $params['ed_begin_time']) {
E('_ERR_SIGN_DEADLINE_GT_BEGIN_TIME');
}
// 培训报名截止时间不能小于当前时间
if ($params['ed_sign_deadline'] && $params['ed_sign_deadline'] < MILLI_TIME) {
E('_ERR_SIGN_TIME_DEADLINE_LT_NOWTIME');
}
// 验证收费类型
if ($params['ed_charge_type'] && !in_array($params['ed_charge_type'], [Constant::ED_CHARGE_TYPE_IS_INTEGRAL, Constant::ED_CHARGE_TYPE_IS_CREDIT])) {
E('_ERR_CHARGE_TYPE_OVER_RANGE');
}
// 验证收费数量
if ($params['ed_is_charge'] && empty($params['ed_charge_score'])) {
E('_EMPTY_ED_CHARGE_SCORE');
}
// 导入人员为空时,验证权限范围数据
if (empty($params['import_users'])) {
// 格式化权限数据
$format_rights = $this->right_serv->format_post_data($params['right_list']);
// 权限参数不正确
if (empty($format_rights)) {
E('_ERR_EDUCATION_RIGHT_PARAM');
}
}
// 评价项目列表大于限制值
if (count($params['comment_type_list']) > self::COMMENT_LIMIT) {
E('_ERR_COMMENT_LIST_GT_LIMIT');
}
// 评价项目不能为空
if (empty($params['comment_type_list'])) {
E('_EMPTY_COMMENT_LIST_GT');
}
// 开启收费则不需要审核
if ($params['ed_is_charge']) {
$params['ed_is_check'] = self::EDUCATION_UN_CHECK;
}
return true;
}
/**
* 发送推送消息和首页feed流推荐
*
* @author houyingcai
* @param int $ed_id 培训ID
* @param array $education_data 培训数据
* @param array $right_list 权限列表
* @param array $import_users 导入人员列表
* @param array $right_uids 班级成员uid集合
*/
protected function sen_msg_and_recommend($ed_id, $education_data, $right_list, $import_users, $right_uids)
{
// 如果是发布状态
if (self::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education_data['ed_status']) {
// 开启消息推送
if (self::PUSH_MSG_OPEN == $education_data['ed_is_push_msg']) {
$post_right = [
'is_all' => $right_list['is_all'],
'uids' => array_column($right_list['user_list'], 'memID'),
'tag_ids' => array_column($right_list['tag_list'], 'tagID'),
'dp_ids' => array_column($right_list['dp_list'], 'dpID'),
'job_ids' => array_column($right_list['job_list'], 'jobID'),
'role_ids' => array_column($right_list['role_list'], 'roleID'),
];
// 获取需要发送通知的所有人UID
$uids = $this->right_serv->list_post_right_uids($post_right);
if (!empty($import_users)) {
$uids = array_filter(array_merge($uids, $import_users));
}
$msg_params = [
'ed_id' => $ed_id,
'name' => $education_data['ed_name'],
'begin_time' => $education_data['ed_begin_time'],
'end_time' => $education_data['ed_end_time'],
'address' => $education_data['ed_address'],
'img_id' => $education_data['ed_cover_id'],
'uids' => $uids,
];
// 发送提醒消息
if (!empty($uids)) {
$this->send_msg($msg_params, EducationService::MSG_EDUCATION_PUBLISH);
}
}
// 开启首页推荐
if (self::RECOMMEND_INDEX_TRUE == $education_data['ed_is_recommend']) {
$url = rpcUrl('/Public/Rpc/Recommender/ArticleNew');
$rpc_params = [
'app' => 'train',
'dataCategoryId' => '',
'dataId' => $ed_id,
'title' => $education_data['ed_name'],
'summary' => $education_data['ed_introductions'],
'attachId' => $education_data['ed_cover_id'],
'pic' => !empty($education_data['ed_cover_id']) ? imgUrlReal($education_data['ed_cover_id']) : '',
'url' => 'Train/Frontend/Index/Msg?&ed_id=' . $ed_id,
];
// 是否推荐给全公司
if ($right_list['is_all'] != self::IS_ALL && !empty($right_uids)) {
// 获取权限数据
$rpc_params['right'] = $this->format_feed_data($right_uids);
\Com\Rpc::phprpc($url)->invoke('Index', $rpc_params);
} else {
\Com\Rpc::phprpc($url)->invoke('Index', $rpc_params);
}
}
}
}
/**
* 格式化feed流权限数据
*
* @author houyingcai
* @param array $right_uids 班级成员列表
* @return array
*/
protected function format_feed_data($right_uids)
{
$feed_right = [];
// 格式化权限字段
if (!empty($right_uids)) {
$feed_right['users'] = !empty($right_uids) ? $right_uids : '';
$feed_right['departments'] = '';
$feed_right['jobs'] = '';
$feed_right['roles'] = '';
}
return $feed_right;
}
/**
* 编辑培训
* @param &$result
* @param array $params 请求参数
* @return bool
*/
public function edit_education(&$result, $params)
{
$ed_id = intval($params['ed_id']);
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 获取旧数据
$old_education = $this->get($ed_id);
// 培训不存在
if (empty($old_education)) {
E('_ERR_EDUCATION_NOT_EXIST');
}
$old_ed_status = (int)$old_education['ed_status'];
$ed_status = isset($params['ed_status']) ? $params['ed_status'] : $old_ed_status;
// 初始化权限数据
$right_list = [];
// 初始化导入人员数据
$import_users = [];
// 获取用户提交的培训数据
$education_data = $this->fetch_education_data($params);
// 如果是编辑第一步
if ($params['step_type'] == self::STEP_TYPE_ONE) {
// 编辑培训第一步数据验证
$this->validate_for_edit_step_one($education_data);
// 初始化数据
$data = [
'ed_name' => $education_data['ed_name'],
'ed_address' => $education_data['ed_address'],
'ed_cover_id' => $education_data['ed_cover_id'],
'ed_introductions' => $education_data['ed_introductions']
];
// 更新培训数据
$row = $this->_d->update($ed_id, $data);
if (!$row) {
E('_ERR_EDUCATION_UPDATE_FAIL');
}
// 更新首页推荐
$this->recommender($ed_id);
//返回结果集
$result['ed_id'] = (int)$ed_id;
/**
* 附件操作处理 start
*/
$this->attach_operation($ed_id, $education_data, 'edit');
/**
* 附件操作处理 end
*/
return true;
}
// 如果是编辑第二步
if ($params['step_type'] == self::STEP_TYPE_TWO) {
// 校验培训安排
$this->stage_serv->check_data($education_data['plan_list'], $ed_status, $old_ed_status);
// 保存培训安排
$this->stage_serv->save_stage($ed_id, $education_data['plan_list'], $params['stage_del_ids'], $params['plan_del_ids'], $ed_status);
//返回结果集
$result['ed_id'] = (int)$ed_id;
return true;
}
// 原来是草稿状态 || 原来是草稿状态,现改为已发布(需校验权限和评价项目)
if (self::EDUCATION_PUBLISH_STATUS_DRAFT == $education_data['ed_status']
|| (self::EDUCATION_PUBLISH_STATUS_DRAFT == $old_education['ed_status']
&& self::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education_data['ed_status'])
) {
// 验证培训数据
$this->validate_for_add($education_data);
// 权限列表
$right_list = $education_data['right_list'];
// 评价项目列表
$comment_option_list = $education_data['comment_type_list'];
// 验证评价项目列表
if (empty($comment_option_list)) {
$this->com_opt_serv->validate_com_opt_list($comment_option_list);
}
// 导入人员ID列表
if (!empty($education_data['import_users'])) {
$import_users = json_decode($education_data['import_users']);
}
// 格式化用户提交过来的权限
$post_right = [
'is_all' => $right_list['is_all'],
'uids' => array_column($right_list['user_list'], 'memID'),
'tag_ids' => array_column($right_list['tag_list'], 'tagID'),
'dp_ids' => array_column($right_list['dp_list'], 'dpID'),
'job_ids' => array_column($right_list['job_list'], 'jobID'),
'role_ids' => array_column($right_list['role_list'], 'roleID'),
];
// 获取权限范围内的人员UID列表
$uids = $this->right_serv->list_post_right_uids($post_right);
// 统计应参与人数
$count_uids = count(array_unique(array_merge($uids, $import_users)));
$education_data['ed_join_count'] = $count_uids;
}
// 培训计划列表
$plan_list = $education_data['plan_list'];
// 校验培训安排
$this->stage_serv->check_data($plan_list, $ed_status, $old_ed_status);
unset(
$education_data['right_list'],
$education_data['plan_list'],
$education_data['comment_type_list'],
$education_data['import_users']
);
$right_uids = [];
try {
// 开始事务
$this->start_trans();
// 如果是草稿才更新:培训基本数据、权限数据、班级人员、评价项目等
if (self::EDUCATION_PUBLISH_STATUS_DRAFT == $education_data['ed_status']
|| (self::EDUCATION_PUBLISH_STATUS_DRAFT == $old_education['ed_status']
&& self::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education_data['ed_status'])
) {
// 获取分类的状态
$category = $this->category_d->get($education_data['ca_id']);
// 分类不为空
if (!empty($category)) {
$education_data['ca_status'] = $category['ca_status'];
}
// 更新培训数据
$row = $this->_d->update($ed_id, $education_data);
if (!$row) {
E('_ERR_EDUCATION_UPDATE_FAIL');
}
// 权限数据入库
$row_right = $this->right_serv->save_data(['ed_id' => $ed_id], $right_list, $import_users);
if (!$row_right) {
E('_ERR_RIGHT_SAVE');
}
// 班级人员入库(发布状态班级人员才入库,草稿不入库)
if (self::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education_data['ed_status']) {
$right_uids = $this->right_users_serv->save_right_users(
$ed_id,
$education_data['ed_is_sign_up']
);
}
// 评价项目入库
if (!empty($comment_option_list)) {
$this->com_opt_serv->save_comment_option($ed_id, $comment_option_list, 'edit');
}
}
// 保存培训安排
$this->stage_serv->save_stage($ed_id, $plan_list, $params['stage_del_ids'], $params['plan_del_ids'], $ed_status);
// 提交事务
$this->commit();
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->rollback();
return false;
}
// 如果是草稿更新为已发布,才推送消息
if (self::EDUCATION_PUBLISH_STATUS_DRAFT == $old_education['ed_status']
&& self::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education_data['ed_status']
) {
// 推送消息和首页feed流推荐
$this->sen_msg_and_recommend($ed_id, $education_data, $right_list, $import_users, $right_uids);
}
/**
* 附件操作处理 start
*/
$this->attach_operation($ed_id, $education_data, 'edit');
/**
* 附件操作处理 end
*/
//返回结果集
$result['ed_id'] = (int)$ed_id;
return true;
}
/**
* 【后台】编辑第一步验证
* @param &$params
* @return bool
*/
public function validate_for_edit_step_one(&$params)
{
// 验证规则
$rules = [
'ca_id' => 'require',
'ed_name' => 'require|max:64',
];
// 错误提示
$message = [
'ca_id' => L('_EMPTY_CATEGORY_ID'),
'ed_name.require' => L('_EMPTY_ED_NAME'),
'ed_name.max' => L('_ERR_ED_NAME_LENGTH'),
];
// 开始验证
$validate = new Validate($rules, $message);
if (!$validate->check($params)) {
E($validate->getError());
}
return true;
}
/**
* 获取培训详情(编辑用)
*
* @author houyingcai
* @param int $ed_id 培训ID
*
* @return array
*/
public function get_education($ed_id = 0)
{
// 培训ID不能为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 获取培训详情
$data = $this->_d->get($ed_id);
// 培训不存在
if (empty($data)) {
E('_ERR_EDUCATION_NOT_EXIST');
}
// 获取分类名称
$category = $this->category_d->get($data['ca_id']);
$data['ca_name'] = $category['ca_name'];
$data['ed_sign_deadline'] = $data['ed_sign_deadline'] ? $data['ed_sign_deadline'] : '';
// 获取封面图地址
$data['ed_cover_url'] = $this->format_cover($data['ed_cover_id']);
$conds = ['ed_id' => $ed_id];
$page_option = null;
// 获取培训计划列表
$plan_order = ['plan_order' => 'ASC'];
$plan_field = 'plan_id,plan_obj_id,plan_name,plan_type,plan_use_status,plan_use_time,plan_sign_begin,plan_sign_end,plan_sign_comments, plan_allow_upload,plan_order,stage_id';
$data['plan_list'] = $this->plan_serv->list_by_conds($conds, $page_option, $plan_order, $plan_field);
// 获取线下培训对应ID数据
$plan_obj_ids = array_column($data['plan_list'], 'plan_obj_id');
// 请求地址
$url = rpcUrl('/Exam/Rpc/Train/ListInfo');
// 请求参数
$data_send ['app_data_ids'] = $plan_obj_ids;
// 发送请求,获取结果
$res_data = \Com\Rpc::phprpc($url)->invoke('Index', $data_send);
// 替换主键
$res_data = array_combine_by_key($res_data, 'app_data_id');
// 格式化启用时间
foreach ($data['plan_list'] as &$val) {
$val['plan_type'] = intval($val['plan_type']);
$val['plan_use_time'] = $val['plan_use_time'] ? $val['plan_use_time'] : '';
$val['ep_type'] = intval($res_data[$val['plan_obj_id']]['ep_type']);
$val['paper_type'] = intval($res_data[$val['plan_obj_id']]['paper_type']);
$val['exam_status'] = intval($res_data[$val['plan_obj_id']]['exam_status']);
}
// 获取培训评价选项列表
$option_order = ['option_order' => 'ASC'];
$option_field = 'option_id,option_type,option_name,option_score';
$data['comment_type_list'] = $this->com_opt_serv->list_by_conds($conds, $page_option, $option_order,
$option_field);
// 获取非导入人员权限信息
$conds['uid_is_import'] = RightService::NOT_IMPORT_USER;
$data['right_list'] = $this->right_serv->get_data($conds);
// 获取所有导入人员信息
$conds['uid_is_import'] = RightService::IMPORT_USER;
$data['import_users'] = $this->right_serv->list_import_users($conds);
// 格式化培训阶段
$data['plan_list'] = $this->stage_serv->format_data($ed_id, $data['plan_list']);
return $data;
}
/**
* 获取培训详情(查看用)
*
* @author houyingcai
* @param int $ed_id 培训ID
*
* @return array
*/
public function get_education_view($ed_id = 0)
{
// 培训ID不能为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 获取培训详情
$data = $this->_d->get($ed_id);
// 培训不存在
if (empty($data)) {
E('_ERR_EDUCATION_NOT_EXIST');
}
// 获取分类名称
$category = $this->category_d->get($data['ca_id']);
$data['ca_name'] = $category['ca_name'];
// 格式化标签
$data['ed_tags'] = array_filter(explode(';', $data['ed_tags']));
// 获取培训计划列表
$conds = ['ed_id' => $ed_id];
$plan_order = ['plan_order' => 'ASC'];
$page_option = null;
// $plan_field = 'plan_id,plan_name,plan_type,plan_use_status,plan_use_time,plan_obj_id,plan_order,stage_id';
// 查询培训计划
$plan_list = $this->plan_serv->list_by_conds($conds, $page_option, $plan_order);
// 考试计划获取试卷ID
$exam_ids = [];
foreach ($plan_list as $v) {
if (PlanService::PLAN_TYPE_EXAM == $v['plan_type']) {
$exam_ids[] = $v['plan_obj_id'];
}
}
// 如果存在查询
if (!empty($exam_ids)) {
$url = rpcUrl('/Exam/Rpc/Train/FetchExam');
$rpc_params = [
'ep_ids' => $exam_ids
];
$exam_list = \Com\Rpc::phprpc($url)->invoke('Index', $rpc_params);
$exam_list = array_combine_by_key($exam_list, 'ep_id');
foreach ($plan_list as &$value) {
if (PlanService::PLAN_TYPE_EXAM == $value['plan_type']) {
$value['paper_type'] = isset($exam_list[$value['plan_obj_id']]['paper_type']) ? $exam_list[$value['plan_obj_id']]['paper_type'] : '';
}
}
}
// 格式化培训阶段
$data['plan_list'] = $this->stage_serv->format_data($ed_id, $plan_list);
// 获取封面图地址
$data['ed_cover_url'] = $this->format_cover($data['ed_cover_id']);
// 获取培训评价选项列表
$res = $this->com_opt_serv->get_composite_score($ed_id);
$data['comment_type_list'] = [
'score' => $res['score'],
'options_score' => $res['options_score']
];
$conds = ['ed_id' => $ed_id];
// 获取人员权限信息(含导入及非导入的信息,用作详情展示)
// $conds['uid_is_import'] = RightService::NOT_IMPORT_USER;
$data['right_list'] = $this->right_serv->get_data($conds);
// 获取所有导入人员信息
$conds['uid_is_import'] = RightService::IMPORT_USER;
// 初始化导入的UID集合
$import_uid = [];
if (!empty($conds)) {
// 查询导入UID是否存在
$list = $this->right_serv->list_by_conds($conds, null, [], 'obj_id');
if (!empty($list)) {
$import_uid = array_unique(array_column($list, 'obj_id'));
}
}
// 获取导入UID集合总数
$data['import_users_total'] = count($import_uid);
return $data;
}
/**
* 培训列表接口
*
* @author 蔡建华
* @param $params array 搜索参数
*
* @return array
*/
public function education_search_list($params = [])
{
// 默认值
$page = !empty($params['page']) ? intval($params['page']) : self::PAGE_DEFAULT;
$limit = !empty($params['limit']) ? intval($params['limit']) : self::PAGE_LIMIT_DEFAULT;
// 分页
list($start, $limit) = page_limit($page, $limit);
$page_option = [$start, $limit];
// 按照创建时的ID降序排列
$order_option = ['ed_id' => 'desc'];
// 列表总数
$total = $this->_d->count_by_education($params);
if ($total > 0) {
$fields = 'ed_id,ca_id,ed_name,ed_begin_time,ed_end_time,ed_address,ed_joined_count,ed_join_count,ed_status';
$list = $this->_d->list_by_education($params, $page_option, $order_option, $fields);
$data = $this->format_education_list($list);
}
// 组装返回数
return [
'total' => intval($total),
'limit' => intval($limit),
'page' => intval($page),
'list' => !empty($data) ? $data : [],
];
}
/**
* 培训列表数据格式化
*
* @author 蔡建华
* @param $list array 培训列表数据格式化
*
* @return array
*/
protected function format_education_list($list = [])
{
if (empty($list)) {
return [];
}
$ca_ids = array_column($list, 'ca_id');
// 查询分类
$cate = $this->category_d->list_by_conds(['ca_id' => $ca_ids], null, 'ca_id desc');
// 重组临时分类
$cate_temp = array_combine_by_key($cate, 'ca_id');
$data = [];
// 格式化培训数据
foreach ($list as $key => $val) {
$value = [];
$value['ed_id'] = intval($val['ed_id']);
$value['ed_name'] = $val['ed_name'];
$value['ed_begin_time'] = $val['ed_begin_time'];
$value['ed_end_time'] = $val['ed_end_time'];
$value['ca_name'] = isset($cate_temp[$val['ca_id']]) ? $cate_temp[$val['ca_id']]['ca_name'] : '';
$value['ed_address'] = $val['ed_address'];
$value['ca_id'] = intval($val['ca_id']);
$ed_status = self::EDUCATION_PROCESS_STOP == $this->get_ed_status($val) ? self::EDUCATION_PROCESS_END : $this->get_ed_status($val);
$value['ed_status'] = $ed_status;
$value['ed_join_nums'] = [
'ed_joined_count' => intval($val['ed_joined_count']),
'ed_join_count' => intval($val['ed_join_count'])
];
$data[$key] = $value;
}
return $data;
}
/**
* 获取培训进行状态
* @author wanghuan
*
* @param array $education 培训详情
*
* @return int 培训进行状态
*/
public function get_ed_status($education = [])
{
// 培训发布状态
$ed_status = $education['ed_status'];
// 培训进行状态
$ed_show_status = 0;
// 培训发布状态:已终止
if (self::EDUCATION_PUBLISH_STATUS_STOP == $ed_status) {
// 已终止
$ed_show_status = self::EDUCATION_PROCESS_STOP;
} elseif (self::EDUCATION_PUBLISH_STATUS_DRAFT == $ed_status) {
// 草稿
$ed_show_status = self::EDUCATION_PROCESS_DRAFT;
} else {
// 当前时间
$now_time = MILLI_TIME;
// 培训开始时间
$ed_begin_time = $education['ed_begin_time'];
// 培训结束时间
$ed_end_time = $education['ed_end_time'];
// 当前时间小于培训开始时间
if ($now_time < $ed_begin_time) {
// 未开始
$ed_show_status = self::EDUCATION_PROCESS_STATUS_UN_BEGIN;
} elseif ($now_time > $ed_end_time) {
// 已结束
$ed_show_status = self::EDUCATION_PROCESS_END;
} elseif ($ed_begin_time <= $now_time && $now_time <= $ed_end_time) {
// 进行中
$ed_show_status = self::EDUCATION_PROCESS_ON_GOING;
}
}
return $ed_show_status;
}
/**
* 附件操作
*
* @param int $ed_id 培训ID
* @param array $education_data 培训数据
* @param string $type 操作类型(add:添加,edit:编辑)
*
* @return bool
*/
private function attach_operation($ed_id, $education_data, $type = 'add')
{
/**
* 附件操作处理 start
*/
$attach_serv = new AttachOperation();
$attach_ids = [];
if (!empty($education_data['ed_cover_id'])) {
// 封面图
$attach_ids[] = $education_data['ed_cover_id'];
}
if (!empty($education_data['ed_introductions'])) {
// 内容中的附件
$content_at_ids = $attach_serv->match_at_ids($education_data['ed_introductions']);
$attach_ids = array_merge($attach_ids, $content_at_ids);
}
if (!empty($attach_ids)) {
// 添加
if ($type == 'add') {
$attach_serv->insert_attach(
APP_DIR,
'education',
$ed_id,
['attach_ids' => $attach_ids]
);
}
// 编辑
if ($type == 'edit') {
$attach_serv->update_attach(
APP_DIR,
'education',
$ed_id,
['attach_ids' => $attach_ids]
);
}
}
return true;
}
}