github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/service/admin/group.go (about) 1 package admin 2 3 import ( 4 model "github.com/cloudreve/Cloudreve/v3/models" 5 "github.com/cloudreve/Cloudreve/v3/pkg/serializer" 6 "strconv" 7 ) 8 9 // AddGroupService 用户组添加服务 10 type AddGroupService struct { 11 Group model.Group `json:"group" binding:"required"` 12 } 13 14 // GroupService 用户组ID服务 15 type GroupService struct { 16 ID uint `uri:"id" json:"id" binding:"required"` 17 } 18 19 // Get 获取用户组详情 20 func (service *GroupService) Get() serializer.Response { 21 group, err := model.GetGroupByID(service.ID) 22 if err != nil { 23 return serializer.Err(serializer.CodeGroupNotFound, "", err) 24 } 25 26 return serializer.Response{Data: group} 27 } 28 29 // Delete 删除用户组 30 func (service *GroupService) Delete() serializer.Response { 31 // 查找用户组 32 group, err := model.GetGroupByID(service.ID) 33 if err != nil { 34 return serializer.Err(serializer.CodeGroupNotFound, "", err) 35 } 36 37 // 是否为系统用户组 38 if group.ID <= 3 { 39 return serializer.Err(serializer.CodeInvalidActionOnSystemGroup, "", err) 40 } 41 42 // 检查是否有用户使用 43 total := 0 44 row := model.DB.Model(&model.User{}).Where("group_id = ?", service.ID). 45 Select("count(id)").Row() 46 row.Scan(&total) 47 if total > 0 { 48 return serializer.Err(serializer.CodeGroupUsedByUser, strconv.Itoa(total), nil) 49 } 50 51 model.DB.Delete(&group) 52 53 return serializer.Response{} 54 } 55 56 // Add 添加用户组 57 func (service *AddGroupService) Add() serializer.Response { 58 if service.Group.ID > 0 { 59 if err := model.DB.Save(&service.Group).Error; err != nil { 60 return serializer.DBErr("Failed to save group record", err) 61 } 62 } else { 63 if err := model.DB.Create(&service.Group).Error; err != nil { 64 return serializer.DBErr("Failed to create group record", err) 65 } 66 } 67 68 return serializer.Response{Data: service.Group.ID} 69 } 70 71 // Groups 列出用户组 72 func (service *AdminListService) Groups() serializer.Response { 73 var res []model.Group 74 total := 0 75 76 tx := model.DB.Model(&model.Group{}) 77 if service.OrderBy != "" { 78 tx = tx.Order(service.OrderBy) 79 } 80 81 for k, v := range service.Conditions { 82 tx = tx.Where(k+" = ?", v) 83 } 84 85 // 计算总数用于分页 86 tx.Count(&total) 87 88 // 查询记录 89 tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res) 90 91 // 统计每个用户组的用户总数 92 statics := make(map[uint]int, len(res)) 93 for i := 0; i < len(res); i++ { 94 total := 0 95 row := model.DB.Model(&model.User{}).Where("group_id = ?", res[i].ID). 96 Select("count(id)").Row() 97 row.Scan(&total) 98 statics[res[i].ID] = total 99 } 100 101 // 汇总用户组存储策略 102 policies := make(map[uint]model.Policy) 103 for i := 0; i < len(res); i++ { 104 for _, p := range res[i].PolicyList { 105 if _, ok := policies[p]; !ok { 106 policies[p], _ = model.GetPolicyByID(p) 107 } 108 } 109 } 110 111 return serializer.Response{Data: map[string]interface{}{ 112 "total": total, 113 "items": res, 114 "statics": statics, 115 "policies": policies, 116 }} 117 }