diff --git a/backend/country/src/logic/delete.go b/backend/country/src/logic/delete.go index 697a16f..1ee98c8 100644 --- a/backend/country/src/logic/delete.go +++ b/backend/country/src/logic/delete.go @@ -1 +1,167 @@ -package logic \ No newline at end of file +package logic + +import ( + "net/http" + "country/db" + "time" + "github.com/google/uuid" + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +// DeleteRequest 删除请求参数结构 +type DeleteRequest struct { + CountryID string `json:"country_id" binding:"required"` // 国家ID,必填 +} + +// DeleteResponse 删除响应结构 +type DeleteResponse struct { + Success bool `json:"success"` // 操作是否成功 + Message string `json:"message"` // 提示信息 +} + +// DeleteHandler 处理国家删除逻辑(软删除) +func DeleteHandler(c *gin.Context) { + startTime := time.Now() + reqID := c.Request.Header.Get("X-DeleteRequest-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 DeleteRequest + // 绑定并验证请求参数 + if err := c.ShouldBindJSON(&req); err != nil { + zap.L().Warn("⚠️ 请求参数验证失败", + zap.String("req_id", reqID), + zap.Error(err), + ) + c.JSON(http.StatusBadRequest, DeleteResponse{ + Success: false, + Message: "请求参数错误:country_id为必填项", + }) + return + } + + zap.L().Debug("✅ 请求参数验证通过", + zap.String("req_id", reqID), + zap.String("country_id", req.CountryID), + ) + + // 开启数据库事务 + tx, err := db.DB.Begin() + if err != nil { + zap.L().Error("❌ 事务开启失败", + zap.String("req_id", reqID), + zap.Error(err), + ) + c.JSON(http.StatusInternalServerError, DeleteResponse{ + Success: false, + Message: "系统错误,请稍后重试", + }) + return + } + + // 延迟处理panic情况 + 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, DeleteResponse{ + Success: false, + Message: "系统错误,请稍后重试", + }) + } + }() + + // 3.1 更新country表 + _, err = tx.Exec("UPDATE country SET deleted = TRUE WHERE id = $1", req.CountryID) + if err != nil { + tx.Rollback() + zap.L().Error("❌ country表更新失败", + zap.String("req_id", reqID), + zap.String("country_id", req.CountryID), + zap.Error(err), + ) + c.JSON(http.StatusInternalServerError, DeleteResponse{ + Success: false, + Message: "删除国家记录失败", + }) + return + } + + // 3.2 更新name表 + _, err = tx.Exec("UPDATE name SET deleted = TRUE WHERE country_id = $1", req.CountryID) + if err != nil { + tx.Rollback() + zap.L().Error("❌ name表更新失败", + zap.String("req_id", reqID), + zap.String("country_id", req.CountryID), + zap.Error(err), + ) + c.JSON(http.StatusInternalServerError, DeleteResponse{ + Success: false, + Message: "删除名称信息失败", + }) + return + } + + // 3.3 更新code表 + _, err = tx.Exec("UPDATE code SET deleted = TRUE WHERE country_id = $1", req.CountryID) + if err != nil { + tx.Rollback() + zap.L().Error("❌ code表更新失败", + zap.String("req_id", reqID), + zap.String("country_id", req.CountryID), + zap.Error(err), + ) + c.JSON(http.StatusInternalServerError, DeleteResponse{ + Success: false, + Message: "删除代码信息失败", + }) + return + } + + // 提交事务 + if err := tx.Commit(); 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, DeleteResponse{ + Success: false, + Message: "数据提交失败,请稍后重试", + }) + return + } + + // 记录请求处理耗时 + duration := time.Since(startTime) + zap.L().Info("✅ 国家删除请求处理完成", + zap.String("req_id", reqID), + zap.String("country_id", req.CountryID), + zap.Duration("duration", duration), + ) + + // 返回成功响应 + c.JSON(http.StatusOK, DeleteResponse{ + Success: true, + Message: "删除成功", + }) +} \ No newline at end of file diff --git a/backend/country/src/main.go b/backend/country/src/main.go index 19d130e..3643fe5 100644 --- a/backend/country/src/main.go +++ b/backend/country/src/main.go @@ -21,9 +21,10 @@ func main() { gin.SetMode(gin.ReleaseMode) r := gin.Default() - // 创建接口 r.POST("/country/create", logic.CreateHandler) - zap.L().Info("✅ 登录接口注册完成: POST /country/create") + zap.L().Info("✅ 创建接口注册完成: POST /country/create") + r.POST("/country/delete", logic.DeleteHandler) + zap.L().Info("✅ 删除接口注册完成: POST /country/delete") // 启动服务,监听80端口 zap.L().Info("✅ 服务启动在80端口") diff --git a/backend/提示词条.md b/backend/提示词条.md index a71e27c..ebbb53e 100644 --- a/backend/提示词条.md +++ b/backend/提示词条.md @@ -1,6 +1,17 @@ +--- 分析这个项目,在 create.go 中完成以下需求: 1、接收 name,code 两个参数。 2、确认提交的 name,code 两个参数不能为空,如果有空,则返回提示。 -3、第二步通过后,在 country 表中,通过: "INSERT INTO "country" DEFAULT VALUES RETURNING id" 获得ID。 +3、第二步通过后,在 country 表中,通过: "INSERT INTO country DEFAULT VALUES RETURNING id" 获得ID。 4、通过 3 中的 id,开启事务保存到 name 和 code 的表中。 +--- +分析这个项目,在 delete.go 中完成以下需求: + +1、接收 country_id 参数。 +2、确认提交的 country_id 参数不能为空,如果有空,则返回提示。 +3、开启事务处理以下逻辑: +3.1、把 country 中,country.id==req.country_id 的 deleted 字段更新为true。 +3.2、把 name 中,name.country_id==req.country_id 的 deleted 字段更新为true。 +3.3、把 code code.country_id==req.country_id 的 deleted 字段更新为true。 +--- \ No newline at end of file