优化角色创建,新增批量方法

This commit is contained in:
郝大全
2018-08-30 00:01:07 +08:00
parent 51a71930c2
commit dbcc43aab2
2 changed files with 20 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ import (
"time"
"fmt"
"github.com/astaxie/beego"
"github.com/george518/PPGo_Job/models"
)
@@ -100,13 +101,18 @@ func (self *RoleController) AjaxSave() {
if id, err := models.RoleAdd(role); err != nil {
self.ajaxMsg(err.Error(), MSG_ERR)
} else {
ra := new(models.RoleAuth)
ras := make([]models.RoleAuth, 0)
authsSlice := strings.Split(auths, ",")
for _, v := range authsSlice {
//ra := new(models.RoleAuth)
ra := models.RoleAuth{}
aid, _ := strconv.Atoi(v)
ra.AuthId = aid
ra.RoleId = id
models.RoleAuthAdd(ra)
ras = append(ras, ra)
}
if len(ras) > 0 {
models.RoleAuthBatchAdd(&ras)
}
}
self.ajaxMsg("", MSG_OK)
@@ -120,15 +126,20 @@ func (self *RoleController) AjaxSave() {
} else {
// 删除该角色权限
models.RoleAuthDelete(role_id)
ra := new(models.RoleAuth)
ras := make([]models.RoleAuth, 0)
authsSlice := strings.Split(auths, ",")
for _, v := range authsSlice {
//ra := new(models.RoleAuth)
ra := models.RoleAuth{}
aid, _ := strconv.Atoi(v)
ra.AuthId = aid
ra.RoleId = int64(role_id)
models.RoleAuthAdd(ra)
ras = append(ras, ra)
}
if len(ras) > 0 {
models.RoleAuthBatchAdd(&ras)
}
}
self.ajaxMsg("", MSG_OK)
}

View File

@@ -28,6 +28,10 @@ func RoleAuthAdd(ra *RoleAuth) (int64, error) {
return orm.NewOrm().Insert(ra)
}
func RoleAuthBatchAdd(ras *[]RoleAuth) (int64, error) {
return orm.NewOrm().InsertMulti(100, ras)
}
func RoleAuthGetById(id int) ([]*RoleAuth, error) {
list := make([]*RoleAuth, 0)
query := orm.NewOrm().QueryTable(TableName("uc_role_auth"))