add
This commit is contained in:
@@ -15,6 +15,7 @@ type UpdateRequest struct {
|
||||
CountryID string `json:"country_id" binding:"required"` // 国家ID,必填
|
||||
Name string `json:"name"` // 国家名称,可选
|
||||
Code string `json:"code"` // 国家代码,可选
|
||||
Flag string `json:"flag"` // 国旗信息,可选(新增字段)
|
||||
}
|
||||
|
||||
// UpdateResponse 更新响应结构
|
||||
@@ -54,16 +55,16 @@ func UpdateHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证name和code不能同时为空
|
||||
if req.Name == "" && req.Code == "" {
|
||||
// 验证name、code和flag不能同时为空
|
||||
if req.Name == "" && req.Code == "" && req.Flag == "" {
|
||||
zap.L().Warn("⚠️ 请求参数验证失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", req.CountryID),
|
||||
zap.String("reason", "name和code不能同时为空"),
|
||||
zap.String("reason", "name、code和flag不能同时为空"),
|
||||
)
|
||||
c.JSON(http.StatusBadRequest, UpdateResponse{
|
||||
Success: false,
|
||||
Message: "请求参数错误:name和code不能同时为空",
|
||||
Message: "请求参数错误:name、code和flag不能同时为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -73,6 +74,7 @@ func UpdateHandler(c *gin.Context) {
|
||||
zap.String("country_id", req.CountryID),
|
||||
zap.String("name", req.Name),
|
||||
zap.String("code", req.Code),
|
||||
zap.String("flag", req.Flag), // 新增国旗参数日志
|
||||
)
|
||||
|
||||
// 开启数据库事务
|
||||
@@ -111,7 +113,7 @@ func UpdateHandler(c *gin.Context) {
|
||||
|
||||
// 如果name不为空,更新name表
|
||||
if req.Name != "" {
|
||||
_, err = tx.Exec("UPDATE country_name SET name = $1 WHERE country_id = $2", req.Name, req.CountryID)
|
||||
_, err = tx.Exec("UPDATE country_name SET name = $1, updated_at = CURRENT_TIMESTAMP WHERE country_id = $2", req.Name, req.CountryID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("❌ country_name表更新失败",
|
||||
@@ -133,7 +135,7 @@ func UpdateHandler(c *gin.Context) {
|
||||
|
||||
// 如果code不为空,更新code表
|
||||
if req.Code != "" {
|
||||
_, err = tx.Exec("UPDATE country_code SET code = $1 WHERE country_id = $2", req.Code, req.CountryID)
|
||||
_, err = tx.Exec("UPDATE country_code SET code = $1, updated_at = CURRENT_TIMESTAMP WHERE country_id = $2", req.Code, req.CountryID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("❌ country_code表更新失败",
|
||||
@@ -153,6 +155,52 @@ func UpdateHandler(c *gin.Context) {
|
||||
)
|
||||
}
|
||||
|
||||
// 新增:如果flag不为空,更新flag表
|
||||
if req.Flag != "" {
|
||||
// 先检查是否存在国旗记录
|
||||
var flagExists bool
|
||||
err = tx.QueryRow("SELECT EXISTS(SELECT 1 FROM country_flag WHERE country_id = $1 AND deleted = FALSE)", req.CountryID).Scan(&flagExists)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("❌ 检查国旗记录存在性失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", req.CountryID),
|
||||
zap.Error(err),
|
||||
)
|
||||
c.JSON(http.StatusInternalServerError, UpdateResponse{
|
||||
Success: false,
|
||||
Message: "检查国旗信息失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if flagExists {
|
||||
// 存在则更新
|
||||
_, err = tx.Exec("UPDATE country_flag SET flag = $1, updated_at = CURRENT_TIMESTAMP WHERE country_id = $2", req.Flag, req.CountryID)
|
||||
} else {
|
||||
// 不存在则插入新记录
|
||||
_, err = tx.Exec("INSERT INTO country_flag (country_id, flag) VALUES ($1, $2)", req.CountryID, req.Flag)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("❌ country_flag表更新/插入失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", req.CountryID),
|
||||
zap.Error(err),
|
||||
)
|
||||
c.JSON(http.StatusInternalServerError, UpdateResponse{
|
||||
Success: false,
|
||||
Message: "更新国旗信息失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
zap.L().Debug("📝 flag表更新/插入成功",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", req.CountryID),
|
||||
)
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
@@ -181,4 +229,4 @@ func UpdateHandler(c *gin.Context) {
|
||||
Success: true,
|
||||
Message: "更新成功",
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user