fn_b/app/Console/Commands/BackfillEditorialLikes.php

53 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class BackfillEditorialLikes extends Command
{
protected $signature = 'article:backfill-editorial-likes {--apply : Apply missing settings; otherwise only list candidates}';
protected $description = '为已发布编辑文章补齐缺失点赞配置,保留已有参数和进度';
public function handle(): int
{
$ids = DB::table('editorial_documents')->whereNotNull('article_id')
->whereNotNull('published_revision')->orderBy('article_id')->pluck('article_id');
$result = [];
foreach ($ids as $id) {
$row = DB::transaction(function () use ($id) {
$a = DB::table('articles')->where('id', $id)->lockForUpdate()->first();
if (! $a || (int) $a->iStatus !== 1 || (int) $a->iYearLikeTarget !== 0
|| (int) $a->iYearLikeAdded !== 0 || $a->sYearLikeStartedAt !== null) {
return null;
}
$missingDefaults = (int) $a->i7like === 0 && (int) $a->i365like === 0;
$row = ['id' => $id, 'slug' => $a->sSlug, 'fill_defaults' => $missingDefaults,
'before' => ['seven_day' => (int) $a->i7like, 'yearly' => (int) $a->i365like],
'applied' => (bool) $this->option('apply')];
if ($this->option('apply')) {
$fields = ['iYearLikeTarget' => random_int(100, 500), 'sYearLikeStartedAt' => now()];
if ($missingDefaults) {
// Expired short-term campaigns are not restarted by changing article dates.
$fields['i7like'] = now()->lt(\Carbon\Carbon::parse($a->created_at)->addDays(7)) ? random_int(5, 35) : 0;
$fields['i365like'] = 50;
}
DB::table('articles')->where('id', $id)->update($fields);
$row['settings'] = $fields;
}
return $row;
}, 3);
if ($row) {
$result[] = $row;
}
}
$this->line(json_encode(['mode' => $this->option('apply') ? 'apply' : 'preview', 'count' => count($result),
'articles' => $result], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
return self::SUCCESS;
}
}