CacheAgentInfo.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Events\CancelOrderEvent;
  4. use App\Models\Comment;
  5. use App\Models\DwbsUser;
  6. use App\Models\DwbsWarea;
  7. use App\Models\GiftOrders;
  8. use App\Models\Order;
  9. use App\Models\OrderCancel as OrderCancelDB;
  10. use App\Models\Store;
  11. use App\Models\User;
  12. use Carbon\Carbon;
  13. use Illuminate\Console\Command;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\DB;
  17. class CacheAgentInfo extends Command
  18. {
  19. /**
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'cache_agent_info';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = '缓存代理信息';
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. }
  40. /**
  41. * Execute the console command.
  42. *
  43. * @return mixed
  44. */
  45. public function handle()
  46. {
  47. Log::info('cweshi --010-- '.microtime());
  48. $cache=Cache::get('cache-agent_info');
  49. $num=2000;
  50. $count=Store::count();
  51. if($cache>=1 && $cache<ceil($count/$num)){
  52. $cache+=3;
  53. Cache::put('cache-agent_info',$cache);
  54. }else{
  55. $cache=1;
  56. Cache::put('cache-agent_info',$cache);
  57. }
  58. Log::info('开始缓存代理信息'.($cache-1)*$num .'---'.$cache*$num );
  59. Log::info('cweshi --0-- '.microtime());
  60. // $store=Store::all();
  61. $store=Store::with('user:id,level,agent_id')->has('user')->orderBy('id')->skip(($cache-1)*$num)->take($num)->get();
  62. $wareas=DwbsWarea::pluck('name','id');
  63. // $store_user_ids=Store::pluck('user_id');
  64. $store_user_ids=Store::orderBy('id')->skip(($cache-1)*$num)->take($num)->pluck('user_id');
  65. $warea_user_ids=DwbsUser::withTrashed()->whereIn('id',$store_user_ids)->pluck('warea_id','id');
  66. Log::info('cweshi --1-- '.microtime());
  67. if(!Cache::has('gift_orders')){
  68. $gift_orders=GiftOrders::where('type',0)->groupBy('store_id')
  69. ->select('store_id',DB::raw('sum(num*bonus) as integral'))->pluck('integral','store_id')->toArray();
  70. Cache::add('gift_orders', $gift_orders, 60);
  71. }
  72. Cache::remember('wareas',Carbon::now()->addHours(2),function (){
  73. return DwbsWarea::pluck('name','id');
  74. });
  75. Log::info('cweshi --2-- '.microtime());
  76. foreach($store as $key=>$val){
  77. $second=mt_rand(2880,7200);
  78. if(!$val->user){
  79. continue;
  80. }
  81. if(!Cache::has('warea-'.$val->user->id)){
  82. $warea_id=array_key_exists($val->user_id,$warea_user_ids)?$warea_user_ids[$val->user_id]:null;
  83. Cache::put('warea-'.$val->user->id, array_key_exists($warea_id,$wareas)?$wareas[$warea_id]:null, $second);
  84. }
  85. // if(array_key_exists($val->id, $gift_orders)){
  86. // $list[$key]->finish_integral=$gift_orders[$val->id];
  87. // }else{
  88. // $list[$key]->finish_integral=0;
  89. // }
  90. // $list[$key]->company='';
  91. if(!empty($val->user) && $val->user->level==2){
  92. if(!Cache::has('company-'.$val->user->id)){
  93. $company=DwbsUser::withTrashed()->where('id',$val->user->agent_id)->value('nickname');
  94. Cache::add('company-'.$val->user->id, $company, $second);
  95. }
  96. }
  97. if(!empty($val->user) && $val->user->level==1){
  98. if(!Cache::has('company-'.$val->user->id)){
  99. $agent=DwbsUser::withTrashed()->with('agent:id,nickname')->where('id',$val->user->agent_id)->first();
  100. if($agent->level==3){
  101. $company=$agent->nickname;
  102. }else{
  103. $company=$agent->agent->nickname;
  104. }
  105. Cache::put('company-'.$val->user->id, $company, $second);
  106. }
  107. }
  108. if(!empty($val->user) && $val->user->level==3){
  109. if(!Cache::has('company-'.$val->user->id)){
  110. $company=$val->user->nickname;
  111. Cache::add('company-'.$val->user->id, $company, $second);
  112. }
  113. }
  114. }
  115. Log::info('cweshi --3-- '.microtime());
  116. Log::info('结束缓存代理信息');
  117. }
  118. }