Files
tougao/application/api/controller/UserFieldAi.php
2026-05-20 11:58:10 +08:00

93 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\controller;
use think\Db;
use think\Validate;
use app\common\UserFieldAiService;
/**
* 用户主领域 AI 总结(写入 t_user_reviewer_info.field_ai
*
* POST startChain 启动链式队列(扫描全部符合条件的用户)
* POST processOne 同步处理单个 user_id调试
* GET preview 预览某用户是否 eligible 及上下文摘要
*/
class UserFieldAi extends Base
{
/**
* 启动链式处理。需 worker: php think queue:work --queue UserFieldAi
*/
public function startChain()
{
$force = intval($this->request->param('force', 0)) === 1;
$delay = max(0, intval($this->request->param('delay', 1)));
$svc = new UserFieldAiService();
$started = $svc->startChain($force, $delay);
return jsonSuccess([
'started' => $started,
'queue' => UserFieldAiService::QUEUE_NAME,
'force' => $force,
'msg' => $started ? 'chain enqueued' : 'no pending users',
]);
}
/**
* 同步处理单个用户(不调队列)。
*/
public function processOne()
{
$userId = intval($this->request->param('user_id', 0));
$force = intval($this->request->param('force', 0)) === 1;
if ($userId <= 0) {
return jsonError('user_id required');
}
$svc = new UserFieldAiService();
$result = $svc->processUser($userId, $force);
if (empty($result['ok'])) {
return jsonError(isset($result['error']) ? $result['error'] : 'failed');
}
return jsonSuccess($result);
}
/**
* 预览:是否满足条件、当前 field_ai 状态。
*/
public function preview()
{
$userId = intval($this->request->param('user_id', 0));
if ($userId <= 0) {
return jsonError('user_id required');
}
$svc = new UserFieldAiService();
$svc->ensureReviewerInfoRow($userId);
$uri = Db::name('user_reviewer_info')->where('reviewer_id', $userId)->find();
return jsonSuccess([
'user_id' => $userId,
'has_articles' => $svc->hasSubmittedArticles($userId),
'profile_complete' => $svc->isReviewerProfileComplete($uri),
'eligible' => $svc->isEligible($userId, $uri),
'field_ai' => $uri ? (string) $uri['field_ai'] : '',
'field_ai_status' => $uri ? intval($uri['field_ai_status']) : 0,
'field_ai_utime' => $uri ? intval($uri['field_ai_utime']) : 0,
'field_ai_status_text' => $this->statusLabel($uri ? intval($uri['field_ai_status']) : 0),
]);
}
private function statusLabel($status)
{
$map = [
UserFieldAiService::STATUS_PENDING => 'pending',
UserFieldAiService::STATUS_DONE => 'done',
UserFieldAiService::STATUS_INSUFFICIENT => 'insufficient',
UserFieldAiService::STATUS_FAILED => 'failed',
];
return isset($map[$status]) ? $map[$status] : 'unknown';
}
}