AfterSalesExport.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/11/4
  6. * Time: 14:59
  7. */
  8. namespace app\backend\modules\refund\services;
  9. use Maatwebsite\Excel\Concerns\FromArray;
  10. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  11. use Maatwebsite\Excel\Concerns\WithStyles;
  12. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  13. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  14. use Maatwebsite\Excel\Concerns\WithHeadings;
  15. use Maatwebsite\Excel\Concerns\WithTitle;
  16. class AfterSalesExport implements FromArray, WithStyles,WithHeadings, WithTitle
  17. {
  18. public $data;//订单数据
  19. public $column = 14; //总行数
  20. public $goodsNum = []; //一个订单的商品数量
  21. protected $page = 1;
  22. public function __construct($data, $page = 1)
  23. {
  24. $this->data = $data;
  25. $this->page = $page;
  26. }
  27. //设置宽度
  28. // public function columnWidths(): array
  29. // {
  30. // return array('A' => 10,'B' => 13,'C' => 35, 'D'=> 12,'E'=>10,'F'=>18);
  31. // }
  32. public function title(): string
  33. {
  34. return '第' . $this->page . '页';
  35. }
  36. public function headings(): array
  37. {
  38. return [
  39. ['售后编号', '订单编号', '会员编号', '会员昵称', '订单类型',
  40. '售后类型', '售后状态', '退款金额', '商品名称', '退款数量',
  41. '申请时间', '完成时间', '退款原因', '标识'],
  42. ];
  43. }
  44. public function array(): array
  45. {
  46. $list = [];
  47. foreach ($this->data as $key => $value) {
  48. $this->goodsNum[] = count($value['goods'])?:1;
  49. if ($value['goods']) {
  50. foreach ($value['goods'] as $good) {
  51. $title = $good['goods_title'];
  52. if ($good['goods_option_title']) {
  53. $title .= ":{$good['goods_option_title']}";
  54. }
  55. $list[] = [
  56. $value['refund_sn'],
  57. $value['order_sn'],
  58. $value['uid'],
  59. $value['nickname'],
  60. $value['order_type_name'],
  61. $value['refund_type_name'],
  62. $value['status_name'],
  63. $value['price'],
  64. $title,
  65. $good['refund_total'],
  66. $value['create_time'],
  67. $value['refund_time'],
  68. $value['reason'],
  69. $value['part_refund_name'],
  70. ];
  71. }
  72. } else {
  73. $list[] = [
  74. $value['refund_sn'],
  75. $value['order_sn'],
  76. $value['uid'],
  77. $value['nickname'],
  78. $value['order_type_name'],
  79. $value['refund_type_name'],
  80. $value['status_name'],
  81. $value['price'],
  82. '无记录',
  83. '全额退款',
  84. $value['create_time'],
  85. $value['refund_time'],
  86. $value['reason'],
  87. $value['part_refund_name'],
  88. ];
  89. }
  90. }
  91. return $list;
  92. }
  93. public function styles(Worksheet $sheet)
  94. {
  95. for ($i = 0; $i < $this->column; $i++) {
  96. $y = ($i / 26);
  97. if ($y >= 1) {
  98. $y = intval($y);
  99. $cell[] = chr($y + 64) . chr($i - $y * 26 + 65);
  100. } else {
  101. $cell[] = chr($i + 65);
  102. }
  103. }
  104. // $sheet->mergeCells('A1:' . array_pop($cell) . '1'); //合并单元格
  105. //
  106. // $alignment = $sheet->getStyle('A1')->getAlignment();
  107. // $alignment->setHorizontal(Alignment::HORIZONTAL_CENTER); //水平居中
  108. // $alignment->setVertical(Alignment::VERTICAL_CENTER);////垂直居中
  109. //
  110. // $sheet->getStyle('A1')->getFont()->setSize(16)->setBold(true);//字体加粗
  111. $cell = array_filter($cell, function ($v) {
  112. return !in_array($v, ['I', 'J']);
  113. });
  114. foreach ($cell as $item) {
  115. $start = 2;
  116. foreach ($this->goodsNum as $key => $value) {
  117. $end = $start + $value - 1;
  118. if ($value > 1) {
  119. $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
  120. }
  121. $start = $end + 1;
  122. }
  123. }
  124. }
  125. }