CommentDelController.class.php
3.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
<?php
/**
* CommentDelController.class.php
* 删除话题回帖
* Date: 2018-06-28
*/
namespace Apicp\Controller\Topic;
use Common\Common\AttachOperation;
use Common\Common\DataCenter;
use Common\Service\AttachmentService;
use Common\Service\CircleService;
use Common\Service\LikeService;
class CommentDelController 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()
{
// 接收参数
$id = I('post.id');
// 参数验证
if (empty($id)) {
$this->_set_error('_EMPTY_COMMENTID');
return false;
}
// 查询回帖下的 所有评论数据
$comment = $this->_circle_serv->list_by_conds(['pid' => $id], null, [], 'id');
// 回帖下的 评论id集合
$cids = [];
if (!empty($comment)) {
$cids = array_column($comment, 'id');
}
// 获取需要删除的附件ID
$del_attach_ids = [];
$del_attach = $this->_attach_serv->list_by_conds(['cid' => $id], [], [], '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' => $id]);
// 删除评论数据
$this->_circle_serv->delete_by_conds(['pid' => $id]);
// 删除附件表数据
$this->_attach_serv->delete_by_conds(['cid' => $id]);
// 删除帖子点赞数据
$this->_like_serv->delete_by_conds(['cid' => $id]);
// 提交事务
$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;
}
if (!empty($cids)) {
$cids = array_merge($cids, [$id]);
} else {
$cids = $id;
}
// 数据中心:删除回帖 删除点赞记录
$datacenter =& DataCenter::instance();
$datacenter->delLike($id);
// 执行删除回帖数据
$datacenter->delComment($cids);
// 从UC服务器删除附件文件
if (!empty($del_attach_ids)) {
$attach_serv = new AttachOperation();
$attach_serv->delete_attach(
APP_DIR,
'circle',
$id
);
}
return true;
}
}