DeleteController.class.php 4.2 KB
<?php
/**
 * 【后台】 删除培训接口
 * DeleteController.class.php
 * User: yingcai
 * Date: 2017/8/29
 * Time: 下午6:15
 */

namespace Apicp\Controller\Education;

use Common\Common\AttachOperation;
use Common\Service\CommentOptionsService;
use Common\Service\EducationService;
use Common\Service\PlanPicService;
use Common\Service\PlanService;
use Common\Service\RightService;
use Common\Service\RightUsersService;
use Common\Service\SignService;


class DeleteController extends \Apicp\Controller\AbstractController
{
    /** @var  EducationService  实例化权限表对象 */
    protected $education_service;
    /** @var  RightService  实例化权限表对象 */
    protected $right_service;
    /** @var  RightUsersService  班级人员表对象 */
    protected $right_user_service;
    /** @var  PlanService  培训计划表对象 */
    protected $plan_service;
    /** @var  CommentOptionsService  培训评价选项表对象 */
    protected $com_opt_service;
    /** @var  SignService  签到表对象 */
    protected $sign_service;
    /** @var  PlanPicService  照片表对象 */
    protected $plan_pic_service;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->education_service = new EducationService();
        $this->right_service = new RightService();
        $this->right_user_service = new RightUsersService();
        $this->plan_service = new PlanService();
        $this->com_opt_service = new CommentOptionsService();
        $this->sign_service = new SignService();
        $this->plan_pic_service = new PlanPicService();

        return true;
    }

    public function Index_post()
    {
        $ed_ids = I('post.ed_ids');

        // 培训ID不能为空
        if (empty($ed_ids) || !is_array($ed_ids)) {
            E('_EMPTY_ED_ID');
        }

        try {

            $this->education_service->start_trans();

            // 获取所有培训
            $education_list = $this->education_service->list_by_pks($ed_ids);

            // 删除培训
            $this->education_service->delete($ed_ids);
            // 删除培训权限
            $this->right_service->delete_by_conds(['ed_id' => $ed_ids]);
            // 删除班级人员
            $this->right_user_service->delete_by_conds(['ed_id' => $ed_ids]);
            // 删除培训评价项
            $this->com_opt_service->delete_by_conds(['ed_id' => $ed_ids]);
            // 删除签到
            $this->sign_service->delete_by_conds(['ed_id' => $ed_ids]);

            // 获取被删除培训关联的照片墙计划ID
            $conds = [
                'ed_id' => $ed_ids,
                'plan_type' => PlanService::PLAN_TYPE_PHOTO
            ];
            $page_option = null;
            $order_option = [];
            $fields = 'plan_id';
            $plans = $this->plan_service->list_by_conds($conds, $page_option, $order_option, $fields);
            $plan_ids = array_column($plans, 'plan_id');
            // 删除照片
            $this->plan_pic_service->delete_by_conds(['plan_id' => $plan_ids]);
            // 删除培训计划
            $this->plan_service->delete_by_conds(['ed_id' => $ed_ids]);

            $this->education_service->commit();

        } catch (\Exception $e) {

            $this->education_service->rollback();
            E('_ERR_DELETE_EDUCATION_FAILED');

            return false;
        }

        // 删除首页推荐feed流
        foreach ($education_list as $education) {

            if ($education['ed_is_recommend']) {

                $url = rpcUrl('/Public/Rpc/Recommender/ArticleDelete');
                $params = [
                    'app' => 'train',
                    'dataCategoryId' => '',
                    'dataId' => $education['ed_id'],
                ];

                \Com\Rpc::phprpc($url)->invoke('Index', $params);
            }
        }

        /**
         * 删除附件操作
         */
        $attach_serv = new AttachOperation();

        // 删除培训主表附件
        $attach_serv->delete_attach(APP_DIR, 'education', $ed_ids);
        // 删除照片墙附件
        $attach_serv->delete_attach(APP_DIR, 'plan', $plan_ids);

        return true;
    }
}