DeleteController.class.php
3.66 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
<?php
/**
* 【销售活动-后台】删除评论接口
*
* User: WJY
* Date: 2017-11-02
*/
namespace Apicp\Controller\Comment;
use Common\Common\AttachOperation;
use Common\Service\ActivityService;
use Common\Service\CommentService;
use Common\Service\LikeService;
use Common\Service\ReplyService;
class DeleteController extends \Apicp\Controller\AbstractController
{
/** @var CommentService */
protected $comment_s;
/** @var LikeService */
protected $like_s;
/** @var ReplyService */
protected $reply_s;
/** @var ActivityService */
protected $activity_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化评论表
$this->comment_s = new CommentService();
// 实例化点赞表
$this->like_s = new LikeService();
// 实例化回复表
$this->reply_s = new ReplyService();
// 实例化活动表
$this->activity_s = new ActivityService();
return true;
}
public function Index_post()
{
$comment_id = I('post.comment_id', 0, 'intval');
// 评论存在验证
if (empty($comment_id)) {
E('_ERR_COMMENT_ID_EMPTY');
}
// 开始删除评论
$this->del_comment_pc($comment_id);
// 附件删除操作
$attach_serv = new AttachOperation();
$attach_serv->delete_attach(APP_DIR, 'comment', [$comment_id]);
$this->_result = [];
return true;
}
/**
* 后端删除我的评论
*
* @param int $comment_id 评论id
*
* @return bool
*/
public function del_comment_pc($comment_id = 0)
{
// 查询评论数据
$data = $this->comment_s->get($comment_id);
if (empty($data)) {
// 评论数据不存在
E('_ERR_DATA_NOT_EXIST');
}
try {
// 开始事务
$this->comment_s->start_trans();
// 删除评论
$this->comment_s->delete($comment_id);
//删除评论点赞
$this->like_s->delete_by_conds(['obj_id' => $comment_id, 'type' => ActivityService::LIKE_COMMENT_TYPE]);
// 查询该评论下的所有回复
$reply_list = $this->reply_s->list_by_conds(['comment_id' => $comment_id]);
// 获取该评论回复的ID集合
$reply_ids = array_column($reply_list, 'reply_id');
if (!empty($reply_ids)) {
// 删除回复
$this->reply_s->delete_by_conds(['reply_id' => $reply_ids]);
//删除回复点赞
$this->like_s->delete_by_conds([
'obj_id in (?)' => $reply_ids,
'type' => ActivityService::DEL_REPLY_TYPE
]);
}
if (ActivityService::CHECK_OK == $data['check_status']) {
// 如果删除的是审核通过的分享,此处给该活动的评论数减1
$this->activity_s->update($data['ac_id'], ['comments = comments-?' => 1]);
}
// 提交事务
$this->comment_s->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->comment_s->rollback();
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->comment_s->rollback();
return false;
}
return true;
}
}