DownPdfController.class.php 6.77 KB
<?php
/**
 * 【后台】批量生成学员成绩单
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2017/8/9
 * Time: 下午3:12
 */

namespace Frontend\Controller\Callback;

use Common\Common\Cache;
use Common\Common\Department;
use Common\Common\ExamHelper;
use Common\Common\ExportDownload;
use Common\Common\User;
use Common\Service\AnswerService;
use Common\Service\AnswerDetailService;
use Common\Service\PaperService;
use Org\Net\PHPZip;
use Think\Exception;
use Think\Log;


class DownPdfController extends AbstractController
{
    protected $_require_login = false;

    /** @var  PaperService  实例化试卷对象 */
    protected $paper_service;
    /** @var  AnswerService  实例化答卷表对象 */
    protected $answer_service;
    /** @var  AnswerDetailService  实例化答卷详情表对象 */
    protected $answer_detail_service;
    /** @var Department */
    protected $department;
    // 毫秒时间戳
    protected $time = 0;
    // 下载中心ID
    protected $id = 0;
    // 用户ID
    protected $user_id = 0;
    // 文件名称
    protected $title = '';

    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()
    {
        $back = $this->callBackData;
        $this->user_id = $back['user_id'];
        $this->id = $back['id'];
        $this->time = $back['time'];
        $this->title = $back['title'];

        Log::record('=========批量生成PDF回调参数=======:' . print_r($back, true));

        // 用户ID不能为空
        if (empty($this->user_id)) {

            return true;
        }

        // 时间不能为空
        if (empty($this->time)) {

            return true;
        }

        // 文件ID
        if (empty($this->id)) {

            return true;
        }

        $ea_ids = [];
        try {
            $ea_ids = Cache::instance()->get('Common.Exam_Down_Pdf_' . $this->id);
        } catch (Exception $e) {

            Log::write('=========批量生成PDF已全部生成=====文件ID==' . $this->id, 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

            // 获取需要打包的目录
            $dir_tmp = ExportDownload::get_down_dir($this->user_id . $this->time . D_S . $this->time . $this->id . $this->user_id);

            $phpzip = new PHPZip();

            // 组装压缩文件名称
            $filename = ExportDownload::get_down_dir($this->user_id .'H'. microtime(true)) . microtime(true). '.zip';
            
            // 打包文件
            $phpzip->zipDir($dir_tmp, $filename, false);

            // 更新状态
            $data = [
                'url' => $dir_tmp,
                'id' => $this->id,
                'size'=>filesize($filename)
            ];

            ExportDownload::update_down_load($data);

            unlink($filename);

            return false;
        }

        Log::write('=========批量生成PDF获取到的缓存数据=======' . print_r($ea_ids, true), 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

        // 如果存在数据
        if (!empty($ea_ids)) {

            $i = 0;
            foreach ($ea_ids as $key => $v) {
                if ($i < 3) {

                    Log::write('=========批量生成PDF文件ID=======' . $this->id . '====开始执行次数======' . $i . '==答卷ID===' . $v, 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

                    $this->create_pdf($v);
                    unset($ea_ids[$key]);

                    Log::write('=========批量生成PDF文件ID=======' . $this->id . '====已执行次数======' . $i, 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');
                } else {
                    break;
                }
                $i++;
            }
        }

        // 拼接参数
        $params = [
            'user_id' => $this->user_id,
            'time' => $this->time,
            'id' => $this->id,
            'title' => $this->title
        ];


        Log::write('=========批量生成PDF异步提交参数=======' . print_r($params, true), 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

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

        // 写入缓存
        Cache::instance()->set('Common.Exam_Down_Pdf_' . $this->id, empty($ea_ids) ? null : $ea_ids);

        return true;
    }

    /**
     * 通过答卷ID生成pdf
     * @param int $ea_id 答卷ID
     * @return bool
     */
    public function create_pdf($ea_id = 0)
    {

        // 获取答卷信息
        $answer = $this->answer_service->get($ea_id);
        // 答卷不存在
        if (empty($answer)) {

            return true;
        }

        // 根据UID获取用户信息
        $user_service = &User::instance();

        // 获取答卷用户信息
        $user = $user_service->getByUid($answer['uid']);

        // 获取阅卷人信息
        if (empty($answer['marking_name'])) {

            $marking_user = $user_service->getByUid($answer['marking_uid']);
            $answer['marking_name'] = $marking_user['memUsername'];
        }

        // 获取答卷详情
        $answer_detail = $this->answer_detail_service->list_by_conds(['ea_id' => $answer['ea_id']]);

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

        // 格式化答卷详情
        $answer_list = $this->answer_detail_service->question_list_format(
            $answer_detail,
            $paper['marking_type'],
            AnswerService::ANSWER_PC_ANALYSIS
        );

        // 组装返回数据
        $data = array(
            'ep_name' => $paper['ep_name'],
            'my_score' => $answer['my_score'],
            'username' => $user['memUsername'],
            'marking_name' => $answer['marking_name'] ? $answer['marking_name'] : '系统阅卷',
            'answer_status' => intval($answer['answer_status']),
            'marking_time' => $answer['marking_time'],
            'marking_type' => intval($paper['marking_type']),
            'list' => $answer_list
        );

        Log::write('=========批量生成PDF生成PDF参数=======' . print_r($data, true), 'ERR', '', C('LOG_PATH') . date('y_m_d') . '.txt');

        // 成绩单名称
        $title = $paper['ep_name'].'_'.$user['memUsername'] . '_成绩单';

        // 生成PDF
        ExamHelper::instance()->create_pdf($title, $data, $this->user_id . $this->time . D_S . $this->time . $this->id . $this->user_id);

        return true;
    }
}