FranchiseeEvent.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 FranchiseeEvent
  14. {
  15. public function grant($params)
  16. {
  17. try {
  18. Log::info("进入加盟合伙人事件", $params);
  19. $config = SysConfig::where('config_key', 'app-identity-franchisee-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_franchisee_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_franchisee' => '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']) && !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' => 'FRANCHISEE',
  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' => 0,
  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 + $added;
  65. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $added;
  66. $memberAccount->save();
  67. // 给上级提成
  68. CommissionService::commission($configParams, $params['member_id'], $params['orderId'], $params['order_amount_pay'], '推荐加盟合伙人');
  69. //发放优惠券
  70. $params['gettype'] = 'FRANCHISEE';
  71. CouponService::autoSendCouponByGoods($params);
  72. } catch (BusinessException $e) {
  73. _syslog('加盟合伙人', '加盟合伙人处理失败:' . $e->getMessage());
  74. throw new BusinessException("加盟合伙人处理失败");
  75. } catch (\Exception $e) {
  76. Log::info('FRANCHISEE_DISPOSE_ERROR:' . $e->getMessage(), $params);
  77. _syslog('加盟合伙人', '加盟合伙人处理失败');
  78. throw new BusinessException("加盟合伙人处理失败");
  79. }
  80. }
  81. }