PlatformordersController.php
7.6 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
<?php
namespace app\admin\controller;
use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use function GuzzleHttp\Psr7\str;
use think\Db;
class PlatformordersController extends AdminBaseController{
/**
* 平台订单列表
*/
public function plat_list(){
$param = $this->request->param();
$where = [
"indent_type" => 1
];
if(!empty($param['start_time']) && !empty($param['end_time'])){
$start_time = strtotime($param['start_time']);
$end_time = strtotime($param['end_time']);
$where['create_time'] = [['>=',$start_time],['<=',$end_time]];
}
if(!empty($param['keyword'])){
$where['order_number|name|phone'] = ['like','%'.trim($param['keyword']).'%'];
}
if(!empty($param['state'])){
$where['state'] = $param['state'];
}
$data = Db::name('indent') -> where($where)->order('create_time','DESC') -> paginate(12);
$data->appends($param);
$data1 = $data -> toArray();
if(count($data1['data'])!=0){
foreach ($data1['data'] as $key => $val){
$data1['data'][$key]['dizhi'] = Db::name('address') -> where('id',$val['indent_address']) -> find();
}
}
$this -> assign('data1',$data1['data']);
$this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
$this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
$this->assign('keyword', isset($param['keyword']) ? trim($param['keyword']) : '');
$this->assign('state', isset($param['state']) ? $param['state'] : '');
$this -> assign('data',$data);
return $this -> fetch();
}
/**
*平台订单查看
*/
public function plat_view(){
$id = $this -> request -> param();
$indent_id = $id['id'];
$data_goods = Db::name('indent_goods') -> where('indent_id',$indent_id) -> select();
$data_in = Db::name('indent') -> where('id',$indent_id) -> find();
//地址查询
$address = Db::name('address') -> where("id",$data_in['indent_address']) -> find();
if($address){
$data_in['name'] = $address['name'];
$data_in['phone'] = $address['phone'];
$this -> assign('address',$address);
}else{
$this -> assign('address',null);
}
$this -> assign('data_goods',$data_goods);
$this -> assign('data_in',$data_in);
return $this -> fetch();
}
public function out()
{
// 进行查询条件的处理
$param = $this->request->param();
$where = [
"indent_type" => 1
];
if(!empty($param['start_time']) && !empty($param['end_time'])){
$start_time = strtotime($param['start_time']);
$end_time = strtotime($param['end_time']);
$where['create_time'] = [['>=',$start_time],['<=',$end_time]];
}
if(!empty($param['keyword'])){
$where['order_number|name|phone'] = ['like','%'.$param['keyword'].'%'];
}
if(!empty($param['state'])){
$where['state'] = $param['state'];
}
$data = Db::name('indent') -> where($where)->order('create_time','DESC') -> paginate(12);
$data->appends($param);
$data1 = $data -> toArray();
if(count($data1['data'])!=0){
foreach ($data1['data'] as $key => $val){
$data1['data'][$key]['dizhi'] = Db::name('address') -> where('id',$val['indent_address']) -> find();
}
}
$this->expList($data1['data']);
}
// 数据导出
public function expList($array){
//导出Excel
$objPHPExcel = new \PHPExcel();
//Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '序号')
->setCellValue('B1', '订单号')
->setCellValue('C1', '姓名')
->setCellValue('D1', '手机号')
->setCellValue('E1', '收货地址')
->setCellValue('F1', '数量')
->setCellValue('G1', '价钱')
->setCellValue('H1', '订单状态')
->setCellValue('I1', '创建时间');
// sheet个数
$sheet_index = 0;
$sheet_count = 1;
if(count($array) > 65535) {
$sheet_count = ceil(count($array)/65535);
for ($j = 1; $j < $sheet_count; $j++) {
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($sheet_index+$j)
->setCellValue('A1', '序号')
->setCellValue('B1', '订单号')
->setCellValue('C1', '姓名')
->setCellValue('D1', '手机号')
->setCellValue('E1', '收货地址')
->setCellValue('F1', '数量')
->setCellValue('G1', '价钱')
->setCellValue('H1', '订单状态')
->setCellValue('I1', '创建时间');
}
}
// 设置单元格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(25);
$i = 2;
// 计数器
$cnt = 0;
// 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
$limit = 35000;
foreach ($array as $k=>$v) {
$cnt ++;
if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
ob_flush();
flush();
$cnt = 0;
}
$state = '待付款';
if($v['state'] == 1) {
$state = '已付款';
}
if($v['state'] == 2) {
$state = '未完成';
}
if($v['state'] == 3) {
$state = '已完成';
}
$objPHPExcel->setActiveSheetIndex($sheet_index)
->setCellValue('A' . $i, $v['id'])
->setCellValue('B' . $i, $v['order_number'])
->setCellValue('C' . $i, $v['name'])
->setCellValue('D' . $i, $v['phone'])
->setCellValue('E' . $i, $v['dizhi']['region'].' '.$v['dizhi']['detailed'])
->setCellValue('F' . $i, $v['book_num'])
->setCellValue('G' . $i, $v['money'])
->setCellValue('H' . $i, $state)
->setCellValue('I' . $i, date('Y-m-d H:i:s',$v['create_time']));
$i++;
if($k == ($sheet_index+1)*65535) {
$i = 2;
$sheet_index++;
}
}
$filename = '平台订单数据'. date('Y-m-d').'.xls';
ob_end_clean();
header('Content-Type: text/csv');
// header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
}