ArticleMonitor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Article;
  4. use App\Http\Models\ArticleStatistic;
  5. use App\Http\Models\Site;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Database\Eloquent\Relations\HasOne;
  8. class ArticleMonitor extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'article:monitor';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $sites = Site::query()->selectRaw('expired_at,id,status,renewal_at,order_at')->with(['siteInfo' => function (HasOne $q) {
  39. $q->selectRaw('site_id,article_need_num');
  40. }])->whereIn('status', [2,3])->get();
  41. $beginAt = date('Y-m-01 00:00:00');
  42. $endAt = date('Y-m-t 23:59:59');
  43. $siteIds = array_column($sites->toArray(), 'id');
  44. $createMap = Article::query()->selectRaw('SUM(id) as total,site_id')->whereIn('site_id', $siteIds)->where([['created_at', '>=', $beginAt], ['created_at', '<=', $endAt]])
  45. ->groupBy('site_id')->pluck('total', 'site_id')->toArray();
  46. // dump($createMap);
  47. $pubMap = Article::query()->selectRaw('SUM(id) as total,site_id')->whereIn('site_id', $siteIds)->where([['publish_at', '>=', $beginAt], ['publish_at', '<=', $endAt]])
  48. ->groupBy('site_id')->pluck('total', 'site_id')->toArray();
  49. // dd($pubMap);
  50. $totalNeedNum = [
  51. 'exec' => 0, //实施期
  52. 'server' => 0 //服务期
  53. ];
  54. //本月待查找文章数:实施期:xxx篇 服务期:xxx篇
  55. //本月待更新文章数:实施期:xxx篇 服务期:xxx篇
  56. $totalCreateNum = $totalPubNum = ['exec' => 0, 'server' => 0];
  57. // dump('$siteIds=',$siteIds);
  58. foreach ($sites as $site) {
  59. if ($site->siteInfo->article_need_num ?? 0) {
  60. $needNum = $site->siteInfo->article_need_num;
  61. } else {
  62. $whereAt = null;
  63. if (!empty($site->renewal_at)) {
  64. $whereAt = $site->renewal_at;
  65. } else {
  66. $whereAt = $site->order_at;
  67. }
  68. if (!$whereAt || empty($site->expired_at)) {
  69. // echo sprintf('whereAt continue=%d%s',$site->id,PHP_EOL);
  70. continue;
  71. }
  72. //距离到期时间的月份数
  73. $md = $this->getMonthNum(strtotime($site->expired_at));
  74. if ($md < 1) {
  75. // echo sprintf('md continue=%d%s',$site->id,PHP_EOL);
  76. continue;
  77. }
  78. //剩余文章数
  79. $surplusNum = Article::query()->where([['created_at', '>', $whereAt], ['site_id', '=', $site->id]])->count();
  80. $needNum = floor($surplusNum / $md);
  81. }
  82. if ($site->status == 2) { //实施期
  83. $needNum = $needNum < 10 ? 10 : $needNum;
  84. $totalNeedNum['exec'] += $needNum;
  85. $createNum = $createMap[$site->id] ?? 0;
  86. if ($createNum > $needNum) {
  87. $createNum = $needNum;
  88. }
  89. $pubNum = $pubMap[$site->id] ?? 0;
  90. $totalCreateNum['exec'] += $createNum;
  91. $totalPubNum['exec'] += $pubNum;
  92. }
  93. if ($site->status == 3) {
  94. $needNum = $needNum < 4 ? 4 : $needNum;
  95. $totalNeedNum['server'] += $needNum;
  96. $createNum = $createMap[$site->id] ?? 0;
  97. if ($createNum > $needNum) {
  98. $createNum = $needNum;
  99. }
  100. $pubNum = $pubMap[$site->id] ?? 0;
  101. $totalCreateNum['server'] += $createNum;
  102. $totalPubNum['server'] += $pubNum;
  103. }
  104. }
  105. //本月待查找文章数量=所有项目的本月需更新文章数总和-已上传数量(根据上传时间计算)
  106. //本月待更新文章数量=所有项目的本月需更新文章数总和-已发布数量(根据发布时间计算)
  107. ArticleStatistic::query()->create([
  108. 'day' => date('Ymd'),
  109. 'query_num' =>
  110. [
  111. 'exec' => $totalNeedNum['exec'] - $totalCreateNum['exec'],
  112. 'server' => $totalNeedNum['server'] - $totalCreateNum['server'],
  113. ]
  114. ,
  115. 'update_num' =>
  116. [
  117. 'exec' => $totalNeedNum['exec'] - $totalPubNum['exec'],
  118. 'server' => $totalNeedNum['server'] - $totalPubNum['server']
  119. ]
  120. ]);
  121. dump($totalNeedNum);
  122. $this->info('success');
  123. return;
  124. }
  125. protected function getMonthNum($targetTime)
  126. {
  127. $target = date('Ym', $targetTime);
  128. $now = date('Ym');
  129. $ty = substr($target, 0, 4);
  130. $tm = substr($target, 4, 2);
  131. $ny = substr($now, 0, 4);
  132. $nm = substr($now, 4, 2);
  133. $yd = intval($ty) - intval($ny);
  134. $md = intval($tm - $nm);
  135. $result = $yd * 12 + $md;
  136. return $result;
  137. }
  138. }