60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Exporters;
|
|
|
|
use App\Models\User;
|
|
use Encore\Admin\Grid\Exporters\CsvExporter;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
|
|
|
class OrderExporter extends CsvExporter implements WithMapping, WithStrictNullComparison
|
|
{
|
|
protected $mapTrue = true;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->fileName = '订单导出' . date('YmdHis') . '.csv';
|
|
}
|
|
|
|
protected $headings = ['订单编号', '商品总数', '订单类型', '商品信息', '下单用户', '价格', '状态', '下单时间', '付款时间'];
|
|
|
|
public function map($info): array
|
|
{
|
|
$data = [
|
|
'`' . $info->orderid,
|
|
$info->details()->sum('number'),
|
|
$info->type_text,
|
|
self::getGoodsValue($info->details),
|
|
$info->user->info->nickname . "\r\n" . $info->user->username,
|
|
"商品小计:" . number_format($info->amount, 2) . "\r\n" . "应收总额:" . number_format($info->total, 2),
|
|
self::getOrderStatus($info),
|
|
$info->created_at,
|
|
$info->paid_at,
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function getGoodsValue($details)
|
|
{
|
|
$ret = '';
|
|
foreach ($details as $detail) {
|
|
$ret .= $detail['item']['name'] . number_format($detail['price'], 2) . "\r\n";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
protected function getOrderStatus($order)
|
|
{
|
|
$ret = $order->state_text . "\r\n";
|
|
if ($order->payment) {
|
|
$ret .= $order->payment->type_text . "\r\n";
|
|
$ret .= $order->payment->trade_no . "\r\n";
|
|
$ret .= $order->payment->transaction_id;
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
}
|