From e2d0c62efc39eefe794d3d18f4d8212bf9f6fbff Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Mon, 31 Aug 2020 18:03:54 +0800 Subject: [PATCH] =?UTF-8?q?errors.Is=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=B5=8C=E5=A5=97=E5=B1=82=E7=BA=A7=E7=AE=80=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/exa_breakpoint_continue.go | 13 ++++++------- server/service/exa_simple_uploader.go | 3 +-- server/service/sys_authority.go | 12 ++++-------- server/service/sys_base_menu.go | 3 +-- server/service/sys_dictionary.go | 4 ++-- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/server/service/exa_breakpoint_continue.go b/server/service/exa_breakpoint_continue.go index 27164aab..cfc3b988 100644 --- a/server/service/exa_breakpoint_continue.go +++ b/server/service/exa_breakpoint_continue.go @@ -22,16 +22,15 @@ func FindOrCreateFile(fileMd5 string, fileName string, chunkTotal int) (err erro cfile.FileMd5 = fileMd5 cfile.FileName = fileName cfile.ChunkTotal = chunkTotal - notHaveSameMd5Finish := errors.Is(global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", fileMd5, true).First(&file).Error, gorm.ErrRecordNotFound) - if notHaveSameMd5Finish { + + if errors.Is(global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", fileMd5, true).First(&file).Error, gorm.ErrRecordNotFound) { err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error return err, file - } else { - cfile.IsFinish = true - cfile.FilePath = file.FilePath - err = global.GVA_DB.Create(&cfile).Error - return err, cfile } + cfile.IsFinish = true + cfile.FilePath = file.FilePath + err = global.GVA_DB.Create(&cfile).Error + return err, cfile } // @title CreateFileChunk diff --git a/server/service/exa_simple_uploader.go b/server/service/exa_simple_uploader.go index f888bd9f..25777c3d 100644 --- a/server/service/exa_simple_uploader.go +++ b/server/service/exa_simple_uploader.go @@ -28,8 +28,7 @@ func MergeFileMd5(md5 string, fileName string) (err error) { finishDir := "./finish/" dir := "./chunk/" + md5 //如果文件上传成功 不做后续操作 通知成功即可 - notFinish := errors.Is(global.GVA_DB.First(&model.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, true).Error, gorm.ErrRecordNotFound) - if !notFinish { + if !errors.Is(global.GVA_DB.First(&model.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, true).Error, gorm.ErrRecordNotFound) { return nil } diff --git a/server/service/sys_authority.go b/server/service/sys_authority.go index dc6b86df..1ef4c921 100644 --- a/server/service/sys_authority.go +++ b/server/service/sys_authority.go @@ -19,8 +19,7 @@ import ( func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) { var authorityBox model.SysAuthority - notHas := errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) - if !notHas { + if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) { return errors.New("存在相同角色id"), auth } err = global.GVA_DB.Create(&auth).Error @@ -36,8 +35,7 @@ func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAut func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, authority model.SysAuthority) { var authorityBox model.SysAuthority - notHas := errors.Is(global.GVA_DB.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) - if !notHas { + if !errors.Is(global.GVA_DB.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) { return errors.New("存在相同角色id"), authority } copyInfo.Authority.Children = []model.SysAuthority{} @@ -79,12 +77,10 @@ func UpdateAuthority(auth model.SysAuthority) (err error, authority model.SysAut // 删除角色 func DeleteAuthority(auth *model.SysAuthority) (err error) { - notHas := errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysUser{}).Error, gorm.ErrRecordNotFound) - if !notHas { + if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysUser{}).Error, gorm.ErrRecordNotFound) { return errors.New("此角色有用户正在使用禁止删除") } - notHas = errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) - if !notHas { + if !errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) { return errors.New("此角色存在子角色不允许删除") } db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth) diff --git a/server/service/sys_base_menu.go b/server/service/sys_base_menu.go index eee9e525..787acc96 100644 --- a/server/service/sys_base_menu.go +++ b/server/service/sys_base_menu.go @@ -52,8 +52,7 @@ func UpdateBaseMenu(menu model.SysBaseMenu) (err error) { db := global.GVA_DB.Where("id = ?", menu.ID).Find(&oldMenu) if oldMenu.Name != menu.Name { - notSame := errors.Is(global.GVA_DB.Where("id <> ? AND name = ?", menu.ID, menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) - if !notSame { + 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修改失败") } diff --git a/server/service/sys_dictionary.go b/server/service/sys_dictionary.go index b3992fe4..832ded6b 100644 --- a/server/service/sys_dictionary.go +++ b/server/service/sys_dictionary.go @@ -53,9 +53,9 @@ func UpdateSysDictionary(sysDictionary *model.SysDictionary) (err error) { } else { if (!errors.Is(global.GVA_DB.First(&model.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) { return errors.New("存在相同的type,不允许创建") - } else { - err = db.Updates(sysDictionaryMap).Error } + err = db.Updates(sysDictionaryMap).Error + } return err }