ExcellentResidents.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $time = Carbon::now()->startOfDay()->toDateTimeString();
  59. $sum_quantity_issued = $this->behavior
  60. ->leftJoin('comment_account_record as c','behavior.virus_behavior_id','=','c.virus_behavior_id')
  61. ->leftJoin('general_ledger_record as g','behavior.virus_behavior_id','=','g.virus_behavior_id')
  62. ->leftJoin('registered_accounts_record as a','behavior.virus_behavior_id','=','a.virus_behavior_id')
  63. ->leftJoin('release_record as r','behavior.virus_behavior_id','=','r.virus_behavior_id')
  64. ->where('c.created_at', '>=', $time)
  65. ->orwhere('g.created_at', '>=', $time)
  66. ->orwhere('a.created_at', '>=', $time)
  67. ->orwhere('r.created_at', '>=', $time)
  68. ->get(array(
  69. DB::raw('SUM(c.quantity_issued) as c_quantity_issued'),
  70. DB::raw('SUM(g.quantity_issued) as g_quantity_issued'),
  71. DB::raw('SUM(a.quantity_issued) as a_quantity_issued'),
  72. DB::raw('SUM(r.quantity_issued) as r_quantity_issued')
  73. )
  74. )->toArray();
  75. //昨日发放总豆
  76. $all_quantity_issued = 0;
  77. foreach ($sum_quantity_issued as $k=>$v){
  78. $all_quantity_issued += $v['c_quantity_issued'] + $v['g_quantity_issued'] + $v['a_quantity_issued'] + $v['r_quantity_issued'];
  79. }
  80. //文章被评论最多用户
  81. $comment = $this->commentRecord
  82. ->where('created_at', '>=', $time)
  83. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  84. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  85. $comment = $comment->toArray();
  86. foreach ($comment as $k=>$v){
  87. $comment[$k]['type'] = 'comment';//类型
  88. }
  89. //昨日拉新最多用户
  90. $registered = $this->registeredRecord
  91. ->where('created_at', '>=', $time)
  92. ->select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
  93. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
  94. $registered = $registered->toArray();
  95. foreach ($registered as $k=>$v){
  96. $registered[$k]['type'] = 'registered';
  97. }
  98. //文章被收藏最多用户
  99. $virus_id = $this->behavior
  100. ->select('virus_behavior_id')
  101. ->where('behavior_identification', 'collect')
  102. ->first();
  103. $collent = $this->generalRecord
  104. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  105. ->where('created_at', '>=', $time)
  106. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  107. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  108. $collent = $collent->toArray();
  109. foreach ($collent as $k=>$v){
  110. $collent[$k]['type'] = 'collent';
  111. }
  112. //文章被喜欢最多用户
  113. $virus_id = $this->behavior
  114. ->select('virus_behavior_id')
  115. ->where('behavior_identification', 'like')
  116. ->first();
  117. $like = $this->generalRecord
  118. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  119. ->where('created_at', '>=', $time)
  120. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  121. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  122. $like = $like->toArray();
  123. foreach ($like as $k=>$v){
  124. $like[$k]['type'] = 'like';
  125. }
  126. //文章被转发最多用户
  127. $virus_id = $this->behavior
  128. ->select('virus_behavior_id')
  129. ->where('behavior_identification', 'forward')
  130. ->first();
  131. $forward = $this->generalRecord
  132. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  133. ->where('created_at', '>=', $time)
  134. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  135. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  136. $forward = $forward->toArray();
  137. foreach ($forward as $k=>$v){
  138. $forward[$k]['type'] = 'forward';
  139. }
  140. //文章被阅读最多用户
  141. $virus_id = $this->behavior
  142. ->select('virus_behavior_id')
  143. ->where('behavior_identification', 'read')
  144. ->first();
  145. $read = $this->generalRecord
  146. ->where('virus_behavior_id', $virus_id->virus_behavior_id)
  147. ->where('created_at', '>=', $time)
  148. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  149. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  150. $read = $read->toArray();
  151. foreach ($read as $k=>$v){
  152. $read[$k]['type'] = 'read';
  153. }
  154. $all_merge = array_merge($comment, $registered, $collent, $like, $forward, $read);
  155. $all_excellent_residents = json_encode($all_merge);
  156. Log::info('统计昨日优秀居民内容'.json_encode($all_excellent_residents));
  157. Log::info('昨日发放总彩虹豆'.json_encode($all_quantity_issued));
  158. Redis::set('yesterday_excellent_residents', $all_excellent_residents);
  159. Redis::set('yesterday_quantity_issued', $all_quantity_issued);
  160. $this->line("统计昨日内容结束");
  161. }
  162. }