Эх сурвалжийг харах

【Add】订单下载优化

liuxiangxin 4 сар өмнө
parent
commit
d1108477b7

+ 91 - 94
app/Http/Controllers/Admin/Orders.php

@@ -431,7 +431,7 @@ class Orders extends Auth{
 		if( $endTime )			$map[] = ['orders_product.insert_time','<=',strtotime($endTime)];
 		if( $status )			$map[] = ['orders_product.status','=',$status];
 		// 查询数据
-		$list					= $OrdersProduct->query()
+		$data					= $OrdersProduct->query()
 									->join('custom','orders_product.custom_uid','=','custom.uid')
 									->join('orders_addr','orders_addr.order_id','=','orders_product.order_id')
 									->where($map)
@@ -439,7 +439,6 @@ class Orders extends Auth{
 									->select([
 										'orders_product.id as id',
 										'orders_product.order_id',
-										'orders_product.custom_uid',
 										'orders_product.product_id',
 										'orders_product.product_name',
 										'orders_product.sku_attr_names as product_spec',
@@ -449,45 +448,56 @@ class Orders extends Auth{
 										'orders_product.coupon_total',
 										'orders_product.pay_total',
 										'orders_product.status',
+										'orders_addr.contact_area',
+										'orders_addr.contact_school',
+										'orders_addr.contact_grade',
+										'orders_addr.contact_class',
+										'orders_addr.contact_name',
+										'orders_addr.contact_phone',
+										'custom.uid as custom_uid',
+										'custom.username as custom_name',
+										'custom.external_userid as external_userid',
 										'orders_product.insert_time',
-										'custom.username as custom_name','custom.external_userid as external_userid',
-										'orders_addr.contact_area','orders_addr.contact_school','orders_addr.contact_grade','orders_addr.contact_class','orders_addr.contact_name','orders_addr.contact_phone',
 									])->get()->toArray();
-		// 返回结果
-		$data						= [];
 		// 循环处理数据
-		foreach ($list as $value) {
-			// id转编号
-			$value['order_id']		= $Model->idToCode($value['order_id']);
-			$value['status']		= $Model->getState($value['status'],'state');
-			$value['custom_uid']	= $Custom->idToCode($value['custom_uid']);
-			$value['product_price']	= $value['buy_num'] ? ($value['price_total'] / $value['buy_num']) : $value['buy_num'];
-			$value['pay_price']		= $value['buy_num'] ? ($value['pay_total'] / $value['buy_num']) : $value['buy_num'];
-			// 重组
-			$data[$value['order_id']]['order_id'] = $value['order_id'];
-			$data[$value['order_id']]['custom_uid'] = $value['custom_uid'];
-			$data[$value['order_id']]['custom_name'] = $value['custom_name'];
-			$data[$value['order_id']]['external_userid'] = $value['external_userid'];
-			$data[$value['order_id']]['status'] = $value['status'];
-			$data[$value['order_id']]['insert_time'] = $value['insert_time'];
-			// 地址
-			$data[$value['order_id']]['contact_name'] = $value['contact_name'];
-			$data[$value['order_id']]['contact_phone'] = $value['contact_phone'];
-			$data[$value['order_id']]['contact_area'] = $value['contact_area'];
-			$data[$value['order_id']]['contact_school'] = $value['contact_school'];
-			$data[$value['order_id']]['contact_grade'] = $value['contact_grade'];
-			$data[$value['order_id']]['contact_class'] = $value['contact_class'];
-			// 子订单
-			$data[$value['order_id']]['product'][] 	  = ['product_id'=>$value['product_id'] ? $Product->idToCode($value['product_id']) : '— —','product_name'=>$value['product_name'],'product_spec'=>$value['product_spec'],'product_thumb'=>$value['product_thumb'],'product_price'=>$value['product_price'],'pay_price'=>$value['pay_price'],'buy_num'=>$value['buy_num'],'pay_total'=>$value['pay_total'],'price_total'=>$value['price_total'],'coupon_total'=>$value['coupon_total']];
-		}
-
+        foreach ($data as $key => $value) {
+            // id转编号
+            $value['order_id']		= $Model->idToCode($value['order_id']);
+            $value['status']		= $Model->getState($value['status'],'state');
+            $value['custom_uid']	= $Custom->idToCode($value['custom_uid']);
+			$value['product_id']	= $value['product_id'] ? $Product->idToCode($value['product_id']) : '— —';
+            $value['product_price']	= $value['buy_num'] ? ($value['price_total'] / $value['buy_num']) : $value['buy_num'];
+            $value['pay_price']		= $value['buy_num'] ? ($value['pay_total'] / $value['buy_num']) : $value['buy_num'];
+            // 重组
+            $data[$key]             = [
+                'order_id'		    => $value['order_id'],
+                'status'		    => $value['status'],
+                'contact_area'		=> $value['contact_area'],
+                'contact_school'	=> $value['contact_school'],
+				'contact_grade'		=> $value['contact_grade'],
+				'contact_class'		=> $value['contact_class'],
+                'contact_name'		=> $value['contact_name'],
+                'contact_phone'		=> $value['contact_phone'],
+                'product_id'		=> $value['product_id'],
+                'product_name'		=> $value['product_name'],
+                'product_spec'		=> $value['product_spec'],
+                'product_price'		=> $value['product_price'],
+                'pay_price'		    => $value['pay_price'],
+                'buy_num'		    => $value['buy_num'],
+                'coupon_total'		=> $value['coupon_total'],
+                'pay_total'		    => $value['pay_total'],
+				'custom_uid'		=> $value['custom_uid'],
+                'custom_name'		=> $value['custom_name'],
+                'external_userid'	=> $value['external_userid'],
+                'insert_time'		=> date('Y-m-d H:i:s',$value['insert_time']),
+            ];
+        }
 		try {
 			// 去下载
 			$this->toDown($data);
 		} catch (\Throwable $th) {
 			echo $th->getMessage();
 		}
-		
 	}
 
 
@@ -495,68 +505,55 @@ class Orders extends Auth{
 	 * 去下载
 	 */
 	private function  toDown($data){
-		// 创建新的电子表格对象
-		$spreadsheet			= new Spreadsheet();
-		// 设置合并单元格的行和列,例如合并A1到B2的单元格
-		$sheet					= $this->setStyle($spreadsheet);
-		// 从第二行写入
-		$row					= 2;
-		// 循环写入
-		foreach ($data as $key => $value) {
-			// 如果有多个商品
-			$count				= count($value['product']);
-			// 如果有多个商品
-			if( $count > 1 )	{
-				// 合并单元格
-				$sheet->mergeCells('A'.$row.':'.'A'.($row+$count-1));
-				$sheet->mergeCells('B'.$row.':'.'B'.($row+$count-1));
-				$sheet->mergeCells('C'.$row.':'.'C'.($row+$count-1));
-				$sheet->mergeCells('D'.$row.':'.'D'.($row+$count-1));
-				$sheet->mergeCells('E'.$row.':'.'E'.($row+$count-1));
-				$sheet->mergeCells('F'.$row.':'.'F'.($row+$count-1));
-				$sheet->mergeCells('G'.$row.':'.'G'.($row+$count-1));
-				$sheet->mergeCells('H'.$row.':'.'H'.($row+$count-1));
-				$sheet->mergeCells('I'.$row.':'.'I'.($row+$count-1));
-				$sheet->mergeCells('J'.$row.':'.'J'.($row+$count-1));
-				$sheet->mergeCells('R'.$row.':'.'R'.($row+$count-1));
-				$sheet->mergeCells('S'.$row.':'.'S'.($row+$count-1));
-			}
-			// 单元格内容写入
-			$sheet->setCellValue('A'.$row, $value['order_id']);
-			$sheet->setCellValue('B'.$row, $value['custom_uid']);
-            //避免 = + - 识别成公式
-            $sheet->setCellValueExplicit('C'.$row, $value['custom_name'],DataType::TYPE_STRING);
-			$sheet->setCellValue('D'.$row, $value['status']);
-			$sheet->setCellValue('E'.$row, $value['contact_name']);
-			$sheet->setCellValue('F'.$row, $value['contact_phone']);
-			$sheet->setCellValue('G'.$row, $value['contact_area']);
-			$sheet->setCellValue('H'.$row, $value['contact_school']);
-			$sheet->setCellValue('I'.$row, $value['contact_grade']);
-			$sheet->setCellValue('J'.$row, $value['contact_class']);
-			$sheet->setCellValue('S'.$row, $value['external_userid']);
-			$sheet->setCellValue('T'.$row, date('Y-m-d H:i:s',$value['insert_time']));
-			// 循环产品
-			foreach ($value['product'] as $v) {
-				$sheet->setCellValue('K'.$row, $v['product_id']);
-				$sheet->setCellValue('L'.$row, $v['product_name']);
-				$sheet->setCellValue('M'.$row, $v['product_spec']);
-				$sheet->setCellValue('N'.$row, $v['product_price']);
-				$sheet->setCellValue('O'.$row, $v['pay_price']);
-				$sheet->setCellValue('P'.$row, $v['buy_num']);
-				$sheet->setCellValue('Q'.$row, $v['coupon_total']);
-				$sheet->setCellValue('R'.$row, $v['pay_total']);
-				// 函数自增
-				$row++;
-			}
-		}
-		// 
-		// 创建内容
-		$writer 				= IOFactory::createWriter($spreadsheet, 'Xlsx');
-		header('Pragma: public');
-		header('Content-type:application/vnd.ms-excel');
-		header('Content-Disposition: inline;filename=下载订单.xlsx');
-		// 输出数据流
-		return $writer->save('php://output');
+		try {
+            $config     = [
+                'path' =>public_path().'/uploads/' // xlsx文件保存路径
+            ];
+            $excel      = new \Vtiful\Kernel\Excel($config);
+            $header     =   [
+                '订单ID',
+                '订单状态',
+                '区域',
+                '学校',
+                '年级',
+                '班级',
+                '学生姓名',
+                '收货手机',
+                '产品编码',
+                '产品名称',
+                '产品规格',
+                '产品单价',
+                '折后单价',
+                '产品数量',
+                '优惠金额',
+                '产品金额',
+				'客户ID',
+                '客户昵称',
+				'企微ID',
+                '下单时间',
+            ];
+            $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
+                ->header($header)
+                ->data($data)
+                ->output();
+            $filename   =   '订单下载'.date('Y-m-d His').'.xlsx';
+            header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            header('Content-Disposition: attachment;filename="' . $filename . '"');
+            header('Content-Length: ' . filesize($filePath));
+            header('Content-Transfer-Encoding: binary');
+            header('Cache-Control: must-revalidate');
+            header('Cache-Control: max-age=0');
+            header('Pragma: public');
+            ob_clean();
+            flush();
+            if (copy($filePath, 'php://output') === false) {
+                dd('下载出错');
+            }
+            @unlink($filePath);
+            exit();
+        }catch (\Throwable $th) {
+            return $th->getMessage();
+        }
 	}
 
 	/**