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.

159 lines
5.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. Db::beginTransaction();
  70. try {
  71. $this->orderOnlineService->doRefund($global_order_id, $user_id);
  72. Db::commit();
  73. return [
  74. "status" => 200,
  75. "code" => 0,
  76. "result" => [],
  77. "message" => '处理成功'
  78. ];
  79. } catch (\Exception $e) {
  80. Db::rollBack();
  81. $this->log->event(LogLabel::ORDER_REFUND_LOG, ['jsonrpc_order_service_exception_onlineRefund' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
  82. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  83. }
  84. }
  85. /**
  86. * 线上订单单笔退款,主要用于后台强行操作退单退款
  87. * 支持单商品、单店、整单
  88. * 按比例计算红包进行退款
  89. * 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券
  90. * 退2元商品时,退款金额为
  91. * 红包 (2/(98+2)*10 = 0.2
  92. * 退款:2-0.2=1.8
  93. * @param $user_id *用户ID
  94. * @param $global_order_id *全局总订单ID
  95. * @param $child_order_id *主订单ID,
  96. * @param $order_goods_id *订单商品ID
  97. * @param $note
  98. */
  99. public function onlineSingleRefund($user_id, $note, $global_order_id, $child_order_id=null, $order_goods_id=null)
  100. {
  101. if (!$user_id || !$global_order_id || !$note) {
  102. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  103. 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
  104. 'params' => json([$global_order_id, $user_id, $note])
  105. ]);
  106. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  107. }
  108. // 主订单
  109. $orderMain = OrderMain::query()
  110. ->where(['global_order_id' => $global_order_id])
  111. ->whereIn('state', OrderState::CAN_REFUND_DIRECT)
  112. ->first();
  113. if (empty($orderMain)) {
  114. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  115. 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
  116. 'params' => json([$global_order_id, $user_id, $note])
  117. ]);
  118. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  119. }
  120. // 子订单
  121. if ($child_order_id) {
  122. $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->first();
  123. }
  124. // 单商品退
  125. if ($order_goods_id) {
  126. if (!$child_order_id) {
  127. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  128. 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对[单品]',
  129. 'params' => json([$global_order_id, $user_id, $note, $child_order_id])
  130. ]);
  131. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  132. }
  133. }
  134. }
  135. }