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.

156 lines
4.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\JsonRpc;
  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\Exception\ErrorCodeException;
  8. use App\Model\v3\Order;
  9. use App\Model\v3\OrderMain;
  10. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  11. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\RpcServer\Annotation\RpcService;
  14. use Hyperf\Di\Annotation\Inject;
  15. use function AlibabaCloud\Client\json;
  16. /**
  17. * @RpcService(name="OrderService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="")
  18. */
  19. class OrderService implements OrderServiceInterface
  20. {
  21. /**
  22. * @Inject
  23. * @var Log
  24. */
  25. protected $log;
  26. /**
  27. * @Inject
  28. * @var OrderOnlineServiceInterface
  29. */
  30. protected $orderOnlineService;
  31. /**
  32. * @Inject
  33. * @var SeparateAccountsServiceInterface
  34. */
  35. protected $separateAccountsService;
  36. /**
  37. * 订单完成
  38. * @param $global_order_id
  39. * @param $user_id
  40. * @return array
  41. */
  42. public function onlineComplete($global_order_id, $user_id)
  43. {
  44. Db::beginTransaction();
  45. try {
  46. $this->orderOnlineService->doComplete($global_order_id, $user_id);
  47. $this->separateAccountsService->orderOnlineCompleted($global_order_id, $user_id);
  48. Db::commit();
  49. return [
  50. "status" => 200,
  51. "code" => 0,
  52. "result" => [],
  53. "message" => '处理成功'
  54. ];
  55. } catch (\Exception $e) {
  56. Db::rollBack();
  57. $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
  58. throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL);
  59. }
  60. }
  61. /**
  62. * 线上订单退款,整个订单退,这个是专门用于处理用户的申请退款的同意退款操作
  63. * @param $global_order_id
  64. * @param $user_id
  65. * @return array
  66. */
  67. public function onlineRefund($global_order_id, $user_id)
  68. {
  69. try {
  70. $this->orderOnlineService->doRefund($global_order_id, $user_id);
  71. return [
  72. "status" => 200,
  73. "code" => 0,
  74. "result" => [],
  75. "message" => '处理成功'
  76. ];
  77. } catch (\Exception $e) {
  78. return [
  79. "status" => 200,
  80. "code" => $e->getCode(),
  81. "result" => [],
  82. "message" => $e->getMessage()
  83. ];
  84. }
  85. }
  86. /**
  87. * 线上订单单笔退款,主要用于后台强行操作退单退款
  88. * 支持单商品、单店、整单
  89. * 按比例计算红包进行退款
  90. * 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券
  91. * 退2元商品时,退款金额为
  92. * 红包 (2/(98+2)*10 = 0.2
  93. * 退款:2-0.2=1.8
  94. * @param $user_id *用户ID
  95. * @param $global_order_id *全局总订单ID
  96. * @param $child_order_id *主订单ID,
  97. * @param $order_goods_id *订单商品ID
  98. * @param $note
  99. */
  100. public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id = null, $order_goods_id = null)
  101. {
  102. if (!$user_id || !$global_order_id || !$note) {
  103. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  104. 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
  105. 'params' => json([$global_order_id, $user_id, $note])
  106. ]);
  107. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  108. }
  109. // 主订单
  110. $orderMain = OrderMain::query()
  111. ->where(['global_order_id' => $global_order_id])
  112. ->whereIn('state', OrderState::CAN_REFUND_DIRECT)
  113. ->first();
  114. if (empty($orderMain)) {
  115. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  116. 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
  117. 'params' => json([$global_order_id, $user_id, $note])
  118. ]);
  119. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  120. }
  121. // 子订单
  122. if ($child_order_id) {
  123. $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->first();
  124. }
  125. // 单商品退
  126. if ($order_goods_id) {
  127. if (!$child_order_id) {
  128. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  129. 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对[单品]',
  130. 'params' => json([$global_order_id, $user_id, $note, $child_order_id])
  131. ]);
  132. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  133. }
  134. }
  135. }
  136. }