OrderService.php 2.9 KB

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