DeleteController.class.php 3.69 KB
<?php
/**
 * 【销售活动-后台】删除活动接口
 *
 * User: WJY
 * Date: 2017-11-02
 */

namespace Apicp\Controller\Activity;


use Common\Common\AttachOperation;
use Common\Common\Helper;
use Common\Service\ActivityService;
use Common\Service\CommentService;
use Common\Service\ReplyService;
use Common\Service\RightService;
use Think\Exception;

class DeleteController extends \Apicp\Controller\AbstractController
{

    /** @var ActivityService */
    protected $activity_s;
    /** @var CommentService */
    protected $comment_s;
    /** @var RightService */
    protected $right_s;
    /** @var ReplyService */
    protected $reply_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化活动信息表
        $this->activity_s = new ActivityService();
        // 实例化评论表
        $this->comment_s = new CommentService();
        // 实例化权限表
        $this->right_s = new RightService();
        // 实例化回复表
        $this->reply_s = new ReplyService();

        return true;
    }

    public function Index_post()
    {

        $params = I('post.');

        // 执行删除操作
        $this->delete_activity($params);

        $this->_result = [];

        return true;
    }

    /**
     * 删除活动信息
     *
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    protected function delete_activity($reqData)
    {

        $ids = $reqData['ac_ids'];

        if (empty($ids) || !is_array($ids)) {
            // 活动ID存在性判断
            E('_EMPTY_ACTIVITY_ID');
        }

        // 组建条件数组
        $conds = ['ac_id' => $ids];
        // 开启红包的活动无法删除
        $arrayList = $this->activity_s->list_by_conds($conds);

        foreach ($arrayList as $value) {
            // 如开起开开启红包
            if ($value['is_red_open'] == ActivityService::RED_OPENED) {
                // 获取当前活动状态1:草稿,2:未开始,3:进行中,4:已结束,5:已终止
                $status = $this->activity_s->activity_status($value['activity_status'], $value['begin_time'],
                    $value['end_time']);

                // 不能删除的状态
                $cant_del_list = [
                    Helper::STATUS_ING, // 进行中
                    Helper::STATUS_END, // 已结束
                    Helper::STATUS_STOP // 已终止
                ];

                if (in_array($status, $cant_del_list)) {
                    // 进行中,已结束,已终止的活动无法删除
                    E('_ERR_RED_ACTIVITY_CANT_DELETE');
                }
            }
        }

        // 获取评论ID集合
        $comment_list = $this->comment_s->list_by_conds($conds);
        $comment_ids = array_column($comment_list, 'comment_id');

        try {
            // 删除开始
            $this->activity_s->start_trans();

            // 删除活动主题
            $this->activity_s->delete($ids);
            // 删除权限
            $this->right_s->delete_by_conds($conds);
            // 删除评论
            $this->comment_s->delete_by_conds($conds);
            // 删除回复
            $this->reply_s->delete_by_conds($conds);

            // 提交修改
            $this->activity_s->commit();
        } catch (Exception $e) {

            $this->activity_s->rollback();
            E('_ERR_DELETE_FAILED');
        }

        // 附件删除操作
        $attach_serv = new AttachOperation();
        $attach_serv->delete_attach(APP_DIR, 'activity', $ids);
        $attach_serv->delete_attach(APP_DIR, 'comment', $comment_ids);

        return true;
    }
}