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.

103 lines
3.4 KiB

  1. <?php
  2. namespace App\Service;
  3. use App\Commons\Log;
  4. use App\Constants\LogLabel;
  5. use App\Model\OrderMain;
  6. use App\Model\UserRelationBind;
  7. use Hyperf\DbConnection\Db;
  8. use Hyperf\Di\Annotation\Inject;
  9. class SeparateAccountsService implements SeparateAccountsServiceInterface
  10. {
  11. /**
  12. * @Inject
  13. * @var Log
  14. */
  15. protected $log;
  16. /**
  17. * @Inject
  18. * @var UserServiceInterface
  19. */
  20. protected $userService;
  21. /**
  22. * @Inject
  23. * @var FinancialRecordServiceInterface
  24. */
  25. protected $financialRecordService;
  26. /**
  27. * @inheritDoc
  28. */
  29. public function orderOnlinePaid($global_order_id)
  30. {
  31. // TODO: Implement orderOnlinePaid() method.
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. public function orderOnlineCompleted($global_order_id)
  37. {
  38. // 线上订单完成(用户点击确认收货完成/管理后台点击完成/配送员点击完成/自动收货等),进行相关分账
  39. // 订单
  40. $orderMain = OrderMain::query()
  41. ->where(['global_order_id' => $global_order_id])
  42. ->whereIn('state', [OrderMain::ORDER_STATE_COMPLETE,OrderMain::ORDER_STATE_EVALUATED,OrderMain::ORDER_STATE_UNREFUND])
  43. ->first();
  44. if (empty($orderMain)) {
  45. return;
  46. }
  47. Db::beginTransaction();
  48. try {
  49. // =======社区服务点分账 / Start=======
  50. // 前提:用户线上下单并且订单完成
  51. // 奖励规则A:用户是平台新用户,奖励社区服务点平台新用户奖励x元+平台新用户首单奖励y元+订单商品金额z%的分成
  52. // 奖励规则B:用户是非新用户,奖励社区服务点订单实际支付金额z%的分成
  53. // =======社区服务点分账 / Start=======
  54. // 奖励/分账金额 TODO 届时查询出来一个配置
  55. $award = ['new_user' => 1, 'first_order' => 2, 'separate_rate' => 2];
  56. // 当前用户的社区服务点绑定关系
  57. $communityBind = UserRelationBind::query()
  58. ->where(['bind_type' => UserRelationBind::BIND_TYPE_COMMUNITY, 'user_id' => $orderMain->user_id])
  59. ->first();
  60. // 平台新用户
  61. if ($this->userService->isPlatformNewUser($orderMain->user_id, $orderMain->id)) {
  62. $this->financialRecordService->communityAwardByPlatNewUser($communityBind->source_id, $orderMain->global_order_id, $award['new_user']);
  63. $this->financialRecordService->communityAwardByPlatNewUserFirstOLOrder($communityBind->source_id, $orderMain->global_order_id, $award['first_order']);
  64. }
  65. // 账单分成
  66. $money = bcmul($orderMain->money, bcdiv($award['separate_rate'], 100, 6), 2);
  67. $this->financialRecordService->communitySeparateAccountsByOrderComp($communityBind->source_id, $orderMain->global_order_id, $money);
  68. // =======社区服务点分账 / End=======
  69. Db::commit();
  70. return true;
  71. } catch (\Exception $e) {
  72. $this->log->event(LogLabel::SEPARATE_ACCOUNTS_LOG, ['exception' => $e->getMessage(), 'order_main' => json_encode($orderMain)]);
  73. Db::rollBack();
  74. return false;
  75. }
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function orderOfflinePaid($global_order_id)
  81. {
  82. // TODO: Implement orderOfflinePaid() method.
  83. }
  84. }