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.

143 lines
4.3 KiB

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. $store = Store::find($storeId);
  34. // 获取分布式全局ID
  35. $generator = ApplicationContext::getContainer()->get(IdGeneratorInterface::class);
  36. $globalOrderId = $generator->generate();
  37. $dataMain = [
  38. 'market_id' => $store->market_id,
  39. 'order_num' => $globalOrderId,
  40. 'global_order_id' => $globalOrderId,
  41. 'user_id' => $userId,
  42. 'type' => OrderType::OFFLINE,
  43. 'money' => $money,
  44. 'total_money' => $money,
  45. 'services_money' => 0,
  46. 'coupon_money' => 0,
  47. 'delivery_money' => 0,
  48. 'state' => OrderState::UNPAID,
  49. 'tel' => '',
  50. 'address' => '',
  51. 'lat' => '',
  52. 'lng' => '',
  53. 'name' => '',
  54. 'plat' => $plat,
  55. 'delivery_time_note' => ''
  56. ];
  57. $orderMain = OrderMain::query()->create($dataMain);
  58. // 店铺今天的订单数
  59. $count = Order::query()
  60. ->join('lanzu_order_main as main', 'main.id', '=', 'lanzu_order.order_main_id')
  61. ->where(['lanzu_order.store_id' => $storeId, 'main.type' => OrderType::OFFLINE])
  62. ->whereBetween('lanzu_order.created_at', [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))])
  63. ->count();
  64. // 子订单数据
  65. $dataChildren = [
  66. 'order_main_id' => $orderMain->global_order_id,
  67. 'user_id' => $userId,
  68. 'store_id' => $storeId,
  69. 'money' => $money,
  70. 'order_num' => date('YmdHis').mt_rand(1000, 9999),
  71. 'note' => '',
  72. 'oid' => $count + 1
  73. ];
  74. $orderChild = Order::query()->create($dataChildren);
  75. $orderChildId = $orderChild->id;
  76. Db::commit();
  77. // 支付
  78. return $this->paymentService->do($globalOrderId, $money, $userId, config('wechat.notify_url.offline'));
  79. } catch (\Exception $e) {
  80. Db::rollBack();
  81. $this->log->event(LogLabel::ORDER_OFFLINE_LOG, ['exception_msg' => $e->getMessage()]);
  82. throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL);
  83. }
  84. }
  85. public function check()
  86. {
  87. // TODO: Implement check() method.
  88. }
  89. public function undo()
  90. {
  91. // TODO: Implement undo() method.
  92. }
  93. public function doPaid($globalOrderId)
  94. {
  95. Db::beginTransaction();
  96. try {
  97. // 主订单状态更新
  98. $orderMain = OrderMain::query()
  99. ->where(['global_order_id' => $globalOrderId, 'type' => OrderType::OFFLINE])
  100. ->first();
  101. if (empty($orderMain)) {
  102. $this->log->event(
  103. LogLabel::ORDER_OFFLINE_PAID_LOG,
  104. ['order_not_found' => $orderMain->global_order_id]
  105. );
  106. Db::rollBack();
  107. return false;
  108. }
  109. $currentTime = time();
  110. $orderMain->state = OrderState::COMPLETED;
  111. $orderMain->pay_time = $currentTime;
  112. $orderMain->save();
  113. Db::commit();
  114. return true;
  115. } catch (\Exception $e) {
  116. $this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]);
  117. Db::rollBack();
  118. return false;
  119. }
  120. }
  121. }