Skip to content

Commit

Permalink
fix(theme):修改新增主题色权限使用批量保存
Browse files Browse the repository at this point in the history
  • Loading branch information
Penryn committed Oct 13, 2024
1 parent b6f13a2 commit 2b4e1b1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app/services/themeServices/themePermissionService.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func AddThemePermission(themeID int, reqStudentIDs []string) ([]string, error) {
}
}

err = database.DB.Save(&permissions).Error
// 使用批量保存
err = savePermissionsInBatches(permissions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -208,3 +209,22 @@ func containThemeID(themeIDs []int, id int) bool {
}
return false
}

const batchSize = 100 // 每次保存 100 条记录

func savePermissionsInBatches(permissions []models.ThemePermission) error {
totalPermissions := len(permissions)
for i := 0; i < totalPermissions; i += batchSize {
end := i + batchSize
if end > totalPermissions {
end = totalPermissions
}

// 保存当前批次
err := database.DB.Save(permissions[i:end]).Error
if err != nil {
return err
}
}
return nil
}

0 comments on commit 2b4e1b1

Please sign in to comment.