diff --git a/application/api/controller/Recommend.php b/application/api/controller/Recommend.php index 982e7d5..91d4f1e 100644 --- a/application/api/controller/Recommend.php +++ b/application/api/controller/Recommend.php @@ -4,7 +4,7 @@ namespace app\api\controller; use app\api\controller\Base; use think\Db; - +use app\common\Reviewer; /** * @title 推荐投稿人 * @description @@ -20,281 +20,48 @@ class Recommend extends Base } /** - * 投稿人列表 + * 自动推荐审稿人 * @param $messages 内容 * @param article_id 文章ID * @param */ - public function lists(){ + public function lists($aParam = []){ + //获取参数 - $aParam = $this->request->post(); - //分页 - $iSize = empty($aParam['size']) ? 10 : $aParam['size']; - $iPage = empty($aParam['page']) ? 1 : $aParam['page']; - $limit_start = ($iPage - 1) * $iSize; + $aParam = empty($aParam) ? $this->request->post() : $aParam; - if(empty($aParam['article_id'])){ - exit(json_encode(array('status' => 2,'msg' => 'Please select an article' ))); + $iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id']; + if(empty($iArticleId)){ + return json_encode(['status' => 2,'msg' => 'Please select the article to query']); } - - //查询文章 - $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' ))); - } - - //查询文章领域 - $aMajorId = Db::table('t_major_to_article')->where('article_id',$aArticle['article_id'])->column('major_id'); - if(empty($aMajorId)){ - exit(json_encode(array('status' => 1,'msg' => 'No field found for the article','data' => []))); - } - - //查询文章领域下的审稿人 - $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' => 1,'msg' => 'No reviewers in the field of the article were found','data' => []))); - } - $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]; - - //查询文章作者详情 作者和审稿人的机构不一致 - $aUserInfo = Db::name('user')->field('email')->where('user_id',$aArticle['user_id'])->find(); - $sEmail = empty($aUserInfo['email']) ? '' : $aUserInfo['email']; - if(!empty($sEmail)){ - $aAuthor = Db::name('article_author')->field('company')->where(['article_id'=>$aArticle['article_id'],'email' => $sEmail,'state' => 0])->find(); - if(empty($aAuthor)){ - exit(json_encode(array('status' => 1,'msg' => 'No detailed information about the author of the article was found','data' => []))); - } - } - //查询审稿人的补充信息表拼接参数 - $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)){ - - //查询该用户关联的文章ID - $aArticleId = Db::name('article_author')->where('email', $sEmail)->column('article_id'); - if(!empty($aArticleId)){ - //查询审稿人的邮箱 - $aUserEmail = Db::table('t_user')->where($aWhere)->column('email'); - - //查询作者不为同一篇文章的审稿人 - $aArticleId = array_unique($aArticleId); - $aAuthorEmail = Db::name('article_author')->where(['article_id'=>['not in',$aArticleId],'email' => ['in',$aUserEmail]])->column('email'); - if(!empty($aAuthorEmail)){ - 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'); + // $aParam['order'] = 't_user.major_times desc,t_user.right_rate desc,t_user.review_num asc';//排序字段 + $aParam['select'] = 't_user.user_id,t_user.account,t_user.email,t_user.realname,t_user.rs_num,t_user.rd_num,t_user.right_times,t_user.error_times,t_user.right_rate,t_user.error_rate,t_user.review_num,t_user.major_times,t_user.major_rate'; + //获取审稿人信息 + $oReviewer = new Reviewer; + $aResult = json_decode($oReviewer->get($aParam),true); + $aUser = empty($aResult['data']['lists']) ? [] : $aResult['data']['lists']; + if(empty($aUser)){ + return json_encode($aResult); + } + //查询审稿人详细信息 + $aUserId = array_column($aUser, 'user_id'); + $aReviewerInfo = Db::name('user_reviewer_info')->field('reviewer_id,technical,country,introduction,company,field')->whereIn('reviewer_id',$aUserId)->select(); + $aInfo = empty($aReviewerInfo) ? [] : array_column($aReviewerInfo, null,'reviewer_id'); + if(empty($aReviewerInfo)){ + return json_encode($aResult); + } + //数据处理 foreach ($aUser as $key => $value) { - $value += empty($aInfo[$value['user_id']]) ? [] : $aInfo[$value['user_id']]; + $value += empty($aReviewerInfo[$value['user_id']]) ? [] : $aReviewerInfo[$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 ?? []; + $aResult['data']['lists'] = $aUser; + return json_encode($aResult); } - }