GoodsOrderService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Services\OrderService;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Str;
  5. class GoodsOrderService extends OrderBase
  6. {
  7. public function __construct($userId)
  8. {
  9. parent::__construct($userId);
  10. }
  11. /**
  12. * 创建订单
  13. */
  14. public function create($type, $goodsInfo = "", $order_no = "")
  15. {
  16. // year_price quarter_price month_price
  17. if ($type == 1) {
  18. $totalPrice = $goodsInfo->price1; //所有的商品总金额
  19. } elseif ($type == 2) {
  20. $totalPrice = $goodsInfo->price2; //所有的商品总金额
  21. } else if ($type == 3) {
  22. $totalPrice = $goodsInfo->price3; //所有的商品总金额
  23. } else if ($type == 4) {
  24. $totalPrice = $goodsInfo->price4; //所有的商品总金额
  25. } else if ($type == 5) {
  26. $totalPrice = $goodsInfo->price5; //所有的商品总金额
  27. } else {
  28. $totalPrice = $goodsInfo->price4;
  29. }
  30. $order_amount_total = $totalPrice; //实际付款金额
  31. //
  32. $preferential_type = 0; // 1 会员卡
  33. $totalPreferentialPrice = 0;
  34. $order_status = 1; //1 未支付 2 已支付
  35. // GO 前缀是支付成功后,回调判断是哪个订单类型
  36. $order_no = $order_no ?: createNewOrderNo();
  37. if (!str_contains("GO", $order_no)) {
  38. $order_no = 'GO' . $order_no;
  39. }
  40. $info = [
  41. 'user_id' => $this->userId,
  42. 'order_no' => $order_no,
  43. 'order_status' => $order_status, //1 未支付
  44. 'product_amount_total' => $totalPrice, //所有购买商品总价
  45. 'order_amount_total' => $order_amount_total, //实际付款金额
  46. 'pay_amount_total' => 0,
  47. 'preferential_price' => $totalPreferentialPrice, //订单优惠金额(优惠券等)
  48. 'preferential_type' => $preferential_type, // //优惠类型 1会员卡
  49. 'pay_channel' => 0,
  50. 'product_id' => $goodsInfo->id,
  51. 'product_sku' => $type, // 产品类型
  52. 'mid' => Str::random(12),
  53. 'created_at' => time(),
  54. 'updated_at' => time(),
  55. ];
  56. $isSuccess = DB::table('order_product')->insertGetId($info);
  57. if ($isSuccess) {
  58. $info['id'] = $isSuccess;
  59. return $info;
  60. } else {
  61. return [];
  62. }
  63. }
  64. /**
  65. * @param string $orderNo
  66. * @param array $where
  67. * @return array
  68. */
  69. public function getOrderInfo(string $orderNo, array $where = [], array $selectArr = []): array
  70. {
  71. if (!str_contains("GO", $orderNo)) {
  72. $orderNo = 'GO' . $orderNo;
  73. }
  74. $find = DB::table('order_product')->where('order_no', $orderNo);
  75. if ($where) {
  76. $find->where($where);
  77. }
  78. $info = $find->first();
  79. if ($info) {
  80. return get_object_vars($info);
  81. } else {
  82. return [];
  83. }
  84. }
  85. }