From 01643b2816a3c9b90be134b7f6ee8009a5fd7e1e Mon Sep 17 00:00:00 2001 From: feng626 <57284900+feng626@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:45:05 +0800 Subject: [PATCH] perf: Optimize FTP sync function for better performance and readability (#7479) --- backend/app/service/ftp.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/backend/app/service/ftp.go b/backend/app/service/ftp.go index bd9048afd..c9b0c3ccc 100644 --- a/backend/app/service/ftp.go +++ b/backend/app/service/ftp.go @@ -103,23 +103,28 @@ func (f *FtpService) Sync() error { if err != nil { return err } + + currentData := make(map[string]model.Ftp) + for _, item := range listsInDB { + currentData[item.User] = item + } + sameData := make(map[string]struct{}) for _, item := range lists { - for _, itemInDB := range listsInDB { - if item.User == itemInDB.User { - sameData[item.User] = struct{}{} - if item.Path != itemInDB.Path || item.Status != itemInDB.Status { - _ = ftpRepo.Update(itemInDB.ID, map[string]interface{}{"path": item.Path, "status": item.Status}) + if itemInDB, ok := currentData[item.User]; ok { + sameData[item.User] = struct{}{} + if item.Path != itemInDB.Path || item.Status != itemInDB.Status { + if err := ftpRepo.Update(itemInDB.ID, map[string]interface{}{"path": item.Path, "status": item.Status}); err != nil { + return err } - break + } + } else { + if err := ftpRepo.Create(&model.Ftp{User: item.User, Path: item.Path, Status: item.Status}); err != nil { + return err } } } - for _, item := range lists { - if _, ok := sameData[item.User]; !ok { - _ = ftpRepo.Create(&model.Ftp{User: item.User, Path: item.Path, Status: item.Status}) - } - } + for _, item := range listsInDB { if _, ok := sameData[item.User]; !ok { _ = ftpRepo.Update(item.ID, map[string]interface{}{"status": "deleted"})