SubscribeAlert.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Libs\SMS;
  5. use Illuminate\Support\Facades\Cache;
  6. use DB;
  7. class SubscribeAlert extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'subscribealert {--type=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '粉丝和强关预警';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $type = $this->option('type');
  38. if($type == 'getTopAlert'){
  39. $this->getTopAlert();
  40. }
  41. if($type == 'subscribeRateAlert'){
  42. $this->subscribeRateAlert();
  43. }
  44. }
  45. /**
  46. * 商户平台日粉丝到达上限90%、达到总上限90%
  47. */
  48. private function getTopAlert(){
  49. $sql = "SELECT distribution_channel_id,GROUP_CONCAT(nickname)as nickname,GROUP_CONCAT(appid) as appid,SUM(subscribe_day_maximum) as `max` FROM official_accounts WHERE is_auth = 1 and is_enabled = 1 GROUP BY distribution_channel_id";
  50. $res = DB::select($sql);
  51. foreach ($res as $v){
  52. $count = DB::table('force_subscribe_users')
  53. ->whereIn('appid',explode(',',$v->appid))
  54. ->where('is_subscribed',1)
  55. ->where('created_at','>=',date('Y-m-d'))
  56. ->where('created_at','<',date('Y-m-d',time()+86400))
  57. ->count();
  58. if($count>= $v->max*0.9){
  59. $key = date('Y-m-d').'-distribution_channel_id-'.$v->distribution_channel_id;
  60. $old = Cache::store('file')->get($key);
  61. if($old){
  62. continue;
  63. }else{
  64. Cache::store('file')->put($key,time(),24*60);
  65. $content = '强关峰值预警,渠道'.$v->distribution_channel_id.'日粉丝到达上限90%,公众号:'.$v->nickname;
  66. $this->send($content);
  67. }
  68. }
  69. return;
  70. }
  71. }
  72. private function send($content){
  73. //(君平15858178353 张总 13858057394 陈帅军15088790066 zw 186.. 栋波15868100210 )
  74. $phones = ['15858178353','18668420256','15868100210'];
  75. //$phones = ['18668029091'];
  76. foreach ($phones as $phone){
  77. SMS::send($phone,$content);
  78. }
  79. }
  80. public function subscribeRateAlert(){
  81. $key = 'getSubscribeRate';
  82. $old = Cache::store('file')->get($key);
  83. if($old){
  84. return;
  85. }
  86. $now_hour = date('G');
  87. //echo '$now_hour--'.$now_hour.PHP_EOL;
  88. $rate = $this->getSubscribeRate();
  89. //echo '$tate is:'.$rate.PHP_EOL;
  90. if($now_hour>=4 && $now_hour<=7){
  91. if($rate < 0.2){
  92. $content = '平台强关率预警,当前强关率'.$rate;
  93. $this->send($content);
  94. Cache::store('file')->put($key,time(),60*3);
  95. }
  96. }else{
  97. if($rate < 0.25){
  98. $content = '平台强关率预警,当前强关率'.$rate;
  99. $this->send($content);
  100. Cache::store('file')->put($key,time(),60*3);
  101. }
  102. }
  103. }
  104. private function getSubscribeRate(){
  105. $register_user_num = DB::table('users')->where('created_at','>=',date('Y-m-d H',time()-3600))->where('created_at','<',date('Y-m-d H'))->count();
  106. $force_subscribe_users_num = DB::table('force_subscribe_users')->where('created_at','>=',date('Y-m-d H',time()-3600))->where('created_at','<',date('Y-m-d H'))->count();
  107. if($register_user_num == 0){
  108. return 0;
  109. }
  110. return round($force_subscribe_users_num/$register_user_num,2);
  111. }
  112. }