RemindEndController.class.php 2.78 KB
<?php
/**
 * 考试结束前定时提醒回调
 */

namespace Frontend\Controller\Callback;

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

class RemindEndController extends AbstractController
{
    /** @var  RightService  用户回复信息表 */
    protected $right_s;

    /** @var PaperService 试卷信息表 */
    protected $paper_s;

    /** @var AnswerService 回答信息表 */
    protected $answer_s;

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

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

        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
        );

        // 获取未参与考试人员列表及人数
        $data_users = $this->answer_s->get_unjoin_data($conds, $ep_id, $data['is_all']);

        $uids = $data_users['unjoin_list'];

        // 无需发送消息的情况
        if (empty($uids)) {
            return true;
        }

        $params['name'] = $data['ep_name'];
        $params['description'] = $data['intro'];
        $params['img_id'] = $data['cover_id'];
        $params['is_cover_open'] = $data['is_cover_open'];
        $params['begin_time'] = $data['begin_time'];
        $params['end_time'] = $data['end_time'];
        $params['id'] = $ep_id;

        Logger::write(var_export($params, true));

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

        // 分批发送
        foreach ($uid_groups as $uid_group) {

            $params['uids'] = $uid_group;

            // 发送考试结束前提醒
            $this->paper_s->send_msg($params, PaperService::ANSWER_COMING_END);

        }

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

        Logger::write('=========over结束前提醒定时任务=======ep_id:' . $ep_id);
        return true;
    }
}