PdfZipController.class.php 5.16 KB
<?php
/**
 * 【后台】批量导出学员成绩单
 * PdfZipController.class.php
 * @author:heyuelong
 * @date:2018年4月23日10:01:06
 */

namespace Apicp\Controller\Export;

use Common\Common\Cache;
use Common\Common\Department;
use Common\Common\ExamHelper;
use Common\Common\ExportDownload;
use Common\Service\AnswerService;
use Common\Service\AnswerDetailService;
use Common\Service\PaperService;

class PdfZipController extends AbstractController
{

    /** @var  PaperService  实例化试卷对象 */
    protected $paper_service;
    /** @var  AnswerService  实例化答卷表对象 */
    protected $answer_service;
    /** @var  AnswerDetailService  实例化答卷详情表对象 */
    protected $answer_detail_service;
    /** @var Department */
    protected $department;

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

        $this->paper_service = new PaperService();
        $this->answer_service = new AnswerService();
        $this->answer_detail_service = new AnswerDetailService();
        $this->department = &Department::instance();
        return true;
    }

    public function Index()
    {

        $params = I('post.');

        // 试卷id为空,返回提示""
        if (!$params['ep_id']) {
            return true;
        }

        $types = array(
            ExamHelper::HAS_JOINED,
            ExamHelper::NOT_JOINED
        );

        // 导出类型不正确
        if (!in_array($params['type'], $types)) {
            E('_ERR_EXPORT_TYPE');
        }


        // 获取试卷信息
        $paper = $this->paper_service->get($params['ep_id']);

        // 试卷不存在
        if (empty($paper)) {
            E('_ERR_PAPER_NOT_FOUND');
        }

        // 拼接数组
        if (!empty($params['dpIds'])) {
            $params['dpIds'] = explode(';', $params['dpIds']);
        }

        // 拼接数组
        if (!empty($params['end_time'])) {

            $params['end_time'] = explode(';', $params['end_time']);
        }

        // 初始化列表数据
        $lists = [];

        // 【已参与】人员列表(测评试卷)
        if (
            $params['type'] == ExamHelper::HAS_JOINED &&
            $paper['paper_type'] == PaperService::EVALUATION_PAPER_TYPE
        ) {

            // 组装查询条件
            $conds = $this->answer_service->get_conditions($paper, $params);

            // 【考试完成时间】
            if (isset($params['end_time']) && !empty($params['end_time']) && is_array($params['end_time'])) {

                $end_time = $params['end_time'];
                $end_time1 = $end_time[0];
                $end_time2 = $end_time[1];
                if ($end_time1 && $end_time2) {

                    $conds['my_end_time>=?'] = $end_time1;
                    $conds['my_end_time<=?'] = $end_time2;
                }
            }

            // 最高分记录
            $conds['is_score_top'] = PaperService::IS_SCORE_TOP_TRUE;

            // 参与这场考试的人员的考试信息
            $lists = $this->answer_service->list_by_conds($conds, null, [], 'ea_id');

        }

        // 【已参与】人员列表(模拟试卷)
        if (
            $params['type'] == ExamHelper::HAS_JOINED &&
            $paper['paper_type'] == PaperService::SIMULATION_PAPER_TYPE
        ) {

            // 组装查询条件
            $conds = $this->answer_service->get_conditions($paper, $params);
            // 【考试完成时间】
            if (isset($params['end_time']) && !empty($params['end_time']) && is_array($params['end_time'])) {

                $end_time = $params['end_time'];
                $end_time1 = $end_time[0];
                $end_time2 = $end_time[1];
                if ($end_time1 && $end_time2) {

                    $conds['my_end_time>=?'] = $end_time1;
                    $conds['my_end_time<=?'] = $end_time2;
                }
            }

            // 参与考试的人员的考试信息
            $lists = $this->answer_service->get_mock_join_list($conds, null, []);
        }

        // 如果存在数据则生成
        if (!empty($lists)) {

            $title = $paper['ep_name'] . '_已参与批量导出成绩单';

            $params_down = [
                'title' => $title,
                'ea_id' => $this->_login->user['eaId'],
                'username' => $this->_login->user['eaRealname'],
                'type' => ExportDownload::ZIP_TYPE
            ];

            // 新增开始生成状态
            $id = ExportDownload::set_down_load($params_down);

            $ea_ids = array_column($lists, 'ea_id');

            $params = [
                'user_id' => $this->_login->user['eaId'],
                'time' => microtime(true),
                'id' => $id,
                'title' => $title
            ];

            // 生成缓存
            Cache::instance()->set('Common.Exam_Down_Pdf_' . $id, $ea_ids);

            $url = oaUrl('Frontend/Callback/DownPdf');

            // 生成定时任务
            ExportDownload::set_Cron($params, $url, '考试中心成绩单生成PDF批量下载', 'Exam_Down_Pdf_' . $id);

        };

        return true;
    }
}