MemberLevelEvent.php 4.0 KB

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