OrderService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Modules\Admin\Services;
  3. use App\Base\BaseService;
  4. use App\Base\Utils;
  5. use App\Models\Order\Order;
  6. use App\Models\Order\OrderGoods;
  7. use App\Models\User\User;
  8. use Illuminate\Database\Eloquent\Builder;
  9. class OrderService extends BaseService
  10. {
  11. public function paginate($params)
  12. {
  13. $p = Order::when($params['id'], function (Builder $query) use ($params) {
  14. $query->where("id", $params['id']);
  15. })->when($params['userinfo'], function (Builder $query) use ($params) {
  16. //根据userinfo获取用户信息
  17. if ($user = User::where("email", $params['userinfo'])->orWhere("phone", $params['userinfo'])->orWhere("id", $params['userinfo'])->first()) {
  18. $query->where("user_id", $user->id);
  19. } else {
  20. $query->where("id", 0);
  21. }
  22. })->orderByDesc("id")->paginate($params['page_size']);
  23. return [
  24. "total" => $p->total(),
  25. "page_total" => $p->lastPage(),
  26. "list" => array_map(function (Order $model) {
  27. /** @var OrderGoods $firstGoods */
  28. $firstGoods = $model->goods->first();
  29. return [
  30. "id" => $model->id,
  31. "no" => $model->goods->pluck("sn")->implode("\sn"),
  32. "goods_name" => $firstGoods->goods->name,
  33. "goods_sn" => $firstGoods->sn,
  34. "user_id" => Utils::idNo4($model->user_id),
  35. "thumb" => $firstGoods->goods->thumb,
  36. "email" => $model->user->email,
  37. "created_at" => $model->created_at->format("Y-m-d H:i:s"),
  38. "goods" => $model->goods->map(function (OrderGoods $goods) {
  39. return [
  40. "goods_id" => $goods->id,
  41. "goods_name" => $goods->goods->name,
  42. "goods_thumb" => $goods->goods->thumb,
  43. "goods_sn" => $goods->sn,
  44. "goods_count" => $goods->count,
  45. "custom" => $goods->custom,
  46. ];
  47. }),
  48. "phone" => $model->user->phone,
  49. "name" => $model->user->name,
  50. "source" => $model->source,
  51. ];
  52. }, $p->items()),
  53. ];
  54. }
  55. }