add
This commit is contained in:
@@ -15,6 +15,7 @@ type UpdateRequest struct {
|
|||||||
CountryID string `json:"country_id" binding:"required"` // 国家ID,必填
|
CountryID string `json:"country_id" binding:"required"` // 国家ID,必填
|
||||||
Name string `json:"name"` // 国家名称,可选
|
Name string `json:"name"` // 国家名称,可选
|
||||||
Code string `json:"code"` // 国家代码,可选
|
Code string `json:"code"` // 国家代码,可选
|
||||||
|
Flag string `json:"flag"` // 国旗信息,可选(新增字段)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateResponse 更新响应结构
|
// UpdateResponse 更新响应结构
|
||||||
@@ -54,16 +55,16 @@ func UpdateHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证name和code不能同时为空
|
// 验证name、code和flag不能同时为空
|
||||||
if req.Name == "" && req.Code == "" {
|
if req.Name == "" && req.Code == "" && req.Flag == "" {
|
||||||
zap.L().Warn("⚠️ 请求参数验证失败",
|
zap.L().Warn("⚠️ 请求参数验证失败",
|
||||||
zap.String("req_id", reqID),
|
zap.String("req_id", reqID),
|
||||||
zap.String("country_id", req.CountryID),
|
zap.String("country_id", req.CountryID),
|
||||||
zap.String("reason", "name和code不能同时为空"),
|
zap.String("reason", "name、code和flag不能同时为空"),
|
||||||
)
|
)
|
||||||
c.JSON(http.StatusBadRequest, UpdateResponse{
|
c.JSON(http.StatusBadRequest, UpdateResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
Message: "请求参数错误:name和code不能同时为空",
|
Message: "请求参数错误:name、code和flag不能同时为空",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -73,6 +74,7 @@ func UpdateHandler(c *gin.Context) {
|
|||||||
zap.String("country_id", req.CountryID),
|
zap.String("country_id", req.CountryID),
|
||||||
zap.String("name", req.Name),
|
zap.String("name", req.Name),
|
||||||
zap.String("code", req.Code),
|
zap.String("code", req.Code),
|
||||||
|
zap.String("flag", req.Flag), // 新增国旗参数日志
|
||||||
)
|
)
|
||||||
|
|
||||||
// 开启数据库事务
|
// 开启数据库事务
|
||||||
@@ -111,7 +113,7 @@ func UpdateHandler(c *gin.Context) {
|
|||||||
|
|
||||||
// 如果name不为空,更新name表
|
// 如果name不为空,更新name表
|
||||||
if req.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 {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
zap.L().Error("❌ country_name表更新失败",
|
zap.L().Error("❌ country_name表更新失败",
|
||||||
@@ -133,7 +135,7 @@ func UpdateHandler(c *gin.Context) {
|
|||||||
|
|
||||||
// 如果code不为空,更新code表
|
// 如果code不为空,更新code表
|
||||||
if req.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 {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
zap.L().Error("❌ country_code表更新失败",
|
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 {
|
if err := tx.Commit(); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|||||||
Reference in New Issue
Block a user