CacheAgentInfoThree.php 3.9 KB

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