CacheAgentInfoEven.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 CacheAgentInfoEven extends Command
  18. {
  19. /**
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'cache_agent_info_even';
  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. $cache=Cache::get('cache-agent_info_even');
  48. $num=2000;
  49. $count=Store::count();
  50. if($cache>=2 && $cache<ceil($count/$num)){
  51. $cache+=3;
  52. Cache::put('cache-agent_info_even',$cache);
  53. }else{
  54. $cache=2;
  55. Cache::put('cache-agent_info_even',$cache);
  56. }
  57. Log::info('even开始缓存代理信息'.($cache-1)*$num .'---'.$cache*$num );
  58. $store=Store::with('user:id,level,agent_id')->has('user')->orderBy('id')->skip(($cache-1)*$num)->take($num)->get();
  59. $wareas=DwbsWarea::pluck('name','id');
  60. $store_user_ids=Store::orderBy('id')->skip(($cache-1)*$num)->take($num)->pluck('user_id');
  61. $warea_user_ids=DwbsUser::withTrashed()->whereIn('id',$store_user_ids)->pluck('warea_id','id');
  62. if(!Cache::has('gift_orders')){
  63. $gift_orders=GiftOrders::where('type',0)->groupBy('store_id')
  64. ->select('store_id',DB::raw('sum(num*bonus) as integral'))->pluck('integral','store_id')->toArray();
  65. Cache::add('gift_orders', $gift_orders, 60);
  66. }
  67. Cache::remember('wareas',Carbon::now()->addHours(2),function (){
  68. return DwbsWarea::pluck('name','id');
  69. });
  70. foreach($store as $key=>$val){
  71. $second=mt_rand(2880,7200);
  72. if(!Cache::has('warea-'.$val->user->id)){
  73. $warea_id=array_key_exists($val->user_id,$warea_user_ids)?$warea_user_ids[$val->user_id]:null;
  74. Cache::put('warea-'.$val->user->id, array_key_exists($warea_id,$wareas)?$wareas[$warea_id]:null, $second);
  75. }
  76. if(!empty($val->user) && $val->user->level==2){
  77. if(!Cache::has('company-'.$val->user->id)){
  78. $company=DwbsUser::withTrashed()->where('id',$val->user->agent_id)->value('nickname');
  79. Cache::add('company-'.$val->user->id, $company, $second);
  80. }
  81. }
  82. if(!empty($val->user) && $val->user->level==1){
  83. if(!Cache::has('company-'.$val->user->id)){
  84. $agent=DwbsUser::withTrashed()->with('agent:id,nickname')->where('id',$val->user->agent_id)->first();
  85. if($agent->level==3){
  86. $company=$agent->nickname;
  87. }else{
  88. $company=$agent->agent->nickname;
  89. }
  90. Cache::put('company-'.$val->user->id, $company, $second);
  91. }
  92. }
  93. if(!empty($val->user) && $val->user->level==3){
  94. if(!Cache::has('company-'.$val->user->id)){
  95. $company=$val->user->nickname;
  96. Cache::add('company-'.$val->user->id, $company, $second);
  97. }
  98. }
  99. }
  100. Log::info('even结束缓存代理信息');
  101. }
  102. }