package busmodels import ( "device-manage/app/admin/models" orm "device-manage/common/global" "errors" "fmt" "strconv" ) type GroupsId struct { GroupId int `json:"groupId" gorm:"primary_key;AUTO_INCREMENT"` } //group type 有两种, type:1, 管理群组, 普通用户不可删除,admin用户可以删除. type:2 普通群组, 全部用户可以删除. //默认admin帐号初始帐号为管理群组, 新建用户的默认群组也是管理群组. type BusGroup struct { GroupsId GroupName string `json:"groupName" gorm:"size:128;"` ParentId int `json:"parentId"` GroupType int `json:"groupType"` Remark string `json:"remark" gorm:"size:255;"` UpdateBy string `json:"updateBy" gorm:"size:128;"` CreateBy string `json:"createBy" gorm:"size:128;"` BaseModel OwnerId int `json:"ownerId"` BusAppGroupBind BusAppGroupBind `gorm:"foreignKey:GroupId;references:GroupId"` } type QueryGroupParams struct { GroupName string `json:"groupName" gorm:"size:128;"` PageSize int `json:"pageSize"` PageIndex int `json:"pageIndex"` Gids []int `json:"gids"` } type GroupInfo struct { BusGroup OwnerUser string `json:"ownerUser" gorm:"size:128;"` ParentGroup string `json:"parentGroup" gorm:"size:128;"` } func (BusGroup) TableName() string { return "bus_group" } func (e *BusGroup) Get() (BusGroup, error) { var doc BusGroup table := orm.Eloquent.Table(e.TableName()) if e.GroupId != 0 { table = table.Where("group_id = ?", e.GroupId) } if err := table.First(&doc).Error; err != nil { return doc, err } return doc, nil } func (e *BusGroup) GetGroupByName() (int, error) { var count int64 = 0 table := orm.Eloquent.Table(e.TableName()) if e.GroupName != "" { table = table.Where("group_name = ?", e.GroupName) } if err := table.Count(&count).Error; err != nil { return int(count), err } return int(count), nil } //查询用户群组id列表 func (e *BusGroup) GetGroupIds(userIds []int) (gids []int, err error) { var doc []GroupsId fmt.Println(">> ", len(userIds)) for _, vall := range userIds { fmt.Println(">> ", vall) } table := orm.Eloquent.Table(e.TableName()) if len(userIds) > 0 { table = table.Where("owner_id in (?)", userIds) if err := table.Find(&doc).Error; err != nil { return gids, err } } for _, id := range doc { fmt.Println("GroupId:", id.GroupId) gids = append(gids, id.GroupId) } return gids, err } func (e *BusGroup) GetPage(pageSize int, pageIndex int, gids []int) ([]GroupInfo, int, error) { var doc []GroupInfo table := orm.Eloquent.Table(e.TableName()) if e.GroupName != "" { table = table.Where("group_name like ?", "%"+e.GroupName+"%") } if len(gids) > 0 { table = table.Where("group_id in (?)", gids) } var count int64 if err := table.Order("group_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil { return nil, 0, err } for k, v := range doc { var group BusGroup table = orm.Eloquent.Table(e.TableName()) tableUser := orm.Eloquent.Table("sys_user") if err := table.Where("group_id = ?", v.ParentId).Find(&group).Error; err == nil { user := models.SysUser{} user2 := models.SysUser{} id, _ := strconv.Atoi(v.CreateBy) if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil { doc[k].UpdateBy = user.Username } id = v.OwnerId if err := orm.Eloquent.Table("sys_user").Where("user_id = ?", id).Find(&user2).Error; err == nil { doc[k].OwnerUser = user2.Username } doc[k].ParentGroup = group.GroupName doc[k].OwnerId = v.OwnerId } } return doc, int(count), nil } func (e *BusGroup) Insert() (id int, err error) { // check 用户名 var count int64 orm.Eloquent.Table(e.TableName()).Where("group_name = ?", e.GroupName).Count(&count) if count > 0 { err = errors.New("grop name 已存在!") return } //添加数据 if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil { return } id = e.GroupId fmt.Println("id:", id) return } func (e *BusGroup) Update(id int) (update BusGroup, err error) { //group id : 1 basegroup can not modified. if id == 1 { err = errors.New("group name 不支持修改!") return } if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil { return } //参数1:是要修改的数据 //参数2:是修改的数据 fmt.Println("new data:", e) fmt.Println("old data:", update) if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil { return } return } //delelte log func (e *BusGroup) Delete(id int) (success bool, err error) { //group id : 1 basegroup can not delete. if id == 1 { err = errors.New("group name 不支持删除!") return } if err = orm.Eloquent.Table(e.TableName()).Where("group_id = ?", id).Delete(&BusDevice{}).Error; err != nil { success = false return } success = true return } func (e *BusGroup) BatchDelete(id []int, roleId int) (Result bool, err error) { //group id : 1 basegroup can not delete. var groups []BusGroup var apps []BusApplication var devices []BusDevice for _, v := range id { if v == 1 { err = errors.New("basegroup 不可删除!") return } } if err = orm.Eloquent.Table(e.TableName()).Where("parent_id in (?)", id).Find(&groups).Error; err != nil { return } if len(groups) > 0 { err = errors.New("存在子群组, 不可删除!") return } //查询是否包含应用 if err = orm.Eloquent.Table("bus_application").Where("group_id in (?)", id).Find(&apps).Error; err != nil { return } if len(apps) > 0 { err = errors.New("群组存在应用, 不可删除!") return } //查询是否包含设备 if err = orm.Eloquent.Table("bus_device").Where("group_id in (?)", id).Find(&devices).Error; err != nil { return } if len(devices) > 0 { err = errors.New("群组存在设备, 不可删除!") return } //roleId = 1 是 管理权限的用户,可以删除GroupType=1的管理群组. roleId=2的普通用户 无权限删除管理群组. if roleId == 2 { var gInfos []BusGroup if err = orm.Eloquent.Table(e.TableName()).Where("group_id in (?)", id).Find(&gInfos).Error; err != nil { return } for _, gInfo := range gInfos { if gInfo.GroupType == 1 { err = errors.New("管理群组, 不可删除!") return } } } if err = orm.Eloquent.Table(e.TableName()).Where("group_id in (?)", id).Delete(&BusGroup{}).Error; err != nil { return } Result = true return }