同步
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user