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

83 lines
2.7 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Food;
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 FoodExport implements FromArray,WithHeadings,WithStyles,WithEvents
{
protected $shops;
public function __construct($shops)
{
$this->shops = $shops;
}
public function array():array
{
$shops = $this->shops;
$rows = [];
foreach ($shops as $shop)
{
$sku_name ='';
$freight = '';
$price = '';
foreach ($shop['sku'] as $sku) {
$sku_name .= $sku['name'] . "/" . $sku["stock"] . ' | ';
if (isset($sku['ship_num'])) {
$ready_agent_price = $sku['ready_agent_price']??"";
$freight .= "{$sku['ship_num']}{$sku['ship_price']}元,每增加一件{$sku['ship_each_price']}元,最高{$sku['ship_max_price']}元 | ";
$price .= "{$sku["price"]}/{$ready_agent_price}/{$sku['agent_price']}/{$sku['staff_price']} | ";
}
}
$rows[] = ['id'=>$shop['id'], 'name'=>$shop['icon'], 'title'=>$shop['title'], 'unit'=>$shop->shopFood->unit, 'quantity'=>$shop->shopFood->quantity, 'sku_name'=>$sku_name, "price"=>$price, 'freight'=>$freight, 'is_show'=>$shop->shopFood->is_sale?"上架":"下架"];
}
return $rows;
}
public function headings(): array
{
return [
['id','商品图片','商品名称',"单位","", '规格名称/库存', "客户价/准批发商价/批发商价格/员工价", '邮费', "上架状态"]
];
}
public function styles(Worksheet $sheet)
{
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();
},
];
}
}