appgroupconfig.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package busmodels
  2. import (
  3. "device-manage/app/admin/models"
  4. orm "device-manage/common/global"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. )
  9. type BusAppGroupBind struct {
  10. BindId int `json:"bindId" gorm:"primary_key;AUTO_INCREMENT"`
  11. AppId int `json:"appId"`
  12. GroupId int `json:"groupId;unique, not null"`
  13. UpdateBy string `json:"updateBy" gorm:"size:128;"`
  14. CreateBy string `json:"createBy" gorm:"size:128;"`
  15. BaseModel
  16. }
  17. type AppGroupData struct {
  18. AppId int `json:"appId"`
  19. Groups []int `json:"groupId"`
  20. }
  21. type QueryBindedGroupParams struct {
  22. GroupName string `json:"groupName" gorm:"size:128;"`
  23. AppId int `json:"appId"`
  24. PageSize int `json:"pageSize"`
  25. PageIndex int `json:"pageIndex"`
  26. Gids []int `json:"gids"`
  27. }
  28. type AppGroupBindInfo struct {
  29. BusAppGroupBind
  30. GroupName string `json:"groupName" gorm:"size:128;"`
  31. ParentGroup string `json:"parentGroup" gorm:"size:128;"`
  32. }
  33. func (BusAppGroupBind) TableName() string {
  34. return "bus_app_group_bind"
  35. }
  36. func (e *BusAppGroupBind) GetBindedPage(pageSize int, pageIndex int, groupName string, gids []int) ([]AppGroupBindInfo, int, error) {
  37. var doc []AppGroupBindInfo
  38. var groups []BusGroup
  39. tableBind := orm.Eloquent.Table(e.TableName())
  40. tableGroup := orm.Eloquent.Table("bus_group")
  41. //根据群组名称得到所有的群组id
  42. if groupName != "" {
  43. tableGroup = tableGroup.Where("group_name = ?", groupName)
  44. }
  45. //根据当前用户拥有的群组, 过滤目标群组
  46. if len(gids) > 0 {
  47. tableGroup = tableGroup.Where("group_id in (?)", gids)
  48. }
  49. if err := tableGroup.Find(&groups).Error; err != nil {
  50. return nil, 0, err
  51. }
  52. groupids := make([]int, len(groups))
  53. for _, gid := range groups {
  54. groupids = append(groupids, gid.GroupId)
  55. }
  56. //根据群组名称查出来的群组id 过滤当前应用已经绑定的群组
  57. var count int64
  58. if e.AppId != 0 {
  59. tableBind = tableBind.Where("app_id = ?", e.AppId)
  60. }
  61. 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 {
  62. return nil, 0, err
  63. }
  64. for k, v := range doc {
  65. var group, groups BusGroup
  66. tableBind = orm.Eloquent.Table(e.TableName())
  67. tableGroup = orm.Eloquent.Table("bus_group")
  68. tableUser := orm.Eloquent.Table("sys_user")
  69. if err := tableGroup.Where("group_id = ?", v.GroupId).Find(&group).Error; err == nil {
  70. user := models.SysUser{}
  71. id, _ := strconv.Atoi(v.CreateBy)
  72. if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
  73. doc[k].UpdateBy = user.Username
  74. }
  75. doc[k].GroupName = group.GroupName
  76. tableGroup = orm.Eloquent.Table("bus_group")
  77. if err := tableGroup.Where("group_id = ?", group.ParentId).Find(&groups).Error; err == nil {
  78. doc[k].ParentGroup = groups.GroupName
  79. }
  80. }
  81. }
  82. return doc, int(count), nil
  83. }
  84. func (e *BusAppGroupBind) GetUnBindedPage(pageSize int, pageIndex int, groupName string, gids []int) ([]GroupInfo, int, error) {
  85. var bindedGroups []BusAppGroupBind
  86. var doc []GroupInfo
  87. tableBind := orm.Eloquent.Table(e.TableName())
  88. tableGroup := orm.Eloquent.Table("bus_group")
  89. //首先查询出来当前已经和应用绑定的群组
  90. if e.AppId != 0 {
  91. tableBind = tableBind.Where("app_id = ?", e.AppId)
  92. }
  93. if err := tableBind.Find(&bindedGroups).Error; err != nil {
  94. return nil, 0, err
  95. }
  96. if len(gids) > 0 {
  97. tableGroup = tableGroup.Where("group_id in (?)", gids)
  98. }
  99. var count int64
  100. fmt.Println(len(bindedGroups))
  101. if len(bindedGroups) > 0 {
  102. //取出groupid 切片
  103. groupids := make([]int, len(bindedGroups))
  104. for _, gid := range bindedGroups {
  105. groupids = append(groupids, gid.GroupId)
  106. }
  107. tableGroup = tableGroup.Where("group_id not in (?)", groupids)
  108. }
  109. //群组表过滤 未和当前应用绑定的群组
  110. if groupName != "" {
  111. tableGroup = tableGroup.Where("group_name like ?", "%"+groupName+"%")
  112. }
  113. if err := tableGroup.Order("group_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  114. return nil, 0, err
  115. }
  116. for k, v := range doc {
  117. var group BusGroup
  118. tableGroup = orm.Eloquent.Table("bus_group")
  119. //tableUser := orm.Eloquent.Table("sys_user")
  120. fmt.Println("ParentGroup:%d", v.ParentId)
  121. if err := tableGroup.Where("group_id = ?", v.ParentId).Find(&group).Error; err == nil {
  122. /* user := models.SysUser{}
  123. id, _ := strconv.Atoi(v.CreateBy)
  124. if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
  125. doc[k].UpdateBy = user.Username
  126. }
  127. id = group.OwnerId
  128. if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
  129. doc[k].OwnerUser = user.Username
  130. } */
  131. doc[k].ParentGroup = group.GroupName
  132. //doc[k].OwnerId = group.OwnerId
  133. }
  134. }
  135. return doc, int(count), nil
  136. }
  137. func (e *BusAppGroupBind) Insert(bindData AppGroupData) (id []int, err error) {
  138. var count int64
  139. id = make([]int, len(bindData.Groups))
  140. for _, groupid := range bindData.Groups {
  141. e.BindId = 0
  142. e.GroupId = groupid
  143. e.AppId = bindData.AppId
  144. orm.Eloquent.Table(e.TableName()).Where("app_id = ?", e.AppId).Where("group_id = ?", e.GroupId).Count(&count)
  145. if count > 0 {
  146. err = errors.New("group 已存在!")
  147. id = append(id, groupid)
  148. } else {
  149. if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
  150. id = append(id, groupid)
  151. }
  152. }
  153. }
  154. return
  155. }
  156. //delelte log
  157. func (e *BusAppGroupBind) Delete(id int) (success bool, err error) {
  158. if err = orm.Eloquent.Table(e.TableName()).Where("bind_id = ?", id).Delete(&BusDevice{}).Error; err != nil {
  159. success = false
  160. return
  161. }
  162. success = true
  163. return
  164. }
  165. func (e *BusAppGroupBind) BatchDelete(id []int) (Result bool, err error) {
  166. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> %d", id[0])
  167. if err = orm.Eloquent.Table(e.TableName()).Where("bind_id in (?)", id).Delete(&BusAppGroupBind{}).Error; err != nil {
  168. return
  169. }
  170. Result = true
  171. return
  172. }