Files
tougao/application/api/controller/Country.php
wangjinlei d2a6c3ff30 1
2026-04-20 10:56:25 +08:00

251 lines
7.3 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\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));
$where = [];
if ($keyword !== '') {
$where[] = ['zh_name|en_name|code', 'like', '%' . $keyword . '%'];
}
if ($isHot !== '' && $isHot !== '-1') {
$where[] = ['is_hot', '=', intval($isHot)];
}
if ($partition !== '' && $partition !== '-1') {
$where[] = ['partition', '=', intval($partition)];
}
$countQuery = Db::name('country');
foreach ($where as $w) {
$countQuery->where($w[0], $w[1], $w[2]);
}
$total = $countQuery->count();
$listQuery = Db::name('country');
foreach ($where as $w) {
$listQuery->where($w[0], $w[1], $w[2]);
}
$list = $listQuery
->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]);
}
}