Files
tougao/application/api/job/RecommendReviewer.php
2025-07-02 15:09:38 +08:00

99 lines
4.3 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\job;
use think\queue\Job;
use app\common\QueueJob;
use app\common\Reviewer;
class RecommendReviewer
{
// 推荐审稿人任务
public function fire(Job $job, $data)
{
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/RecommendReviewer_'.date('Ymd').'.log';
file_put_contents($sLogPath,'-----------Queue job started-----------' . json_encode($data)."\n",FILE_APPEND);
//获取任务ID
$iLogId = 0;
try {
//实例化
$oQueueJob = new QueueJob;
$sMsg = '获取推荐审稿人信息成功';
// 记录异常日志
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true);
$aParam = [
'job_id' => empty($aJob['id']) ? 'RecommendReviewer'.rand(100, 999) : $aJob['id'],
'job_class' => get_class($this),
'status' => 0,
'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
];
$iLogId = $oQueueJob->addLog($aParam);
// 步骤1上传素材图片
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
if (!empty($iArticleId)) {
$aParam = ['article_id' => $iArticleId,'page' => 1,'size' => empty($data['size']) ? 5 : $data['size']];
//获取推荐审稿人信息
$oReviewer = new Reviewer;
$aResult = json_decode($oReviewer->recommend($aParam),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '' : $aResult['msg'];
//数量
$iCount = empty($aResult['data']['total']) ? 0 : $aResult['data']['total'];
//推荐数量
$iSize = empty($aResult['data']['size']) ? 0 : $aResult['data']['size'];
//判断是否给期刊管理者发邮件【数据库的审稿数量小于推荐数量】
if($iCount < $iSize){
$aSendEmailResult = json_decode($oReviewer->emailForEditor($aParam),true);
$sMsg .= ';';
$sMsg .= empty($aSendEmailResult['msg']) ? '发送邮件入队成功' : $aSendEmailResult['msg'];
}
//推荐审稿人数据
$aResult = empty($aResult['data']['lists']) ? [] : $aResult['data']['lists'];
if(empty($aResult)){
$sMsg .= ';';
$sMsg .= 'No qualified reviewers were selected';
}
if(!empty($aResult)){
$aParam = ['article_id' => $iArticleId,'reviewer_id' => array_column($aResult, 'reviewer_id')];
$aResult = json_decode($oReviewer->add($aParam),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg .= ';';
$sMsg .= empty($aResult['msg']) ? 'Reviewer data insertion failed' : $aResult['msg'];
//发送邀请审稿人审稿邮件
if($iStatus == 1){
$aResult = json_decode($oReviewer->email($aParam),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg .= ';';
$sMsg .= empty($aResult['msg']) ? 'Reviewer data insertion failed' : $aResult['msg'];
}
}
}
$job->delete();
//更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
//记录日志
file_put_contents($sLogPath,'-----------Queue job end---------'."\n\n\n",FILE_APPEND);
} catch (\Exception $e) {
//实例化
$oQueueJob = new QueueJob;
//更新任务状态
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage();
$aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
$job->delete();
}finally {
gc_collect_cycles(); // 强制垃圾回收
}
}
}