12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package role
- import (
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/admin"
- "zhiyuan/services/auth"
- "zhiyuan/services/form"
- )
- var Role models.Role
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Role, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return Role.GetMulti(where, fields, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Role, error) {
- return Role.GetOne(where, fields, retVal)
- }
- func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Role, error) {
- return GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(Role.TableName(), where)
- }
- func Add(form form.RoleAdd) (int64, error) {
- if CheckRoleDuplicate(utils.ToStr(form.Name)) {
- return 0, errors.New("role already exists")
- }
- authList, _ := auth.GetList(map[string]interface{}{"id in": form.AuthIds}, []string{"id, auth"}, app.Page{}, nil)
- roleMap := map[string]interface{}{
- "name": form.Name,
- "auth_ids": utils.JoinIntSlice(form.AuthIds, ","),
- "auth_list": utils.JoinSliceMap(authList, "auth", ","),
- "data_auth": form.DataAuth,
- }
- roleID, err := db.InsertOne(Role.TableName(), roleMap)
- if err != nil {
- return 0, nil
- }
- return roleID, nil
- }
- func EditByID(form form.RoleAdd, id int) error {
- roleInfo, err := GetInfoByID(id, nil, nil)
- if roleInfo == nil {
- return errors.New("invalid auth id")
- }
- if roleInfo.Name != form.Name && CheckRoleDuplicate(form.Name) {
- return errors.New("role already exists")
- }
- authList, _ := auth.GetList(map[string]interface{}{"id in": form.AuthIds}, []string{"id, auth"}, app.Page{}, nil)
- roleMap := map[string]interface{}{
- "name": form.Name,
- "auth_ids": utils.JoinIntSlice(form.AuthIds, ","),
- "auth_list": utils.JoinSliceMap(authList, "auth", ","),
- "data_auth": form.DataAuth,
- }
- _, err = db.Update(Role.TableName(), map[string]interface{}{"id": id}, roleMap)
- admin.ClearAuthCacheByRole(id)
- return err
- }
- func CheckRoleDuplicate(name string) bool {
- roleInfo, err := GetOne(map[string]interface{}{"name": name}, nil, nil)
- return roleInfo != nil && err == nil
- }
- func DeleteByID(id int) error {
- authInfo, _ := GetInfoByID(id, nil, nil)
- if authInfo == nil {
- return errors.New("invalid auth id")
- }
- _, err := db.Delete(Role.TableName(), map[string]interface{}{"id": id})
- return err
- }
- func GetNameByID(roleIds []string) []string {
- roleList, _ := GetList(map[string]interface{}{"id in": roleIds}, []string{"id, name"}, app.Page{}, nil)
- roleNames := make([]string, 0)
- for _, v := range roleList {
- roleNames = append(roleNames, v.Name)
- }
- return roleNames
- }
|