123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace App\Exports\Car;
- use App\Repositories\Enums\Car\BillDisburseEnum;
- use App\Repositories\Enums\Car\OrderDrivingStatusEnum;
- use App\Repositories\Enums\Car\OrderPayTypeEnum;
- use App\Repositories\Enums\Check\StatusEnum;
- use App\Repositories\Models\Base\Dict;
- use App\Repositories\Models\Car\Bill;
- use App\Repositories\Models\Car\Order;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromQuery;
- use Maatwebsite\Excel\Concerns\ShouldAutoSize;
- use Maatwebsite\Excel\Concerns\WithColumnWidths;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithMapping;
- use Maatwebsite\Excel\Concerns\WithStyles;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
- class OrderExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
- {
- use Exportable;
- const BaseFields = [
- [
- 'name' => '序号',
- '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);
- }
- }
|