123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace App\Console\Commands;
- use App\Http\Middleware\EncryptCookies;
- use App\Models\Basic;
- use App\Models\DwbsUser;
- use App\Models\Enroll;
- use App\Models\Intelog;
- use App\Models\RankDays;
- use App\Models\RankTemplate;
- use App\Models\User;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Intervention\Image\Facades\Image;
- class MakeNumOneLevelCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'command:name';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $start_time=Carbon::yesterday();
- $end_time=Carbon::today();
- $season=Basic::where('keys','season')->value('value');
- $season_start=Basic::where('keys','round_start_time')->value('value');
- $season_end=Basic::where('keys','round_end_time')->value('value');
- $time=Carbon::now()->timestamp;
- if ($time>$season_start && $time<$season_end){
- $crown_ids=User::where('integral','>',0)->where('level_name','批发商')->pluck('id');
- $crown_inte=Intelog::where('season',$season)->where('type',1)
- ->whereIn('uid',$crown_ids)->whereBetween('time',[strtotime($start_time),strtotime($end_time)-1])
- ->select(DB::raw('SUM(integral) as all_integral'),'uid')->orderByDesc('all_integral')->groupBy('uid')
- ->limit(1)->get();
- $re=Intelog::where('season',$season)->where('type',1)
- ->whereIn('uid',$crown_ids)->whereBetween('time',[strtotime($start_time),strtotime($end_time)-1])
- ->select(DB::raw('SUM(integral) as all_integral'),'uid')->orderByDesc('all_integral')->groupBy('uid')
- ->Having('all_integral','=',$crown_inte[0]->all_integral)->get();
- foreach ($re as $k=>$v){
- $this->GetImg($v->uid,'3',$season);
- }
- $jingli_ids=User::where('integral','>',0)->where('level_name','经销商')->pluck('id');
- $jingli_inte=Intelog::where('season',$season)->where('type',1)
- ->whereIn('uid',$jingli_ids)->whereBetween('time',[strtotime($start_time),strtotime($end_time)-1])
- ->select(DB::raw('SUM(integral) as all_integral'),'uid')
- ->orderByDesc('all_integral')->groupBy('uid')->limit(1)->get();
- $res=Intelog::where('season',$season)->where('type',1)
- ->whereIn('uid',$jingli_ids)->whereBetween('time',[strtotime($start_time),strtotime($end_time)-1])
- ->select(DB::raw('SUM(integral) as all_integral'),'uid')
- ->orderByDesc('all_integral')->groupBy('uid')->Having('all_integral','=',$jingli_inte[0]->all_integral)->get();
- foreach ($res as $k=>$v){
- $this->GetImg($v->uid,'2',$season);
- }
- $zhuguan_ids=User::where('integral','>',0)->where('level_name','销售员')->pluck('id');
- $zhuguan_inte=Intelog::where('season',$season)->where('type',1)
- ->whereIn('uid',$zhuguan_ids)->whereBetween('time',[strtotime($start_time),strtotime($end_time)-1])
- ->select(DB::raw('SUM(integral) as all_integral'),'uid')
- ->orderByDesc('all_integral')->groupBy('uid')->limit(1)->get();
- $zhuguan_re=Intelog::where('season',$season)->where('type',1)
- ->whereIn('uid',$zhuguan_ids)->whereBetween('time',[strtotime($start_time),strtotime($end_time)-1])
- ->select(DB::raw('SUM(integral) as all_integral'),'uid')
- ->orderByDesc('all_integral')->groupBy('uid')->Having('all_integral','=',$zhuguan_inte[0]->all_integral)->get();
- foreach ($zhuguan_re as $k=>$v){
- $this->GetImg($v->uid,'1',$season);
- }
- }
- Log::error('生成每日排名第一成功');
- }
- public function GetImg($phone,$level,$season){
- $user=User::where('id',$phone)->first();
- $dw_user=DwbsUser::find($user->dwbs_id);
- $name=$dw_user->remark_name==null?$dw_user->nickname:$dw_user->remark_name;
- $url=RankTemplate::where('level',$level)->where('season',$season)->where('type',2)->where('url_type',0)->value('url');
- $Image=Image::make($url);
- $logo_url=RankTemplate::where('type',2)->where('season',$season)->where('url_type',1)->value('url');
- $logo=Image::make($logo_url);
- try {
- // $avatar=mb_substr($user->avatar);
- $avatar=mb_substr($user->avatar,0,-3).'0';
- $avatar=Image::make($avatar)->resize(725,725);;
- } catch (\Exception $exception) {
- try{
- $avatar = Image::make($dw_user->headimgurl)->resize(725,725);
- }catch (\Exception $exception){
- $avatar = Image::make(public_path() . '/logo3.png');
- }
- }
- $avatar=$this->bg_resize($avatar);
- $Image->insert($avatar,'',190,635);
- $Image->insert($logo);
- $name=$this->filterEmoji($name);
- $length=mb_strlen($name);
- if ($length>=6){
- $name=mb_substr($name,0,6);
- $h=835-40;
- $y=80;
- }elseif($length==5){
- $h=835-20;
- $y=80;
- }elseif ($length==4){
- $h=835;
- $y=80;
- }elseif($length==3){
- $h=835+40;
- $y=120;
- }elseif($length==2){
- $h=835+80;
- $y=160;
- }else{
- $h=835+160;
- $y=0;
- }
- for ($i=0;$i<$length;$i++){
- $Image->text(mb_substr($name,$i,1),951,$h+$i*$y,function ($font){
- $font->file(public_path().'/ziti/ht.otf');
- $font->size(73);
- $font->color('FFF');
- $font->angle(0);
- });
- }
- $time=date('m月d日',time()-24*3600);
- $Image->text($time,150,450,function ($font){
- $font->file(public_path().'/uploads/images/background/zi.otf');
- $font->size(174);
- $font->color('FFF');
- $font->angle(0);
- });
- $folder_name = "uploads/images/rankImg/" . date("Ym/d", time());
- // 文件具体存储的物理路径,`public_path()` 获取的是 `public` 文件夹的物理路径。
- // 值如:/home/vagrant/Code/larabbs/public/uploads/images/avatars/201709/21/
- $upload_path = '/public/' . $folder_name;
- // 获取文件的后缀名,因图片从剪贴板里黏贴时后缀名为空,所以此处确保后缀一直存在
- $extension = 'png';
- // 拼接文件名,加前缀是为了增加辨析度,前缀可以是相关数据模型的 ID
- // 值如:1_1493521050_7BVc9v9ujP.png
- $filename = $level . '_' . time() . '_' . str_random(10) . '.' . $extension;
- if (!file_exists(public_path($upload_path))) {
- mkdir(public_path($upload_path), 0777, true);
- }
- // File::isDirectory(public_path().$upload_path) or File::makeDirectory(public_path().$upload_path,077,true,true);
- // 将图片移动到我们的目标存储路径中
- $Image->save(public_path().$upload_path.'/'.$filename);
- $rank_path=env('APP_URL').$upload_path.'/'.$filename;
- $rank_days=new RankDays();
- $rank_days->season=$season;
- $rank_days->level=$level;
- $rank_days->type=2;
- $rank_days->img_url=$rank_path;
- $rank_days->user_ids=json_encode([$user->id]);
- $rank_days->time=Carbon::today()->toDateString();
- $rank_days->save();
- }
- public function bg_resize($img2)
- {
- $img2 = $img2->resize(700, 700);
- $r = $img2->width() / 2;
- $img3 = Image::canvas(700, 700);
- for ($x = 0; $x < $img2->width(); $x++) {
- for ($y = 0; $y < $img2->height(); $y++) {
- if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
- $c = $img2->pickColor($x, $y, 'array');
- $img3->pixel($c, $x, $y);
- }
- }
- }
- return $img3;
- }
- function filterEmoji($str=''){
- $str = json_encode($str);
- $str = json_decode(preg_replace("#(\\\ud[0-9a-f]{3})#i", "", $str));
- return $str;
- }
- }
|