Compare commits
2 Commits
c8bb3d4ebd
...
abb1c8500c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abb1c8500c | ||
|
|
edade96d4a |
@@ -11,9 +11,10 @@ class AddCountryPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _AddCountryPageState extends State<AddCountryPage> {
|
class _AddCountryPageState extends State<AddCountryPage> {
|
||||||
// 输入控制器
|
// 输入控制器 - 新增国旗控制器
|
||||||
final TextEditingController _nameController = TextEditingController();
|
final TextEditingController _nameController = TextEditingController();
|
||||||
final TextEditingController _codeController = TextEditingController();
|
final TextEditingController _codeController = TextEditingController();
|
||||||
|
final TextEditingController _flagController = TextEditingController(); // 新增
|
||||||
|
|
||||||
// 加载状态
|
// 加载状态
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
@@ -21,7 +22,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
// 表单验证键
|
// 表单验证键
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
// 创建国家
|
// 创建国家 - 调整请求数据
|
||||||
Future<void> _createCountry() async {
|
Future<void> _createCountry() async {
|
||||||
if (!_formKey.currentState!.validate()) {
|
if (!_formKey.currentState!.validate()) {
|
||||||
return;
|
return;
|
||||||
@@ -32,7 +33,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 获取用户ID
|
// 获取用户ID(保持不变)
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
final userId = prefs.getString('user_id');
|
final userId = prefs.getString('user_id');
|
||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
@@ -43,7 +44,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 准备请求数据
|
// 准备请求数据 - 新增flag字段
|
||||||
final baseUrl = HostUtils().currentHost;
|
final baseUrl = HostUtils().currentHost;
|
||||||
const path = '/country/create';
|
const path = '/country/create';
|
||||||
final url = '$baseUrl$path';
|
final url = '$baseUrl$path';
|
||||||
@@ -51,9 +52,10 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
final requestData = {
|
final requestData = {
|
||||||
'name': _nameController.text.trim(),
|
'name': _nameController.text.trim(),
|
||||||
'code': _codeController.text.trim(),
|
'code': _codeController.text.trim(),
|
||||||
|
'flag': _flagController.text.trim(), // 新增国旗参数
|
||||||
};
|
};
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求(保持不变)
|
||||||
final dio = Dio();
|
final dio = Dio();
|
||||||
final response = await dio.post(
|
final response = await dio.post(
|
||||||
url,
|
url,
|
||||||
@@ -61,7 +63,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
options: Options(headers: {'Content-Type': 'application/json'}),
|
options: Options(headers: {'Content-Type': 'application/json'}),
|
||||||
);
|
);
|
||||||
|
|
||||||
// 处理响应
|
// 处理响应(保持不变)
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final result = response.data;
|
final result = response.data;
|
||||||
if (result['success'] == true) {
|
if (result['success'] == true) {
|
||||||
@@ -81,6 +83,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
|
// 异常处理(保持不变)
|
||||||
String errorMessage = '网络请求失败';
|
String errorMessage = '网络请求失败';
|
||||||
if (e.response != null) {
|
if (e.response != null) {
|
||||||
errorMessage = '请求失败: ${e.response?.statusCode}';
|
errorMessage = '请求失败: ${e.response?.statusCode}';
|
||||||
@@ -106,7 +109,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 显示对话框
|
// 显示对话框(保持不变)
|
||||||
void _showDialog(String title, String content, [VoidCallback? onConfirm]) {
|
void _showDialog(String title, String content, [VoidCallback? onConfirm]) {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -159,7 +162,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
key: _formKey,
|
key: _formKey,
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
// 国家名称输入框
|
// 国家名称输入框(保持不变)
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _nameController,
|
controller: _nameController,
|
||||||
style: TextStyle(color: theme.colorScheme.onSurface),
|
style: TextStyle(color: theme.colorScheme.onSurface),
|
||||||
@@ -180,7 +183,7 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 24),
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
// 国家代码输入框
|
// 国家代码输入框(保持不变)
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _codeController,
|
controller: _codeController,
|
||||||
style: TextStyle(color: theme.colorScheme.onSurface),
|
style: TextStyle(color: theme.colorScheme.onSurface),
|
||||||
@@ -199,6 +202,22 @@ class _AddCountryPageState extends State<AddCountryPage> {
|
|||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
|
// 新增国旗输入框
|
||||||
|
TextFormField(
|
||||||
|
controller: _flagController,
|
||||||
|
style: TextStyle(color: theme.colorScheme.onSurface),
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: '国旗URL',
|
||||||
|
hintText: '请输入国旗图片URL(可选)',
|
||||||
|
prefixIcon: Icon(
|
||||||
|
Icons.flag,
|
||||||
|
color: theme.colorScheme.secondary,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
// 国旗为可选字段,不添加验证器
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
205
frontend/asset_assistant/lib/pages/create.go
Normal file
205
frontend/asset_assistant/lib/pages/create.go
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
package logic4country
|
||||||
|
|
||||||
|
import (
|
||||||
|
"asset_assistant/db"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateRequest 注册请求参数结构
|
||||||
|
type CreateRequest struct {
|
||||||
|
Name string `json:"name" binding:"required"` // 国家名称,必填
|
||||||
|
Code string `json:"code" binding:"required"` // 国家代码,必填
|
||||||
|
Flag string `json:"flag"` // 国旗信息,可选
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateResponse 注册响应结构
|
||||||
|
type CreateResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data CreateData `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateData 响应数据结构
|
||||||
|
type CreateData struct {
|
||||||
|
CountryID string `json:"country_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateHandler 处理国家创建逻辑
|
||||||
|
func CreateHandler(c *gin.Context) {
|
||||||
|
startTime := time.Now()
|
||||||
|
reqID := c.Request.Header.Get("X-RegisterRequest-ID")
|
||||||
|
if reqID == "" {
|
||||||
|
reqID = uuid.New().String()
|
||||||
|
zap.L().Debug("✨ 生成新的请求ID", zap.String("req_id", reqID))
|
||||||
|
}
|
||||||
|
|
||||||
|
zap.L().Info("📥 收到国家创建请求",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("path", c.Request.URL.Path),
|
||||||
|
zap.String("method", c.Request.Method),
|
||||||
|
)
|
||||||
|
|
||||||
|
var req CreateRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
zap.L().Warn("⚠️ 请求参数验证失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.Error(err),
|
||||||
|
zap.Any("request_body", c.Request.Body),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusBadRequest, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "请求参数错误:name和code为必填项",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
zap.L().Debug("✅ 请求参数验证通过",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("name", req.Name),
|
||||||
|
zap.String("code", req.Code),
|
||||||
|
zap.String("flag", req.Flag), // 新增国旗参数日志
|
||||||
|
)
|
||||||
|
|
||||||
|
// 开启数据库事务
|
||||||
|
tx, err := db.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Error("❌ 事务开启失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "系统错误,请稍后重试",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
if err := tx.Rollback(); err != nil {
|
||||||
|
zap.L().Error("💥 panic后事务回滚失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
zap.L().Error("💥 事务处理发生panic",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.Any("recover", r),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "系统错误,请稍后重试",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 1. 创建country主表记录
|
||||||
|
var countryID string
|
||||||
|
err = tx.QueryRow("INSERT INTO country DEFAULT VALUES RETURNING id").Scan(&countryID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ country表插入失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "创建国家记录失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
zap.L().Debug("📝 country表插入成功",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
)
|
||||||
|
|
||||||
|
// 2. 插入国家名称
|
||||||
|
_, err = tx.Exec("INSERT INTO country_name (country_id, name) VALUES ($1, $2)", countryID, req.Name)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ country_name表插入失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "保存名称信息失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 插入国家代码
|
||||||
|
_, err = tx.Exec("INSERT INTO country_code (country_id, code) VALUES ($1, $2)", countryID, req.Code)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ country_code表插入失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "保存代码信息失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 新增:插入国旗信息(如果提供)
|
||||||
|
if req.Flag != "" {
|
||||||
|
_, err = tx.Exec("INSERT INTO country_flag (country_id, flag) VALUES ($1, $2)", countryID, req.Flag)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ country_flag表插入失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "保存国旗信息失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
zap.L().Debug("📝 country_flag表插入成功",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提交事务
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ 事务提交失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "数据提交失败,请稍后重试",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
duration := time.Since(startTime)
|
||||||
|
zap.L().Info("✅ 国家创建请求处理完成",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("country_id", countryID),
|
||||||
|
zap.Duration("duration", duration),
|
||||||
|
)
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, CreateResponse{
|
||||||
|
Success: true,
|
||||||
|
Message: "创建成功",
|
||||||
|
Data: CreateData{
|
||||||
|
CountryID: countryID,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,227 +0,0 @@
|
|||||||
package logic4country
|
|
||||||
|
|
||||||
import (
|
|
||||||
"asset_assistant/db"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ReadRequest 读取请求参数结构
|
|
||||||
type ReadRequest struct {
|
|
||||||
CountryID string `form:"country_id"` // 国家ID,可选
|
|
||||||
Name string `form:"name"` // 国家名称,可选
|
|
||||||
Code string `form:"code"` // 国家代码,可选
|
|
||||||
Flag string `form:"flag"` // 国旗信息,新增可选参数
|
|
||||||
Page string `form:"page"` // 页码,可选
|
|
||||||
PageSize string `form:"page_size"` // 每页条数,可选
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadData 读取响应数据结构
|
|
||||||
type ReadData struct {
|
|
||||||
Total int64 `json:"total"` // 总条数
|
|
||||||
Page int `json:"page"` // 当前页码
|
|
||||||
PageSize int `json:"page_size"` // 每页条数
|
|
||||||
Items []CountryInfoViewItem `json:"items"` // 数据列表
|
|
||||||
}
|
|
||||||
|
|
||||||
// CountryInfoViewItem 视图数据项结构,新增国旗字段
|
|
||||||
type CountryInfoViewItem struct {
|
|
||||||
CountryID string `json:"country_id"` // 国家ID
|
|
||||||
Name string `json:"name"` // 国家名称
|
|
||||||
Code string `json:"code"` // 国家代码
|
|
||||||
Flag string `json:"flag"` // 国旗信息,新增字段
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadResponse 读取响应结构
|
|
||||||
type ReadResponse struct {
|
|
||||||
Success bool `json:"success"` // 操作是否成功
|
|
||||||
Message string `json:"message"` // 提示信息
|
|
||||||
Data ReadData `json:"data"` // 响应数据
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadHandler 处理国家信息查询逻辑
|
|
||||||
func ReadHandler(c *gin.Context) {
|
|
||||||
startTime := time.Now()
|
|
||||||
// 获取或生成请求ID
|
|
||||||
reqID := c.Request.Header.Get("X-ReadRequest-ID")
|
|
||||||
if reqID == "" {
|
|
||||||
reqID = uuid.New().String()
|
|
||||||
zap.L().Debug("✨ 生成新的请求ID", zap.String("req_id", reqID))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 记录请求接收日志
|
|
||||||
zap.L().Info("📥 收到国家查询请求",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.String("path", c.Request.URL.Path),
|
|
||||||
zap.String("method", c.Request.Method),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 绑定请求参数
|
|
||||||
var req ReadRequest
|
|
||||||
if err := c.ShouldBindQuery(&req); err != nil {
|
|
||||||
zap.L().Warn("⚠️ 请求参数解析失败",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.Error(err),
|
|
||||||
)
|
|
||||||
c.JSON(http.StatusBadRequest, ReadResponse{
|
|
||||||
Success: false,
|
|
||||||
Message: "请求参数格式错误",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理分页参数默认值
|
|
||||||
page, err := strconv.Atoi(req.Page)
|
|
||||||
if err != nil || page < 1 {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, err := strconv.Atoi(req.PageSize)
|
|
||||||
if err != nil || pageSize < 1 {
|
|
||||||
pageSize = 20
|
|
||||||
}
|
|
||||||
|
|
||||||
zap.L().Debug("✅ 请求参数验证通过",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.String("country_id", req.CountryID),
|
|
||||||
zap.String("name", req.Name),
|
|
||||||
zap.String("code", req.Code),
|
|
||||||
zap.String("flag", req.Flag), // 新增国旗查询参数日志
|
|
||||||
zap.Int("page", page),
|
|
||||||
zap.Int("page_size", pageSize),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 构建查询条件和参数
|
|
||||||
whereClauses := []string{}
|
|
||||||
args := []interface{}{}
|
|
||||||
paramIndex := 1
|
|
||||||
|
|
||||||
if req.CountryID != "" {
|
|
||||||
whereClauses = append(whereClauses, "country_id = $"+strconv.Itoa(paramIndex))
|
|
||||||
args = append(args, req.CountryID)
|
|
||||||
paramIndex++
|
|
||||||
}
|
|
||||||
if req.Name != "" {
|
|
||||||
whereClauses = append(whereClauses, "name LIKE $"+strconv.Itoa(paramIndex))
|
|
||||||
args = append(args, "%"+req.Name+"%")
|
|
||||||
paramIndex++
|
|
||||||
}
|
|
||||||
if req.Code != "" {
|
|
||||||
whereClauses = append(whereClauses, "code LIKE $"+strconv.Itoa(paramIndex))
|
|
||||||
args = append(args, "%"+req.Code+"%")
|
|
||||||
paramIndex++
|
|
||||||
}
|
|
||||||
// 新增国旗查询条件
|
|
||||||
if req.Flag != "" {
|
|
||||||
whereClauses = append(whereClauses, "flag LIKE $"+strconv.Itoa(paramIndex))
|
|
||||||
args = append(args, "%"+req.Flag+"%")
|
|
||||||
paramIndex++
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建基础SQL,新增flag字段查询
|
|
||||||
baseSQL := "SELECT country_id, name, code, flag FROM country_info_view"
|
|
||||||
countSQL := "SELECT COUNT(*) FROM country_info_view"
|
|
||||||
if len(whereClauses) > 0 {
|
|
||||||
whereStr := " WHERE " + strings.Join(whereClauses, " AND ")
|
|
||||||
baseSQL += whereStr
|
|
||||||
countSQL += whereStr
|
|
||||||
}
|
|
||||||
|
|
||||||
// 计算分页偏移量
|
|
||||||
offset := (page - 1) * pageSize
|
|
||||||
|
|
||||||
// 拼接分页SQL
|
|
||||||
querySQL := fmt.Sprintf("%s ORDER BY country_id LIMIT $%d OFFSET $%d", baseSQL, paramIndex, paramIndex+1)
|
|
||||||
args = append(args, pageSize, offset)
|
|
||||||
|
|
||||||
// 查询总条数
|
|
||||||
var total int64
|
|
||||||
countArgs := args[:len(args)-2] // 排除分页参数
|
|
||||||
err = db.DB.QueryRow(countSQL, countArgs...).Scan(&total)
|
|
||||||
if err != nil {
|
|
||||||
zap.L().Error("❌ 查询总条数失败",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.Error(err),
|
|
||||||
)
|
|
||||||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
|
||||||
Success: false,
|
|
||||||
Message: "查询数据失败,请稍后重试",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 执行分页查询
|
|
||||||
rows, err := db.DB.Query(querySQL, args...)
|
|
||||||
if err != nil {
|
|
||||||
zap.L().Error("❌ 分页查询失败",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.Error(err),
|
|
||||||
)
|
|
||||||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
|
||||||
Success: false,
|
|
||||||
Message: "查询数据失败,请稍后重试",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
// 处理查询结果,新增flag字段扫描
|
|
||||||
var items []CountryInfoViewItem
|
|
||||||
for rows.Next() {
|
|
||||||
var item CountryInfoViewItem
|
|
||||||
if err := rows.Scan(&item.CountryID, &item.Name, &item.Code, &item.Flag); err != nil {
|
|
||||||
zap.L().Error("❌ 解析查询结果失败",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.Error(err),
|
|
||||||
)
|
|
||||||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
|
||||||
Success: false,
|
|
||||||
Message: "数据处理失败,请稍后重试",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
items = append(items, item)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查行迭代过程中是否发生错误
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
zap.L().Error("❌ 行迭代错误",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.Error(err),
|
|
||||||
)
|
|
||||||
c.JSON(http.StatusInternalServerError, ReadResponse{
|
|
||||||
Success: false,
|
|
||||||
Message: "查询数据失败,请稍后重试",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 记录请求处理耗时
|
|
||||||
duration := time.Since(startTime)
|
|
||||||
zap.L().Info("✅ 国家查询请求处理完成",
|
|
||||||
zap.String("req_id", reqID),
|
|
||||||
zap.Int64("total", total),
|
|
||||||
zap.Int("page", page),
|
|
||||||
zap.Int("page_size", pageSize),
|
|
||||||
zap.Duration("duration", duration),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 返回成功响应
|
|
||||||
c.JSON(http.StatusOK, ReadResponse{
|
|
||||||
Success: true,
|
|
||||||
Message: "查询成功",
|
|
||||||
Data: ReadData{
|
|
||||||
Total: total,
|
|
||||||
Page: page,
|
|
||||||
PageSize: pageSize,
|
|
||||||
Items: items,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user