Link.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\LinkStatistical;
  4. use App\Http\Models\Role;
  5. use App\Http\Models\Site;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Database\Eloquent\Builder;
  8. class Link extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'link:link';
  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()
  39. ->whereIn('status', [2, 3])
  40. ->with(['users' => function (\Illuminate\Database\Eloquent\Relations\BelongsToMany $q) {
  41. $q->select(['id', 'username', 'nickname', 'role_id']);
  42. }, 'linkDetails' => function (\Illuminate\Database\Eloquent\Relations\HasMany $q) {
  43. $q->select(['id', 'redundant_site_id', 'link_id'])
  44. ->where(['enable' => 1, 'link_tasks_detail.status' => 5]);
  45. }])->withCount([
  46. 'linkUrls as publishThisMonth' => function (Builder $b) {
  47. $b->where('link_tasks_detail.enable', 1)
  48. ->where('link_tasks_detail.status', 5)
  49. ->where('link_tasks_url.status', 5)
  50. ->whereBetween('link_tasks_url.created_at', [date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')]);
  51. },
  52. 'linkUrls as publishLastMonth' => function (Builder $b) {
  53. $b->where('link_tasks_detail.enable', 1)
  54. ->where('link_tasks_detail.status', 5)
  55. ->where('link_tasks_url.status', 5)
  56. ->whereBetween('link_tasks_url.created_at', [date('Y-m-01 00:00:00', strtotime('last month')), date('Y-m-d 23:59:59', strtotime(date('Y-m-1') . '-1 day'))]);
  57. },
  58. 'linkUrls as linkUrls' => function (Builder $query) {
  59. $query->where('link_tasks_detail.enable', 1)
  60. ->where('link_tasks_detail.status', 5)
  61. ->where('link_tasks_url.status', 5);
  62. }])->orderByDesc('id')->get();
  63. $data = [];
  64. foreach ($sites as $value) {
  65. $data[] = [
  66. 'site_id' => $value->id,
  67. 'cn_title' => $value->cn_title ?? '',
  68. 'domain' => $value->domain ?? '',
  69. 'site_status' => $value->status,
  70. 'link_goal' => $value->link_goal ?? 0,
  71. 'link_pact_count' => $value->linkDetails->groupBy('link_id')->count(),
  72. 'link_urls' => $value->linkUrls ?? 0,
  73. 'publish_last_month' => $value->publishLastMonth ?? 0,
  74. 'publish_this_month' => $value->publishThisMonth ?? 0,
  75. 'server_people_id' => implode(',', $value->users->where('role_id', Role::TYPE_SERVER)->pluck('id')->toArray()),
  76. 'chongqing_link_people_id' => implode(',', $value->users->where('role_id', Role::TYPE_LINK_PART_CHONGQING)->pluck('id')->toArray()),
  77. 'optimizer_people_id' => implode(',', $value->users->where('role_id', Role::TYPE_OPTIMIZER)->pluck('id')->toArray()),
  78. 'optimizer_editing_people_id' => implode(',', $value->users->where('role_id', Role::TYPE_OPTIMIZATION_EDITING)->pluck('id')->toArray()),
  79. 'created_at' => date('Y-m-d H:i:s')
  80. ];
  81. }
  82. LinkStatistical::query()->delete();
  83. LinkStatistical::query()->insert($data);
  84. }
  85. }