Files
tougao/application/api/controller/Crontask.php

281 lines
11 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\Controller;
use think\Db;
class Crontask extends Controller
{
/**
* 批量处理审稿人审稿质量
* @return void
*/
public function reviewerQuality()
{
//查询文章状态
$aWhere = [
'state'=>['in',[3,5]]
];
$iCount = Db::name('article')->field('article_id,state')->where($aWhere)->count();
if(empty($iCount)){
$this->showMessage('未查询到满足要求的审稿人数据',2);
exit;
}
$iSize = 1000;
$iDealNum = ceil($iCount/$iSize);
//数据处理
Db::startTrans();
for ($iPage=1; $iPage <= $iDealNum; $iPage++) {
echo '======='.$iPage.'========='."\n";
$iStart = ($iPage - 1) * $iSize;
$aArticle = Db::name('article')->field('article_id,state')->where($aWhere)->limit($iStart,$iSize)->select();
if(empty($aArticle)){
continue;
}
//获取该文章审核人的信息
$aWhere = [
'article_id'=>['in',array_column($aArticle, 'article_id')],
'state'=>['in',[2,3]]
];
$aReviewer = Db::name('article_reviewer')->field('article_id,reviewer_id,state')->where($aWhere)->order('article_id asc')->select();
if(empty($aReviewer)){
continue;
}
//查询审核人信息
$aUserId = array_keys($aReviewer);
$aUser = Db::name('user')->field('user_id,rs_num,right_times,error_times')->whereIn('user_id',$aUserId)->select();
if(empty($aUser)){
continue;
}
$aUser = array_column($aUser, null,'user_id');
//处理数据并组装数据
$aArticleState = array_column($aArticle, 'state','article_id');
$aCase = ['right_times' => '', 'right_rate' => '','error_times' => '', 'error_rate' => ''];
$aToState = [2 => 3,3 => 5];//文章3拒稿5录用 审稿人2拒稿3通过
foreach ($aReviewer as $key => $item) {
//文章状态
$iArticleState = empty($aArticleState[$item['article_id']]) ? 0 : $aArticleState[$item['article_id']];
if(empty($iArticleState)){
continue;
}
if(empty($aToState[$item['state']])){
continue;
}
$aUserInfo = $aUser[$item['reviewer_id']] ?? [];
if(empty($aUserInfo)){
continue;
}
if($iArticleState == $aToState[$item['state']]){
$iTimes = $aUserInfo['right_times']+1;
$iRightNum = empty($aUserInfo['rs_num']) ? 0 : round($iTimes/$aUserInfo['rs_num']/100,2);
$aCase['right_times'] .= "WHEN {$item['reviewer_id']} THEN ";
$aCase['right_times'] .= "'{$iTimes}' ";
$aCase['right_rate'] .= "WHEN {$item['reviewer_id']} THEN ";
$aCase['right_rate'] .= "'{$iRightNum}' ";
}
if($iArticleState != $item['state']){
$iErrorTimes = $aUserInfo['error_times']+1;
$iErrorNum = empty($aUserInfo['rs_num']) ? 0 : round($iErrorTimes/$aUserInfo['rs_num']/100,2);
$aCase['error_times'] .= "WHEN {$item['reviewer_id']} THEN ";
$aCase['error_times'] .= "'{$iErrorTimes}' ";
$aCase['error_rate'] .= "WHEN {$item['reviewer_id']} THEN ";
$aCase['error_rate'] .= "'{$iErrorNum}' ";
}
$aId[] = $item['reviewer_id'];
}
//更新数据库
foreach ($aCase as $key => $value) {
if(empty($value)){
continue;
}
$aUpdate[$key] = Db::raw('CASE user_id '.$value.'END');
}
if(empty($aUpdate) || empty($aId)){
$this->showMessage('未查询到满足要求的审稿人数据4',2);
return false;
}
$result = Db::name('user')
->where('user_id', 'IN', $aId)
->limit(count($aId))
->update($aUpdate);
echo '---------'.$iPage.'--------'."\n";
}
Db::commit();
$this->showMessage('审稿人质量数据处理完成',1);
}
/**
* 批量处理审稿人活跃度/疲劳度[近两个月]
*
* @return void
*/
public function reviewerActivity(){
$sDate = strtotime(date('Y-m-d 00:00:00', strtotime('-2 month')));
//获取该文章审核人的信息
$aWhere = [
'ctime'=>['>',$sDate],
'state'=>['in',[1,2,3]]
];
$aReviewer = Db::name('article_reviewer')->field('reviewer_id,count(article_id) as review_num ')->where($aWhere)->order('reviewer_id asc')->group('reviewer_id')->select();
//查询审稿人数量不为0的审稿信息
$aUserWhere = [
// 'is_reviewer' => 1,
'review_num' => ['>',0]
];
$aUser = Db::name('user')->field('user_id,review_num')->where($aUserWhere)->select();
if(empty($aReviewer) && empty($aUser)){
$this->showMessage('未查询到待处理的审稿人数据',2);
exit;
}
$aUser = empty($aUser) ? [] : array_column($aUser, null,'user_id');
if(!empty($aReviewer)){
$aChunk = array_chunk($aReviewer, 100);
Db::startTrans();
foreach ($aChunk as $key => $item) { //数据分片操作
//需要更新的用户ID
$aUpdateId = [];
//SQL拼接
$aCase['review_num'] = 'CASE user_id ';
foreach ($item as $key => $value) {
//用户ID
$iUserId = $value['reviewer_id'];
//拼接更新语句
if(empty($aUser[$iUserId])){
//更新数量
$aCase['review_num'] .= "WHEN {$iUserId} THEN ";
$aCase['review_num'] .= "'{$value['review_num']}' ";
$aUpdateId[] = $iUserId;
continue;
}
//审核数量无变化,跳过更新
if($aUser[$iUserId]['review_num'] == $value['review_num']){
unset($aUser[$iUserId]);
continue;
}
//审核数量有,变化更新数量
$aCase['review_num'] .= "WHEN {$iUserId} THEN ";
$aCase['review_num'] .= "'{$value['review_num']}' ";
$aUpdateId[] = $iUserId;
unset($aUser[$iUserId]);
}
//SQL拼接最后结尾
$aCase['review_num'] .= 'END';
//执行更新
if(empty($aUpdateId)){
continue;
}
$result = Db::name('user')
->where(['user_id' => ['in',$aUpdateId]])
->update([
'review_num' => Db::raw($aCase['review_num']),
]);
}
Db::commit();
// $this->showMessage('更新审稿人审核数量成功',1);
}
if(!empty($aUser)){
$aChunk = array_chunk($aUser, 100);
Db::startTrans();
foreach ($aChunk as $key => $item) { //数据分片操作
$aUserId = array_column($item, 'user_id');
if(empty($aUserId)){
continue;
}
$result = Db::name('user')->where(['is_reviewer' => 1,'user_id' => ['in',$aUserId]])
->limit(count($aUserId))
->update([
'review_num' => 0,
]);
// if(!$result){
// $this->showMessage('清空审稿人审核数量失败:'.Db::getLastSql(),2);
// }
}
Db::commit();
// $this->showMessage('清空审稿人审核数量成功:'.Db::getLastSql(),1);
}
$this->showMessage('批量更新审稿人审核数量成功',1);
}
/**
* @title 审稿人拒绝审稿[超过七日默认自动拒绝审稿]
*
*/
public function refuseReviewArticle(){
$sDate = strtotime(date('Y-m-d 00:00:00', strtotime('-7 day')));
//获取该文章审核人的信息
$aWhere = [
'ctime'=>['<',$sDate],
'state'=> 5
];
//统计每个审稿人未审稿的数量
$aReviewerNum = Db::name('article_reviewer')->field('reviewer_id,count(art_rev_id) as num')->where($aWhere)->group('reviewer_id')->order('reviewer_id asc')->select();
if(empty($aReviewerNum)){
$this->showMessage('未查询到需要超过七日的稿件',2);
exit;
}
//更新超过七日未审核的数据
Db::startTrans();
//更新审稿人未审稿的数量
$aChunkReviewerNum = array_chunk($aReviewerNum, 100);
foreach ($aChunkReviewerNum as $key => $value) {
$aId = array_column($value, 'reviewer_id');
if(empty($aId)){
continue;
}
$aWhere['reviewer_id']=['in',$aId];
$iCount = array_sum(array_column($value, 'num'));
Db::name('article_reviewer')->where($aWhere)->limit($iCount)->update(['state' => 4]);
$aCase['review_num'] = 'CASE user_id ';
foreach ($value as $key => $item) {
//审核数量有,变化更新数量
$aCase['review_num'] .= "WHEN {$item['reviewer_id']} THEN ";
$aCase['review_num'] .= Db::raw("review_num + {$item['num']}")." ";
$aUpdateId[] = $item['reviewer_id'];
}
//SQL拼接最后结尾
$aCase['review_num'] .= 'END';
//执行更新
$result = Db::name('user')
->where(['user_id' => ['in',$aUpdateId]])
->update([
'rd_num' => Db::raw($aCase['review_num']),
]);
}
Db::commit();
$this->showMessage('批量更新超过七日未审稿的状态成功',2);
}
/**
*
* 格式化信息输出
*
* @access public
* @return void
* @author huangpu
* @date 2018.09.28
* @param $[message] [<显示信息>]
* @param $[status] [<输出信息1成功2失败>]
*/
private function showMessage($message, $status = 1) {
if ($status == 1) {
echo "[SUCCESS]";
} else {
echo "[ERROR]";
}
echo date("Y-m-d H:i:s") . " " . $message . "\n";
}
}