AbstractController.class.php 8.67 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhoutao
 * Date: 2018/1/8
 * Time: 下午3:54
 */

namespace Apicp\Controller\VhLiveOperate;

use Common\Common\Constant;
use Common\Common\Vhall;
use Common\Model\CronModel;
use Common\Service\VhallService;
use Common\Service\VhMsgLogService;
use VcySDK\Cron;
use VcySDK\Exception;
use VcySDK\Service;

class AbstractController extends \Apicp\Controller\AbstractController
{

    /** @var VhMsgLogService */

    protected $vhMsgLogServ;

    /** @var VhallService */
    protected $vhall_service;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }
        $this->vhall_service = new VhallService();
        // 微吼直播日志
        $this->vhMsgLogServ = new VhMsgLogService();
        return true;
    }


    public function getLiveSetting($params = [], $field = [])
    {
        $setting = [];

        //校验提醒时间 时候大于当前时间
        if ($params['notice_will_type'] == Constant::NOTICE_WILL_TYPE_ON) {
            $times = $params['liveStartTime'] - to_milli_time($params['notice_will_start']);

            if ($times < MILLI_TIME) {
                E('_ERR_SEND_WILL_TIME_MAX_NOW_TIME');
            }
        }

        // 直播开始前 (分钟)推送消息提醒
        if (isset($params['notice_will_type']) && $params['notice_will_type'] == Constant::NOTICE_START_TRUE) {
            if (isset($params['notice_will_start']) && $params['notice_will_start'] <= 0) {
                E('_ERR_SEND_WILL_TIME_MUST_ABOVE');
            }
        }

        if (empty($params)) {
            E('_ERR_PARAMS_CAN_NOT_EMPTY');
        }

        foreach ($field as $item) {
            if (array_key_exists($item, $params)) {
                $setting[$item] = $params[$item];
            }
        }

        if (empty($setting)) {
            E('_ERR_SETTING_FILED_IS_EMPTY');
        }
        return $setting;
    }


    /**
     * 更新发送消息
     * @param array $params
     * @param array $uc_info UC详情
     * @param array $vh_set_info 数据表详情
     */
    public function update_send_msg($params = [], $uc_info = [], $vh_set_info = [])
    {

        // 初始化微吼直播类
        $vh_class = Vhall::instance();
        // 获取微吼直播详情
        $info = $vh_class->details($params['id']);

        $params['liveVhallId'] = $info['liveVhallId'];

        //如果内部讲师  推送消息
        if ($params['teacher_type'] == Constant::INSIDE_TEACHER_TYPE && $uc_info['liveType'] == Constant::LIVE_WEB_STATUS_DRAFT_TYPE) {
            //复制数据 导师发送消息过程需 去除权限数据
            $data = $params;
            $this->vhMsgLogServ->send_teacher_msg($data, Constant::MSG_LECTURER_APPOINTMENT);
        }

        // 如果选择了推送消息
        if (Constant::NOTICE_POSTING_TRUE == $params['notice_posting'] && $uc_info['liveType'] == Constant::LIVE_WEB_STATUS_DRAFT_TYPE) {

            // 如果是全公司
            if (!empty($params['liveScopeAll'])) {
                // 写入记录
                $this->vhMsgLogServ->sendMsgRangeDiffUids($params['id'], [], true);

            } else {

                // 获取有权限的用户UIDS集合
                $uids = $this->vhMsgLogServ->get_auth_uids($params);

                // 组装发消息数据
                $params['liveScopeUsers'] = $this->vhMsgLogServ->sendMsgRangeDiffUids($params['id'], $uids);
            }

            // 直播开始发送消息
            $this->vhMsgLogServ->send_vh_Msg($params, Constant::MSG_LIVE_PUBLISH);
        }

        $Cron = new Cron(Service::instance());

        // 更新条件
        $conds = [
            'vh_id' => $params['id']
        ];

        // 直播开始前分钟数开关选中
        if ($params['notice_will_type'] == Constant::NOTICE_START_TRUE) {

            if ($params['notice_will_start'] != $vh_set_info['notice_will_start']
                ||
                $uc_info['liveType'] == Constant::LIVE_WEB_STATUS_DRAFT_TYPE) {
                // 如果存在计划任务就删除
                if (!empty($vh_set_info['notice_will_start_cron_id'])) {
                    try {
                        $Cron->delete($vh_set_info['notice_will_start_cron_id']);
                    } catch (Exception $exception) {
                    }

                }

                // 更新为没有定时任务
                $this->vhall_service->update_by_conds($conds, ['notice_will_start_cron_id' => '']);

                $this->vhMsgLogServ->createCron(
                    $params['id'],
                    'live_notice_will_start',
                    Constant::TYPE_LIVE_WILL_START,
                    $params['liveStartTime'] - $params['notice_will_start'] * 60 * 1000
                );
            }
        } else {
            // 删除任务
            if (!empty($vh_set_info['notice_will_start_cron_id'])) {
                try {
                    $Cron->delete($vh_set_info['notice_will_start_cron_id']);
                } catch (Exception $exception) {
                }
                $this->vhall_service->update_by_conds($conds, ['notice_will_start_cron_id' => '']);
            }

        }

        // 直播开始时推送消息
        if (Constant::NOTICE_START_TRUE == $params['notice_start']) {

            if ($params['liveStartTime'] != $uc_info['liveStartTime']
                ||
                $uc_info['liveType'] == Constant::LIVE_WEB_STATUS_DRAFT_TYPE
            ) {
                if (!empty($vh_set_info['notice_notice_start_cron_id'])) {
                    $Cron->delete($vh_set_info['notice_notice_start_cron_id']);
                    $this->vhall_service->update_by_conds($conds, ['notice_notice_start_cron_id' => '']);
                }

                // 直播开始时
                $this->vhMsgLogServ->createCron(
                    $params['id'],
                    'live_notice_start',
                    Constant::TYPE_LIVE_START,
                    $params['liveStartTime']
                );
            }
        }

        // 首页推荐
        if (Constant::RECOMEND_TRUE == $params['recomend']) {
            //移除之前推荐
            $this->vhall_service->openOrCloseRecommender($params, false);
            // 新建移动端首页推荐
            $this->vhall_service->openOrCloseRecommender($params);
        }

        // 首页取消推荐
        if (Constant::RECOMEND_FALSE == $params['recomend'] && $params['recomend'] != $vh_set_info['recomend']) {
            // 移动端取消推荐
            $this->vhall_service->openOrCloseRecommender($params, false);
        }
    }

    /**
     * 处理 "更多设置"
     * @param       $data
     * @return bool
     */
    protected function dealWithMoreSetting($data = [])
    {
        // 发布时推送消息提醒
        if (isset($data['notice_posting']) && $data['notice_posting'] == Constant::NOTICE_POSTING_TRUE) {

            // 如果是全公司
            if (!empty($data['liveScopeAll'])) {
                // 写入记录
                $this->vhMsgLogServ->sendMsgRangeDiffUids($data['id'], [], true);

            } else {
                // 获取有权限的用户UIDS集合
                $uids = $this->vhMsgLogServ->get_auth_uids($data);

                // 组装发消息数据
                $data['liveScopeUsers'] = $this->vhMsgLogServ->sendMsgRangeDiffUids($data['id'], $uids);
            }

            // 直播地址
            $this->vhMsgLogServ->send_vh_Msg($data, Constant::MSG_LIVE_PUBLISH);
        }

        // 直播开始前 (分钟)推送消息提醒
        if (isset($data['notice_will_type']) && $data['notice_will_type'] == Constant::NOTICE_START_TRUE) {
            if (isset($data['notice_will_start']) && $data['notice_will_start'] > 0) {
                // 直播开始前 推送消息的时间点
                $this->vhMsgLogServ->createCron(
                    $data['id'],
                    'live_notice_will_start',
                    CronModel::TYPE_LIVE_WILL_START,
                    $data['liveStartTime'] - $data['notice_will_start'] * 60 * 1000
                );
            }
        }


        // 直播开始时推送消息
        if (isset($data['notice_start']) && $data['notice_start'] == Constant::NOTICE_START_TRUE) {
            $this->vhMsgLogServ->createCron(
                $data['id'],
                'live_notice_start',
                CronModel::TYPE_LIVE_START,
                $data['liveStartTime']
            );
        }

        if (isset($data['recomend']) && $data['recomend'] == Constant::RECOMEND_TRUE) {
            // 移动端首页推荐
            $this->vhall_service->openOrCloseRecommender($data);
        }

        return true;
    }
}