ExcellentResidents.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. //昨日发放总豆
  71. $comment_quantity_issued = CommentRecord::select('quantity_issued')
  72. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  73. ->sum('quantity_issued');
  74. $general_quantity_issued = GeneralRecord::select('quantity_issued')
  75. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  76. ->sum('quantity_issued');
  77. $registered_quantity_issued = RegisteredRecord::select('quantity_issued')
  78. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  79. ->sum('quantity_issued');
  80. $release_quantity_issued = ReleaseRecord::select('quantity_issued')
  81. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  82. ->sum('quantity_issued');
  83. $all_quantity_issued = $comment_quantity_issued + $general_quantity_issued + $registered_quantity_issued + $release_quantity_issued;
  84. //文章被评论最多用户
  85. $comment = $this->commentRecord
  86. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  87. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  88. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  89. $comment = $comment->toArray();
  90. foreach ($comment as $k => $v) {
  91. $comment[$k]['type'] = 'comment';//类型
  92. }
  93. //昨日拉新最多用户
  94. $registered = $this->registeredRecord
  95. ->where('superior_uid', '!=', 0)
  96. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  97. ->select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
  98. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
  99. $registered = $registered->toArray();
  100. foreach ($registered as $k => $v) {
  101. $registered[$k]['type'] = 'registered';
  102. }
  103. //文章被收藏最多用户
  104. $virus_id = $this->behavior
  105. ->select('virus_behavior_id')
  106. ->where('behavior_identification', 'collect')
  107. ->first();
  108. $collent = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
  109. $collent = $collent->toArray();
  110. foreach ($collent as $k => $v) {
  111. $collent[$k]['type'] = 'collect';
  112. }
  113. //文章被喜欢最多用户
  114. $virus_id = $this->behavior
  115. ->select('virus_behavior_id')
  116. ->where('behavior_identification', 'like')
  117. ->first();
  118. $like = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
  119. $like = $like->toArray();
  120. foreach ($like as $k => $v) {
  121. $like[$k]['type'] = 'like';
  122. }
  123. //文章被转发最多用户
  124. $virus_id = $this->behavior
  125. ->select('virus_behavior_id')
  126. ->where('behavior_identification', 'forward')
  127. ->first();
  128. $forward = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
  129. $forward = $forward->toArray();
  130. foreach ($forward as $k => $v) {
  131. $forward[$k]['type'] = 'forward';
  132. }
  133. //文章被阅读最多用户
  134. $virus_id = $this->behavior
  135. ->select('virus_behavior_id')
  136. ->where('behavior_identification', 'read')
  137. ->first();
  138. $read = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
  139. $read = $read->toArray();
  140. foreach ($read as $k => $v) {
  141. $read[$k]['type'] = 'read';
  142. }
  143. $all_merge = array_merge($comment, $registered, $collent, $like, $forward, $read);
  144. $all_excellent_residents = json_encode($all_merge);
  145. Log::info('统计昨日优秀居民内容' . json_encode($all_excellent_residents));
  146. Log::info('昨日发放总U米' . json_encode($all_quantity_issued));
  147. Redis::set('yesterday_excellent_residents', $all_excellent_residents);
  148. Redis::set('yesterday_quantity_issued', $all_quantity_issued);
  149. $this->line("统计昨日内容结束");
  150. }
  151. }