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.

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