MemberLevelEvent.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  57. } elseif (!empty($memberRole->member_role_id) && empty($member->join_member_role_id)) {
  58. $member->join_member_role_id = $memberRole->member_role_id;
  59. $member->save();
  60. RechargeService::disposeAdded($memberRole->member_role_id, $memberId);
  61. }
  62. Db::commit();
  63. _syslog("会员升级", "会员升级成功", false, ['member' => $memberId], 1001);
  64. }catch (BusinessException $e){
  65. Db::rollBack();
  66. _syslog("会员升级","会员升级失败,原因:".$e->getMessage(),false,['member'=>$memberId],1001);
  67. }catch (\Exception $e){
  68. Db::rollBack();
  69. _syslog("会员升级","会员升级失败,原因:".$e->getMessage(),false,['member'=>$memberId],1001);
  70. }
  71. }
  72. }