ExcellentResidents.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: durong
  5. * Date: 2019/6/19
  6. * Time: 下午3:45
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\Behavior;
  10. use App\Models\CommentRecord;
  11. use App\Models\GeneralRecord;
  12. use App\Models\RegisteredRecord;
  13. use App\Models\ReleaseRecord;
  14. use Illuminate\Console\Command;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. class ExcellentResidents extends Command
  20. {
  21. /**
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'excellent:residents';
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '昨日优秀居民统计';
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(Behavior $behavior, RegisteredRecord $registeredRecord,
  39. CommentRecord $commentRecord,
  40. GeneralRecord $generalRecord,
  41. ReleaseRecord $releaseRecord)
  42. {
  43. parent::__construct();
  44. $this->behavior = $behavior;
  45. $this->commentRecord = $commentRecord;
  46. $this->generalRecord = $generalRecord;
  47. $this->registeredRecord = $registeredRecord;
  48. $this->releaseRecord = $releaseRecord;
  49. }
  50. /**
  51. * Execute the console command.
  52. *
  53. * @return mixed
  54. */
  55. public function handle()
  56. {
  57. $this->line("开始统计星球居民相关内容");
  58. $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
  59. $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
  60. $sum_quantity_issued = $this->behavior
  61. ->leftJoin('comment_account_record as c','behavior.virus_behavior_id','=','c.virus_behavior_id')
  62. ->leftJoin('general_ledger_record as g','behavior.virus_behavior_id','=','g.virus_behavior_id')
  63. ->leftJoin('registered_accounts_record as a','behavior.virus_behavior_id','=','a.virus_behavior_id')
  64. ->leftJoin('release_record as r','behavior.virus_behavior_id','=','r.virus_behavior_id')
  65. ->whereBetween('c.created_at', [$yesterday_start,$yesterday_end])
  66. ->orwhereBetween('g.created_at', [$yesterday_start,$yesterday_end])
  67. ->orwhereBetween('a.created_at', [$yesterday_start,$yesterday_end])
  68. ->orwhereBetween('r.created_at', [$yesterday_start,$yesterday_end])
  69. ->get(array(
  70. DB::raw('SUM(c.quantity_issued) as c_quantity_issued'),
  71. DB::raw('SUM(g.quantity_issued) as g_quantity_issued'),
  72. DB::raw('SUM(a.quantity_issued) as a_quantity_issued'),
  73. DB::raw('SUM(r.quantity_issued) as r_quantity_issued')
  74. )
  75. )->toArray();
  76. //昨日发放总豆
  77. $all_quantity_issued = 0;
  78. foreach ($sum_quantity_issued as $k=>$v){
  79. $all_quantity_issued += $v['c_quantity_issued'] + $v['g_quantity_issued'] + $v['a_quantity_issued'] + $v['r_quantity_issued'];
  80. }
  81. //文章被评论最多用户
  82. $comment = $this->commentRecord
  83. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  84. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  85. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  86. $comment = $comment->toArray();
  87. foreach ($comment as $k=>$v){
  88. $comment[$k]['type'] = 'comment';//类型
  89. }
  90. //昨日拉新最多用户
  91. $registered = $this->registeredRecord
  92. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  93. ->select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
  94. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
  95. $registered = $registered->toArray();
  96. foreach ($registered as $k=>$v){
  97. $registered[$k]['type'] = 'registered';
  98. }
  99. //文章被收藏最多用户
  100. $virus_id = $this->behavior
  101. ->select('virus_behavior_id')
  102. ->where('behavior_identification', 'collect')
  103. ->first();
  104. $collent = $this->generalRecord
  105. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  106. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  107. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  108. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  109. $collent = $collent->toArray();
  110. foreach ($collent as $k=>$v){
  111. $collent[$k]['type'] = 'collent';
  112. }
  113. //文章被喜欢最多用户
  114. $virus_id = $this->behavior
  115. ->select('virus_behavior_id')
  116. ->where('behavior_identification', 'like')
  117. ->first();
  118. $like = $this->generalRecord
  119. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  120. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  121. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  122. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  123. $like = $like->toArray();
  124. foreach ($like as $k=>$v){
  125. $like[$k]['type'] = 'like';
  126. }
  127. //文章被转发最多用户
  128. $virus_id = $this->behavior
  129. ->select('virus_behavior_id')
  130. ->where('behavior_identification', 'forward')
  131. ->first();
  132. $forward = $this->generalRecord
  133. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  134. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  135. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  136. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  137. $forward = $forward->toArray();
  138. foreach ($forward as $k=>$v){
  139. $forward[$k]['type'] = 'forward';
  140. }
  141. //文章被阅读最多用户
  142. $virus_id = $this->behavior
  143. ->select('virus_behavior_id')
  144. ->where('behavior_identification', 'read')
  145. ->first();
  146. $read = $this->generalRecord
  147. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  148. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  149. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  150. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  151. $read = $read->toArray();
  152. foreach ($read as $k=>$v){
  153. $read[$k]['type'] = 'read';
  154. }
  155. $all_merge = array_merge($comment, $registered, $collent, $like, $forward, $read);
  156. $all_excellent_residents = json_encode($all_merge);
  157. Log::info('统计昨日优秀居民内容'.json_encode($all_excellent_residents));
  158. Log::info('昨日发放总彩虹豆'.json_encode($all_quantity_issued));
  159. Redis::set('yesterday_excellent_residents', $all_excellent_residents);
  160. Redis::set('yesterday_quantity_issued', $all_quantity_issued);
  161. $this->line("统计昨日内容结束");
  162. }
  163. }