DeleteController.class.php
3.57 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
<?php
/**
*【线下培训-后台】班级成员删除
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-08-29 10:03:53
* @version $Id$
*/
namespace Apicp\Controller\Team;
use Common\Service\EducationService;
use Common\Service\RightUsersService;
use Common\Service\SignService;
class DeleteController extends \Apicp\Controller\AbstractController
{
// 用户报名状态:未报名
const SIGN_NO = 0;
// 用户报名状态:报名成功
const SIGN_SUCCESS = 1;
public function Index_post()
{
$ru_ids = I('post.ru_ids');
$right_users_server = new RightUsersService();
$sign_server = new SignService();
// 班级人员ID不能为空
if (empty($ru_ids)) {
E('_EMPTY_RU_IDS');
}
$right_cond = [
'ru_id' => $ru_ids
];
$right_users = $right_users_server->list_by_conds($right_cond);
$ed_id = 0;
try {
// 事务开始
$right_users_server->start_trans();
// 执行删除操作
$rows = $right_users_server->delete($ru_ids);
// 如果删除成功,更新培训表中的已参与人员数量
if ($rows) {
$ed_id = $right_users[0]['ed_id'];
$education_service = new EducationService();
// 获取培训数据
$education = $education_service->get($ed_id);
$count = 0;
$ed_join_count = 0;
foreach ($right_users as $value) {
$ed_join_count++;
// 用户报名状态
$sign_up_status = $right_users_server->get_sign_up_status($education, $value);
if (self::SIGN_SUCCESS == $sign_up_status) {
$count++;
}
}
// 删除签到记录
$del_uids = array_column($right_users, 'ru_uid');
if (!empty($del_uids)) {
// 根据用户ID和培训ID删除签到记录
$where = [
'ed_id' => $ed_id,
'sign_uid' => $del_uids
];
$del_count = $sign_server->count_by_conds($where);
// 如果记录存在,进行删除
if ($del_count) {
$sign_server->delete_by_conds($where);
}
}
// 根据培训ID更新应参加人数
$conds = ['ed_id' => $ed_id];
// 更新应参与人数
$update_data['ed_join_count'] = $education['ed_join_count'] - $ed_join_count;
$education_service->update_by_conds($conds, $update_data);
// 已参与人数减去删除人数不为负数
if ($count && ($education['ed_joined_count'] - $count >= 0)) {
// 待更新数据
$update_data = [
'ed_joined_count' => $education['ed_joined_count'] - $count
];
// 更新培训已参与人数
$education_service->update($ed_id, $update_data);
}
}
// 提交事务
$right_users_server->commit();
} catch (\Exception $e) {
$right_users_server->rollback();
return false;
}
if ($ed_id) {
// 更新首页推荐权限
$right_users_server->recommender($ed_id);
}
return true;
}
}