From 30f137e85a76420d872a96c30b2177f59e9706d2 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期一, 17 七月 2023 16:13:59 +0800
Subject: [PATCH] add

---
 service/authority.go |   33 +++++++++++++--------------------
 1 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/service/authority.go b/service/authority.go
index b2aefb2..b4cdb92 100644
--- a/service/authority.go
+++ b/service/authority.go
@@ -5,7 +5,6 @@
 	"aps_crm/pkg/ecode"
 	"github.com/flipped-aurora/gin-vue-admin/server/global"
 	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
-	"gorm.io/gorm"
 	"strconv"
 )
 
@@ -13,33 +12,30 @@
 
 var AuthorityServiceApp = new(AuthorityService)
 
-func (authorityService *AuthorityService) CreateAuthority(auth model.Authority) int {
-	_, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.AuthorityId).Find()
-	if err != gorm.ErrRecordNotFound {
-		return ecode.RoleExist
-	}
-
-	err = model.NewSysAuthoritySearch().Create(&auth)
+// CreateAuthority create authority and return authority id and error code
+func (authorityService *AuthorityService) CreateAuthority(auth model.Authority) (int, uint) {
+	// check if authority name exists
+	err := model.NewSysAuthoritySearch().Create(&auth)
 	if err != nil {
-		return ecode.SubOrderExist
+		return ecode.RoleExist, 0
 	}
 
-	return ecode.OK
+	return ecode.OK, auth.Id
 }
 
 func (authorityService *AuthorityService) UpdateAuthority(auth model.Authority) int {
-	_, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.AuthorityId).Find()
+	_, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Find()
 	if err != nil {
 		return ecode.RoleNotExist
 	}
 
-	err = model.NewSysAuthoritySearch().SetAuthorityId(auth.AuthorityId).Update(&auth)
+	err = model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Update(&auth)
 
 	return ecode.OK
 }
 
 func (authorityService *AuthorityService) DeleteAuthority(auth *model.Authority) int {
-	_, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.AuthorityId).Find()
+	_, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Find()
 	if err != nil {
 		return ecode.RoleNotExist
 	}
@@ -48,7 +44,7 @@
 		return ecode.RoleDeleteErr1
 	}
 
-	authorityId := strconv.Itoa(int(auth.AuthorityId))
+	authorityId := strconv.Itoa(int(auth.Id))
 	CasbinServiceApp.ClearCasbin(0, authorityId)
 	return ecode.OK
 }
@@ -65,7 +61,7 @@
 }
 
 //@author: [piexlmax](https://github.com/piexlmax)
-//@function: SetDataAuthority
+//@function: SetMenuAuthority
 //@description: 璁剧疆瑙掕壊璧勬簮鏉冮檺
 //@param: auth model.Authority
 //@return: error
@@ -83,9 +79,6 @@
 //@param: auth *model.Authority
 //@return: error
 
-func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
-	var s system.SysAuthority
-	global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
-	err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
-	return err
+func (authorityService *AuthorityService) SetMenuAuthority(auth *model.Authority) error {
+	return model.NewSysAuthoritySearch().SetMenuAuthority(auth)
 }

--
Gitblit v1.8.0