CommissionService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\service\order;
  3. use app\admin\service\member\MemberService;
  4. use app\model\Member;
  5. use app\model\MemberAccount;
  6. use app\model\MemberAccountList;
  7. use app\model\SysConfig;
  8. use support\Log;
  9. class CommissionService
  10. {
  11. /*
  12. {
  13. "config": {
  14. "name": "名称",
  15. "valid": 365, //有效天
  16. "protocol": 229, //服务协议分类ID
  17. "notice":["", ""] //注意事项数组
  18. },
  19. "goods": {
  20. "goods_id": "", //产品ID
  21. "goods_sku_id": 0, //产品SKUID
  22. "amount": "goods_sales_price" //核算金额字段
  23. },
  24. "commission": {
  25. "direct": 0.2, //直属提成
  26. "indirect": 0.1, //间属提成
  27. "relate-consum": 0.1, //消费关联提成 带客
  28. "invite-non-identity": 0.05, //邀请人非身份会员达成提成
  29. "agent-senior-join": 0.05, //中介加盟合伙人提成
  30. "agent-senior-strategy": 0.05, //中介战略合伙人提成
  31. "account":"CASH" //提成写入账户
  32. },
  33. "account": {
  34. "account":"CASH", //达成金额进入账户 不则为空
  35. "added":0.12 //达成金额增值 不则为0
  36. },
  37. "control": {
  38. "notify": "procMemberPartnerExec", //回调函数
  39. "if_installment": "N", //是否支持分期
  40. "if_commission_out": "Y" //提成是否可提现
  41. }
  42. }
  43. */
  44. /**
  45. * @Desc 处理提成
  46. * @Author Gorden
  47. * @Date 2024/9/28 16:09
  48. *
  49. * @param $config
  50. * @param $memberId
  51. * @param $orderId
  52. * @param $amount
  53. * @param $accountListCategory
  54. * @return void
  55. */
  56. public static function commission($config, $memberId, $orderId, $amount, $accountListCategory)
  57. {
  58. Log::info("开始处理提成", ['member_id' => $memberId, 'amount' => $amount]);
  59. try {
  60. $member = Member::where('member_id', $memberId)->first();
  61. $configParams1 = [];
  62. $configParams2 = [];
  63. if (!empty($member->join_invite_member_id)) {
  64. $member1 = Member::where('member_id', $member->join_invite_member_id)->first();
  65. if ($member1->member_is_partner == 'Y') {
  66. $config1 = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  67. } else if ($member1->member_is_referrer == 'Y') {
  68. $config1 = SysConfig::where('config_key', 'app-identity-referrer-config')->first();
  69. }
  70. if (!empty($config1)){
  71. $configParams1 = json_decode($config1->config_value_json, true);
  72. Log::info("一级提成参数", ['config' => $configParams1]);
  73. }
  74. if (!empty($member1->join_invite_member_id)) {
  75. $member2 = Member::where('member_id', $member1->join_invite_member_id)->first();
  76. if ($member2->member_is_partner == 'Y') {
  77. $config2 = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  78. } else if ($member1->member_is_referrer == 'Y') {
  79. $config2 = SysConfig::where('config_key', 'app-identity-referrer-config')->first();
  80. }
  81. if (!empty($config2)){
  82. $configParams2 = json_decode($config2->config_value_json, true);
  83. Log::info("二级提成参数", ['config' => $configParams2]);
  84. }
  85. }
  86. }
  87. if (empty($configParams1) && empty($configParams2)) {
  88. Log::info("提成失败,提成参数异常", ['member_id' => $memberId, 'amount' => $amount]);
  89. }
  90. // 提成记录JSON
  91. $accountListJson = [
  92. 'line' => 'relate-consum',
  93. 'amount' => $amount,
  94. 'order_id' => $orderId,
  95. 'master_member_id' => $memberId
  96. ];
  97. // 直属提成
  98. if (!empty($member1) && ($member1->member_is_partner == 'Y' || $member1->member_is_referrer == 'Y') && !empty($configParams1)) {
  99. $accountListJson['identity'] = MemberService::getIdentity($member1->member_is_partner, $member1->member_is_referrer);
  100. $accountListJson['line'] = 'direct';
  101. $member1CommissionAmount = $configParams1['commission']['direct'] * $amount;
  102. self::commissionToMember($member1->member_id, $accountListJson, $member1CommissionAmount, $accountListCategory);
  103. Log::info("直属提成参数", ['member_id' => $member1->member_id, 'account_list_json' => $accountListJson, 'amount' => $member1CommissionAmount, 'category' => $accountListCategory]);
  104. }
  105. // 间属提成
  106. if (!empty($member2) && ($member2->member_is_partner == 'Y' || $member2->member_is_referrer == 'Y') && !empty($configParams2)) {
  107. $accountListJson['identity'] = MemberService::getIdentity($member2->member_is_partner, $member2->member_is_referrer);
  108. $accountListJson['line'] = 'indirect';
  109. $member2CommissionAmount = $configParams2['commission']['indirect'] * $amount;
  110. self::commissionToMember($member2->member_id, $accountListJson, $member2CommissionAmount, $accountListCategory);
  111. Log::info("间属提成参数", ['member_id' => $member2->member_id, 'account_list_json' => $accountListJson, 'amount' => $member2CommissionAmount, 'category' => $accountListCategory]);
  112. }
  113. } catch (\Exception $e) {
  114. dump('提成处理失败:'.$e->getMessage());
  115. _syslog("提成处理失败", '提成处理失败');
  116. }
  117. }
  118. /**
  119. * @Desc 提成到账户
  120. * @Author Gorden
  121. * @Date 2024/9/25 15:14
  122. *
  123. * @param $memberId
  124. * @param $accountListJson
  125. * @param $money
  126. * @return void
  127. */
  128. private static function commissionToMember($memberId, $accountListJson, $money, $accountListCategory)
  129. {
  130. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  131. $account->member_account_income = $account->member_account_income + $money;
  132. $account->member_account_surplus = $account->member_account_surplus + $money;
  133. $account->save();
  134. MemberAccountList::insert([
  135. 'join_list_member_account_nbr' => $memberId . '-CASH',
  136. 'join_member_account_list_member_id' => $memberId,
  137. 'member_account_list_status' => 'ACTIVED',
  138. 'member_account_list_attr' => 'IN',
  139. 'member_account_list_classify' => $accountListJson['identity'],
  140. 'member_account_list_category' => $accountListCategory,
  141. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  142. 'member_account_list_amount' => $money,
  143. 'member_account_list_json' => json_encode($accountListJson),
  144. 'member_account_list_addtimes' => time()
  145. ]);
  146. }
  147. }