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.

146 lines
4.6 KiB

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::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. return $this->paymentService->do($globalOrderId, $money, $userId, config('wechat.notify_url.offline'));
  81. } catch (\Exception $e) {
  82. Db::rollBack();
  83. $this->log->event(LogLabel::ORDER_OFFLINE_LOG, ['exception_msg' => $e->getMessage()]);
  84. throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL);
  85. }
  86. }
  87. public function check()
  88. {
  89. // TODO: Implement check() method.
  90. }
  91. public function undo()
  92. {
  93. // TODO: Implement undo() method.
  94. }
  95. public function doByPaid($globalOrderId)
  96. {
  97. Db::beginTransaction();
  98. try {
  99. // 主订单状态更新
  100. $orderMain = OrderMain::query()
  101. ->where(['global_order_id' => $globalOrderId, 'type' => OrderType::OFFLINE])
  102. ->first();
  103. if (empty($orderMain)) {
  104. $this->log->event(
  105. LogLabel::ORDER_OFFLINE_PAID_LOG,
  106. ['order_not_found' => $orderMain->global_order_id]
  107. );
  108. Db::rollBack();
  109. return false;
  110. }
  111. $currentTime = time();
  112. $orderMain->state = OrderState::COMPLETED;
  113. $orderMain->pay_time = $currentTime;
  114. $orderMain->save();
  115. Db::commit();
  116. return true;
  117. } catch (\Exception $e) {
  118. $this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]);
  119. Db::rollBack();
  120. return false;
  121. }
  122. }
  123. }