MemberLevelEvent.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class MemberLevelEvent
  10. {
  11. public function payOrderLevelUp($memberId)
  12. {
  13. Db::beginTransaction();
  14. try{
  15. $member = Member::where('member_id',$memberId)->select('member_id','join_member_role_id')->first();
  16. $account = MemberAccount::where('join_account_member_id',$memberId)->where('member_account_classify','CASH')->first();
  17. $income = $account->member_account_income;
  18. $paySuccessDetail = PayDetail::where('join_pay_member_id',$memberId)
  19. ->where('pay_status','SUCCESS')
  20. ->where('pay_category','<>','RECHARGE')
  21. ->where(function ($query){
  22. $query->where('pay_prepayid','WXPAY')
  23. ->orWhere('pay_prepayid','ALIPAY')
  24. ->orWhere('pay_prepayid','like','%CASH%');
  25. });
  26. $payAmount = $paySuccessDetail->sum('pay_amount');
  27. $paySuccessDetail = $paySuccessDetail->select('pay_id','join_pay_order_id','pay_amount')
  28. ->get()
  29. ->toArray();
  30. $orderIds = array_column($paySuccessDetail,'join_pay_order_id');
  31. $refundAmount = PayDetail::whereIn('join_pay_order_id',$orderIds)
  32. ->where('pay_category','REFUND')
  33. ->where('pay_status','SUCCESS')
  34. ->sum('pay_amount');
  35. $amount = $payAmount - $refundAmount;
  36. $resultAmount = max($income,$amount);
  37. $memberRole = MemberRole::where('member_role_range_begin' ,'<=',$resultAmount)
  38. ->where('member_role_range_end','>',$resultAmount)
  39. ->first();
  40. if(!empty($memberRole->member_role_id) && !empty($member->join_member_role_id) && $memberRole->member_role_id != $member->join_member_role_id){
  41. $oldRole = MemberRole::where('member_role_id',$member->join_member_role_i)->first();
  42. if (!empty($oldRole->member_role_range_begin) && $oldRole->member_role_range_begin < $memberRole->member_role_range_begin){
  43. $member->join_member_role_id = $memberRole->member_role_id;
  44. $member->save();
  45. RechargeService::disposeAdded($memberRole->member_role_id,$memberId);
  46. _syslog("会员升级","会员升级成功",false,['member'=>$memberId],1001);
  47. Db::commit();
  48. }
  49. }elseif (!empty($memberRole->member_role_id) && empty($member->join_member_role_id)){
  50. $member->join_member_role_id = $memberRole->member_role_id;
  51. $member->save();
  52. RechargeService::disposeAdded($memberRole->member_role_id,$memberId);
  53. _syslog("会员升级","会员升级成功",false,['member'=>$memberId],1001);
  54. Db::commit();
  55. }
  56. }catch (\Exception $e){
  57. Db::rollBack();
  58. _syslog("会员升级","会员升级失败,原因:".$e->getMessage(),false,['member'=>$memberId],1001);
  59. }
  60. }
  61. }