This commit is contained in:
Alex-larget
2026-03-24 18:45:32 +08:00
parent dcb7961945
commit f3d74ce94a
68 changed files with 2461 additions and 2535 deletions

View File

@@ -523,6 +523,7 @@ func DBBookAction(c *gin.Context) {
TargetPartTitle string `json:"targetPartTitle"`
TargetChapterTitle string `json:"targetChapterTitle"`
ID string `json:"id"`
NewID string `json:"newId"`
Title string `json:"title"`
Content string `json:"content"`
Price *float64 `json:"price"`
@@ -762,12 +763,46 @@ func DBBookAction(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()})
return
}
err = db.Model(&model.Chapter{}).Where("id = ?", body.ID).Updates(updates).Error
newID := strings.TrimSpace(body.NewID)
idChanged := newID != "" && newID != body.ID
if idChanged {
var existed int64
if err := db.Model(&model.Chapter{}).Where("id = ? AND id <> ?", newID, body.ID).Count(&existed).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()})
return
}
if existed > 0 {
c.JSON(http.StatusOK, gin.H{"success": false, "error": "章节ID已存在请换一个"})
return
}
updates["id"] = newID
}
if idChanged {
err = db.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&model.Chapter{}).Where("id = ?", body.ID).Updates(updates).Error; err != nil {
return err
}
// 同步历史关联数据,避免改 ID 后订单/阅读记录断链
if err := tx.Model(&model.Order{}).
Where("product_type = ? AND product_id = ?", "section", body.ID).
Update("product_id", newID).Error; err != nil {
return err
}
if err := tx.Table("reading_progress").
Where("section_id = ?", body.ID).
Update("section_id", newID).Error; err != nil {
return err
}
return nil
})
} else {
err = db.Model(&model.Chapter{}).Where("id = ?", body.ID).Updates(updates).Error
}
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "error": err.Error()})
return
}
cache.InvalidateChapterContentByID(body.ID)
cache.InvalidateChapterContent(existing.MID)
cache.InvalidateBookParts()
InvalidateChaptersByPartCache()
cache.InvalidateBookCache()