123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Exports;
- use App\Base\Utils;
- use App\Models\Order\Order;
- use App\Models\Order\OrderGoods;
- use App\Models\User\User;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Arr;
- use Maatwebsite\Excel\Concerns\FromCollection;
- class AdminOrderExport implements FromCollection
- {
- public $params;
- /**
- * @param $params
- */
- public function __construct($params)
- {
- $this->params = $params;
- }
- public function collection()
- {
- $data = Order::when($this->params['id'], function (Builder $query) {
- $query->where("id", $this->params['id']);
- })->when($this->params['userinfo'], function (Builder $query) {
- //根据userinfo获取用户信息
- if ($user = User::where("email", $this->params['userinfo'])->orWhere("phone", $this->params['userinfo'])->orWhere("id", Utils::idHas4($this->params['userinfo']))->first()) {
- $query->where("user_id", $user->id);
- } else {
- $query->where("id", 0);
- }
- })->orderByDesc("id")->get()->map(function (Order $model) {
- $goodsInfo = "";
- foreach ($model->goods as $g) {
- $goodsInfo .= sprintf("%s %s x %s (%s) \n", $g->goods->name, $g->sn, $g->count, implode("-", Arr::pluck($g->custom ?? [], "value")));
- }
- return [
- "id" => $model->id,
- "goods" => $goodsInfo,
- "user_id" => Utils::idNo4($model->user_id),
- "name" => $model->user->name,
- "phone" => $model->user->phone,
- "email" => $model->user->email,
- "created_at" => $model->created_at->format("Y-m-d H:i:s"),
- ];
- });
- $data->prepend(["编号", "商品信息", "用户ID", "用户昵称", "用户手机", "用户邮箱", "询价时间"]);
- return $data;
- }
- }
|