| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- package busmodels
- import (
- "device-manage/app/admin/models"
- orm "device-manage/common/global"
- "errors"
- "fmt"
- "strconv"
- )
- type BusAppGroupBind struct {
- BindId int `json:"bindId" gorm:"primary_key;AUTO_INCREMENT"`
- AppId int `json:"appId"`
- GroupId int `json:"groupId;unique, not null"`
- UpdateBy string `json:"updateBy" gorm:"size:128;"`
- CreateBy string `json:"createBy" gorm:"size:128;"`
- BaseModel
- }
- type AppGroupData struct {
- AppId int `json:"appId"`
- Groups []int `json:"groupId"`
- }
- type QueryBindedGroupParams struct {
- GroupName string `json:"groupName" gorm:"size:128;"`
- AppId int `json:"appId"`
- PageSize int `json:"pageSize"`
- PageIndex int `json:"pageIndex"`
- Gids []int `json:"gids"`
- }
- type AppGroupBindInfo struct {
- BusAppGroupBind
- GroupName string `json:"groupName" gorm:"size:128;"`
- ParentGroup string `json:"parentGroup" gorm:"size:128;"`
- }
- func (BusAppGroupBind) TableName() string {
- return "bus_app_group_bind"
- }
- func (e *BusAppGroupBind) GetBindedPage(pageSize int, pageIndex int, groupName string, gids []int) ([]AppGroupBindInfo, int, error) {
- var doc []AppGroupBindInfo
- var groups []BusGroup
- tableBind := orm.Eloquent.Table(e.TableName())
- tableGroup := orm.Eloquent.Table("bus_group")
- //根据群组名称得到所有的群组id
- if groupName != "" {
- tableGroup = tableGroup.Where("group_name = ?", groupName)
- }
- //根据当前用户拥有的群组, 过滤目标群组
- if len(gids) > 0 {
- tableGroup = tableGroup.Where("group_id in (?)", gids)
- }
- if err := tableGroup.Find(&groups).Error; err != nil {
- return nil, 0, err
- }
- groupids := make([]int, len(groups))
- for _, gid := range groups {
- groupids = append(groupids, gid.GroupId)
- }
- //根据群组名称查出来的群组id 过滤当前应用已经绑定的群组
- var count int64
- if e.AppId != 0 {
- tableBind = tableBind.Where("app_id = ?", e.AppId)
- }
- if err := tableBind.Where("group_id in (?)", groupids).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, groups BusGroup
- tableBind = orm.Eloquent.Table(e.TableName())
- tableGroup = orm.Eloquent.Table("bus_group")
- tableUser := orm.Eloquent.Table("sys_user")
- if err := tableGroup.Where("group_id = ?", v.GroupId).Find(&group).Error; err == nil {
- user := models.SysUser{}
- id, _ := strconv.Atoi(v.CreateBy)
- if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
- doc[k].UpdateBy = user.Username
- }
- doc[k].GroupName = group.GroupName
- tableGroup = orm.Eloquent.Table("bus_group")
- if err := tableGroup.Where("group_id = ?", group.ParentId).Find(&groups).Error; err == nil {
- doc[k].ParentGroup = groups.GroupName
- }
- }
- }
- return doc, int(count), nil
- }
- func (e *BusAppGroupBind) GetUnBindedPage(pageSize int, pageIndex int, groupName string, gids []int) ([]GroupInfo, int, error) {
- var bindedGroups []BusAppGroupBind
- var doc []GroupInfo
- tableBind := orm.Eloquent.Table(e.TableName())
- tableGroup := orm.Eloquent.Table("bus_group")
- //首先查询出来当前已经和应用绑定的群组
- if e.AppId != 0 {
- tableBind = tableBind.Where("app_id = ?", e.AppId)
- }
- if err := tableBind.Find(&bindedGroups).Error; err != nil {
- return nil, 0, err
- }
- if len(gids) > 0 {
- tableGroup = tableGroup.Where("group_id in (?)", gids)
- }
- var count int64
- fmt.Println(len(bindedGroups))
- if len(bindedGroups) > 0 {
- //取出groupid 切片
- groupids := make([]int, len(bindedGroups))
- for _, gid := range bindedGroups {
- groupids = append(groupids, gid.GroupId)
- }
- tableGroup = tableGroup.Where("group_id not in (?)", groupids)
- }
- //群组表过滤 未和当前应用绑定的群组
- if groupName != "" {
- tableGroup = tableGroup.Where("group_name like ?", "%"+groupName+"%")
- }
- if err := tableGroup.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
- tableGroup = orm.Eloquent.Table("bus_group")
- //tableUser := orm.Eloquent.Table("sys_user")
- fmt.Println("ParentGroup:%d", v.ParentId)
- if err := tableGroup.Where("group_id = ?", v.ParentId).Find(&group).Error; err == nil {
- /* user := 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 = group.OwnerId
- if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
- doc[k].OwnerUser = user.Username
- } */
- doc[k].ParentGroup = group.GroupName
- //doc[k].OwnerId = group.OwnerId
- }
- }
- return doc, int(count), nil
- }
- func (e *BusAppGroupBind) Insert(bindData AppGroupData) (id []int, err error) {
- var count int64
- id = make([]int, len(bindData.Groups))
- for _, groupid := range bindData.Groups {
- e.BindId = 0
- e.GroupId = groupid
- e.AppId = bindData.AppId
- orm.Eloquent.Table(e.TableName()).Where("app_id = ?", e.AppId).Where("group_id = ?", e.GroupId).Count(&count)
- if count > 0 {
- err = errors.New("group 已存在!")
- id = append(id, groupid)
- } else {
- if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
- id = append(id, groupid)
- }
- }
- }
- return
- }
- //delelte log
- func (e *BusAppGroupBind) Delete(id int) (success bool, err error) {
- if err = orm.Eloquent.Table(e.TableName()).Where("bind_id = ?", id).Delete(&BusDevice{}).Error; err != nil {
- success = false
- return
- }
- success = true
- return
- }
- func (e *BusAppGroupBind) BatchDelete(id []int) (Result bool, err error) {
- fmt.Println(">>>>>>>>>>>>>>>>>>>>>> %d", id[0])
- if err = orm.Eloquent.Table(e.TableName()).Where("bind_id in (?)", id).Delete(&BusAppGroupBind{}).Error; err != nil {
- return
- }
- Result = true
- return
- }
|