-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel.php
More file actions
212 lines (157 loc) · 6.88 KB
/
excel.php
File metadata and controls
212 lines (157 loc) · 6.88 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
<?php
$dbh = new PDO('mysql:host=localhost;dbname=', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec('set names utf8');
$classroom = $dbh->prepare("SELECT id, classType, categoryId, title, courseNum, expiryDay, createdTime, recommended, recommendedapp, status, fitCrowd, speService, virtualNum, iconNum FROM classroom ORDER BY id DESC");
$classroom->execute();
$classroom = $classroom->fetchAll(PDO::FETCH_ASSOC);
$classroom_id = implode(',', array_column($classroom, 'id'));
//$sql = "select ownerId, tagId from tag_owner WHERE ownerType = 'classroom' and ownerId IN ($classroom_id)";
//
//var_dump($sql);die;
$tag_owner = $dbh->prepare("select ownerId, tagId from tag_owner WHERE ownerType = 'classroom' and ownerId IN ($classroom_id)");
$tag_owner->execute();
$tag_owner = $tag_owner->fetchAll(PDO::FETCH_ASSOC);
$tag_id = implode(',', array_column($tag_owner, 'tagId'));
$tags = $dbh->prepare("select id, name from tag WHERE id IN ($tag_id)");
$tags->execute();
$tags = $tags->fetchAll(PDO::FETCH_ASSOC);
$tags_id = array_column($tags, 'id');
$tags = array_combine($tags_id, $tags);
foreach ($tag_owner as $tag_owner_key => $tag_owner_item) {
if (isset($tags[$tag_owner_item['tagId']])) {
$tag_owner[$tag_owner_key]['标签'] = $tags[$tag_owner_item['tagId']]['name'];
}
}
$tag_owner_id = array_column($tag_owner, 'ownerId');
$tag_owner = array_combine($tag_owner_id, $tag_owner);
//$category_id = implode(',', array_column($classroom, 'categoryId'));
$categorys = $dbh->prepare("select id, name, parentId from category");
$categorys->execute();
$categorys = $categorys->fetchAll(PDO::FETCH_ASSOC);
$categorys_id = array_column($categorys, 'id');
$categorys = array_combine($categorys_id, $categorys);
$sku_property = $dbh->prepare("select id, name from sku_property");
$sku_property->execute();
$sku_property = $sku_property->fetchAll(PDO::FETCH_ASSOC);
$sku_property_id = array_column($sku_property, 'id');
$sku_property = array_combine($sku_property_id, $sku_property);
//var_dump($sku_property);die;
$sku = $dbh->prepare("select id, sn, targetId, price, markPrice from sku WHERE targetType = 'classroom' and disable = 0 and targetId IN ($classroom_id)");
$sku->execute();
$sku = $sku->fetchAll(PDO::FETCH_ASSOC);
$class_room_id = array_column($sku, 'targetId');
$sku = array_combine($class_room_id, $sku);
foreach ($sku as $sku_key => $sku_item) {
$sku[$sku_key]['sn'] = explode('|', trim($sku_item['sn'], '|'));
}
$new_classroom = [];
foreach ($classroom as $key => $value) {
$new_classroom[$key]['班型编号'] = $value['id'];
$temps = explode(',', substr($value['classType'], 1, -1));
$new_classroom[$key]['班型'] = '';
foreach ($temps as $temp) {
// var_dump($temp);die;
if ($temp == '"online"') {
$new_classroom[$key]['班型'] .= ' 线上课';
} elseif ($temp == '"linedown"') {
$new_classroom[$key]['班型'] .= ' 面授课';
}
}
$new_classroom[$key]['课程名称'] = $value['title'];
$new_classroom[$key]['含课程数'] = $value['courseNum'];
$new_classroom[$key]['有效期'] = date('Y-m-d', $value['expiryDay']);
$new_classroom[$key]['创建日期'] = date('Y-m-d', $value['createdTime']);
if ($value['recommended'] == 1 || $value['recommendedapp'] == 1) {
$new_classroom[$key]['推荐状态'] = '已推荐';
} else {
$new_classroom[$key]['推荐状态'] = '未推荐';
}
$new_classroom[$key]['推荐项'] = '';
if ($value['recommended'] == 1) {
$new_classroom[$key]['推荐项'] .= ' PC';
}
if ($value['recommendedapp'] == 1) {
$new_classroom[$key]['推荐项'] .= ' APP';
}
$new_classroom[$key]['发布状态'] = '';
if ($value['status'] == 'draft') {
$new_classroom[$key]['发布状态'] = '未发布';
} elseif ($value['status'] == 'published') {
$new_classroom[$key]['发布状态'] = '发布';
} elseif ($value['status'] == 'closed') {
$new_classroom[$key]['发布状态'] = '关闭';
}
$new_classroom[$key]['适合人群'] = $value['fitCrowd'];
$new_classroom[$key]['特色服务'] = $value['speService'];
$new_classroom[$key]['虚拟学习人数'] = $value['virtualNum'];
$new_classroom[$key]['虚拟收藏人数'] = $value['iconNum'];
$new_classroom[$key]['标签'] = '';
if (isset($tag_owner[$value['id']])) {
$new_classroom[$key]['标签'] = $tag_owner[$value['id']]['标签'];
}
$new_classroom[$key]['一级分类'] = '';
if (isset($categorys[$value['categoryId']]) && $categorys[$value['categoryId']]['parentId'] == 0) {
$new_classroom[$key]['一级分类'] = $categorys[$value['categoryId']]['name'];
}
$new_classroom[$key]['二级分类'] = '';
if (isset($categorys[$value['categoryId']]) && $categorys[$value['categoryId']]['parentId'] != 0) {
$new_classroom[$key]['二级分类'] = $categorys[$value['categoryId']]['name'];
}
$new_classroom[$key]['标价'] = '';
if (isset($sku[$value['id']])) {
$new_classroom[$key]['标价'] = $sku[$value['id']]['price'];
}
$new_classroom[$key]['售价'] = '';
if (isset($sku[$value['id']])) {
$new_classroom[$key]['售价'] = $sku[$value['id']]['markPrice'];
}
$new_classroom[$key]['年限'] = '';
if (isset($sku[$value['id']])) {
if (current($sku[$value['id']]['sn']) == 38 ) {
$new_classroom[$key]['年限'] = 1;
} elseif (current($sku[$value['id']]['sn']) == 39 ) {
$new_classroom[$key]['年限'] = 2;
} elseif (current($sku[$value['id']]['sn']) == 40 ) {
$new_classroom[$key]['年限'] = 3;
}
}
$new_classroom[$key]['站点'] = '';
if (isset($sku[$value['id']])) {
// $new_classroom[$key]['站点'] = end($sku[$value['id']]['sn']);
if (isset($sku_property[end($sku[$value['id']]['sn'])])) {
$new_classroom[$key]['站点'] = $sku_property[end($sku[$value['id']]['sn'])]['name'];
}
}
}
//var_dump($new_classroom);die;
$tileArray = array_keys($new_classroom[0]);
//var_dump($tileArray);die;
//
//var_dump($tileArray, $classroom);
exportToExcel('date-test.csv', $tileArray, $new_classroom);
function exportToExcel($filename, $tileArray = [], $dataArray = [])
{
ini_set('memory_limit', '512M');
ini_set('max_execution_time', 0);
ob_end_clean();
ob_start();
header("Content-Type: text/csv");
header("Content-Disposition:filename=" . $filename);
$fp = fopen('php://output', 'w');
fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//转码 防止乱码(比如微信昵称(乱七八糟的))
fputcsv($fp, $tileArray);
$index = 0;
foreach ($dataArray as $item) {
if ($index == 1000) {
$index = 0;
ob_flush();
flush();
}
$index++;
fputcsv($fp, $item);
}
ob_flush();
flush();
ob_end_clean();
}