CommentDelController.class.php 3.49 KB
<?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;
    }

}