DelController.class.php 4.7 KB
<?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;
    }

}