Files
tougao/application/api/controller/Recommend.php
2025-04-18 15:30:55 +08:00

137 lines
5.4 KiB
PHP

<?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;
if(empty($aParam['article_id'])){
exit(json_encode(array('status' => 2,'msg' => 'Please select an article' )));
}
//查询文章
$aArticle = Db::table('t_major_to_article')->field('article_id')->where('article_id',$aParam['article_id'])->find();
if(empty($aArticle)){
exit(json_encode(array('status' => 3,'msg' => 'No articles requiring review were 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' => 4,'msg' => 'No field found for the article' )));
}
// //根据文章领域获取满足该领域父类
// $aMajorId = Db::table('t_major')->field('major_id,major_title,pid')->whereIn('major_id', $aMajorArticle)->where('pid != 1')->select();
$aParentId = Db::table('t_major')->where(['major_id' => ['in',$aMajorId]])->column('major_id,pid');
$aMajorId = $this->getChildIds(array_values($aParentId));
//数据处理拼接父类ID为1的领域
foreach ($aParentId as $key => $value) {
if(in_array($value, [0,1])){
array_push($aMajorId,$key);
continue;
}
}
//查询所属该领域的审稿人
if(empty($aMajorId)){
exit(json_encode(array('status' => 5,'msg' => 'No reviewers in the field of the article were found' )));
}
$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];
if(!empty($aParam['email'])){//根据邮箱搜索
$aWhere['email'] = ['like',"%" . $aParam["email"] . "%"];
}
if(!empty($aParam['field'])){//根据领域搜索
$aReviewerWhere['reviewer_id'] = ['in',$aMajorUser];
$aReviewerWhere['field'] = ['like',"%" . $aParam["field"] . "%"];
$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];
//统计数量
$iCount = Db::table('t_user')->where($aWhere)->count();
if(empty($iCount)){
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => 0,'lists' => []])));
}
$sOrder = 'review_num asc,right_rate desc';
$aUser = Db::table('t_user')->field('user_id,account,email,realname,rs_num,right_times,error_times,right_rate,error_rate,review_num')->where($aWhere)->order($sOrder)->limit($limit_start, $iSize)->select();
//查询审稿人详细信息
$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'] = $value['right_rate']*100;
$value['error_rate'] = $value['error_rate']*100;
$aUser[$key] = $value;
}
exit(json_encode(array('status' => 1,'msg' => '','data' => ['total' => $iCount,'lists' => $aUser])));
//
}
/**
* 递归获取某个分类下的所有子分类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 ?? [];
}
}