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.

129 lines
3.8 KiB

5 years ago
  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::create($dataMain);
  52. $orderMainId = $orderMain->id;
  53. // 子订单数据
  54. $dataChildren = [
  55. 'order_main_id' => $orderMainId,
  56. 'user_id' => $userId,
  57. 'store_id' => $storeId,
  58. 'money' => $money,
  59. 'order_num' => date('YmdHis').mt_rand(1000, 9999),
  60. 'note' => ''
  61. ];
  62. $orderChild = Order::create($dataChildren);
  63. $orderChildId = $orderChild->id;
  64. Db::commit();
  65. // 支付
  66. return $this->paymentService->do($globalOrderId, $money, $userId, config('site_host') . '/v3/wechat/notify/offline');
  67. } catch (\Exception $e) {
  68. Db::rollBack();
  69. $this->log->event(LogLabel::ORDER_ONLINE_LOG, ['exception_msg' => $e->getMessage()]);
  70. throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL);
  71. }
  72. }
  73. public function check()
  74. {
  75. // TODO: Implement check() method.
  76. }
  77. public function undo()
  78. {
  79. // TODO: Implement undo() method.
  80. }
  81. public function doPaid($orderMainId)
  82. {
  83. Db::beginTransaction();
  84. try {
  85. // 主订单状态更新
  86. $orderMain = OrderMain::query()
  87. ->where(['id' => $orderMainId, 'type' => OrderType::OFFLINE])
  88. ->first();
  89. if (empty($orderMain)) {
  90. $this->log->event(
  91. LogLabel::ORDER_OFFLINE_PAID_LOG,
  92. ['order_not_found' => $orderMain->global_order_id]
  93. );
  94. Db::rollBack();
  95. return false;
  96. }
  97. $currentTime = time();
  98. $orderMain->state = OrderState::COMPLETED;
  99. $orderMain->pay_time = $currentTime;
  100. $orderMain->save();
  101. Db::commit();
  102. return true;
  103. } catch (\Exception $e) {
  104. $this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]);
  105. Db::rollBack();
  106. return false;
  107. }
  108. }
  109. }