Export.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\service\order;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  15. use app\store\model\Order as OrderModel;
  16. use app\store\model\order\Export as ExportModel;
  17. use app\store\model\OrderAddress as OrderAddressModel;
  18. use app\common\library\helper;
  19. use app\common\service\BaseService;
  20. use app\common\enum\order\PayType as PayTypeEnum;
  21. use app\common\enum\order\PayStatus as PayStatusEnum;
  22. use app\common\enum\order\OrderSource as OrderSourceEnum;
  23. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  24. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  25. use app\common\enum\order\ReceiptStatus as ReceiptStatusEnum;
  26. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  27. use app\common\enum\order\export\ExportStatus as ExportStatusEnum;
  28. use app\common\exception\BaseException;
  29. /**
  30. * 服务层:订单导出Excel
  31. * Class Export
  32. * @package app\store\service\order
  33. */
  34. class Export extends BaseService
  35. {
  36. // 表格文件标题名称
  37. private $title = '订单导出结果';
  38. /**
  39. * 执行订单导出excel
  40. * @param array $param
  41. * @return bool
  42. * @throws BaseException
  43. */
  44. public function exportOrder(array $param)
  45. {
  46. // 根据条件查询订单列表
  47. $orderList = $this->getOrderList($param);
  48. // 格式化生成表格数据
  49. $excelList = $this->getExcelList($orderList->toArray(), $param['columns']);
  50. // 获取导出的记录列名集
  51. $columns = $this->getColumns($param['columns']);
  52. // 输出并写入到excel文件
  53. $filePath = $this->outputExcel($columns, $excelList);
  54. // 新增订单导出记录
  55. $this->record($param, $filePath);
  56. return true;
  57. }
  58. /**
  59. * 根据条件查询订单列表
  60. * @param array $param
  61. * @return mixed
  62. * @throws BaseException
  63. */
  64. private function getOrderList(array $param)
  65. {
  66. // 根据条件查询订单列表
  67. $model = new OrderModel;
  68. $orderList = $model->getListAll(OrderModel::LIST_TYPE_ALL, $param);
  69. if ($orderList->isEmpty()) {
  70. throwError('很抱歉,没有查询到订单数据');
  71. }
  72. return $orderList;
  73. }
  74. /**
  75. * 输出并写入到excel文件
  76. * @param array $columns 列名
  77. * @param array $excelList 表格内容
  78. * @return bool
  79. * @throws BaseException
  80. */
  81. private function outputExcel(array $columns, array $excelList)
  82. {
  83. // 生成工作表
  84. $spreadsheet = new Spreadsheet();
  85. $sheet = $spreadsheet->getActiveSheet()->setTitle($this->title);
  86. // 列宽
  87. $sheet->getDefaultColumnDimension()->setWidth(24);
  88. // 默认行高
  89. $sheet->getDefaultRowDimension()->setRowHeight(20);
  90. // 字体加粗(第一行)
  91. $sheet->getStyle('1')->getFont()->setBold(true);
  92. // 写入标题数据
  93. foreach ($columns as $key => $val) {
  94. $sheet->setCellValueByColumnAndRow($key + 1, 1, $val);
  95. }
  96. // 写入内容数据
  97. foreach ($excelList as $key => $item) {
  98. $row = $key + 2;
  99. foreach (array_values($item) as $k => $val) {
  100. $sheet->setCellValueByColumnAndRow($k + 1, $row, $val);
  101. }
  102. }
  103. // 生成文件路径
  104. $fileName = 'order-' . time() . '.xlsx';
  105. $filePath = 'temp/' . $this->getStoreId() . '/';
  106. // 保存到文件
  107. try {
  108. $writer = new Xlsx($spreadsheet);
  109. $writer->save(public_path() . $filePath . $fileName);
  110. } catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
  111. throwError($e->getMessage());
  112. }
  113. return $filePath . $fileName;
  114. }
  115. // 记录导出记录
  116. private function record(array $param, string $filePath)
  117. {
  118. // 生成记录数据
  119. $data = [
  120. 'file_path' => $filePath,
  121. 'status' => ExportStatusEnum::COMPLETED,
  122. 'store_id' => $this->getStoreId(),
  123. ];
  124. // 起止时间
  125. if (!empty($param['betweenTime'])) {
  126. $times = between_time($param['betweenTime']);
  127. $data['start_time'] = $times['start_time'];
  128. $data['end_time'] = $times['end_time'];
  129. }
  130. // 新增记录
  131. $model = new ExportModel;
  132. return $model->add($data);
  133. }
  134. /**
  135. * 获取导出的记录列名集
  136. * @param array $onlyFields
  137. * @return array
  138. */
  139. private function getColumns(array $onlyFields)
  140. {
  141. return array_values(helper::pick($this->dictionary(), $onlyFields));
  142. }
  143. /**
  144. * 订单记录字典
  145. * @return string[]
  146. */
  147. private function dictionary()
  148. {
  149. return [
  150. 'order_id' => '订单ID',
  151. 'order_no' => '订单号',
  152. 'goods_detail' => '商品信息',
  153. 'total_price' => '商品总额',
  154. 'coupon_money' => '优惠券抵扣金额',
  155. 'points_money' => '积分抵扣金额',
  156. 'update_price' => '后台改价',
  157. 'express_price' => '运费金额',
  158. 'pay_price' => '订单实付款',
  159. 'pay_type' => '支付方式',
  160. 'create_time' => '下单时间',
  161. 'user_info' => '买家信息',
  162. 'buyer_remark' => '买家留言',
  163. 'delivery_type' => '配送方式',
  164. 'receipt_name' => '收货人',
  165. 'receipt_phone' => '联系电话',
  166. 'receipt_address' => '收货地址',
  167. 'express_company' => '物流公司',
  168. 'express_no' => '物流单号',
  169. 'extract_shop_name' => '自提门店名称',
  170. 'extract_linkman' => '自提联系人',
  171. 'extract_phone' => '自提联系电话',
  172. 'pay_status' => '付款状态',
  173. 'pay_time' => '付款时间',
  174. 'delivery_status' => '发货状态',
  175. 'delivery_time' => '发货时间',
  176. 'receipt_status' => '收货状态',
  177. 'receipt_time' => '收货时间',
  178. 'order_status' => '订单状态',
  179. 'is_comment' => '是否已评价',
  180. 'order_source' => '订单来源',
  181. ];
  182. }
  183. /**
  184. * 格式化生成表格数据
  185. * @param array $orderList
  186. * @param array $onlyFields
  187. * @return array
  188. */
  189. private function getExcelList(array $orderList, array $onlyFields)
  190. {
  191. // 获取订单表格数据
  192. $excelList = $this->getOrderDataForExcel($orderList);
  193. // 仅输出用户设置的字段
  194. $data = [];
  195. foreach ($excelList as $item) {
  196. $data[] = helper::pick($item, $onlyFields);
  197. }
  198. return $data;
  199. }
  200. /**
  201. * 获取订单列表数据(用于生成Excel)
  202. * @param $orderList
  203. * @return array
  204. */
  205. private function getOrderDataForExcel($orderList)
  206. {
  207. // 表格内容
  208. $dataArray = [];
  209. foreach ($orderList as $order) {
  210. $dataArray[] = [
  211. 'order_id' => $order['order_id'],
  212. 'order_no' => $order['order_no'],
  213. 'goods_detail' => $this->filterGoodsInfo($order),
  214. 'total_price' => $this->filterValue($order['total_price']) . '元',
  215. 'coupon_money' => $this->filterValue($order['coupon_money']) . '元',
  216. 'points_money' => $this->filterValue($order['points_money']) . '元',
  217. 'update_price' => "{$order['update_price']['symbol']}{$order['update_price']['value']}元",
  218. 'express_price' => $this->filterValue($order['express_price']) . '元',
  219. 'pay_price' => $this->filterValue($order['pay_price']) . '元',
  220. 'pay_type' => PayTypeEnum::data()[$order['pay_type']]['name'],
  221. 'create_time' => $this->filterValue($order['create_time']),
  222. 'user_info' => $this->filterValue($order['user']['nick_name']),
  223. 'buyer_remark' => $this->filterValue($order['buyer_remark']),
  224. 'delivery_type' => DeliveryTypeEnum::data()[$order['delivery_type']]['name'],
  225. 'receipt_name' => $this->filterValue($order['address']['name']),
  226. 'receipt_phone' => $this->filterValue($order['address']['phone']),
  227. 'receipt_address' => !empty($order['address']) ? OrderAddressModel::fullAddress($order['address']) : '',
  228. 'express_company' => !empty($order['express']) ? $order['express']['express_name'] : '',
  229. 'express_no' => $this->filterValue($order['express_no']),
  230. 'extract_shop_name' => !empty($order['extract_shop']) ? $order['extract_shop']['shop_name'] : '',
  231. 'extract_linkman' => !empty($order['extract']) ? $order['extract']['linkman'] : '',
  232. 'extract_phone' => !empty($order['extract']) ? $order['extract']['phone'] : '',
  233. 'pay_status' => PayStatusEnum::data()[$order['pay_status']]['name'],
  234. 'pay_time' => $order['pay_time'],
  235. 'delivery_status' => DeliveryStatusEnum::data()[$order['delivery_status']]['name'],
  236. 'delivery_time' => $order['delivery_time'] ?: '',
  237. 'receipt_status' => ReceiptStatusEnum::data()[$order['receipt_status']]['name'],
  238. 'receipt_time' => $order['receipt_time'] ?: '',
  239. 'order_status' => OrderStatusEnum::data()[$order['order_status']]['name'],
  240. 'is_comment' => $order['is_comment'] ? '是' : '否',
  241. 'order_source' => OrderSourceEnum::data()[$order['order_source']]['name'],
  242. ];
  243. }
  244. return $dataArray;
  245. }
  246. /**
  247. * 格式化商品信息
  248. * @param $order
  249. * @return string
  250. */
  251. private function filterGoodsInfo($order)
  252. {
  253. $content = '';
  254. foreach ($order['goods'] as $key => $goods) {
  255. $content .= ($key + 1) . ".商品名称:{$goods['goods_name']}\n";
  256. !empty($goods['goods_attr']) && $content .= " 商品规格:{$goods['goods_attr']}\n";
  257. $content .= " 购买数量:{$goods['total_num']}\n";
  258. $content .= " 商品总价:{$goods['total_price']}元\n\n";
  259. }
  260. return $content;
  261. }
  262. /**
  263. * 表格值过滤
  264. * @param $value
  265. * @return string
  266. */
  267. private function filterValue($value)
  268. {
  269. return $value;
  270. //return "\t" . $value . "\t";
  271. }
  272. }