AdminOrderExport.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Exports;
  3. use App\Base\Utils;
  4. use App\Models\Order\Order;
  5. use App\Models\Order\OrderGoods;
  6. use App\Models\User\User;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Arr;
  9. use Maatwebsite\Excel\Concerns\FromCollection;
  10. class AdminOrderExport implements FromCollection
  11. {
  12. public $params;
  13. /**
  14. * @param $params
  15. */
  16. public function __construct($params)
  17. {
  18. $this->params = $params;
  19. }
  20. public function collection()
  21. {
  22. $data = Order::when($this->params['id'], function (Builder $query) {
  23. $query->where("id", $this->params['id']);
  24. })->when($this->params['userinfo'], function (Builder $query) {
  25. //根据userinfo获取用户信息
  26. if ($user = User::where("email", $this->params['userinfo'])->orWhere("phone", $this->params['userinfo'])->orWhere("id", Utils::idHas4($this->params['userinfo']))->first()) {
  27. $query->where("user_id", $user->id);
  28. } else {
  29. $query->where("id", 0);
  30. }
  31. })->orderByDesc("id")->get()->map(function (Order $model) {
  32. $goodsInfo = "";
  33. foreach ($model->goods as $g) {
  34. $goodsInfo .= sprintf("%s %s x %s (%s) \n", $g->goods->name, $g->sn, $g->count, implode("-", Arr::pluck($g->custom ?? [], "value")));
  35. }
  36. return [
  37. "id" => $model->id,
  38. "goods" => $goodsInfo,
  39. "user_id" => Utils::idNo4($model->user_id),
  40. "name" => $model->user->name,
  41. "phone" => $model->user->phone,
  42. "email" => $model->user->email,
  43. "created_at" => $model->created_at->format("Y-m-d H:i:s"),
  44. ];
  45. });
  46. $data->prepend(["编号", "商品信息", "用户ID", "用户昵称", "用户手机", "用户邮箱", "询价时间"]);
  47. return $data;
  48. }
  49. }