'序号', 'width' => 8, ], [ 'name' => '报名日期', 'width' => 15, ], [ 'name' => '姓名', 'width' => 15, ], [ 'name' => '手机号', 'width' => 15, ], [ 'name' => '身份证号', 'width' => 30, ], [ 'name' => '班型', 'width' => 15, ], [ 'name' => '缴费类型', 'width' => 15, ], [ 'name' => '总学费', 'width' => 10, ], [ 'name' => '优惠金额', 'width' => 10, ], [ 'name' => '已交学费', 'width' => 10, ], [ 'name' => '学车进度', 'width' => 15, ], [ 'name' => '门店名称', 'width' => 20, ], [ 'name' => '准驾类型', 'width' => 10, ], [ 'name' => '招生老师', 'width' => 15, ], [ 'name' => '优惠备注', 'width' => 30, ], [ 'name' => '科目二教练', 'width' => 15, ], [ 'name' => '科目三教练', 'width' => 15, ], [ 'name' => '科目一挂科次数', 'width' => 15, ], [ 'name' => '科目二挂科次数', 'width' => 15, ], [ 'name' => '科目三挂科次数', 'width' => 15, ], ]; protected $rowNums = 0; protected $where = []; protected $lastC = 'A'; protected $fields = []; public function __construct($where, $field_ids = []) { $this->where = $where; $fields = []; if (!count($field_ids)) { $fields = self::BaseFields; } else { foreach ($field_ids as $field_id) { $fields[] = self::BaseFields[$field_id]; } } $this->fields = $fields; } public function query() { $where = $this->where; return Order::query() ->with(['user', 'shop', 'grade', 'subject_2_admin', 'subject_3_admin', 'salesman_admin']) ->when(array_key_exists('ids', $where) && count($where['ids']), function ($query) use ($where) { return $query->whereIn('id', $where['ids']); }) ->when(array_key_exists('driving_status', $where), function ($query) use ($where) { return $query->where('driving_status', $where['driving_status']); }) ->when(array_key_exists('sign_up_day', $where), function ($query) use ($where) { return $query->where('sign_up_day', $where['sign_up_day']); }) ->when(array_key_exists('start_sign_up_day', $where), function ($query) use ($where) { return $query->where('sign_up_day', '>=', $where['start_sign_up_day']); }) ->when(array_key_exists('end_sign_up_day', $where), function ($query) use ($where) { return $query->where('sign_up_day', '<=', $where['end_sign_up_day']); }) ->when(array_key_exists('salesman_admin_id', $where), function ($query) use ($where) { return $query->where('salesman_admin_id', $where['salesman_admin_id']); }) ->when(array_key_exists('shop_id', $where), function ($query) use ($where) { return $query->where('shop_id', $where['shop_id']); }) ->when(array_key_exists('grade_id', $where), function ($query) use ($where) { return $query->where('grade_id', $where['grade_id']); }) ->orderByDesc('id'); } public function map($row): array { $row = $row->toArray(); $this->rowNums++; $data = []; foreach (array_column($this->fields, 'name') as $k => $field) { if ($field == '序号') $data[$k] = $this->rowNums; if ($field == '报名日期') $data[$k] = $row['sign_up_day']; if ($field == '姓名') $data[$k] = $row['user'] ? $row['user']['name'] : ''; if ($field == '手机号') $data[$k] = $row['user_mobile']; if ($field == '身份证号') $data[$k] = $row['user_id_card']; if ($field == '总学费') $data[$k] = $row['total_money']; if ($field == '优惠金额') $data[$k] = $row['coupon']; if ($field == '已交学费') { $data[$k] = (string)Bill::query()->where('order_id', $row['id'])->where('check_status', StatusEnum::SUCCESS)->where('disburse', BillDisburseEnum::IN)->sum('money'); } if ($field == '优惠备注') $data[$k] = $row['coupon_reason']; if ($field == '科目一挂科次数') $data[$k] = (string)$row['ke_1_nums']; if ($field == '科目二挂科次数') $data[$k] = (string)$row['ke_2_nums']; if ($field == '科目三挂科次数') $data[$k] = (string)$row['ke_3_nums']; if ($field == '班型') $data[$k] = $row['grade'] ? $row['grade']['name'] : ''; if ($field == '门店名称') $data[$k] = $row['shop'] ? $row['shop']['name'] : ''; if ($field == '招生老师') $data[$k] = $row['salesman_admin'] ? $row['salesman_admin']['name'] : ''; if ($field == '科目二教练') $data[$k] = $row['subject_2_admin'] ? $row['subject_2_admin']['name'] : ''; if ($field == '科目三教练') $data[$k] = $row['subject_3_admin'] ? $row['subject_3_admin']['name'] : ''; if ($field == '缴费类型') $data[$k] = OrderPayTypeEnum::getDescription($row['pay_type']); if ($field == '学车进度') $data[$k] = OrderDrivingStatusEnum::getDescription($row['driving_status']); if ($field == '准驾类型') $data[$k] = Dict::byCodeAndIdGetDict('CAR_TYPE', $row['drive_type']); } return $data; } public function headings(): array { $room_name = '学员列表'; $data[] = [$room_name]; $data[] = array_column($this->fields, 'name'); return $data; } public function headingRow(): int { return 2; } public function columnWidths(): array { $i = 65; $data = []; foreach ($this->fields as $k => $field) { $s = intval($k / 26); if (!$s) $key = chr($i + $k); if ($s > 0) { $y = intval($k % 26); $key = chr($s + $i - 1); $key .= chr($y + $i); } $data["{$key}"] = $field['width']; $this->lastC = $key; } return $data; } public function styles(Worksheet $sheet) { $last = $this->lastC; $cellRange = "A1:{$last}1"; $sheet->getStyle($cellRange)->getFont()->setSize(12); $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高 // 文字居中 $lastrow = $sheet->getHighestRow(); $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中 $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true); $sheet->mergeCells("A1:{$last}1"); //合并 $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16); } }