ExportExamCountController.class.php
6.84 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
<?php
/**
* Created by PhpStorm.
* User: yingcai
* Date: 2017/10/13
* Time: 下午8:12
*/
namespace Apicp\Controller\Exam;
use Com\PackageValidate;
use Com\PythonExcel;
use Common\Common\Constant;
use Common\Common\ExamHelper;
use Common\Common\ExportDownload;
use Common\Common\User;
use Common\Model\ExamPaperModel;
class ExportExamCountController extends \Apicp\Controller\AbstractController
{
/**
* ExportExamCount
* @author houyingcai
* @desc 员工考试统计导出
* @param String dp_ids 组织ID
* @param String role_ids 角色ID
* @param String job_ids 岗位ID
* @param String username 姓名
* @param Int ec_id 考试分类ID
* @param String ep_name 试卷名称
* @param Int score_order:1 1:员工考试时间降序,2:成绩升序,3:成绩降序
* @return void
*/
public function Index_post()
{
// 验证规则
$rules = [
'username' => 'max:64',
'ec_id' => 'integer|gt:0',
'ep_name' => 'max:64',
'score_order' => 'integer|in:1,2,3',
];
// 验证请求数据
$postData = I('post.');
$validate = new PackageValidate();
$validate->postData = $postData;
$validate->validateParams($rules);
// 初始化查询条件
$conds = [];
// 组合人员搜索条件
$user_conds = [];
if (isset($postData['dp_ids'])) {
$dp_ids = array_filter($postData['dp_ids']);
$user_conds['dpIdList'] = $dp_ids;
// 按部门条件查询时,表示部门是否递归查询人员 【0:不递归(默认值)、1:递归】
$user_conds['departmentChildrenFlag'] = 1;
}
if (isset($postData['role_ids'])) {
$role_ids = array_filter($postData['role_ids']);
$user_conds['roleIdList'] = $role_ids;
}
if (isset($postData['job_ids'])) {
$job_ids = array_filter($postData['job_ids']);
$user_conds['jobIdList'] = $job_ids;
}
if (isset($postData['username'])) {
$user_conds['memUsername'] = $postData['username'];
}
// 获取符合条件的人员UID
$userServ = &User::instance();
if (!empty($user_conds)) {
$user_conds['memAll'] = 1; // 可以查询删除人员
// 获取符合条件的人员UID
$userList = $userServ->listUsersAll($user_conds);
$userList = array_combine_by_key($userList, 'memUid');
if (!empty($user_conds)) {
$conds['uid'] = empty($userList) ? '' : array_column($userList, 'memUid');
}
}
// 组合试卷搜索条件
$paper_conds = [
'exam_type' => Constant::EXAM_TYPE,
];
if (isset($postData['ec_id'])) {
$paper_conds['ec_id'] = $postData['ec_id'];
}
if (isset($postData['ep_name'])) {
$postData['ep_name'] = str_replace("%", '\%', $postData['ep_name']);
$paper_conds['ep_name like ?'] = '%' . $postData['ep_name'] . '%';
}
// 获取符合条件的试卷ID
$paperModel = new ExamPaperModel();
$paperList = $paperModel->list_by_conds($paper_conds);
$paperList = array_combine_by_key($paperList, 'ep_id');
$conds['ep_id'] = empty($paperList) ? '' : array_column($paperList, 'ep_id');
// 排序
if ($postData['score_order'] == 2) {
$order_option = ['my_score' => 'ASC', 'my_time' => 'ASC'];
} elseif ($postData['score_order'] == 3) {
$order_option = ['my_score' => 'DESC', 'my_time' => 'ASC'];
} else {
$order_option = ['my_begin_time' => 'DESC'];
}
$conds['answer_status'] = 2; // 已批阅(考试中心迭代后数据已更新:1;待批阅,2:已批阅)
$exam_helper = &ExamHelper::instance();
// 获取员工考试统计列表
$result_list = $exam_helper->list_user_exam_count($conds, null, $order_option);
if (empty($userList)) {
$uids = array_unique(array_column($result_list, 'uid'));
// 获取符合条件的人员UID
$userList = $userServ->listByUid($uids);
$userList = array_combine_by_key($userList, 'memUid');
}
// Excel列数据
$rows = [];
foreach ($result_list as $v) {
$paper = $paperList[$v['ep_id']];
$user = $userList[$v['uid']];
// 获取被删除的用户
if (empty($user)) {
$user = $userServ->getByUid($v['uid']);
}
// 组织
$dpNames = !empty($user['dpName']) ? implode(',', array_column($user['dpName'], 'dpName')) : '';
// 是否通过
$my_is_pass = $v['my_is_pass'] ? '及格' : '不及格';
// 计算用时
$m = floor(intval($v['my_time'] / 1000) / 60) . '分';
$s = intval(ceil($v['my_time'] / 1000) % 60);
$s = $s ? $s . '秒' : '';
// 开始考试时间
$my_begin_time = !empty($v['my_begin_time']) ? rgmdate(strval($v['my_begin_time']), 'Y-m-d H:i') : '';
// 交卷试卷
$my_end_time = !empty($v['my_end_time']) ? rgmdate(strval($v['my_end_time']), 'Y-m-d H:i') : '';
// Excel列数据
$rows[] = [
$user['memUsername'],
$dpNames,
$user['memJob'],
$user['memRole'],
$paper['ep_name'],
$my_begin_time,
$my_end_time,
$m . $s,
$v['my_score'],
$my_is_pass,
];
}
// Excel文件名
$fileName = '员工考试统计_' . rgmdate(MILLI_TIME, 'YmdHi');
// Excel首行标题
$titles = ['姓名', '组织', '岗位', '角色', '考试名称', '开考时间', '交卷时间', '用时', '得分', '是否及格'];
$this->_download_by_python($fileName, $titles, $rows);
}
/**
* 通过python导出数据
* @param string $fileName 文件名
* @param array $titles 标题
* @param array $rows 数据列表
*/
private function _download_by_python($fileName, $titles, $rows)
{
$realpath = ExportDownload::get_down_dir($this->_login->user['eaId'].microtime(true)) . $fileName . '.xls';
// 生成 Excel 并输出
$ret = PythonExcel::instance()->write($realpath, $titles, $rows);
if ($ret) {
$data = [
'title' => $fileName,
'ea_id' => $this->_login->user['eaId'],
'username' => $this->_login->user['eaRealname'],
'type' => ExportDownload::EXCEL_TYPE,
'url' => $realpath
];
ExportDownload::insert_down_load($data);
}
return true;
}
}