188 lines
4.7 KiB
Go
188 lines
4.7 KiB
Go
package logic4country
|
||
|
||
import (
|
||
"asset_assistant/db"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/google/uuid"
|
||
"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 country_name SET deleted = TRUE WHERE country_id = $1", req.CountryID)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
zap.L().Error("❌ country_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 country_code SET deleted = TRUE WHERE country_id = $1", req.CountryID)
|
||
if err != nil {
|
||
tx.Rollback()
|
||
zap.L().Error("❌ country_code表更新失败",
|
||
zap.String("req_id", reqID),
|
||
zap.String("country_id", req.CountryID),
|
||
zap.Error(err),
|
||
)
|
||
c.JSON(http.StatusInternalServerError, DeleteResponse{
|
||
Success: false,
|
||
Message: "删除代码信息失败",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 新增:3.4 更新flag表(软删除国旗信息)
|
||
_, err = tx.Exec("UPDATE country_flag SET deleted = TRUE WHERE country_id = $1", req.CountryID)
|
||
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, DeleteResponse{
|
||
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()
|
||
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: "删除成功",
|
||
})
|
||
} |