StatisticsController.class.php 2.55 KB
<?php
/**
 * 考试定时任务
 * User: heyuelong
 * Date: 2017年06月23日16:37:37
 */

namespace Frontend\Controller\Callback;

use Com\Service;
use Common\Service\AnswerDetailService;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use VcySDK\Cron;

class StatisticsController extends AbstractController
{
    // 默认执行条数
    const LIMIT = 500;
    // 锁定计划任务数据状态
    const CRON_CLOSE = 3;

    /** @var  AnswerDetailService */
    protected $answer_serv;
    /** @var  AnswerDetailService */
    protected $answer_detail_serv;
    /** @var  PaperService */
    protected $papar_serv;

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

            return false;
        }

        $this->answer_serv = new AnswerService();
        $this->answer_detail_serv = new AnswerDetailService();
        $this->papar_serv = new PaperService();

        return true;
    }

    public function Index()
    {
        // 获取需要发送的试卷id
        $back = $this->callBackData;
        $ep_id = $back['ep_id'];

        if (empty($ep_id)) {

            $ep_id = I('get.ep_id');
        }
        $time = MILLI_TIME - 900000;
        $this->answer_detail_serv->update_by_conds(array('cron_status' => self::CRON_CLOSE, 'updated<?' => $time),
            array('cron_status' => AnswerService::CRON_UNDO_STATE));
        $condition = array(
            'cron_status' => AnswerService::CRON_UNDO_STATE,
        );

        $condition['ep_id'] = $ep_id;

        $order_by = array('ead_id' => 'DESC');
        $data = $this->answer_detail_serv->list_by_conds($condition, self::LIMIT, $order_by);
        // 统计数据处理
        if (!empty($data)) {
            $ead_ids = array_column($data, 'ead_id');

            $this->answer_detail_serv->update_by_conds(array('ead_id' => $ead_ids),
                array('cron_status' => self::CRON_CLOSE));
            $this->answer_detail_serv->cron_statistics($data, $ep_id);
        }

        $paper = $this->papar_serv->get($ep_id);


        if ($paper['end_time'] < $paper['makeup_end_time']) {
            $end_time = $paper['makeup_end_time'];
        } else {
            $end_time = $paper['end_time'];
        }

        $del_time = $end_time + 2 * 24 * 60 * 60 * 1000;
        if ($del_time < MILLI_TIME && !empty($paper['cron_statistics'])) {
            // 考试结束后两天删除统计任务
            $cron = new Cron(\VcySDK\Service::instance());
            $cron->delete($paper['cron_statistics']);
        }

        return true;
    }
}