海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.8 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. use App\Models\UserMoneyLog;
  6. use App\Models\Order;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 订单
  11. * Class OrderController
  12. * @package App\Http\Controllers\Api
  13. */
  14. class OrderController extends Controller
  15. {
  16. //订单列表
  17. public function index(Request $request)
  18. {
  19. $formData = $request->only(['page', 'status']);
  20. $request->validate([
  21. 'page' => 'regex:/^\d+$/',
  22. 'status' => 'integer'
  23. ], [
  24. 'page.regex' => '页码错误',
  25. 'status.integer' => '未指定订单状态'
  26. ]);
  27. if (array_key_exists('status', $formData) && $formData['status'] !== null) {
  28. $where['status'] = $formData['status'];
  29. }
  30. $where['user_id'] = $this->user_id;
  31. $list = Order::where($where)
  32. ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')])
  33. ->select('id', 'product_id', 'price', 'num', 'created_at')
  34. ->orderBy('id', 'DESC')
  35. ->simplePaginate(15);
  36. return $this->success($list);
  37. }
  38. public function create(Request $request)
  39. {
  40. $formData = $request->only(['id', 'name', 'mobile', 'pay_type', 'num']);
  41. $formData = array_map(fn($v) => trim($v), $formData); //过滤,删除首尾空
  42. //表单验证
  43. $request->validate([
  44. 'id' => ['required', 'regex:/^\d+$/'],
  45. 'name' => ['required', 'between:2,20'],
  46. 'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'],
  47. 'pay_type' => ['required', 'in:0,1,2'],
  48. 'num' => ['required', 'min:1'],
  49. ], [
  50. 'id.required' => '未指定产品ID',
  51. 'name.required' => '请输入联系人姓名',
  52. 'mobile.required' => '请输入联系手机号',
  53. 'id.regex' => '产品ID错误',
  54. 'name.between' => '联系人姓名在2~20字符之间',
  55. 'mobile.regex' => '请输入11位手机号',
  56. 'pay_type.required' => '请选择支付方式',
  57. 'pay_type.in' => '不存在此支付方式',
  58. 'num.required' => '请输入购买数量',
  59. 'num.min' => '购买数量输入错误',
  60. ]);
  61. $agent_product = AgentProduct::query()
  62. ->where('id', $formData['id'])
  63. ->with('coupon')
  64. ->with('product')
  65. ->first();
  66. if (!$agent_product || !$agent_product->product) {
  67. $this->error('产品不存在');
  68. }
  69. DB::beginTransaction();
  70. try {
  71. $price = $this->calc($agent_product, $formData['num']);
  72. $title = $agent_product->product->title; //产品标题
  73. //增加销量
  74. $agent_product->increment('sale', $formData['num']); //代理商产品表销量
  75. $agent_product->product->increment('sale', $formData['num']); //供应商产品表
  76. // 存入订单表
  77. $order = Order::query()->create([
  78. 'user_id' => $this->user_id,
  79. 'order_no' => $this->getOrderNo(),
  80. 'num' => $formData['num'],
  81. 'price' => $price,
  82. 'name' => $formData['name'],
  83. 'mobile' => $formData['mobile'],
  84. 'title' => $title,
  85. 'agent_product_id' => $agent_product->id,
  86. 'product_id' => $agent_product->product_id,
  87. 'status' => 0,
  88. 'pay_type' => $formData['pay_type'],
  89. ]);
  90. //资金流水
  91. UserMoneyLog::query()->create([
  92. 'user_id' => $this->user_id,
  93. 'agent_id' => $this->agent_id,
  94. 'money' => -$price,
  95. 'order_id' => $order->id,
  96. 'type' => 1,
  97. 'desc' => '购买产品:' . $title,
  98. 'created_at' => time(), //已关闭模型自动写入时间
  99. ]);
  100. DB::commit();
  101. } catch (\Exception $e) {
  102. DB::rollBack();
  103. return $e->getMessage();
  104. }
  105. return $this->success();
  106. }
  107. //获取应付金额
  108. public function getPrice(Request $request)
  109. {
  110. $id = (int)$request->input('id');
  111. $num = (float)$request->input('num');
  112. if (!$num || $num < 1) {
  113. return $this->error('未指定产品数量');
  114. }
  115. $agent_product = AgentProduct::query()
  116. ->with(['coupon' => fn($query) => $query->select('*')])
  117. ->find($id);
  118. $final_price = $this->calc($agent_product, $num);
  119. return $this->success(['price' => $final_price]);
  120. }
  121. /**
  122. * 计算最终价格(扣除优惠券之后的价格)
  123. * @param $agent_product
  124. * @param $num
  125. * @return float
  126. */
  127. private function calc($agent_product, $num)
  128. {
  129. $total_price = $agent_product->price * $num;
  130. //没有任何优惠券时直接返回最终价
  131. if ($agent_product->coupon->isEmpty()) {
  132. return $total_price;
  133. }
  134. $coupon = $agent_product->coupon->toArray();
  135. foreach ($coupon as $v) {
  136. if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减
  137. if ($total_price >= $v['detail']['full']) {
  138. $total_price -= $v['detail']['reduction'];
  139. }
  140. } else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折
  141. $total_price *= $v['detail']['discount'];
  142. }
  143. }
  144. return round($total_price, 2);
  145. }
  146. // 生成订单号
  147. private function getOrderNo()
  148. {
  149. list($micro, $sec) = explode(' ', microtime());
  150. $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
  151. return date('YmdHis', $sec) . $micro . mt_rand(100, 999);
  152. }
  153. }