package handler import ( "fmt" "net/http" "time" "soul-api/internal/database" "soul-api/internal/model" "github.com/gin-gonic/gin" ) // DBPersonList GET /api/db/persons 管理端-@提及人物列表 func DBPersonList(c *gin.Context) { var rows []model.Person if err := database.DB().Order("name ASC").Find(&rows).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "persons": rows}) } // DBPersonSave POST /api/db/persons 管理端-新增或更新人物 func DBPersonSave(c *gin.Context) { var body struct { PersonID string `json:"personId"` Name string `json:"name"` Label string `json:"label"` CkbApiKey string `json:"ckbApiKey"` // 存客宝密钥,留空则 fallback 全局 Key } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": "请求体无效"}) return } if body.Name == "" { c.JSON(http.StatusOK, gin.H{"success": false, "error": "name 必填"}) return } if body.PersonID == "" { body.PersonID = fmt.Sprintf("%s_%d", body.Name, time.Now().UnixMilli()) } db := database.DB() var existing model.Person if db.Where("person_id = ?", body.PersonID).First(&existing).Error == nil { existing.Name = body.Name existing.Label = body.Label existing.CkbApiKey = body.CkbApiKey db.Save(&existing) c.JSON(http.StatusOK, gin.H{"success": true, "person": existing}) return } p := model.Person{PersonID: body.PersonID, Name: body.Name, Label: body.Label, CkbApiKey: body.CkbApiKey} if err := db.Create(&p).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true, "person": p}) } // DBPersonDelete DELETE /api/db/persons?personId=xxx 管理端-删除人物 func DBPersonDelete(c *gin.Context) { pid := c.Query("personId") if pid == "" { c.JSON(http.StatusOK, gin.H{"success": false, "error": "缺少 personId"}) return } if err := database.DB().Where("person_id = ?", pid).Delete(&model.Person{}).Error; err != nil { c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"success": true}) }