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.

144 lines
4.4 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. $orderMainId = $orderMain->id;
  59. // 店铺今天的订单数
  60. $count = Order::query()
  61. ->join('lanzu_order_main as main', 'main.id', '=', 'lanzu_order.order_main_id')
  62. ->where(['lanzu_order.store_id' => $storeId, 'main.type' => OrderType::OFFLINE])
  63. ->whereBetween('lanzu_order.created_at', [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))])
  64. ->count();
  65. // 子订单数据
  66. $dataChildren = [
  67. 'order_main_id' => $orderMainId,
  68. 'user_id' => $userId,
  69. 'store_id' => $storeId,
  70. 'money' => $money,
  71. 'order_num' => date('YmdHis').mt_rand(1000, 9999),
  72. 'note' => '',
  73. 'oid' => $count + 1
  74. ];
  75. $orderChild = Order::query()->create($dataChildren);
  76. $orderChildId = $orderChild->id;
  77. Db::commit();
  78. // 支付
  79. return $this->paymentService->do($globalOrderId, $money, $userId, config('wechat.notify_url.offline'));
  80. } catch (\Exception $e) {
  81. Db::rollBack();
  82. $this->log->event(LogLabel::ORDER_OFFLINE_LOG, ['exception_msg' => $e->getMessage()]);
  83. throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL);
  84. }
  85. }
  86. public function check()
  87. {
  88. // TODO: Implement check() method.
  89. }
  90. public function undo()
  91. {
  92. // TODO: Implement undo() method.
  93. }
  94. public function doPaid($orderMainId)
  95. {
  96. Db::beginTransaction();
  97. try {
  98. // 主订单状态更新
  99. $orderMain = OrderMain::query()
  100. ->where(['id' => $orderMainId, 'type' => OrderType::OFFLINE])
  101. ->first();
  102. if (empty($orderMain)) {
  103. $this->log->event(
  104. LogLabel::ORDER_OFFLINE_PAID_LOG,
  105. ['order_not_found' => $orderMain->global_order_id]
  106. );
  107. Db::rollBack();
  108. return false;
  109. }
  110. $currentTime = time();
  111. $orderMain->state = OrderState::COMPLETED;
  112. $orderMain->pay_time = $currentTime;
  113. $orderMain->save();
  114. Db::commit();
  115. return true;
  116. } catch (\Exception $e) {
  117. $this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]);
  118. Db::rollBack();
  119. return false;
  120. }
  121. }
  122. }