RemindBeginController.class.php 3.06 KB
<?php
/**
 * 考试开始前定时提醒回调
 */

namespace Frontend\Controller\Callback;

use Common\Common\Msg;
use Common\Service\AnswerService;
use Common\Service\RightService;
use VcySDK\Cron;
use VcySDK\Logger;
use VcySDK\Service;
use Common\Service\PaperService;

class RemindBeginController extends AbstractController
{
    /** @var PaperService 试卷信息表 */
    protected $paper_s;
    /** @var RightService 用户回复信息表 */
    protected $right_s;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化试卷表
        $this->paper_s = new PaperService();
        // 实例化权限表
        $this->right_s = new RightService();

        return true;
    }

    public function Index()
    {

        // 获取需要发送的试卷id
        $back = $this->callBackData;
        $ep_id = $back['ep_id'];
        Logger::write('=========进入开始前提醒定时任务=======ep_id' . $ep_id);
        // 非空判断
        if (empty($ep_id)) {

            return true;
        }

        // 获取试卷基本详情
        $data = $this->paper_s->get($ep_id);
        if (empty($data)) {

            return true;
        }

        $conds = array(
            'epc_id' => $ep_id,
            'er_type' => AnswerService::RIGHT_PAPER
        );

        // 参与考试的权限范围
        if ($data['is_all'] != RightService::AUTH_ALL) {

            $rights = $this->right_s->list_by_conds($conds);
            // 格式化权限范围
            $right_view = $this->right_s->format_db_data($rights);
        }

        $right_view['is_all'] = $data['is_all'];

        // 获取要参加考试的全部人员
        $all_join = $this->right_s->list_uids_by_right($right_view);

        // 如果还没有发送过消息
        if (count($all_join) > 0) {

            Logger::write('用户ID集合存在');

            sort($all_join);

            // 分割成多个元素分组
            $uid_groups = array_chunk($all_join, Msg::USER_MAX_COUNT);

            $params['id'] = $ep_id;
            $params['name'] = $data['ep_name'];
            $params['description'] = $data['intro'];
            $params['begin_time'] = $data['begin_time'];
            $params['end_time'] = $data['end_time'];
            $params['img_id'] = $data['cover_id'];
            $params['is_cover_open'] = $data['is_cover_open'];
            // 分批发送
            foreach ($uid_groups as $uid_group) {

                $params['uids'] = $uid_group;

                // 推送消息
                $this->paper_s->send_msg($params, PaperService::ANSWER_START);

            }

        }

        $crId = strval($data['cron_send_msg']);
        Logger::write('删除任务ID:' . $crId);

        // 一次执行的定时任务已经执行,所以删除paper表中储存的cronId
        $this->paper_s->update($ep_id,
            array(
                'begin_corn' => ''
            ));

        Logger::write('=========结束开始前提醒定时任务=======');

        return true;
    }
}