ChangeUserLevelListener.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Modules\Order\Listeners;
  3. use Modules\Order\Events\ChangeUserLevel;
  4. use App\Models\WarZone;
  5. use App\Models\Warea;
  6. use App\Models\User;
  7. use App\Models\UserZ;
  8. use App\Models\Service;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. class ChangeUserLevelListener
  13. {
  14. /**
  15. * Create the event listener.
  16. *
  17. * @return void
  18. */
  19. public function __construct()
  20. {
  21. //
  22. }
  23. /**
  24. * Handle the event.
  25. *
  26. * @param ChangeUserLevel $event
  27. * @return void
  28. */
  29. public function handle(ChangeUserLevel $event)
  30. {
  31. $user=$event->user;
  32. if($user->level==1){
  33. $level='销售主管';
  34. }elseif($user->level==2){
  35. $level='销售经理';
  36. }elseif($user->level==3){
  37. $level='代理公司';
  38. }
  39. if($user->before > $user->level){//降级
  40. if($user->level==2){//代理公司降到销售经理
  41. Log::info($user->warea_id);
  42. $warea=Warea::where('id',$user->warea_id)->first();
  43. Log::info($warea->name);
  44. $war_name= explode('战区',$warea->name)[0];
  45. $up=User::where('id',$user->agent_id)->first();
  46. UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['war_names'=>$war_name,'level_name'=>$level,'up_name'=>$up->nickname,'up_phone'=>$up->mobile]);
  47. UserZ::where('up_phone',$user->mobile)->where('level_name','!=','政委')->update(['war_names'=>$war_name,'up_name'=>$up->nickname,'up_phone'=>$up->mobile]);
  48. Service::where('phone',$user->mobile)->delete();
  49. }elseif($user->level==1){
  50. UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['level_name'=>$level]);
  51. }
  52. }elseif($user->before < $user->level){//升级
  53. if($user->level==3){//销售经理升级到代理公司
  54. $warea=Warea::where('id',$user->warea_id)->first();
  55. $war_name= explode('战区',$warea->name)[0];
  56. $up=UserZ::where('war_names',$war_name)->where('level_name','政委')->first();
  57. $userinfo=UserZ::where('phone',$user->mobile)->first();
  58. if($userinfo){
  59. UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['level_name'=>$level,'up_name'=>$up->nickname,'up_phone'=>$up->phone]);
  60. }else{
  61. UserZ::create([
  62. 'phone'=>$user->mobile,
  63. 'openid'=>$user->mobile,
  64. 'nickname'=>$user->nickname,
  65. 'name'=>$user->realname??$user->nickname,
  66. 'up_phone'=>$up->phone,
  67. 'up_name'=>$up->nickname,
  68. 'level_name'=>$level,
  69. 'war_names'=>$war_name,
  70. 'status'=>0,
  71. ]);
  72. }
  73. $u_mobile=User::where('agent_id',$user->id)->pluck('mobile');
  74. if(count($u_mobile)>0){
  75. $u_id=User::where('agent_id',$user->id)->pluck('id');
  76. UserZ::whereIn('phone',$u_mobile)->where('level_name','!=','政委')->update(['up_name'=>$user->nickname,'up_phone'=>$user->mobile]);
  77. foreach($u_id as $k=>$v){
  78. $uu_mobile=User::where('agent_id',$v)->pluck('mobile');
  79. if(count($uu_mobile)>0){
  80. UserZ::whereIn('phone',$uu_mobile)->where('level_name','!=','政委')->update(['up_name'=>$user->nickname,'up_phone'=>$user->mobile]);
  81. }
  82. }
  83. }
  84. Log::info('service1');
  85. Service::create([
  86. 'uid'=>$up->id,
  87. 'phone'=>$user->mobile,
  88. 'war_names'=>$war_name,
  89. ]);
  90. Log::info('service2');
  91. }elseif($user->level==2){
  92. UserZ::where('phone',$user->mobile)->where('level_name','!=','政委')->update(['level_name'=>$level]);
  93. }
  94. }
  95. }
  96. }