From 2b4e1b1c14ddc239a36c117d349b6c8188b67595 Mon Sep 17 00:00:00 2001 From: Penryn <15158052130@163.com> Date: Mon, 14 Oct 2024 01:55:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(theme):=E4=BF=AE=E6=94=B9=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E8=89=B2=E6=9D=83=E9=99=90=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../themeServices/themePermissionService.go | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/services/themeServices/themePermissionService.go b/app/services/themeServices/themePermissionService.go index 57a99ec..ce3d786 100644 --- a/app/services/themeServices/themePermissionService.go +++ b/app/services/themeServices/themePermissionService.go @@ -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 } @@ -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 +}