OrderExport.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Exports\Car;
  3. use App\Repositories\Enums\Car\BillDisburseEnum;
  4. use App\Repositories\Enums\Car\OrderDrivingStatusEnum;
  5. use App\Repositories\Enums\Car\OrderPayTypeEnum;
  6. use App\Repositories\Enums\Check\StatusEnum;
  7. use App\Repositories\Models\Base\Dict;
  8. use App\Repositories\Models\Car\Bill;
  9. use App\Repositories\Models\Car\Order;
  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 OrderExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  20. {
  21. use Exportable;
  22. const BaseFields = [
  23. [
  24. 'name' => '序号',
  25. 'width' => 8,
  26. ],
  27. [
  28. 'name' => '报名日期',
  29. 'width' => 15,
  30. ],
  31. [
  32. 'name' => '姓名',
  33. 'width' => 15,
  34. ],
  35. [
  36. 'name' => '手机号',
  37. 'width' => 15,
  38. ],
  39. [
  40. 'name' => '身份证号',
  41. 'width' => 30,
  42. ],
  43. [
  44. 'name' => '班型',
  45. 'width' => 15,
  46. ],
  47. [
  48. 'name' => '缴费类型',
  49. 'width' => 15,
  50. ],
  51. [
  52. 'name' => '总学费',
  53. 'width' => 10,
  54. ],
  55. [
  56. 'name' => '优惠金额',
  57. 'width' => 10,
  58. ],
  59. [
  60. 'name' => '已交学费',
  61. 'width' => 10,
  62. ],
  63. [
  64. 'name' => '学车进度',
  65. 'width' => 15,
  66. ],
  67. [
  68. 'name' => '门店名称',
  69. 'width' => 20,
  70. ],
  71. [
  72. 'name' => '准驾类型',
  73. 'width' => 10,
  74. ],
  75. [
  76. 'name' => '招生老师',
  77. 'width' => 15,
  78. ],
  79. [
  80. 'name' => '优惠备注',
  81. 'width' => 30,
  82. ],
  83. [
  84. 'name' => '科目二教练',
  85. 'width' => 15,
  86. ],
  87. [
  88. 'name' => '科目三教练',
  89. 'width' => 15,
  90. ],
  91. [
  92. 'name' => '科目一挂科次数',
  93. 'width' => 15,
  94. ],
  95. [
  96. 'name' => '科目二挂科次数',
  97. 'width' => 15,
  98. ],
  99. [
  100. 'name' => '科目三挂科次数',
  101. 'width' => 15,
  102. ],
  103. ];
  104. protected $rowNums = 0;
  105. protected $where = [];
  106. protected $lastC = 'A';
  107. protected $fields = [];
  108. public function __construct($where, $field_ids = [])
  109. {
  110. $this->where = $where;
  111. $fields = [];
  112. if (!count($field_ids)) {
  113. $fields = self::BaseFields;
  114. } else {
  115. foreach ($field_ids as $field_id) {
  116. $fields[] = self::BaseFields[$field_id];
  117. }
  118. }
  119. $this->fields = $fields;
  120. }
  121. public function query()
  122. {
  123. $where = $this->where;
  124. return Order::query()
  125. ->with(['user', 'shop', 'grade', 'subject_2_admin', 'subject_3_admin', 'salesman_admin'])
  126. ->when(array_key_exists('ids', $where) && count($where['ids']), function ($query) use ($where) {
  127. return $query->whereIn('id', $where['ids']);
  128. })
  129. ->when(array_key_exists('driving_status', $where), function ($query) use ($where) {
  130. return $query->where('driving_status', $where['driving_status']);
  131. })
  132. ->when(array_key_exists('sign_up_day', $where), function ($query) use ($where) {
  133. return $query->where('sign_up_day', $where['sign_up_day']);
  134. })
  135. ->when(array_key_exists('start_sign_up_day', $where), function ($query) use ($where) {
  136. return $query->where('sign_up_day', '>=', $where['start_sign_up_day']);
  137. })
  138. ->when(array_key_exists('end_sign_up_day', $where), function ($query) use ($where) {
  139. return $query->where('sign_up_day', '<=', $where['end_sign_up_day']);
  140. })
  141. ->when(array_key_exists('salesman_admin_id', $where), function ($query) use ($where) {
  142. return $query->where('salesman_admin_id', $where['salesman_admin_id']);
  143. })
  144. ->when(array_key_exists('shop_id', $where), function ($query) use ($where) {
  145. return $query->where('shop_id', $where['shop_id']);
  146. })
  147. ->when(array_key_exists('grade_id', $where), function ($query) use ($where) {
  148. return $query->where('grade_id', $where['grade_id']);
  149. })
  150. ->orderByDesc('id');
  151. }
  152. public function map($row): array
  153. {
  154. $row = $row->toArray();
  155. $this->rowNums++;
  156. $data = [];
  157. foreach (array_column($this->fields, 'name') as $k => $field) {
  158. if ($field == '序号') $data[$k] = $this->rowNums;
  159. if ($field == '报名日期') $data[$k] = $row['sign_up_day'];
  160. if ($field == '姓名') $data[$k] = $row['user'] ? $row['user']['name'] : '';
  161. if ($field == '手机号') $data[$k] = $row['user_mobile'];
  162. if ($field == '身份证号') $data[$k] = $row['user_id_card'];
  163. if ($field == '总学费') $data[$k] = $row['total_money'];
  164. if ($field == '优惠金额') $data[$k] = $row['coupon'];
  165. if ($field == '已交学费') {
  166. $data[$k] = (string)Bill::query()->where('order_id', $row['id'])->where('check_status', StatusEnum::SUCCESS)->where('disburse', BillDisburseEnum::IN)->sum('money');
  167. }
  168. if ($field == '优惠备注') $data[$k] = $row['coupon_reason'];
  169. if ($field == '科目一挂科次数') $data[$k] = (string)$row['ke_1_nums'];
  170. if ($field == '科目二挂科次数') $data[$k] = (string)$row['ke_2_nums'];
  171. if ($field == '科目三挂科次数') $data[$k] = (string)$row['ke_3_nums'];
  172. if ($field == '班型') $data[$k] = $row['grade'] ? $row['grade']['name'] : '';
  173. if ($field == '门店名称') $data[$k] = $row['shop'] ? $row['shop']['name'] : '';
  174. if ($field == '招生老师') $data[$k] = $row['salesman_admin'] ? $row['salesman_admin']['name'] : '';
  175. if ($field == '科目二教练') $data[$k] = $row['subject_2_admin'] ? $row['subject_2_admin']['name'] : '';
  176. if ($field == '科目三教练') $data[$k] = $row['subject_3_admin'] ? $row['subject_3_admin']['name'] : '';
  177. if ($field == '缴费类型') $data[$k] = OrderPayTypeEnum::getDescription($row['pay_type']);
  178. if ($field == '学车进度') $data[$k] = OrderDrivingStatusEnum::getDescription($row['driving_status']);
  179. if ($field == '准驾类型') $data[$k] = Dict::byCodeAndIdGetDict('CAR_TYPE', $row['drive_type']);
  180. }
  181. return $data;
  182. }
  183. public function headings(): array
  184. {
  185. $room_name = '学员列表';
  186. $data[] = [$room_name];
  187. $data[] = array_column($this->fields, 'name');
  188. return $data;
  189. }
  190. public function headingRow(): int
  191. {
  192. return 2;
  193. }
  194. public function columnWidths(): array
  195. {
  196. $i = 65;
  197. $data = [];
  198. foreach ($this->fields as $k => $field) {
  199. $s = intval($k / 26);
  200. if (!$s) $key = chr($i + $k);
  201. if ($s > 0) {
  202. $y = intval($k % 26);
  203. $key = chr($s + $i - 1);
  204. $key .= chr($y + $i);
  205. }
  206. $data["{$key}"] = $field['width'];
  207. $this->lastC = $key;
  208. }
  209. return $data;
  210. }
  211. public function styles(Worksheet $sheet)
  212. {
  213. $last = $this->lastC;
  214. $cellRange = "A1:{$last}1";
  215. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  216. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  217. // 文字居中
  218. $lastrow = $sheet->getHighestRow();
  219. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  220. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  221. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  222. $sheet->mergeCells("A1:{$last}1"); //合并
  223. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  224. }
  225. }