ufutx.dma/app/Exports/OfflineOrderExport.php
2026-03-04 14:42:40 +08:00

125 lines
4.1 KiB
PHP

<?php
namespace App\Exports;
use App\Models\OfflineOrder;
use App\Models\Order;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class OfflineOrderExport implements FromArray,WithHeadings,WithStyles,WithEvents
{
protected $keyword;
protected $status;
protected $agency_id;
protected $header;
public function __construct($data)
{
$this->keyword = $data['keyword']??null;
$this->status = $data['status']??null;
$this->agency_id = $data['agency_id']??null;
}
public function array():array
{
$keywords = $this->keyword;
$status = $this->status;
$agency_id = $this->agency_id;
$orders = [];
$offline_orders = OfflineOrder::with([
'main_order',
'agency:id,name,mobile',
'order_commission' => function($commission){
$commission->with(['service_user:user_id,name,mobile'])->select('id','order_id','role','user_id');
}
])
->when($keywords,function ($query) use ($keywords) {
$query->where(function ($query) use ($keywords) {
$query->where('name','like','%'.$keywords.'%')->orWhere('mobile','like','%'.$keywords.'%');
});
})
->when(!empty($status),function ($query) use ($status) {
$query->whereHas('main_order',function ($query) use ($status) {
$query->where('status',$status);
});
})
->when($agency_id,function ($query) use ($agency_id) {
$query->where('agency_id',$agency_id);
})
->orderByDesc('id')
->get();
// dd($offline_orders);
foreach ($offline_orders as $offline_order) {
$order = $offline_order->main_order;
$commission_arr = [
1=>'',
2=>'',
3=>'',
4=>'',
5=>'',
];
foreach ($offline_order->order_commission as $commission){
$commission_arr[$commission->role] .= $commission->service_user?$commission->service_user->name:'' .',';
}
$orders[] = [$order->id, $order->name, $order->mobile, $order->price,'虚拟订单',
['NOTSTART'=>'未开始','STARTING'=>'进行中','FINISHED'=>'已结束'][$order->status],
$order->agency?->name??'',
'',
$commission_arr[5],
$commission_arr[1],
$commission_arr[2],
$commission_arr[3],
$offline_order->contract_no,
$offline_order->created_at,
];
}
return $orders;
}
public function headings(): array
{
return [
['订单id', '联系人姓名', '联系人电话', '订单价格', '订单类型', '服务状态','子公司', '合作伙伴', '渠道商', '主教练', '副教练', '客服', '合同号', '创建时间']
];
}
public function styles(Worksheet $sheet)
{
$sheet->getStyle('C')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
return [
1 => [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
'font' => [
'bold' => true,
],
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
],
],
],
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getDelegate()->calculateColumnWidths();
},
];
}
}