DeleteController.class.php 3.66 KB
<?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;
    }
}