AdminUserExport.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Exports;
  3. use App\Base\Utils;
  4. use App\Models\User\User;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Support\Arr;
  7. use Maatwebsite\Excel\Concerns\FromCollection;
  8. class AdminUserExport implements FromCollection
  9. {
  10. public $params;
  11. /**
  12. * @param $params
  13. */
  14. public function __construct($params)
  15. {
  16. $this->params = $params;
  17. }
  18. public function collection()
  19. {
  20. $data = \App\Models\User\User::query()->with(["group", "company"])->when($this->params['status'], function (Builder $query) {
  21. return $query->where("status", $this->params['status']);
  22. })->when($this->params['userinfo'], function (Builder $query) {
  23. return $query->where(function (Builder $query) {
  24. return $query->where("phone", $this->params['userinfo'])->orWhere("email", $this->params['userinfo'])->orWhere("id", $this->params['userinfo']);
  25. });
  26. })->orderByDesc("id")->get()->map(function (User $u) {
  27. return [
  28. "id" => Utils::idNo4($u->id),
  29. "phone" => $u->phone,
  30. "email" => $u->email,
  31. "company" => optional($u->company)->name,
  32. "status" => $u->status == User::STATUS_OK ? "正常" : "禁用",
  33. "group_name" => optional($u->group)->name ?? "",
  34. "created_at" => $u->created_at->format("Y-m-d"),
  35. ];
  36. });
  37. $data->prepend(["用户ID", "用户手机号", "用户邮箱", "企业用户", "用户状态", "当前身份", "注册时间"]);
  38. return $data;
  39. }
  40. }