WriteListBySheetController.class.php
13.3 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* 【调研中心-后台】20_已填写人员数据导出(导出选项值)
* WriteListBySheetController.class.php
* CreateBy:dj
* Date:2018-04-20
*/
namespace Apicp\Controller\Export;
use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Service\AnswerService;
use Common\Service\BaseinfoService;
use Common\Service\QuestionService;
use Common\Service\RecordService;
use Org\Util\Emoji;
class WriteListBySheetController extends \Apicp\Controller\AbstractController
{
/** @var BaseinfoService */
protected $_base_info_s;
/** @var QuestionService */
protected $_question_s;
/** @var RecordService */
protected $_record_s;
/** @var AnswerService */
protected $_answer_s;
protected $p_type = [
'',
'【单项选择】',
'【多项选择】',
'【下拉选择】',
'【单行文本】',
'【多行文本】',
'【日期时间】',
'【数字】',
'【金额】',
'【评分】',
'【上传图片】',
'【上传文件】',
'【段落说明】',
'【姓名】',
'【性别】',
'【手机号】',
'【邮箱】',
'【微信号】',
'【生日】',
'【公司】',
'【组织】',
'【职位】',
'【地址】',
];
// 前置操作
public function before_action($action = '')
{
if (parent::before_action($action) === false) {
return false;
}
// 调研基本信息表
$this->_base_info_s = new BaseinfoService();
// 调研问题信息表
$this->_question_s = new QuestionService();
// 调研回答详情信息表
$this->_record_s = new RecordService();
// 调研回答用户记录表
$this->_answer_s = new AnswerService();
return true;
}
public function Index()
{
// 调研id
$qu_id = trim(I('post.qu_id'));
// 调研基本信息
$base_info = $this->_base_info_s->get($qu_id);
// 查询调研题目信息列表
$question_list = $this->_question_s->list_by_conds(['qu_id' => $qu_id], null, ['q_order' => 'ASC']);
// 数据格式化
list($list_order, $list_option) = $this->format_data($base_info, $question_list, $qu_id);
$this->export($base_info, $question_list, $list_order, $list_option);
return true;
}
/**
* 获取数据列表并格式化
*
* @param $base_info 调研基本信息
* @param $question_list 问题列表
* @param $qu_id 调研id
*
* @return array
*/
private function format_data($base_info, $question_list, $qu_id)
{
$list_order = []; // 按选项序号展示
$list_option = [];// 按选项内容展示
// 调研填写记录信息
$answer_list = $this->_answer_s->list_by_conds(['qu_id' => $qu_id]);
// 填写用户id集合
$uids = array_unique(array_filter(array_column($answer_list, 'uid')));
sort($uids);
// 调研填写详情记录信息列表
$record_list = $this->_record_s->list_by_conds(['qu_id' => $qu_id]);
$emoji = Emoji::instance();
// 查询用户列表,将用户列表转换成以用户uid为主键的二维数组
$user_list = $this->_base_info_s->getUser($uids);
// 序号
$i = 1;
foreach ($answer_list as $k => $v) {
// 内部用户
if (!empty($v['uid'])) {
// 内部用户
$u_type = '内部用户';
// 实名
if (BaseinfoService::REAL_NAME == $base_info['anonymous']) {
// 用户信息
$user_data = $user_list[$v['uid']];
// 组织名称
$dp_name = '';
// 组织名称赋值
if (!empty($user_data['dpName'])) {
$dp_name_array = array_column($user_data['dpName'], 'dpName');
$dp_name = implode(',', $dp_name_array);
}
$name = $user_data['memUsername'];
$dpName = !empty($dp_name) ? $dp_name : '';
$jobName = !empty($user_data['memJob']) ? $user_data['memJob'] : '';
$roleName = !empty($user_data['memRole']) ? $user_data['memRole'] : '';
$mobile = !empty($user_data['memMobile']) ? $user_data['memMobile'] : '';
} elseif (BaseinfoService::ANONYMOUS == $base_info['anonymous']) { // 匿名
$name = '匿名用户' . $i;
$dpName = '';
$jobName = '';
$roleName = '';
$mobile = '';
}
} elseif (!empty($v['openid'])) { // 外部用户
$name = $v['username']; // 微信昵称
$dpName = '';
$jobName = '';
$roleName = '';
$mobile = '';
$u_type = '外部用户'; // 外部用户
}
$list_order[$k] = [
$i, // 序号
$name, // 用户姓名
$dpName, // 组织名称
$jobName, // 岗位名称
$roleName, // 角色名称
$mobile, // 用户手机
$u_type, // 用户类型:内部用户/外部用户
rgmdate($v['created'], 'Y-m-d H:i') // 填写时间
];
$list_option[$k] = [
$i, // 序号
$name, // 用户姓名
$dpName, // 组织名称
$jobName, // 岗位名称
$roleName, // 角色名称
$mobile, // 用户手机
$u_type, // 用户类型:内部用户/外部用户
rgmdate($v['created'], 'Y-m-d H:i') // 填写时间
];
// 填写详情记录
$answer_data = [];
// 填写详情记录赋值
foreach ($record_list as $_v) {
if ($_v['a_id'] == $v['a_id']) {
$answer_data[] = $_v;
}
}
// 将此数组转换成以问题ID为主键的新数组
$answer_data = array_combine_by_key($answer_data, 'q_id');
// 遍历问题信息
foreach ($question_list as $_val) {
// 问题的填写记录
$answer = $answer_data[$_val['qid']];
// 单选、复选、下拉选择、性别选择
if (in_array($_val['q_type'], QuestionService::OPTION_TYPE)) {
// 问题内容
$option_arr = unserialize($_val['q_field']);
// 回答内容
$answer_arr = unserialize($answer['answer']);
$answer_option = array_column($answer_arr, 'option');
$answer_img = array_unique(array_filter(array_column($answer_arr, 'option_img')));
$answer_index = '';
$answer_str = '';
// 循环选项,看选项是否选中
for ($n = 0; $n < count($option_arr); $n++) {
if (empty($answer['answer'])) {
continue;
}
// 如果选项中不包含图片,则只用 选项值就可以判断
if (empty($option_arr[$n]['option_img'])) {
if (in_array($option_arr[$n]['option'], $answer_option)) { // 选中
if (empty($answer_str)) {
$answer_str = $option_arr[$n]['option'];
$answer_index = $n + 1;
} else {
$answer_str .= ',' . $option_arr[$n]['option'];
$answer_index .= ',' . ($n + 1);
}
}
} else {
if (in_array($option_arr[$n]['option'], $answer_option)
&& in_array($option_arr[$n]['option_img'], $answer_img)
) { // 选中
if (empty($answer_str)) {
$answer_str = $option_arr[$n]['option'];
$answer_index = $n + 1;
} else {
$answer_str .= ',' . $option_arr[$n]['option'];
$answer_index .= ',' . ($n + 1);
}
}
}
}
// 是否包含其他选项(0:不包含,1:包含)
if ($_val['q_other']) {
// 包含其他选项
if (!empty($answer['other'])) {
$other_answer = '【其他】:' . $answer['other'];
if (empty($answer_str)) {
$answer_str = $other_answer;
$answer_index = $other_answer;
} else {
$answer_str .= ',' . $other_answer;
$answer_index .= ',' . $other_answer;
}
}
}
$list_order[$k][] = $answer_index;
$list_option[$k][] = $answer_str;
} else {
// 上传图片和上传文件类型
if (in_array($_val['q_type'], QuestionService::FILE_TYPE)) {
// 回答内容
$answer_arr = unserialize($answer['answer']);
$answer_img = array_column($answer_arr, 'option_img');
$imgs = '';
// 循环选项,看选项是否选中
foreach ($answer_img as $v) {
if (!empty($v)) {
$imgs .= "\r\n" . imgUrl($v);
}
}
// 图片集合
$list_order[$k][] = $imgs;
$list_option[$k][] = $imgs;
} else {
// 地址
if ($_val['q_type'] == 22) {
$answer_res = $answer['answer'] . ' ' . $answer['other'];
$list_order[$k][] = $answer_res;
$list_option[$k][] = $answer_res;
} else {
$answer_res = $emoji->filter_emoji($answer['answer']);
$list_order[$k][] = $answer_res;
$list_option[$k][] = $answer_res;
}
}
}
}
$i++;
}
return [$list_order, $list_option];
}
/**
* 导出模板信息
*
* @param array $base_info 问卷基本信息
* @param array $question_list 问题列表
* @param array $list_order 按选项序号展示列表数据
* @param array $list_option 按选项内容展示列表数据
*/
private function export($base_info, $question_list, $list_order, $list_option)
{
// 文件名
$fileName = $base_info['title'];
// 组装表头问题选项行
$index_arr = ['序号', '姓名', '组织', '岗位', '角色', '手机号', '用户类型', '填写时间'];
foreach ($question_list as $k => $v) {
$q_type = $this->p_type[$v['q_type']];
$index_arr[] = $q_type . ($k + 1) . "." . $v['q_title'];
}
// 组装数组
$data = [
[
'columns' => $index_arr, // 列名
'rows' => $list_order, // 数据
],
[
'columns' => $index_arr, // 列名
'rows' => $list_option, // 数据
],
];
// sheets名数组
$sheets = ["按选项序号展示", "按选项内容展示"];
// Python导出excel
$realpath = ExportDownload::get_down_dir($this->_login->user['eaId'] . microtime(true)) . $fileName.'_已填写人员数据' . ".xls";
$ret = PythonExcel::instance()->writeSheet($realpath, $sheets, $data);
if ($ret) {
$data = [
'title' => $fileName,
'ea_id' => $this->_login->user['eaId'],
'type' => ExportDownload::EXCEL_TYPE,
'username' => $this->_login->user['eaRealname'],
'url' => $realpath,
'app_dir' => APP_DIR
];
ExportDownload::insert_down_load($data);
}
return true;
}
}