DelController.class.php
4.7 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
<?php
/**
* DelController.class.php
* 删除话题
* User: 代军
* Date: 2017-04-24
*/
namespace Apicp\Controller\Topic;
use Common\Common\AttachOperation;
use Common\Common\Constant;
use Common\Common\DataCenter;
use Common\Service\AttachmentService;
use Common\Service\CircleService;
use Common\Service\LikeService;
class DelController extends AbstractController
{
/**
* @var CircleService 帖子信息表
*/
protected $_circle_serv;
/**
* @var AttachmentService 附件图片信息表
*/
protected $_attach_serv;
/**
* @var LikeService 帖子及评论点赞信息表
*/
protected $_like_serv;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化帖子信息表
$this->_circle_serv = new CircleService();
// 实例化帖子附件信息表
$this->_attach_serv = new AttachmentService();
// 实例化帖子点赞信息表
$this->_like_serv = new LikeService();
return true;
}
public function Index_post()
{
// 接收参数
$params = I('post.');
// 参数验证
if (empty($params['list']) || !is_array($params['list'])) {
$this->_set_error('_EMPTY_DATALIST');
return false;
}
// 将列表转换成ID集合(一维数组)
$ids = array_column($params['list'], 'id');
if (empty($ids)) {
$this->_set_error('_EMPTY_DATALIST');
return false;
}
// 查询话题的回帖评论数据
$topic_comment_list = $this->_circle_serv->list_by_conds(
[
'pid' => $ids,
'type' => Constant::CIRCLE_REPLIES_TOPIC_TYPE
],
null,
[],
'id'
);
// 查询所有帖子的评论
$topic_comment_ids = array_column($topic_comment_list, 'id') ? [] : array_column($topic_comment_list, 'id');
$comment_list = [];
if (!empty($topic_comment_ids)) {
$comment_list = $this->_circle_serv->list_by_conds(
[
'pid' => $topic_comment_ids,
'type' => Constant::CIRCLE_COMMENT_TOPIC_TYPE
],
null,
[],
'id'
);
}
$comment_ids = array_column($comment_list, 'id') ? [] : array_column($comment_list, 'id');
// 评论id集合
$cids = array_merge($topic_comment_ids, $comment_ids);
// 获取需要删除的附件ID
$del_attach_ids = [];
$del_attach = $this->_attach_serv->list_by_conds(['cid' => array_merge($ids, $topic_comment_ids)], [], [], 'atid');
if (!empty($del_attach)) {
$del_attach_ids = array_column($del_attach, 'atid');
}
try {
// 开始事务
$this->_circle_serv->start_trans();
// 删除主表、评论数据
$this->_circle_serv->delete_by_conds(['id' => array_merge($ids, $cids)]);
// 删除附件表数据
$this->_attach_serv->delete_by_conds(['cid' => array_merge($ids, $topic_comment_ids)]);
// 删除帖子点赞数据
$this->_like_serv->delete_by_conds(['cid' => array_merge($ids, $cids)]);
// 提交事务
$this->_circle_serv->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->_circle_serv->rollback();
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->_circle_serv->rollback();
return false;
}
// 删除完成后同步更新收藏状态
$this->_circle_serv->update_collection(implode(',', $ids));
// 数据中心:删除主贴时删除点赞记录
$datacenter =& DataCenter::instance();
if (!empty($cids)) {
$cids = array_merge($cids, $ids);
} else {
$cids = $ids;
}
// 执行删除点赞数据
$datacenter->delLike($cids);
// 执行删除评论数据
$datacenter->delComment($cids);
// 从UC服务器删除附件文件
if (!empty($del_attach_ids)) {
$attach_serv = new AttachOperation();
$attach_serv->delete_attach(
APP_DIR,
'circle',
array_merge($ids, $topic_comment_ids)
);
}
return true;
}
}