PublishController.class.php 8.98 KB
<?php
/**
 * 【后台】立即发布培训计划接口
 * PublishController.class.php
 * User: yingcai
 * Date: 2017/9/1
 * Time: 下午3:12
 */

namespace Apicp\Controller\Education;

use Common\Common\Constant;
use Common\Common\Teacher;
use Common\Service\CourseArticleService;
use Common\Service\CronService;
use Common\Service\EducationService;
use Common\Service\PlanService;
use Common\Service\RightService;
use Common\Service\RightUsersService;

class PublishController extends \Apicp\Controller\AbstractController
{
    /** @var  EducationService  实例化权限表对象 */
    protected $education_service;
    /** @var  RightService  实例化权限表对象 */
    protected $right_service;
    /** @var  RightUsersService  班级人员表对象 */
    protected $right_user_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();

        return true;
    }

    public function Index_post()
    {
        $ed_id = I('ed_id', 0, 'intval');
        // 培训ID是否为空
        if (!$ed_id) {

            E('_EMPTY_ED_ID');
        }

        // 判断培训是否存在
        $education = $this->education_service->get($ed_id);

        if (empty($education)) {

            E('_ERR_EDUCATION_NOT_EXIST');
        }
        if ($education['ed_begin_time'] < MILLI_TIME) {
            // 培训开始时间不能小于当前时间
            E('_ERR_BEGIN_TIME_LT_NOWTIME');
        }
        // 报名截止时间小于当前时间
        if (EducationService::EDUCATION_SIGN_OPEN == $education['ed_is_sign_up']) {

            if ($education['ed_sign_deadline'] > $education['ed_begin_time']) {
                // 报名时间截止不能大于培训开始时间
                E('_ERR_SIGN_TIME_DEADLINE_START_NOWTIME');
            }
            if ($education['ed_sign_deadline'] < MILLI_TIME) {
                // 报名时间截止不能大于培训开始时间
                E('_ERR_SIGN_TIME_DEADLINE_LT_NOWTIME');
            }
        }

        try {
            // 开始事务
            $this->education_service->start_trans();

            // 更改培训状态
            $update_data = ['ed_status' => EducationService::EDUCATION_PUBLISH_STATUS_PUBLISHED];
            $rel = $this->education_service->update($ed_id, $update_data);
            if ($rel) {
                // 发布成功更新状态
                $education['ed_status'] = EducationService::EDUCATION_PUBLISH_STATUS_PUBLISHED;

                // 操作已启用的线下课程类培训安排
                $this->plan_course($ed_id);
            }
            // 将权限范围内的所有人员添加到班级人员表
            $uids = $this->right_user_service->save_right_users(
                $ed_id,
                $education['ed_is_sign_up']
            );

            // 提交事务
            $this->education_service->commit();

        } catch (\Exception $e) {
            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->education_service->rollback();
            return false;
        }

        $education['uids'] = $uids;

        // 推送消息和首页feed流推荐
        $this->sen_msg_and_recommend($ed_id, $education);
        return true;
    }

    /**
     * 批量处理线下课程类的培训安排(创建计划任务、发送讲师提醒、同步授课任务至讲师中心)
     * @param int $ed_id 培训id
     * @return bool
     */
    protected function plan_course($ed_id)
    {
        // 已启用、线下课程类的培训安排
        $conds = [
            'ed_id' => $ed_id,
            'plan_type' => Constant::PLAN_TYPE_UNDER_LINE_COURSE,
            'plan_use_status' => Constant::PLAN_USE_STATUS_IS_TRUE,
        ];
        $planServ = new PlanService();
        $plan_list = $planServ->list_by_conds($conds);

        if (!empty($plan_list)) {

            // 线下课程列表
            $article_ids = array_column($plan_list, 'plan_obj_id');
            $articleServ = new CourseArticleService();
            $article_list = $articleServ->list_by_pks($article_ids);

            // 批量查询讲师信息
            $teacherServ = &Teacher::instance();
            $teacher_ids = array_unique(array_filter(array_column($article_list, 'teacher_id')));
            if (!empty($teacher_ids)) {
                // 正常状态的内部讲师
                $teacher_list = $teacherServ->teacherList(['teacher_id' => $teacher_ids, 'teacher_type' => 1, 'teacher_status' => 1]);
                $teacher_list = array_combine_by_key($teacher_list, 'teacher_id');
            }

            $cronServ = new CronService();
            foreach ($article_list as $article) {

                // 通知讲师中心,记录讲师授课任务、课时
                $teacherServ->saveOfflineCourse($article);

                // 开启签到、有学分,保存计划任务
                if ($article['is_sign'] == Constant::COURSE_IS_SIGN_IS_TRUE && $article['credit'] > 0) {
                    $cronServ->saveCron($article);
                }

                // 开启消息推送时,给讲师推送课程提醒
                if ($article['is_notice'] == Constant::ARTICLE_IS_NOTICE_TRUE && !empty($article['teacher_id'])) {
                    $teacher_id = $article['teacher_id'];
                    $uid = isset($teacher_list[$teacher_id]) ? $teacher_list[$teacher_id]['uid'] : '';

                    if ($uid) {
                        $articleServ->sendNotice($uid, $article);
                    }
                }
            }
        }

        return true;
    }

    /**
     * 发送推送消息和首页feed流推荐
     *
     * @author houyingcai
     * @param int $ed_id 培训ID
     * @param array $education 培训数据
     */
    protected function sen_msg_and_recommend($ed_id, $education)
    {
        // 如果是发布状态
        if (EducationService::EDUCATION_PUBLISH_STATUS_PUBLISHED == $education['ed_status']) {
            // 开启消息推送
            if (EducationService::PUSH_MSG_OPEN == $education['ed_is_push_msg']) {

                $msg_params = [
                    'ed_id' => $ed_id,
                    'name' => $education['ed_name'],
                    'begin_time' => $education['ed_begin_time'],
                    'end_time' => $education['ed_end_time'],
                    'img_id' => $education['ed_cover_id'],
                    'uids' => $education['uids'],
                ];
                // 发送提醒消息
                $this->education_service->send_msg($msg_params, EducationService::MSG_EDUCATION_PUBLISH);
            }
            // 开启首页推荐
            if (EducationService::RECOMMEND_INDEX_TRUE == $education['ed_is_recommend']) {

                $url = rpcUrl('/Public/Rpc/Recommender/ArticleNew');

                $rpc_params = [
                    'app' => 'train',
                    'dataCategoryId' => '',
                    'dataId' => $ed_id,
                    'title' => $education['ed_name'],
                    'summary' => $education['ed_introductions'],
                    'attachId' => $education['ed_cover_id'],
                    'pic' => !empty($education['ed_cover_id']) ? imgUrlReal($education['ed_cover_id']) : '',
                    'url' => 'Train/Frontend/Index/Msg?&ed_id=' . $ed_id,
                ];

                // 获取权限表数据
                $right_all = $this->right_service->get_by_conds([
                    'obj_type' => 1, // 权限类型为全公司
                    'ed_id' => $ed_id
                ]);

                // 如果不是全公司
                if (empty($right_all)) {

                    $right_user_list = $this->right_user_service->list_by_conds(['ed_id' => $ed_id], null, [],
                        'ru_uid');

                    $r_uids = array_unique(array_filter(array_column($right_user_list, 'ru_uid')));
                    if (!empty($r_uids)) {
                        $rpc_params['right'] = $this->format_feed_data($r_uids);
                        \Com\Rpc::phprpc($url)->invoke('Index', $rpc_params);
                    }

                } else {
                    $rpc_params['right'] = [];

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

    /**
     * 格式化feed流权限数据
     *
     * @author houyingcai
     * @param  array $right 权限数组
     *
     * @return array
     */
    protected function format_feed_data($uids = [])
    {
        $feed_right = [];
        // 格式化权限字段
        if (!empty($right)) {

            $feed_right['users'] = !empty($uids) ? $uids : '';
            $feed_right['departments'] = '';
            $feed_right['jobs'] = '';
            $feed_right['roles'] = '';
        }
        return $feed_right;
    }
}