ExcellentResidents.php 6.8 KB

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