123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace Modules\Order\Listeners;
- use Modules\Order\Events\ChangeUserLevel;
- use App\Models\WarZone;
- use App\Models\Warea;
- use App\Models\User;
- use App\Models\UserZ;
- use App\Models\Service;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Contracts\Queue\ShouldQueue;
- class ChangeUserLevelListener
- {
- /**
- * Create the event listener.
- *
- * @return void
- */
- public function __construct()
- {
- //
- }
- /**
- * Handle the event.
- *
- * @param ChangeUserLevel $event
- * @return void
- */
- public function handle(ChangeUserLevel $event)
- {
- $user=$event->user;
- if($user->level==1){
- $level='销售主管';
- }elseif($user->level==2){
- $level='销售经理';
- }elseif($user->level==3){
- $level='代理公司';
- }
- if($user->before > $user->level){//降级
- if($user->level==2){//代理公司降到销售经理
- Log::info($user->warea_id);
- $warea=Warea::where('id',$user->warea_id)->first();
- Log::info($warea->name);
- $war_name= explode('战区',$warea->name)[0];
- $up=User::where('id',$user->agent_id)->first();
- UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['war_names'=>$war_name,'level_name'=>$level,'up_name'=>$up->nickname,'up_phone'=>$up->mobile]);
- UserZ::where('up_phone',$user->mobile)->where('level_name','!=','政委')->update(['war_names'=>$war_name,'up_name'=>$up->nickname,'up_phone'=>$up->mobile]);
- Service::where('phone',$user->mobile)->delete();
- }elseif($user->level==1){
- UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['level_name'=>$level]);
- }
- }elseif($user->before < $user->level){//升级
- if($user->level==3){//销售经理升级到代理公司
- $warea=Warea::where('id',$user->warea_id)->first();
- $war_name= explode('战区',$warea->name)[0];
- $up=UserZ::where('war_names',$war_name)->where('level_name','政委')->first();
- $userinfo=UserZ::where('phone',$user->mobile)->first();
- if($userinfo){
- UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['level_name'=>$level,'up_name'=>$up->nickname,'up_phone'=>$up->phone]);
- }else{
- UserZ::create([
- 'phone'=>$user->mobile,
- 'openid'=>$user->mobile,
- 'nickname'=>$user->nickname,
- 'name'=>$user->realname??$user->nickname,
- 'up_phone'=>$up->phone,
- 'up_name'=>$up->nickname,
- 'level_name'=>$level,
- 'war_names'=>$war_name,
- 'status'=>0,
- ]);
- }
- $u_mobile=User::where('agent_id',$user->id)->pluck('mobile');
- if(count($u_mobile)>0){
- $u_id=User::where('agent_id',$user->id)->pluck('id');
- UserZ::whereIn('phone',$u_mobile)->where('level_name','!=','政委')->update(['up_name'=>$user->nickname,'up_phone'=>$user->mobile]);
- foreach($u_id as $k=>$v){
- $uu_mobile=User::where('agent_id',$v)->pluck('mobile');
- if(count($uu_mobile)>0){
- UserZ::whereIn('phone',$uu_mobile)->where('level_name','!=','政委')->update(['up_name'=>$user->nickname,'up_phone'=>$user->mobile]);
- }
- }
- }
- Log::info('service1');
- Service::create([
- 'uid'=>$up->id,
- 'phone'=>$user->mobile,
- 'war_names'=>$war_name,
- ]);
- Log::info('service2');
- }elseif($user->level==2){
- UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['level_name'=>$level]);
- }
- }
- }
- }
|