|
@@ -12,6 +12,7 @@ declare (strict_types=1);
|
|
|
|
|
|
namespace app\store\service\order;
|
|
|
|
|
|
+use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
use app\store\model\{Order as OrderModel, order\Export as ExportModel, OrderAddress as OrderAddressModel};
|
|
@@ -62,6 +63,52 @@ class Export extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 执行订单待发货导出excel
|
|
|
+ * @param array $param
|
|
|
+ * @return bool
|
|
|
+ * @throws BaseException
|
|
|
+ */
|
|
|
+ public function exportDeliveryOrder(array $param): bool
|
|
|
+ {
|
|
|
+ // 根据条件查询订单列表
|
|
|
+ $orderList = $this->getDeliveryOrderList($param);
|
|
|
+ // 格式化生成表格数据
|
|
|
+ $columns = [
|
|
|
+ 'order_id',
|
|
|
+ 'order_no',
|
|
|
+ 'goods_detail',
|
|
|
+ 'total_price',
|
|
|
+ 'pay_price',
|
|
|
+ 'pay_type',
|
|
|
+ 'create_time',
|
|
|
+ 'user_info',
|
|
|
+ 'buyer_remark',
|
|
|
+ 'delivery_type',
|
|
|
+ 'receipt_name',
|
|
|
+ 'receipt_phone',
|
|
|
+ 'receipt_address',
|
|
|
+ 'express_company',
|
|
|
+ 'express_no',
|
|
|
+ 'pay_status',
|
|
|
+ 'pay_time',
|
|
|
+ 'delivery_status',
|
|
|
+ 'delivery_time',
|
|
|
+ 'receipt_status',
|
|
|
+ 'receipt_time',
|
|
|
+ 'order_status',
|
|
|
+ 'order_source'
|
|
|
+ ];;
|
|
|
+ $excelList = $this->getExcelList($orderList->toArray(), $columns);
|
|
|
+ // 获取导出的记录列名集
|
|
|
+ //$columns = $this->getColumns($param['columns']);
|
|
|
+ // 输出并写入到excel文件
|
|
|
+ $filePath = $this->outputDeliveryExcel($excelList);
|
|
|
+ // 新增订单导出记录
|
|
|
+ $this->record($param, $filePath);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 根据条件查询订单列表
|
|
|
* @param array $param
|
|
|
* @return mixed
|
|
@@ -79,6 +126,23 @@ class Export extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 根据条件查询订单列表
|
|
|
+ * @param array $param
|
|
|
+ * @return mixed
|
|
|
+ * @throws BaseException
|
|
|
+ */
|
|
|
+ private function getDeliveryOrderList(array $param)
|
|
|
+ {
|
|
|
+ // 根据条件查询订单列表
|
|
|
+ $model = new OrderModel;
|
|
|
+ $orderList = $model->getListAll(OrderModel::LIST_TYPE_DELIVERY, $param);
|
|
|
+ if ($orderList->isEmpty()) {
|
|
|
+ throwError('很抱歉,没有查询到订单数据');
|
|
|
+ }
|
|
|
+ return $orderList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 输出并写入到excel文件
|
|
|
* @param array $columns 列名
|
|
|
* @param array $excelList 表格内容
|
|
@@ -121,6 +185,48 @@ class Export extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 输出并写入到excel文件
|
|
|
+ * @param array $excelList 表格内容
|
|
|
+ * @return string
|
|
|
+ * @throws BaseException
|
|
|
+ */
|
|
|
+ private function outputDeliveryExcel(array $excelList): string
|
|
|
+ {
|
|
|
+ $reader = IOFactory::createReader('Xlsx');
|
|
|
+ $reader->setReadDataOnly(TRUE);
|
|
|
+ $templateFile = public_path('template');
|
|
|
+ $spreadsheet = $reader->load($templateFile . 'OnePackagePreOrders.xlsx'); //载入excel表格
|
|
|
+
|
|
|
+ $sheet = $spreadsheet->getActiveSheet();
|
|
|
+
|
|
|
+ // 列宽
|
|
|
+ $sheet->getDefaultColumnDimension()->setWidth(24);
|
|
|
+ // 默认行高
|
|
|
+ $sheet->getDefaultRowDimension()->setRowHeight(20);
|
|
|
+ // 字体加粗(第一行)
|
|
|
+ $sheet->getStyle('1')->getFont()->setBold(true);
|
|
|
+
|
|
|
+ // 写入内容数据
|
|
|
+ foreach ($excelList as $key => $item) {
|
|
|
+ $row = $key + 2;
|
|
|
+ foreach (array_values($item) as $k => $val) {
|
|
|
+ $sheet->setCellValueByColumnAndRow($k + 1, $row, $val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 生成文件路径
|
|
|
+ $fileName = 'order-' . time() . '.xlsx';
|
|
|
+ $filePath = $this->getExportPath();
|
|
|
+ // 保存到文件
|
|
|
+ try {
|
|
|
+ $writer = new Xlsx($spreadsheet);
|
|
|
+ $writer->save(public_path() . $filePath . $fileName);
|
|
|
+ } catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
|
|
|
+ throwError($e->getMessage());
|
|
|
+ }
|
|
|
+ return $filePath . $fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取导出的文件夹路径
|
|
|
* @return string
|
|
|
*/
|