'序号', // 'width' => 8, // ], [ 'name' => '订单号', 'width' => 22, 'field' => 'order_no' ], [ 'name' => '快递号', 'width' => 20, 'field' => 'wuliu_no' ], [ 'name' => '快递公司', 'width' => 20, 'field' => 'wuliu_type' ], [ 'name' => '用户昵称', 'width' => 20, ], [ 'name' => '用户手机号', 'width' => 15, ], [ 'name' => '收件人-姓名', 'width' => 20, 'field' => 'sjr_name' ], [ 'name' => '收件人-手机号', 'width' => 15, ], [ 'name' => '收件人地址-省', 'width' => 20, 'field' => 'sjr_address_province' ], [ 'name' => '收件人地址-市', 'width' => 20, 'field' => 'sjr_address_city' ], [ 'name' => '收件人地址-县', 'width' => 20, 'field' => 'sjr_address_area' ], [ 'name' => '收件人地址-地址详情', 'width' => 20, 'field' => 'sjr_address' ], [ 'name' => '商品总数量', 'width' => 15, 'field' => 'nums' ], ]; protected $rowNums = 0; protected $ids = []; protected $lastC = 'A'; protected $fields = []; protected $goods = []; public function __construct($ids, $field_ids = []) { $this->ids = $ids; $fields = []; if (!count($field_ids)) { $fields = self::BaseFields; } else { foreach ($field_ids as $field_id) { $fields[] = self::BaseFields[$field_id]; } } $goods = ShopGood::query()->where('status', ModelStatusEnum::OK)->select(['id', 'name'])->orderBy('id')->get(); $this->goods = $goods; foreach ($goods as $good) { $fields[] = [ 'name' => $good['name'], 'width' => 15, ]; } $fields[] = [ 'name' => '商品汇总', 'width' => 30, ]; $this->fields = $fields; } public function query() { $ids = $this->ids; return ShopOrder::query()->with(['user'])->whereIn('id', $ids)->where('status', ShopOrderStatusEnum::wait_send)->where('check_status', CheckStatusEnum::SUCCESS)->orderByDesc('id'); } public function map($row): array { $this->rowNums++; $data = []; foreach ($this->fields as $k => $item) { $field = $item['name']; if (isset($item['field'])) { $data[$k] = $row[$item['field']]; continue; } if ($field == '序号') { $data[$k] = $this->rowNums; continue; } if ($field == '用户昵称') { $data[$k] = isset($row['user']['nickname']) ? $row['user']['nickname'] : ''; continue; } if ($field == '用户手机号') { $data[$k] = isset($row['user']['mobile_encryption']) ? Crypt::decryptString($row['user']['mobile_encryption']) : ''; continue; } if ($field == '收件人-手机号') { $data[$k] = isset($row['sjr_mobile_encryption']) ? Crypt::decryptString($row['sjr_mobile_encryption']) : ''; continue; } if ($field == '商品汇总') { $da = []; $goods = ShopOrderGood::query()->with(['good'])->where('order_id', $row['id'])->get(); foreach ($goods as $good) { $name = isset($good['good']['name']) ? $good['good']['name'] : '未知'; $da[] = "{$name}*{$good['nums']}"; } $data[$k] = arr2str($da, '、'); continue; } foreach ($this->goods as $good) { if ($field == $good->name) { $data[$k] = (string)(ShopOrderGood::query()->where('order_id', $row['id'])->where('good_id', $good->id)->where('status', ModelStatusEnum::OK)->value('nums') ?? 0); break 1; } } } return $data; } public function headings(): array { $room_name = self::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); } }