MemberLevelEvent.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\event;
  3. use app\admin\service\notify\RechargeService;
  4. use app\model\Member;
  5. use app\model\MemberAccount;
  6. use app\model\MemberRole;
  7. use app\model\PayDetail;
  8. use support\Db;
  9. use support\exception\BusinessException;
  10. class MemberLevelEvent
  11. {
  12. public function payOrderLevelUp($memberId)
  13. {
  14. Db::beginTransaction();
  15. try {
  16. $member = Member::where('member_id', $memberId)->select('member_id', 'join_member_role_id', 'member_mobile')->first();
  17. if ($member->member_mobile == '0000') {
  18. throw new BusinessException("散客不参与升级");
  19. }
  20. // $account = MemberAccount::where('join_account_member_id',$memberId)->where('member_account_classify','CASH')->first();
  21. // $income = $account->member_account_income;
  22. $recharge = PayDetail::where('join_pay_member_id', $memberId)
  23. ->where('pay_category', 'RECHARGE')
  24. ->where('pay_status', 'SUCCESS')
  25. ->where(function ($query) {
  26. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  27. ->orWhere('pay_prepayid', 'like', '%CASH%');
  28. })
  29. ->sum('pay_amount');
  30. $paySuccessDetail = PayDetail::where('join_pay_member_id', $memberId)
  31. ->where('pay_status', 'SUCCESS')
  32. ->whereIn('pay_category', ['GOODS', 'SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE', 'MEALS'])
  33. ->where(function ($query) {
  34. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY']);
  35. });
  36. $payAmount = $paySuccessDetail->sum('pay_amount');
  37. $paySuccessDetail = $paySuccessDetail->select('pay_id', 'join_pay_order_id', 'pay_amount')
  38. ->get()
  39. ->toArray();
  40. $orderIds = array_column($paySuccessDetail, 'join_pay_order_id');
  41. $refundAmount = PayDetail::whereIn('join_pay_order_id', $orderIds)
  42. ->where('pay_category', 'REFUND')
  43. ->where('pay_status', 'SUCCESS')
  44. ->sum('pay_amount');
  45. $amount = $payAmount - $refundAmount;
  46. $resultAmount = max($recharge, $amount);
  47. $memberRole = MemberRole::where('member_role_range_begin', '<=', $resultAmount)
  48. ->where('member_role_range_end', '>', $resultAmount)
  49. ->first();
  50. if (!empty($memberRole->member_role_id) && !empty($member->join_member_role_id) && $memberRole->member_role_id != $member->join_member_role_id) {
  51. $oldRole = MemberRole::where('member_role_id', $member->join_member_role_id)->first();
  52. if (!empty($oldRole->member_role_range_begin) && $oldRole->member_role_range_begin < $memberRole->member_role_range_begin) {
  53. $member->join_member_role_id = $memberRole->member_role_id;
  54. $member->save();
  55. RechargeService::disposeAdded($memberRole->member_role_id, $memberId);
  56. _syslog("会员升级", "会员升级成功", false, ['member' => $memberId], 1001);
  57. }
  58. } elseif (!empty($memberRole->member_role_id) && empty($member->join_member_role_id)) {
  59. $member->join_member_role_id = $memberRole->member_role_id;
  60. $member->save();
  61. RechargeService::disposeAdded($memberRole->member_role_id, $memberId);
  62. _syslog("会员升级", "会员升级成功", false, ['member' => $memberId], 1001);
  63. }
  64. Db::commit();
  65. }catch (BusinessException $e){
  66. Db::rollBack();
  67. _syslog("会员升级","会员升级失败,原因:".$e->getMessage(),false,['member'=>$memberId],1001);
  68. }catch (\Exception $e){
  69. Db::rollBack();
  70. _syslog("会员升级","会员升级失败,原因:".$e->getMessage(),false,['member'=>$memberId],1001);
  71. }
  72. }
  73. }