UserOrderGoodExport.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(1);
  72. }
  73. public function map($row): array
  74. {
  75. $this->rowNums++;
  76. if ($this->rowNums > $this->data['limit']) return [];
  77. $da = $this->data;
  78. $goods = OrderGood::query()->when($da['start_day'], function ($query) use ($da) {
  79. return $query->where('order_time', '>=', $da['start_day']);
  80. })->when($da['end_day'], function ($query) use ($da) {
  81. return $query->where('order_time', '<=', $da['end_day']);
  82. })->where('user_id', $row['user_id'])->whereIn('good_id', $this->gids)->select(['user_id', 'good_id', DB::raw('sum(nums) as good_nums')])->groupBy('good_id')->pluck('good_nums', 'good_id')->toArray();
  83. $data = [];
  84. $data[] = isset($row['user']['nickname']) ? $row['user']['nickname'] : '';
  85. $data[] = isset($row['user']['mobile_encryption']) ? Crypt::decryptString($row['user']['mobile_encryption']) : '';
  86. $data[] = $row['dxf'];
  87. $data[] = array_sum($goods);
  88. foreach ($this->gids as $gid) {
  89. $data[] = isset($goods[$gid]) ? $goods[$gid] : 0;
  90. }
  91. return $data;
  92. }
  93. public function headings(): array
  94. {
  95. $room_name = self::Name;
  96. $data[] = [$room_name];
  97. $data[] = array_column($this->fields, 'name');
  98. return $data;
  99. }
  100. public function headingRow(): int
  101. {
  102. return 2;
  103. }
  104. public function columnWidths(): array
  105. {
  106. $i = 65;
  107. $data = [];
  108. foreach ($this->fields as $k => $field) {
  109. $s = intval($k / 26);
  110. if (!$s) $key = chr($i + $k);
  111. if ($s > 0) {
  112. $y = intval($k % 26);
  113. $key = chr($s + $i - 1);
  114. $key .= chr($y + $i);
  115. }
  116. $data["{$key}"] = $field['width'];
  117. $this->lastC = $key;
  118. }
  119. return $data;
  120. }
  121. public function styles(Worksheet $sheet)
  122. {
  123. $last = $this->lastC;
  124. $cellRange = "A1:{$last}1";
  125. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  126. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  127. // 文字居中
  128. $lastrow = $sheet->getHighestRow();
  129. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  130. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  131. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  132. $sheet->mergeCells("A1:{$last}1"); //合并
  133. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  134. }
  135. }