UserOrderGoodExport.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Exports\Dwbs;
  3. use App\Repositories\Enums\Dwbs\DayTypeEnum;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Models\Dwbs\Good;
  6. use App\Repositories\Models\Dwbs\OrderGood;
  7. use App\Repositories\Models\Dwbs\Ranking;
  8. use Illuminate\Support\Facades\Crypt;
  9. use Illuminate\Support\Facades\DB;
  10. use Maatwebsite\Excel\Concerns\Exportable;
  11. use Maatwebsite\Excel\Concerns\FromQuery;
  12. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  13. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  14. use Maatwebsite\Excel\Concerns\WithHeadings;
  15. use Maatwebsite\Excel\Concerns\WithMapping;
  16. use Maatwebsite\Excel\Concerns\WithStyles;
  17. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  18. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  19. class UserOrderGoodExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  20. {
  21. use Exportable;
  22. const Name = "用户订单商品数量清单";
  23. const BaseFields = [
  24. [
  25. 'name' => '用户昵称',
  26. 'width' => 20,
  27. ],
  28. [
  29. 'name' => '用户手机号',
  30. 'width' => 15,
  31. ],
  32. [
  33. 'name' => '累计学分',
  34. 'width' => 15,
  35. ],
  36. [
  37. 'name' => '商品总计',
  38. 'width' => 15,
  39. ],
  40. ];
  41. protected $rowNums = 0;
  42. protected $data = [];
  43. protected $gids = [];
  44. protected $lastC = 'A';
  45. protected $fields = [];
  46. protected $goods = [];
  47. public function __construct($data)
  48. {
  49. $this->data = $data;
  50. $fields = self::BaseFields;
  51. $goods = Good::query()->where('status', ModelStatusEnum::OK)->select(['id', 'name'])->orderBy('id')->get();
  52. $this->goods = $goods;
  53. $gids = [];
  54. foreach ($goods as $good) {
  55. $fields[] = [
  56. 'name' => $good['name'],
  57. 'width' => 15,
  58. ];
  59. $gids[] = $good['id'];
  60. }
  61. $this->gids = $gids;
  62. $this->fields = $fields;
  63. }
  64. public function query()
  65. {
  66. $da = $this->data;
  67. return Ranking::query()->when($da['start_day'], function ($query) use ($da) {
  68. return $query->where('day', '>=', $da['start_day']);
  69. })->when($da['end_day'], function ($query) use ($da) {
  70. return $query->where('day', '<=', $da['end_day']);
  71. })->where('day_type', DayTypeEnum::day)->with('user')->select(['user_id', DB::raw("sum(day_xuefen) as dxf")])->groupBy('user_id')->orderByDesc('dxf')->limit($da['limit']);
  72. }
  73. public function map($row): array
  74. {
  75. $this->rowNums++;
  76. $da = $this->data;
  77. $goods = OrderGood::query()->when($da['start_day'], function ($query) use ($da) {
  78. return $query->where('order_time', '>=', $da['start_day']);
  79. })->when($da['end_day'], function ($query) use ($da) {
  80. return $query->where('order_time', '<=', $da['end_day']);
  81. })->where('user_id', $row['user_id'])->whereIn('good_id', $this->gids)->select(['user_id', 'good_id', DB::raw('count(nums) as good_nums')])->groupBy('good_id')->pluck('good_nums', 'good_id')->toArray();
  82. $data = [];
  83. $data[] = isset($row['user']['nickname']) ? $row['user']['nickname'] : '';
  84. $data[] = isset($row['user']['mobile_encryption']) ? Crypt::decryptString($row['user']['mobile_encryption']) : '';
  85. $data[] = $row['dxf'];
  86. $data[] = array_sum($goods);
  87. foreach ($this->gids as $gid) {
  88. $data[] = isset($goods[$gid]) ? $goods[$gid] : 0;
  89. }
  90. return $data;
  91. }
  92. public function headings(): array
  93. {
  94. $room_name = self::Name;
  95. $data[] = [$room_name];
  96. $data[] = array_column($this->fields, 'name');
  97. return $data;
  98. }
  99. public function headingRow(): int
  100. {
  101. return 2;
  102. }
  103. public function columnWidths(): array
  104. {
  105. $i = 65;
  106. $data = [];
  107. foreach ($this->fields as $k => $field) {
  108. $s = intval($k / 26);
  109. if (!$s) $key = chr($i + $k);
  110. if ($s > 0) {
  111. $y = intval($k % 26);
  112. $key = chr($s + $i - 1);
  113. $key .= chr($y + $i);
  114. }
  115. $data["{$key}"] = $field['width'];
  116. $this->lastC = $key;
  117. }
  118. return $data;
  119. }
  120. public function styles(Worksheet $sheet)
  121. {
  122. $last = $this->lastC;
  123. $cellRange = "A1:{$last}1";
  124. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  125. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  126. // 文字居中
  127. $lastrow = $sheet->getHighestRow();
  128. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  129. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  130. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  131. $sheet->mergeCells("A1:{$last}1"); //合并
  132. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  133. }
  134. }