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.

136 lines
4.1 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Constants\v3\LogLabel;
  5. use App\Constants\v3\OrderState;
  6. use App\Constants\v3\OrderType;
  7. use App\Exception\ErrorCodeException;
  8. use App\Model\v3\Order;
  9. use App\Model\v3\OrderMain;
  10. use App\Model\v3\Store;
  11. use App\Service\v3\Interfaces\OrderOfflineServiceInterface;
  12. use App\Service\v3\Interfaces\PaymentServiceInterface;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\Snowflake\IdGeneratorInterface;
  15. use Hyperf\Utils\ApplicationContext;
  16. use Hyperf\Di\Annotation\Inject;
  17. class OrderOfflineService implements OrderOfflineServiceInterface
  18. {
  19. /**
  20. * @Inject
  21. * @var PaymentServiceInterface
  22. */
  23. protected $paymentService;
  24. public function do($storeId, $userId, $money, $plat ='')
  25. {
  26. try {
  27. $store = Store::find($storeId);
  28. // 获取分布式全局ID
  29. $generator = ApplicationContext::getContainer()->get(IdGeneratorInterface::class);
  30. $globalOrderId = $generator->generate();
  31. $dataMain = [
  32. 'market_id' => $store->market_id,
  33. 'order_num' => $globalOrderId,
  34. 'global_order_id' => $globalOrderId,
  35. 'user_id' => $userId,
  36. 'type' => OrderType::OFFLINE,
  37. 'money' => $money,
  38. 'total_money' => $money,
  39. 'services_money' => 0,
  40. 'coupon_money' => 0,
  41. 'delivery_money' => 0,
  42. 'state' => OrderState::UNPAID,
  43. 'tel' => '',
  44. 'address' => '',
  45. 'lat' => '',
  46. 'lng' => '',
  47. 'name' => '',
  48. 'plat' => $plat,
  49. 'delivery_time_note' => ''
  50. ];
  51. $orderMain = OrderMain::query()->create($dataMain);
  52. $orderMainId = $orderMain->id;
  53. // 店铺今天的订单数
  54. $count = Order::query()
  55. ->where(['store_id' => $storeId, 'type' => OrderType::OFFLINE])
  56. ->whereBetween('created_at', [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))])
  57. ->count();
  58. // 子订单数据
  59. $dataChildren = [
  60. 'order_main_id' => $orderMainId,
  61. 'user_id' => $userId,
  62. 'store_id' => $storeId,
  63. 'money' => $money,
  64. 'order_num' => date('YmdHis').mt_rand(1000, 9999),
  65. 'note' => '',
  66. 'oid' => $count + 1
  67. ];
  68. $orderChild = Order::query()->create($dataChildren);
  69. $orderChildId = $orderChild->id;
  70. Db::commit();
  71. // 支付
  72. return $this->paymentService->do($globalOrderId, $money, $userId, config('wechat.notify_url.offline'));
  73. } catch (\Exception $e) {
  74. Db::rollBack();
  75. $this->log->event(LogLabel::ORDER_OFFLINE_LOG, ['exception_msg' => $e->getMessage()]);
  76. throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL);
  77. }
  78. }
  79. public function check()
  80. {
  81. // TODO: Implement check() method.
  82. }
  83. public function undo()
  84. {
  85. // TODO: Implement undo() method.
  86. }
  87. public function doPaid($orderMainId)
  88. {
  89. Db::beginTransaction();
  90. try {
  91. // 主订单状态更新
  92. $orderMain = OrderMain::query()
  93. ->where(['id' => $orderMainId, 'type' => OrderType::OFFLINE])
  94. ->first();
  95. if (empty($orderMain)) {
  96. $this->log->event(
  97. LogLabel::ORDER_OFFLINE_PAID_LOG,
  98. ['order_not_found' => $orderMain->global_order_id]
  99. );
  100. Db::rollBack();
  101. return false;
  102. }
  103. $currentTime = time();
  104. $orderMain->state = OrderState::COMPLETED;
  105. $orderMain->pay_time = $currentTime;
  106. $orderMain->save();
  107. Db::commit();
  108. return true;
  109. } catch (\Exception $e) {
  110. $this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]);
  111. Db::rollBack();
  112. return false;
  113. }
  114. }
  115. }