DownPdfController.class.php
6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?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;
}
}