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.

149 lines
4.7 KiB

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