From e851167b4a9b4061808badddabfe3b8727bde44b Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Tue, 10 Nov 2020 11:33:28 +0800 Subject: [PATCH] =?UTF-8?q?menu=E6=90=BA=E5=B8=A6=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/sys_base_menu.go | 38 ++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/server/service/sys_base_menu.go b/server/service/sys_base_menu.go index 4cfdc98b..e5e71242 100644 --- a/server/service/sys_base_menu.go +++ b/server/service/sys_base_menu.go @@ -50,15 +50,37 @@ func UpdateBaseMenu(menu model.SysBaseMenu) (err error) { upDateMap["icon"] = menu.Icon upDateMap["sort"] = menu.Sort - db := global.GVA_DB.Where("id = ?", menu.ID).Find(&oldMenu) - if oldMenu.Name != menu.Name { - if !errors.Is(global.GVA_DB.Where("id <> ? AND name = ?", menu.ID, menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) { - global.GVA_LOG.Debug("存在相同name修改失败") - return errors.New("存在相同name修改失败") + err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { + db := tx.Where("id = ?", menu.ID).Find(&oldMenu) + if oldMenu.Name != menu.Name { + if !errors.Is(tx.Where("id <> ? AND name = ?", menu.ID, menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) { + global.GVA_LOG.Debug("存在相同name修改失败") + return errors.New("存在相同name修改失败") + } } - } - err = global.GVA_DB.Delete(&model.SysBaseMenuParameter{}, "sys_base_menu_id = ?", menu.ID).Error - err = db.Updates(upDateMap).Error + err = tx.Delete(&model.SysBaseMenuParameter{}, "sys_base_menu_id = ?", menu.ID).Error + if err != nil { + global.GVA_LOG.Debug(err.Error()) + return err + } + if len(menu.Parameters) > 0 { + for k, _ := range menu.Parameters { + menu.Parameters[k].SysBaseMenuID = menu.ID + } + err = tx.Create(&menu.Parameters).Error + if err != nil { + global.GVA_LOG.Debug(err.Error()) + return err + } + } + + err = db.Updates(upDateMap).Error + if err != nil { + global.GVA_LOG.Debug(err.Error()) + return err + } + return nil + }) return err }