select('member_id','join_member_role_id')->first(); $account = MemberAccount::where('join_account_member_id',$memberId)->where('member_account_classify','CASH')->first(); $income = $account->member_account_income; $paySuccessDetail = PayDetail::where('join_pay_member_id',$memberId) ->where('pay_status','SUCCESS') ->where('pay_category','<>','RECHARGE') ->where(function ($query){ $query->where('pay_prepayid','WXPAY') ->orWhere('pay_prepayid','ALIPAY') ->orWhere('pay_prepayid','like','%CASH%'); }); $payAmount = $paySuccessDetail->sum('pay_amount'); $paySuccessDetail = $paySuccessDetail->select('pay_id','join_pay_order_id','pay_amount') ->get() ->toArray(); $orderIds = array_column($paySuccessDetail,'join_pay_order_id'); $refundAmount = PayDetail::whereIn('join_pay_order_id',$orderIds) ->where('pay_category','REFUND') ->where('pay_status','SUCCESS') ->sum('pay_amount'); $amount = $payAmount - $refundAmount; $resultAmount = max($income,$amount); $memberRole = MemberRole::where('member_role_range_begin' ,'<=',$resultAmount) ->where('member_role_range_end','>',$resultAmount) ->first(); if(!empty($memberRole->member_role_id) && !empty($member->join_member_role_id) && $memberRole->member_role_id != $member->join_member_role_id){ $oldRole = MemberRole::where('member_role_id',$member->join_member_role_i)->first(); if (!empty($oldRole->member_role_range_begin) && $oldRole->member_role_range_begin < $memberRole->member_role_range_begin){ $member->join_member_role_id = $memberRole->member_role_id; $member->save(); RechargeService::disposeAdded($memberRole->member_role_id,$memberId); _syslog("会员升级","会员升级成功",false,['member'=>$memberId],1001); Db::commit(); } }elseif (!empty($memberRole->member_role_id) && empty($member->join_member_role_id)){ $member->join_member_role_id = $memberRole->member_role_id; $member->save(); RechargeService::disposeAdded($memberRole->member_role_id,$memberId); _syslog("会员升级","会员升级成功",false,['member'=>$memberId],1001); Db::commit(); } }catch (\Exception $e){ Db::rollBack(); _syslog("会员升级","会员升级失败,原因:".$e->getMessage(),false,['member'=>$memberId],1001); } } }