This commit is contained in:
wangjinlei
2026-04-17 09:51:09 +08:00
parent e9a354c663
commit 4417a7ea28
5 changed files with 314 additions and 41 deletions

View File

@@ -0,0 +1,243 @@
<?php
namespace app\api\controller;
use think\Db;
class Country extends Base
{
public function __construct(\think\Request $request = null)
{
parent::__construct($request);
}
/**
* 国家列表(支持筛选 + 分页)
*
* 参数:
* keyword - 搜索中文名/英文名/code
* is_hot - 0/1 筛选热门
* partition - 分区 1/2/3
* page - 页码默认1
* per_page - 每页条数默认20最大100
*/
public function getList()
{
$keyword = trim($this->request->param('keyword', ''));
$isHot = $this->request->param('is_hot', '');
$partition = $this->request->param('partition', '');
$page = max(1, intval($this->request->param('page', 1)));
$perPage = max(1, min(intval($this->request->param('per_page', 20)), 100));
$query = Db::name('country');
if ($keyword !== '') {
$query->where('zh_name|en_name|code', 'like', '%' . $keyword . '%');
}
if ($isHot !== '' && $isHot !== '-1') {
$query->where('is_hot', intval($isHot));
}
if ($partition !== '' && $partition !== '-1') {
$query->where('partition', intval($partition));
}
$total = (clone $query)->count();
$list = $query
->order('is_hot desc, zh_name asc')
->page($page, $perPage)
->select();
return jsonSuccess([
'list' => $list,
'total' => $total,
'page' => $page,
'per_page' => $perPage,
'total_pages' => $total > 0 ? ceil($total / $perPage) : 0,
]);
}
/**
* 国家下拉选项(不分页,用于筛选器)
*
* 参数:
* hot_only - 1 则只返回热门国家
* partition - 按分区筛选
*/
public function getOptions()
{
$hotOnly = intval($this->request->param('hot_only', 0));
$partition = $this->request->param('partition', '');
$query = Db::name('country');
if ($hotOnly) {
$query->where('is_hot', 1);
}
if ($partition !== '' && $partition !== '-1') {
$query->where('partition', intval($partition));
}
$list = $query
->field('country_id, zh_name, en_name, code, is_hot, partition')
->order('is_hot desc, zh_name asc')
->select();
return jsonSuccess($list);
}
/**
* 国家详情
*/
public function getDetail()
{
$id = intval($this->request->param('country_id', 0));
if (!$id) {
return jsonError('country_id is required');
}
$row = Db::name('country')->where('country_id', $id)->find();
if (!$row) {
return jsonError('Country not found');
}
return jsonSuccess($row);
}
/**
* 新增国家
*/
public function add()
{
$data = $this->request->post();
$zhName = trim(isset($data['zh_name']) ? $data['zh_name'] : '');
$enName = trim(isset($data['en_name']) ? $data['en_name'] : '');
$code = strtoupper(trim(isset($data['code']) ? $data['code'] : ''));
if ($zhName === '' && $enName === '') {
return jsonError('zh_name 或 en_name 至少填一个');
}
if ($code !== '') {
$exists = Db::name('country')->where('code', $code)->find();
if ($exists) {
return jsonError('code 已存在: ' . $code);
}
}
$id = Db::name('country')->insertGetId([
'zh_name' => mb_substr($zhName, 0, 32),
'en_name' => mb_substr($enName, 0, 256),
'code' => mb_substr($code, 0, 32),
'is_hot' => intval(isset($data['is_hot']) ? $data['is_hot'] : 0),
'partition' => intval(isset($data['partition']) ? $data['partition'] : 2),
]);
return jsonSuccess(['country_id' => $id]);
}
/**
* 编辑国家
*/
public function edit()
{
$data = $this->request->post();
$id = intval(isset($data['country_id']) ? $data['country_id'] : 0);
if (!$id) {
return jsonError('country_id is required');
}
$row = Db::name('country')->where('country_id', $id)->find();
if (!$row) {
return jsonError('Country not found');
}
$update = [];
if (isset($data['zh_name'])) {
$update['zh_name'] = mb_substr(trim($data['zh_name']), 0, 32);
}
if (isset($data['en_name'])) {
$update['en_name'] = mb_substr(trim($data['en_name']), 0, 256);
}
if (isset($data['code'])) {
$code = strtoupper(trim($data['code']));
$dup = Db::name('country')->where('code', $code)->where('country_id', '<>', $id)->find();
if ($dup) {
return jsonError('code 已被其他国家使用: ' . $code);
}
$update['code'] = mb_substr($code, 0, 32);
}
if (isset($data['is_hot'])) {
$update['is_hot'] = intval($data['is_hot']);
}
if (isset($data['partition'])) {
$update['partition'] = intval($data['partition']);
}
if (empty($update)) {
return jsonError('没有可更新的字段');
}
Db::name('country')->where('country_id', $id)->update($update);
return jsonSuccess([]);
}
/**
* 删除国家
*/
public function delete()
{
$id = intval($this->request->param('country_id', 0));
if (!$id) {
return jsonError('country_id is required');
}
$used = Db::name('expert')->where('country_id', $id)->count();
if ($used > 0) {
return jsonError('该国家下有 ' . $used . ' 位专家关联,无法删除');
}
Db::name('country')->where('country_id', $id)->delete();
return jsonSuccess([]);
}
/**
* 批量设置热门
*/
public function setHot()
{
$ids = $this->request->param('country_ids', '');
$isHot = intval($this->request->param('is_hot', 1));
if (empty($ids)) {
return jsonError('country_ids is required');
}
$idArr = array_map('intval', explode(',', $ids));
$count = Db::name('country')->where('country_id', 'in', $idArr)->update(['is_hot' => $isHot]);
return jsonSuccess(['affected' => $count]);
}
/**
* 批量设置分区
*/
public function setPartition()
{
$ids = $this->request->param('country_ids', '');
$partition = intval($this->request->param('partition', 2));
if (empty($ids)) {
return jsonError('country_ids is required');
}
$idArr = array_map('intval', explode(',', $ids));
$count = Db::name('country')->where('country_id', 'in', $idArr)->update(['partition' => $partition]);
return jsonSuccess(['affected' => $count]);
}
}

View File

@@ -85,13 +85,14 @@ class ExpertFinder extends Base
}
/**
* 启动国家解析:找到第一个缺 country 的专家推入队列,
* 队列处理完后会自动链式找下一个,直到全部处理完。
* 只需调一次即可。
* 启动国家解析:同时启动两条链,分别用不同模型并行处理。
* 只需调一次,两条链各自链式执行直到全部处理完。
*/
public function batchFillCountry(){
$service = new ExpertFinderService();
$started = $service->enqueueNextCountryFill(0);
$chain1 = $service->enqueueNextCountryFill(0, 'FetchExpertCity', '');
$chain2 = $service->enqueueNextCountryFill(0, 'FetchExpertCity1', 'http://125.39.141.154:10002/v1/chat/completions');
$pending = Db::name('expert')
->where('affiliation', '<>', '')
@@ -100,7 +101,8 @@ class ExpertFinder extends Base
->count();
return jsonSuccess([
'started' => $started,
'chain1_started' => $chain1,
'chain2_started' => $chain2,
'pending' => $pending,
]);
}