Files
tougao/application/api/controller/Recommend.php
2025-04-23 09:41:12 +08:00

291 lines
14 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 app\api\controller\Base;
use think\Db;
/**
* @title 推荐投稿人
* @description
*/
class Recommend extends Base
{
public function __construct(\think\Request $request = null) {
parent::__construct($request);
}
/**
* 投稿人列表
* @param $messages 内容
* @param article_id 文章ID
* @param
*/
public function lists(){
//获取参数
$aParam = $this->request->post();
//分页
$iSize = empty($aParam['size']) ? 10 : $aParam['size'];
$iPage = empty($aParam['page']) ? 1 : $aParam['page'];
$limit_start = ($iPage - 1) * $iSize;
// $aParam['article_id'] = 5972;
if(empty($aParam['article_id'])){
exit(json_encode(array('status' => 2,'msg' => 'Please select an article' )));
}
//查询文章
$aArticle = Db::table('t_article')->field('article_id,user_id')->where('article_id',$aParam['article_id'])->find();
if(empty($aArticle)){
exit(json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' )));
}
//查询用户信息
$aUserInfo = Db::name('user')->field('email')->where('user_id',$aArticle['user_id'])->find();
$sEmail = empty($aUserInfo['email']) ? '' : $aUserInfo['email'];
//查询文章作者详情 作者和审稿人的机构不一致
$aAuthor = Db::name('article_author')->field('company')->where(['article_id'=>$aArticle['article_id'],'email' => $sEmail])->find();
if(empty($aAuthor)){
exit(json_encode(array('status' => 4,'msg' => 'No detailed information about the author of the article was found' )));
}
//查询文章领域
$aMajorId = Db::table('t_major_to_article')->where('article_id',$aArticle['article_id'])->column('major_id');
if(empty($aMajorId)){
exit(json_encode(array('status' => 5,'msg' => 'No field found for the article' )));
}
//查询文章领域下的审稿人
$aMajorUser = Db::name('major_to_user')->whereIn('major_id', $aMajorId)->where('state',0)->order('major_id asc')->column('user_id');
if(empty($aMajorUser)){
exit(json_encode(array('status' => 6,'msg' => 'No reviewers in the field of the article were found' )));
}
$aMajorUser = array_unique($aMajorUser);
//查询审稿人是否为劣迹审稿人
$aBlack = Db::name('user_reviewer_black')->whereIn('reviewer_id', $aMajorUser)->where('state',1)->column('reviewer_id');
if(!empty($aBlack)){
$aMajorUser = array_diff($aMajorUser, $aBlack);
}
//条件拼接
$aWhere = ['state' => 0,'is_reviewer' => 1];
//查询审稿人的补充信息表拼接参数
$aReviewerWhere = [];
if(!empty($aParam['field'])){//根据领域搜索
$aReviewerWhere['field'] = ['like',"%" . $aParam["field"] . "%"];
}
if(!empty($aAuthor['company'])){//作者单位
$aReviewerWhere['company'] = ['<>',$aAuthor['company']];
}
if(!empty($aReviewerWhere)){
$aReviewerWhere['reviewer_id'] = ['in',$aMajorUser];
$aReviewerInfo = Db::name('user_reviewer_info')->where($aReviewerWhere)->column('reviewer_id');
if(empty($aReviewerInfo)){
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
}
$aMajorUser = array_intersect($aMajorUser,$aReviewerInfo);
}
//查用户信息
$aWhere['user_id'] = ['in',$aMajorUser];
//查询作者关联的文章
if(!empty($sEmail)){
$aArticleId = Db::name('article_author')->where('email', $sEmail)->column('article_id');
if(!empty($aArticleId)){
$aUserEmail = Db::table('t_user')->where($aWhere)->column('email');
$aAuthorEmail = Db::name('article_author')->where(['article_id'=>['not in',$aArticleId],'email' => ['in',$aUserEmail]])->column('email');
if(empty($aAuthorEmail)){
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
}
unset($aWhere['user_id']);
$aWhere['email'] = ['in',array_unique($aAuthorEmail)];
}
}
$aEmailWhere = [];
if(!empty($aParam['email'])){//根据邮箱搜索
$aEmailWhere['email'] = ['like',"%" . $aParam["email"] . "%"];
}
//统计数量
$iCount = Db::table('t_user')->where($aWhere)->where($aEmailWhere)->count();
if(empty($iCount)){
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
}
//判断页数是否超过最大分页限制
$iPageNum = ceil($iCount/$iSize);
if($iPage > $iPageNum){
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
}
//查询数据
$sOrder = 'major_times desc,right_rate desc,review_num asc';//排序字段
$aUser = Db::table('t_user')->field('user_id,account,email,realname,rs_num,rd_num,right_times,error_times,right_rate,error_rate,review_num,major_times,major_rate')->where($aWhere)->where($aEmailWhere)->order($sOrder)->paginate([
'list_rows' => $iSize,
'page' => $iPage,
'simple' => true, // 简单分页模式
'path' => 'javascript:;' // 禁用URL生成
]);
//查询审稿人详细信息
$aUser = empty($aUser->items()) ? [] : $aUser->items();
$aUserId = array_column($aUser, 'user_id');
$aInfo = Db::name('user_reviewer_info')->field('reviewer_id,technical,country,introduction,company,field')->whereIn('reviewer_id',$aUserId)->select();
$aInfo = empty($aInfo) ? [] : array_column($aInfo, null,'reviewer_id');
foreach ($aUser as $key => $value) {
$value += empty($aInfo[$value['user_id']]) ? [] : $aInfo[$value['user_id']];
$value['right_rate'] = intval($value['right_rate']*100);
$value['error_rate'] = intval($value['error_rate']*100);
$value['major_rate'] = intval($value['major_rate']*100);
$aUser[$key] = $value;
}
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => $iCount,'lists' => $aUser])));
}
// public function lists(){
// //获取参数
// $aParam = $this->request->post();
// //分页
// $iSize = empty($aParam['size']) ? 10 : $aParam['size'];
// $iPage = empty($aParam['page']) ? 1 : $aParam['page'];
// $aParam['article_id'] = 5972;
// if(empty($aParam['article_id'])){
// exit(json_encode(array('status' => 2,'msg' => 'Please select an article' )));
// }
// //查询文章
// $aArticle = Db::table('t_article')->field('article_id,user_id')->where('article_id',$aParam['article_id'])->find();
// if(empty($aArticle)){
// exit(json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' )));
// }
// //查询文章作者详情 作者和审稿人的机构不一致
// $aAuthor = Db::name('article_author')->field('company')->where('article_id',$aArticle['article_id'])->find();
// if(empty($aAuthor)){
// exit(json_encode(array('status' => 4,'msg' => 'No detailed information about the author of the article was found' )));
// }
// //查询文章领域
// $aMajorId = Db::table('t_major_to_article')->where('article_id',$aArticle['article_id'])->column('major_id');
// if(empty($aMajorId)){
// exit(json_encode(array('status' => 5,'msg' => 'No field found for the article' )));
// }
// //查询该领域下的审稿人
// $aMajorUser = Db::name('major_to_user')->whereIn('major_id', $aMajorId)->where('state',0)->order('major_id asc')->column('user_id');
// if(empty($aMajorUser)){
// exit(json_encode(array('status' => 6,'msg' => 'No reviewers in the field of the article were found' )));
// }
// $aMajorUser = array_unique($aMajorUser);
// //查询审稿人是否为劣迹审稿人
// $aBlack = Db::name('user_reviewer_black')->whereIn('reviewer_id', $aMajorUser)->where('state',1)->column('reviewer_id');
// if(!empty($aBlack)){
// $aMajorUser = array_diff($aMajorUser, $aBlack);
// }
// //条件拼接
// $aWhere = ['user.state' => 0,'user.is_reviewer' => 1,'user_id' => ['in',$aMajorUser]];
// if(!empty($aParam['email'])){//根据邮箱搜索
// $aWhere['user.email'] = ['like',"%" . $aParam["email"] . "%"];
// }
// if(!empty($aParam['field'])){//根据领域搜索
// $aWhere['reviewer.field'] = ['like',"%" . $aParam["field"] . "%"];
// }
// if(!empty($aAuthor['company'])){
// $aWhere['reviewer.company'] = ['<>',$aAuthor['company']];
// }
// //统计数量
// $iCount = Db::table('t_user')
// ->alias('user')->join('t_user_reviewer_info reviewer','user.user_id = reviewer.reviewer_id')
// ->where($aWhere)->count();
// if(empty($iCount)){
// exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
// }
// //判断页数是否超过最大分页限制
// $iPageNum = ceil($iCount/$iSize);
// if($iPage > $iPageNum){
// exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
// }
// //查询数据
// $sOrder = 'major_times desc,right_rate desc,review_num asc';//排序字段
// $aUser = Db::table('t_user')
// ->alias('user')
// ->field('user.user_id,user.account,user.email,user.realname,user.rs_num,user.rd_num,user.right_times,user.error_times,user.right_rate,user.error_rate,user.review_num,user.major_rate,user.major_times,reviewer.reviewer_id,reviewer.technical,reviewer.country,reviewer.introduction,reviewer.company,reviewer.field')
// ->join('t_user_reviewer_info reviewer','user.user_id = reviewer.reviewer_id')
// ->where($aWhere)->order($sOrder)
// ->paginate([
// 'list_rows' => $iSize,
// 'page' => $iPage,
// 'simple' => true, // 简单分页模式
// 'path' => 'javascript:;' // 禁用URL生成
// ])->each(function($item) { // 数据格式化
// $item['right_rate'] = intval($item['right_rate']*100);
// $item['error_rate'] = intval($item['error_rate']*100);
// $item['major_rate'] = intval($item['major_rate']*100);
// return $item;
// });
// // echo Db::getLastSql();exit;
// $aUser = empty($aUser->items()) ? [] : $aUser->items();
// exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => $iCount ?? 0,'lists' => $aUser,'last_use_id' => $iLastUserId ?? 0])));
// //
// }
/**
* 游标分页查询
* @param array $ids whereIn的ID列表
* @param mixed $lastValue 上一页最后一条的排序字段值
* @param int $lastId 上一页最后一条的ID防重复
* @param int $pageSize 每页数量
*/
// public static function cursorPaginate($aWhere = [], $lastCursor = [], $pageSize = 30) {
// $query = Db::name('user')->field('user_id,account,email,realname,rs_num,rd_num,right_times,error_times,right_rate,error_rate,review_num,major_rate,major_times')
// ->where($aWhere)
// ->order('major_times DESC, right_rate DESC, review_num ASC, user_id DESC');
// // 游标处理逻辑
// if (!empty($lastCursor)) {
// $query->where(function($q) use ($lastCursor) {
// $q->where('major_times', '<', $lastCursor['major_times'])
// ->whereOr(function($subQ) use ($lastCursor) {
// $subQ->where('major_times', '=', $lastCursor['major_times'])
// ->where('right_rate', '<', $lastCursor['right_rate'])
// ->whereOr(function($sQ) use ($lastCursor) {
// $sQ->where('right_rate', '=', $lastCursor['right_rate'])
// ->where('review_num', '>', $lastCursor['review_num'])
// ->whereOr(function($ssQ) use ($lastCursor) {
// $ssQ->where('review_num', '=', $lastCursor['review_num'])
// ->where('user_id', '<', $lastCursor['user_id']);
// });
// });
// });
// });
// }
// $result = $query->limit($pageSize)->select();
// return $result;
// }
/**
* 递归获取某个分类下的所有子分类ID
* @param int $parentId 父级ID
* @param array &$result 结果存储
*/
private function getChildIds($parentId, &$result = []) {
$parentId = array_unique(array_diff($parentId, [1]));
$children = Db::table('t_major')->whereIn('pid', $parentId)->where('major_state',0)->order('major_id asc')->column('major_id');
$result = array_merge($result,$children);
if (!empty($children)) {
$this->getChildIds($children, $result); // 递归查询子级
}
return $result ?? [];
}
}