Export.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\store\service\order;
  3. use app\store\model\OrderAddress as OrderAddressModel;
  4. /**
  5. * 订单导出服务类
  6. * Class Export
  7. * @package app\store\service\order
  8. */
  9. class Export
  10. {
  11. /**
  12. * 表格标题
  13. * @var array
  14. */
  15. private $tileArray = [
  16. '订单号', '商品信息', '订单总额', '优惠券抵扣', '积分抵扣', '运费金额', '后台改价', '实付款金额', '支付方式', '下单时间',
  17. '买家', '买家留言', '配送方式', '自提门店名称', '自提联系人', '自提联系电话', '收货人姓名', '联系电话', '收货人地址',
  18. '物流公司', '物流单号', '付款状态', '付款时间', '发货状态', '发货时间', '收货状态', '收货时间', '订单状态', '微信支付交易号', '是否已评价'
  19. ];
  20. /**
  21. * 订单导出
  22. * @param $list
  23. */
  24. public function orderList($list)
  25. {
  26. // 表格内容
  27. $dataArray = [];
  28. foreach ($list as $order) {
  29. /* @var OrderAddressModel $address */
  30. $address = $order['address'];
  31. $dataArray[] = [
  32. '订单号' => $this->filterValue($order['order_no']),
  33. '商品信息' => $this->filterGoodsInfo($order),
  34. '订单总额' => $this->filterValue($order['total_price']),
  35. '优惠券抵扣' => $this->filterValue($order['coupon_money']),
  36. '积分抵扣' => $this->filterValue($order['points_money']),
  37. '运费金额' => $this->filterValue($order['express_price']),
  38. '后台改价' => $this->filterValue("{$order['update_price']['symbol']}{$order['update_price']['value']}"),
  39. '实付款金额' => $this->filterValue($order['pay_price']),
  40. '支付方式' => $this->filterValue($order['pay_type']['text']),
  41. '下单时间' => $this->filterValue($order['create_time']),
  42. '买家' => $this->filterValue($order['user']['nickName']),
  43. '买家留言' => $this->filterValue($order['buyer_remark']),
  44. '配送方式' => $this->filterValue($order['delivery_type']['text']),
  45. '自提门店名称' => !empty($order['extract_shop']) ? $this->filterValue($order['extract_shop']['shop_name']) : '',
  46. '自提联系人' => !empty($order['extract']) ? $this->filterValue($order['extract']['linkman']) : '',
  47. '自提联系电话' => !empty($order['extract']) ? $this->filterValue($order['extract']['phone']) : '',
  48. '收货人姓名' => $this->filterValue($order['address']['name']),
  49. '联系电话' => $this->filterValue($order['address']['phone']),
  50. '收货人地址' => $this->filterValue($address ? $address->getFullAddress() : ''),
  51. '物流公司' => $this->filterValue($order['express']['express_name']),
  52. '物流单号' => $this->filterValue($order['express_no']),
  53. '付款状态' => $this->filterValue($order['pay_status']['text']),
  54. '付款时间' => $this->filterTime($order['pay_time']),
  55. '发货状态' => $this->filterValue($order['delivery_status']['text']),
  56. '发货时间' => $this->filterTime($order['delivery_time']),
  57. '收货状态' => $this->filterValue($order['receipt_status']['text']),
  58. '收货时间' => $this->filterTime($order['receipt_time']),
  59. '订单状态' => $this->filterValue($order['order_status']['text']),
  60. '微信支付交易号' => $this->filterValue($order['transaction_id']),
  61. '是否已评价' => $this->filterValue($order['is_comment'] ? '是' : '否'),
  62. ];
  63. }
  64. // 导出csv文件
  65. $filename = 'order-' . date('YmdHis');
  66. return export_excel($filename . '.csv', $this->tileArray, $dataArray);
  67. }
  68. /**
  69. * 批量发货模板
  70. */
  71. public function deliveryTpl()
  72. {
  73. // 导出csv文件
  74. $filename = 'delivery-' . date('YmdHis');
  75. return export_excel($filename . '.csv', ['订单号', '物流单号']);
  76. }
  77. /**
  78. * 格式化商品信息
  79. * @param $order
  80. * @return string
  81. */
  82. private function filterGoodsInfo($order)
  83. {
  84. $content = '';
  85. foreach ($order['goods'] as $key => $goods) {
  86. $content .= ($key + 1) . ".商品名称:{$goods['goods_name']}\n";
  87. !empty($goods['goods_attr']) && $content .= " 商品规格:{$goods['goods_attr']}\n";
  88. $content .= " 购买数量:{$goods['total_num']}\n";
  89. $content .= " 商品总价:{$goods['total_price']}元\n\n";
  90. }
  91. return $content;
  92. }
  93. /**
  94. * 表格值过滤
  95. * @param $value
  96. * @return string
  97. */
  98. private function filterValue($value)
  99. {
  100. return "\t" . $value . "\t";
  101. }
  102. /**
  103. * 日期值过滤
  104. * @param $value
  105. * @return string
  106. */
  107. private function filterTime($value)
  108. {
  109. if (!$value) return '';
  110. return $this->filterValue(date('Y-m-d H:i:s', $value));
  111. }
  112. }