ImportDataService.class.php
2.05 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
<?php
/**
* Created by IntelliJ IDEA.
* User: zhuxun37
* Date: 2017/5/17
* Time: 上午11:45
*/
namespace Common\Service;
use Com\PythonExcel;
use Common\Model\ImportDataModel;
class ImportDataService extends AbstractService
{
// 构造方法
public function __construct()
{
parent::__construct();
$this->_d = new ImportDataModel();
}
// 导出模板
public function exportError($request, $user)
{
$importFlag = (string)$request['importFlag'];
$condition = array(
'import_flag' => $importFlag,
'ea_id' => $user['eaId'],
'is_error' => ImportDataModel::IS_ERROR_TYPE_ERROR
);
$data = $this->_d->list_import_data_by_conds($condition, array('cid_id' => 'ASC'));
$titles = array();
$rows = array();
foreach ($data as $_data) {
if (empty($titles)) {
// 前期的解决方案是帮excel的title存储在库中,故第一条数据是title
$titles = json_decode($_data['data']);
// title中新增失败原因一列
$titles[] = '失败原因';
continue;
}
$errData = json_decode($_data['data']);
$errData[] = $_data['fail_message'];
// 将失败原因一并返回
$rows[] = $errData;
}
$filename = NOW_TIME . random(8) . '.xls';
PythonExcel::instance()->write(get_sitedir() . $filename, $titles, $rows);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename=importError.xls');
header("Content-Transfer-Encoding:binary");
echo file_get_contents(get_sitedir() . $filename);
exit;
}
}