PartnerEvent.php 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\event\order;
  3. use app\admin\service\coupon\CouponService;
  4. use app\admin\service\order\CommissionService;
  5. use app\model\Goods;
  6. use app\model\Member;
  7. use app\model\MemberAccount;
  8. use app\model\PayDetail;
  9. use app\model\SysConfig;
  10. use support\Db;
  11. use support\exception\BusinessException;
  12. use support\Log;
  13. class PartnerEvent
  14. {
  15. public function grant($params)
  16. {
  17. try {
  18. Log::info("进入会员合伙人事件", $params);
  19. $config = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  20. $configParams = json_decode($config->config_value_json, true);
  21. // 更新身份{"duedate": "2025-09-26", "datetime": "2024-09-27 14:40:51", "order_id": "OD2409271440L951"}
  22. $memberUpdateData = [
  23. 'member_partner_json' => json_encode([
  24. 'datetime' => date('Y-m-d H:i:s'),
  25. 'duedate' => date('Y-m-d', strtotime("+" . ($configParams['config']['valid']-1) . ' day')),
  26. 'order_id' => $params['orderId'],
  27. 'dept_id' => $params['dept_id'] ?? '',
  28. 'dept_name' => $params['dept_name'] ?? ''
  29. ]),
  30. 'member_is_partner' => 'Y',
  31. ];
  32. Member::where('member_id', $params['member_id'])->update($memberUpdateData);
  33. // 余额账户
  34. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'CASH')->first();
  35. // 增值到最后一条支付记录
  36. $added = 0;
  37. // $payDetail = PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])->orderBy('pay_addtimes', 'DESC')->first();
  38. if (!empty($configParams['account']['added']) && $configParams['account']['added'] > 0) {
  39. $added = round($params['order_amount_total'] * $configParams['account']['added'], 2);
  40. // $payDetail->pay_extend_json = json_encode(['added_rate' => $configParams['account']['added'], 'added_amount' => $added]);
  41. // $payDetail->save();
  42. $goods = Goods::where('goods_id',$params['join_sheet_goods_id'])->select('goods_id','goods_name')->first();
  43. // 增加一条支付记录
  44. PayDetail::insert([
  45. 'join_pay_member_id' => $params['member_id'],
  46. 'join_pay_order_id' => 'PARTNER',
  47. 'join_pay_object_json' => json_encode([
  48. 'recharge_order_id' => $params['orderId'],
  49. 'recharge_config' => [
  50. 'nbr' => round($params['order_amount_total'] * $configParams['account']['added'], 2)
  51. ],
  52. 'recharge_goods_id' => $params['join_sheet_goods_id'] ?? '',
  53. 'recharge_goods_name' => !empty($goods->goods_name) ? $goods->goods_name : ''
  54. ]),
  55. 'pay_status' => 'SUCCESS',
  56. 'pay_category' => 'RECHARGE',
  57. 'pay_amount'=>$params['order_amount_total'],
  58. 'pay_paytimes'=>date('Y-m-d H:i:s'),
  59. 'pay_prepayid' => $params['member_id'].'-CASH',
  60. 'pay_addtimes'=>time()
  61. ]);
  62. }
  63. // 入账
  64. $memberAccount->member_account_income = $memberAccount->member_account_income + $params['order_amount_total'];
  65. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $params['order_amount_total'];
  66. $memberAccount->member_account_added = $memberAccount->member_account_added + $added;
  67. $memberAccount->save();
  68. // 给上级提成
  69. CommissionService::commission($configParams, $params['member_id'], $params['orderId'], $params['order_amount_pay'], '推荐会员合伙人');
  70. //发放优惠券
  71. $params['gettype'] = 'PARTNER';
  72. CouponService::autoSendCouponByGoods($params);
  73. } catch (BusinessException $e) {
  74. _syslog('会员合伙人', '会员合伙人处理失败:' . $e->getMessage());
  75. throw new BusinessException("会员合伙人处理失败");
  76. } catch (\Exception $e) {
  77. Log::info('PARTNER_DISPOSE_ERROR:' . $e->getMessage(), $params);
  78. _syslog('会员合伙人', '会员合伙人处理失败');
  79. throw new BusinessException("会员合伙人处理失败");
  80. }
  81. }
  82. }