ShopOrderExport.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Exports\Dwbs;
  3. use App\Repositories\Enums\CheckStatusEnum;
  4. use App\Repositories\Enums\Dwbs\ShopOrderStatusEnum;
  5. use App\Repositories\Enums\ModelStatusEnum;
  6. use App\Repositories\Models\Dwbs\ShopGood;
  7. use App\Repositories\Models\Dwbs\ShopOrder;
  8. use App\Repositories\Models\Dwbs\ShopOrderGood;
  9. use Illuminate\Support\Facades\Crypt;
  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 ShopOrderExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  20. {
  21. use Exportable;
  22. const Name = "积分商城兑换订单列表";
  23. const BaseFields = [
  24. // [
  25. // 'name' => '序号',
  26. // 'width' => 8,
  27. // ],
  28. [
  29. 'name' => '订单号',
  30. 'width' => 22,
  31. 'field' => 'order_no'
  32. ],
  33. [
  34. 'name' => '快递号',
  35. 'width' => 20,
  36. 'field' => 'wuliu_no'
  37. ],
  38. [
  39. 'name' => '快递公司',
  40. 'width' => 20,
  41. 'field' => 'wuliu_type'
  42. ],
  43. [
  44. 'name' => '用户昵称',
  45. 'width' => 20,
  46. ],
  47. [
  48. 'name' => '用户手机号',
  49. 'width' => 15,
  50. ],
  51. [
  52. 'name' => '收件人-姓名',
  53. 'width' => 20,
  54. 'field' => 'sjr_name'
  55. ],
  56. [
  57. 'name' => '收件人-手机号',
  58. 'width' => 15,
  59. ],
  60. [
  61. 'name' => '收件人地址-省',
  62. 'width' => 20,
  63. 'field' => 'sjr_address_province'
  64. ],
  65. [
  66. 'name' => '收件人地址-市',
  67. 'width' => 20,
  68. 'field' => 'sjr_address_city'
  69. ],
  70. [
  71. 'name' => '收件人地址-县',
  72. 'width' => 20,
  73. 'field' => 'sjr_address_area'
  74. ],
  75. [
  76. 'name' => '收件人地址-地址详情',
  77. 'width' => 20,
  78. 'field' => 'sjr_address'
  79. ],
  80. [
  81. 'name' => '商品总数量',
  82. 'width' => 15,
  83. 'field' => 'nums'
  84. ],
  85. ];
  86. protected $rowNums = 0;
  87. protected $ids = [];
  88. protected $lastC = 'A';
  89. protected $fields = [];
  90. protected $goods = [];
  91. public function __construct($ids, $field_ids = [])
  92. {
  93. $this->ids = $ids;
  94. $fields = [];
  95. if (!count($field_ids)) {
  96. $fields = self::BaseFields;
  97. } else {
  98. foreach ($field_ids as $field_id) {
  99. $fields[] = self::BaseFields[$field_id];
  100. }
  101. }
  102. $goods = ShopGood::query()->where('status', ModelStatusEnum::OK)->select(['id', 'name'])->orderBy('id')->get();
  103. $this->goods = $goods;
  104. foreach ($goods as $good) {
  105. $fields[] = [
  106. 'name' => $good['name'],
  107. 'width' => 15,
  108. ];
  109. }
  110. $fields[] = [
  111. 'name' => '商品汇总',
  112. 'width' => 30,
  113. ];
  114. $this->fields = $fields;
  115. }
  116. public function query()
  117. {
  118. $ids = $this->ids;
  119. return ShopOrder::query()->with(['user'])->whereIn('id', $ids)->where('status', ShopOrderStatusEnum::wait_send)->where('check_status', CheckStatusEnum::SUCCESS)->orderByDesc('id');
  120. }
  121. public function map($row): array
  122. {
  123. $this->rowNums++;
  124. $data = [];
  125. foreach ($this->fields as $k => $item) {
  126. $field = $item['name'];
  127. if (isset($item['field'])) {
  128. $data[$k] = $row[$item['field']];
  129. continue;
  130. }
  131. if ($field == '序号') {
  132. $data[$k] = $this->rowNums;
  133. continue;
  134. }
  135. if ($field == '用户昵称') {
  136. $data[$k] = isset($row['user']['nickname']) ? $row['user']['nickname'] : '';
  137. continue;
  138. }
  139. if ($field == '用户手机号') {
  140. $data[$k] = isset($row['user']['mobile_encryption']) ? Crypt::decryptString($row['user']['mobile_encryption']) : '';
  141. continue;
  142. }
  143. if ($field == '收件人-手机号') {
  144. $data[$k] = isset($row['sjr_mobile_encryption']) ? Crypt::decryptString($row['sjr_mobile_encryption']) : '';
  145. continue;
  146. }
  147. if ($field == '商品汇总') {
  148. $da = [];
  149. $goods = ShopOrderGood::query()->with(['good'])->where('order_id', $row['id'])->get();
  150. foreach ($goods as $good) {
  151. $name = isset($good['good']['name']) ? $good['good']['name'] : '未知';
  152. $da[] = "{$name}*{$good['nums']}";
  153. }
  154. $data[$k] = arr2str($da, '、');
  155. continue;
  156. }
  157. foreach ($this->goods as $good) {
  158. if ($field == $good->name) {
  159. $data[$k] = (string)(ShopOrderGood::query()->where('order_id', $row['id'])->where('good_id', $good->id)->where('status', ModelStatusEnum::OK)->value('nums') ?? 0);
  160. break 1;
  161. }
  162. }
  163. }
  164. return $data;
  165. }
  166. public function headings(): array
  167. {
  168. $room_name = self::Name;
  169. $data[] = [$room_name];
  170. $data[] = array_column($this->fields, 'name');
  171. return $data;
  172. }
  173. public function headingRow(): int
  174. {
  175. return 2;
  176. }
  177. public function columnWidths(): array
  178. {
  179. $i = 65;
  180. $data = [];
  181. foreach ($this->fields as $k => $field) {
  182. $s = intval($k / 26);
  183. if (!$s) $key = chr($i + $k);
  184. if ($s > 0) {
  185. $y = intval($k % 26);
  186. $key = chr($s + $i - 1);
  187. $key .= chr($y + $i);
  188. }
  189. $data["{$key}"] = $field['width'];
  190. $this->lastC = $key;
  191. }
  192. return $data;
  193. }
  194. public function styles(Worksheet $sheet)
  195. {
  196. $last = $this->lastC;
  197. $cellRange = "A1:{$last}1";
  198. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  199. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  200. // 文字居中
  201. $lastrow = $sheet->getHighestRow();
  202. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  203. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  204. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  205. $sheet->mergeCells("A1:{$last}1"); //合并
  206. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  207. }
  208. }