add
This commit is contained in:
@@ -62,7 +62,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
zap.String("req_id", reqID),
|
zap.String("req_id", reqID),
|
||||||
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), // 新增国旗参数日志
|
zap.String("flag", req.Flag),
|
||||||
)
|
)
|
||||||
|
|
||||||
// 开启数据库事务
|
// 开启数据库事务
|
||||||
@@ -98,6 +98,104 @@ func CreateHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// 唯一性校验 - 国家名称(排除已删除数据)
|
||||||
|
var nameCount int
|
||||||
|
err = tx.QueryRow(
|
||||||
|
"SELECT COUNT(*) FROM country_name WHERE name = $1 AND deleted = false",
|
||||||
|
req.Name,
|
||||||
|
).Scan(&nameCount)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ 国家名称唯一性校验失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("name", req.Name),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "系统错误,校验名称失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if nameCount > 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Warn("⚠️ 国家名称已存在(未删除数据)",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("name", req.Name),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusBadRequest, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "国家名称已存在,请更换名称",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 唯一性校验 - 国家编码(排除已删除数据)
|
||||||
|
var codeCount int
|
||||||
|
err = tx.QueryRow(
|
||||||
|
"SELECT COUNT(*) FROM country_code WHERE code = $1 AND deleted = false",
|
||||||
|
req.Code,
|
||||||
|
).Scan(&codeCount)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ 国家编码唯一性校验失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("code", req.Code),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "系统错误,校验编码失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if codeCount > 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Warn("⚠️ 国家编码已存在(未删除数据)",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("code", req.Code),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusBadRequest, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "国家编码已存在,请更换编码",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 唯一性校验 - 国旗(排除已删除数据,仅当提供了国旗参数时)
|
||||||
|
if req.Flag != "" {
|
||||||
|
var flagCount int
|
||||||
|
err = tx.QueryRow(
|
||||||
|
"SELECT COUNT(*) FROM country_flag WHERE flag = $1 AND deleted = false",
|
||||||
|
req.Flag,
|
||||||
|
).Scan(&flagCount)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Error("❌ 国旗唯一性校验失败",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("flag", req.Flag),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusInternalServerError, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "系统错误,校验国旗失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if flagCount > 0 {
|
||||||
|
tx.Rollback()
|
||||||
|
zap.L().Warn("⚠️ 国旗信息已存在(未删除数据)",
|
||||||
|
zap.String("req_id", reqID),
|
||||||
|
zap.String("flag", req.Flag),
|
||||||
|
)
|
||||||
|
c.JSON(http.StatusBadRequest, CreateResponse{
|
||||||
|
Success: false,
|
||||||
|
Message: "国旗信息已存在,请更换国旗",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 创建country主表记录
|
// 1. 创建country主表记录
|
||||||
var countryID string
|
var countryID string
|
||||||
err = tx.QueryRow("INSERT INTO country DEFAULT VALUES RETURNING id").Scan(&countryID)
|
err = tx.QueryRow("INSERT INTO country DEFAULT VALUES RETURNING id").Scan(&countryID)
|
||||||
@@ -151,7 +249,7 @@ func CreateHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 新增:插入国旗信息(如果提供)
|
// 4. 插入国旗信息(如果提供)
|
||||||
if req.Flag != "" {
|
if req.Flag != "" {
|
||||||
_, err = tx.Exec("INSERT INTO country_flag (country_id, flag) VALUES ($1, $2)", countryID, req.Flag)
|
_, err = tx.Exec("INSERT INTO country_flag (country_id, flag) VALUES ($1, $2)", countryID, req.Flag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -202,4 +300,4 @@ func CreateHandler(c *gin.Context) {
|
|||||||
CountryID: countryID,
|
CountryID: countryID,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -224,4 +224,4 @@ func ReadHandler(c *gin.Context) {
|
|||||||
Items: items,
|
Items: items,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user