123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Console\Commands;
- use App\Events\CancelOrderEvent;
- use App\Models\Comment;
- use App\Models\DwbsUser;
- use App\Models\DwbsWarea;
- use App\Models\GiftOrders;
- use App\Models\Order;
- use App\Models\OrderCancel as OrderCancelDB;
- use App\Models\Store;
- use App\Models\User;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- class CacheAgentInfoEven extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'cache_agent_info_even';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '缓存代理信息';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $cache=Cache::get('cache-agent_info_even');
- $num=2000;
- $count=Store::count();
- if($cache>=2 && $cache<ceil($count/$num)){
- $cache+=3;
- Cache::put('cache-agent_info_even',$cache);
- }else{
- $cache=2;
- Cache::put('cache-agent_info_even',$cache);
- }
- Log::info('even开始缓存代理信息'.($cache-1)*$num .'---'.$cache*$num );
- $store=Store::with('user:id,level,agent_id')->has('user')->orderBy('id')->skip(($cache-1)*$num)->take($num)->get();
- $wareas=DwbsWarea::pluck('name','id');
- $store_user_ids=Store::orderBy('id')->skip(($cache-1)*$num)->take($num)->pluck('user_id');
- $warea_user_ids=DwbsUser::withTrashed()->whereIn('id',$store_user_ids)->pluck('warea_id','id');
- if(!Cache::has('gift_orders')){
- $gift_orders=GiftOrders::where('type',0)->groupBy('store_id')
- ->select('store_id',DB::raw('sum(num*bonus) as integral'))->pluck('integral','store_id')->toArray();
- Cache::add('gift_orders', $gift_orders, 60);
- }
- Cache::remember('wareas',Carbon::now()->addHours(2),function (){
- return DwbsWarea::pluck('name','id');
- });
- foreach($store as $key=>$val){
- $second=mt_rand(2880,7200);
- if(!Cache::has('warea-'.$val->user->id)){
- $warea_id=array_key_exists($val->user_id,$warea_user_ids)?$warea_user_ids[$val->user_id]:null;
- Cache::put('warea-'.$val->user->id, array_key_exists($warea_id,$wareas)?$wareas[$warea_id]:null, $second);
- }
- if(!empty($val->user) && $val->user->level==2){
- if(!Cache::has('company-'.$val->user->id)){
- $company=DwbsUser::withTrashed()->where('id',$val->user->agent_id)->value('nickname');
- Cache::add('company-'.$val->user->id, $company, $second);
- }
- }
- if(!empty($val->user) && $val->user->level==1){
- if(!Cache::has('company-'.$val->user->id)){
- $agent=DwbsUser::withTrashed()->with('agent:id,nickname')->where('id',$val->user->agent_id)->first();
- if($agent->level==3){
- $company=$agent->nickname;
- }else{
- $company=$agent->agent->nickname;
- }
- Cache::put('company-'.$val->user->id, $company, $second);
- }
- }
- if(!empty($val->user) && $val->user->level==3){
- if(!Cache::has('company-'.$val->user->id)){
- $company=$val->user->nickname;
- Cache::add('company-'.$val->user->id, $company, $second);
- }
- }
- }
- Log::info('even结束缓存代理信息');
- }
- }
|