OrderService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Modules\Mini\Services;
  3. use App\Base\BaseService;
  4. use App\Base\Utils;
  5. use App\Events\OrderCreateEvent;
  6. use App\Models\Cart;
  7. use App\Models\Goods\Goods;
  8. use App\Models\Goods\GoodsCustom;
  9. use App\Models\Order\Order;
  10. use App\Models\Order\OrderGoods;
  11. use App\Models\User\User;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Illuminate\Support\Arr;
  14. use Illuminate\Support\Facades\DB;
  15. class OrderService extends BaseService
  16. {
  17. /**
  18. * 询价单个商品
  19. */
  20. public function askOne(User $user, $params)
  21. {
  22. /** @var Order $order */
  23. $order = DB::transaction(function () use ($user, $params) {
  24. $order = new Order();
  25. $order->user_id = $user->id;
  26. $order->source = $params['source'];
  27. $order->save();
  28. $goods = Goods::findOrFail($params['id']);
  29. $og = new OrderGoods();
  30. $og->order_id = $order->id;
  31. $og->goods_id = $params['id'];
  32. $og->sku_id = 0;
  33. $og->count = $params['count'] ?? 1;
  34. $og->sn = $goods->sn;
  35. $og->custom = $params['custom'];
  36. if ($skuCustom = Arr::get($params, "sku_custom")) {
  37. $og->sn = $og->sn . "-" . implode("-", Arr::pluck($skuCustom, "spec_attr_custom"));
  38. }
  39. $og->save();
  40. return $order;
  41. });
  42. OrderCreateEvent::dispatch($order);
  43. return [
  44. "id" => $order->id,
  45. ];
  46. }
  47. public function askByCart(User $user, $params)
  48. {
  49. /** @var Order $order */
  50. $order = DB::transaction(function () use ($user, $params) {
  51. $order = new Order();
  52. $order->user_id = $user->id;
  53. $order->source = $params['source'];
  54. $order->save();
  55. foreach ($params['ids'] as $param) {
  56. $cart = Cart::where("user_id", $user->id)->where("id", $param['id'])->first();
  57. if (is_null($cart)) {
  58. continue;
  59. }
  60. $og = new OrderGoods();
  61. $og->order_id = $order->id;
  62. $og->goods_id = $cart->goods_id;
  63. $og->sku_id = 0;
  64. $og->count = $cart->count;
  65. $og->custom = $cart->custom;
  66. $og->save();
  67. }
  68. return $order;
  69. });
  70. OrderCreateEvent::dispatch($order);
  71. return [
  72. "id" => $order->id,
  73. ];
  74. }
  75. public function paginate($uid, $params)
  76. {
  77. $p = Order::where("user_id", $uid)->orderByDesc("id")->paginate($params['page_size']);
  78. return [
  79. "total" => $p->total(),
  80. "page_total" => $p->lastPage(),
  81. "list" => array_map(function (Order $model) {
  82. /** @var OrderGoods $firstGoods */
  83. $firstGoods = $model->goods->first();
  84. return [
  85. "id" => $model->id,
  86. "no" => $model->goods->pluck("sn")->implode("\sn"),
  87. "goods_name" => $firstGoods->goods->name,
  88. "goods_sn" => $firstGoods->sn,
  89. "user_id" => Utils::idNo4($model->user_id),
  90. "thumb" => $firstGoods->goods->thumb,
  91. "email" => $model->user->email,
  92. "created_at" => $model->created_at->format("Y-m-d H:i:s"),
  93. "goods" => $model->goods->map(function (OrderGoods $goods) {
  94. return [
  95. "goods_id" => $goods->id,
  96. "goods_name" => $goods->goods->name,
  97. "goods_en_name" => $goods->goods->en_name,
  98. "goods_thumb" => $goods->goods->thumb,
  99. "goods_sn" => $goods->sn,
  100. "goods_count" => $goods->count,
  101. "custom" => array_map(function ($custom) {
  102. $custom = GoodsCustom::where("id", $custom['custom_id'])->first();
  103. return [
  104. "custom_id" => $custom['custom_id'],
  105. "title" => $custom->title,
  106. "en_title" => $custom->en_title,
  107. "value" => $custom['value'] ?? "",
  108. ];
  109. }, $goods->custom ?? []),
  110. ];
  111. }),
  112. "phone" => $model->user->phone,
  113. "name" => $model->user->name,
  114. "source" => $model->source,
  115. ];
  116. }, $p->items()),
  117. ];
  118. }
  119. }