group.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 GroupsId struct {
  10. GroupId int `json:"groupId" gorm:"primary_key;AUTO_INCREMENT"`
  11. }
  12. //group type 有两种, type:1, 管理群组, 普通用户不可删除,admin用户可以删除. type:2 普通群组, 全部用户可以删除.
  13. //默认admin帐号初始帐号为管理群组, 新建用户的默认群组也是管理群组.
  14. type BusGroup struct {
  15. GroupsId
  16. GroupName string `json:"groupName" gorm:"size:128;"`
  17. ParentId int `json:"parentId"`
  18. GroupType int `json:"groupType"`
  19. Remark string `json:"remark" gorm:"size:255;"`
  20. UpdateBy string `json:"updateBy" gorm:"size:128;"`
  21. CreateBy string `json:"createBy" gorm:"size:128;"`
  22. BaseModel
  23. OwnerId int `json:"ownerId"`
  24. BusAppGroupBind BusAppGroupBind `gorm:"foreignKey:GroupId;references:GroupId"`
  25. }
  26. type QueryGroupParams struct {
  27. GroupName string `json:"groupName" gorm:"size:128;"`
  28. PageSize int `json:"pageSize"`
  29. PageIndex int `json:"pageIndex"`
  30. Gids []int `json:"gids"`
  31. }
  32. type GroupInfo struct {
  33. BusGroup
  34. OwnerUser string `json:"ownerUser" gorm:"size:128;"`
  35. ParentGroup string `json:"parentGroup" gorm:"size:128;"`
  36. }
  37. func (BusGroup) TableName() string {
  38. return "bus_group"
  39. }
  40. func (e *BusGroup) Get() (BusGroup, error) {
  41. var doc BusGroup
  42. table := orm.Eloquent.Table(e.TableName())
  43. if e.GroupId != 0 {
  44. table = table.Where("group_id = ?", e.GroupId)
  45. }
  46. if err := table.First(&doc).Error; err != nil {
  47. return doc, err
  48. }
  49. return doc, nil
  50. }
  51. func (e *BusGroup) GetGroupByName() (int, error) {
  52. var count int64 = 0
  53. table := orm.Eloquent.Table(e.TableName())
  54. if e.GroupName != "" {
  55. table = table.Where("group_name = ?", e.GroupName)
  56. }
  57. if err := table.Count(&count).Error; err != nil {
  58. return int(count), err
  59. }
  60. return int(count), nil
  61. }
  62. //查询用户群组id列表
  63. func (e *BusGroup) GetGroupIds(userIds []int) (gids []int, err error) {
  64. var doc []GroupsId
  65. fmt.Println(">> ", len(userIds))
  66. for _, vall := range userIds {
  67. fmt.Println(">> ", vall)
  68. }
  69. table := orm.Eloquent.Table(e.TableName())
  70. if len(userIds) > 0 {
  71. table = table.Where("owner_id in (?)", userIds)
  72. if err := table.Find(&doc).Error; err != nil {
  73. return gids, err
  74. }
  75. }
  76. for _, id := range doc {
  77. fmt.Println("GroupId:", id.GroupId)
  78. gids = append(gids, id.GroupId)
  79. }
  80. return gids, err
  81. }
  82. func (e *BusGroup) GetPage(pageSize int, pageIndex int, gids []int) ([]GroupInfo, int, error) {
  83. var doc []GroupInfo
  84. table := orm.Eloquent.Table(e.TableName())
  85. if e.GroupName != "" {
  86. table = table.Where("group_name like ?", "%"+e.GroupName+"%")
  87. }
  88. if len(gids) > 0 {
  89. table = table.Where("group_id in (?)", gids)
  90. }
  91. var count int64
  92. if err := table.Order("group_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  93. return nil, 0, err
  94. }
  95. for k, v := range doc {
  96. var group BusGroup
  97. table = orm.Eloquent.Table(e.TableName())
  98. tableUser := orm.Eloquent.Table("sys_user")
  99. if err := table.Where("group_id = ?", v.ParentId).Find(&group).Error; err == nil {
  100. user := models.SysUser{}
  101. user2 := models.SysUser{}
  102. id, _ := strconv.Atoi(v.CreateBy)
  103. if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
  104. doc[k].UpdateBy = user.Username
  105. }
  106. id = v.OwnerId
  107. if err := orm.Eloquent.Table("sys_user").Where("user_id = ?", id).Find(&user2).Error; err == nil {
  108. doc[k].OwnerUser = user2.Username
  109. }
  110. doc[k].ParentGroup = group.GroupName
  111. doc[k].OwnerId = v.OwnerId
  112. }
  113. }
  114. return doc, int(count), nil
  115. }
  116. func (e *BusGroup) Insert() (id int, err error) {
  117. // check 用户名
  118. var count int64
  119. orm.Eloquent.Table(e.TableName()).Where("group_name = ?", e.GroupName).Count(&count)
  120. if count > 0 {
  121. err = errors.New("grop name 已存在!")
  122. return
  123. }
  124. //添加数据
  125. if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
  126. return
  127. }
  128. id = e.GroupId
  129. fmt.Println("id:", id)
  130. return
  131. }
  132. func (e *BusGroup) Update(id int) (update BusGroup, err error) {
  133. //group id : 1 basegroup can not modified.
  134. if id == 1 {
  135. err = errors.New("group name 不支持修改!")
  136. return
  137. }
  138. if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
  139. return
  140. }
  141. //参数1:是要修改的数据
  142. //参数2:是修改的数据
  143. fmt.Println("new data:", e)
  144. fmt.Println("old data:", update)
  145. if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
  146. return
  147. }
  148. return
  149. }
  150. //delelte log
  151. func (e *BusGroup) Delete(id int) (success bool, err error) {
  152. //group id : 1 basegroup can not delete.
  153. if id == 1 {
  154. err = errors.New("group name 不支持删除!")
  155. return
  156. }
  157. if err = orm.Eloquent.Table(e.TableName()).Where("group_id = ?", id).Delete(&BusDevice{}).Error; err != nil {
  158. success = false
  159. return
  160. }
  161. success = true
  162. return
  163. }
  164. func (e *BusGroup) BatchDelete(id []int, roleId int) (Result bool, err error) {
  165. //group id : 1 basegroup can not delete.
  166. var groups []BusGroup
  167. var apps []BusApplication
  168. var devices []BusDevice
  169. for _, v := range id {
  170. if v == 1 {
  171. err = errors.New("basegroup 不可删除!")
  172. return
  173. }
  174. }
  175. if err = orm.Eloquent.Table(e.TableName()).Where("parent_id in (?)", id).Find(&groups).Error; err != nil {
  176. return
  177. }
  178. if len(groups) > 0 {
  179. err = errors.New("存在子群组, 不可删除!")
  180. return
  181. }
  182. //查询是否包含应用
  183. if err = orm.Eloquent.Table("bus_application").Where("group_id in (?)", id).Find(&apps).Error; err != nil {
  184. return
  185. }
  186. if len(apps) > 0 {
  187. err = errors.New("群组存在应用, 不可删除!")
  188. return
  189. }
  190. //查询是否包含设备
  191. if err = orm.Eloquent.Table("bus_device").Where("group_id in (?)", id).Find(&devices).Error; err != nil {
  192. return
  193. }
  194. if len(devices) > 0 {
  195. err = errors.New("群组存在设备, 不可删除!")
  196. return
  197. }
  198. //roleId = 1 是 管理权限的用户,可以删除GroupType=1的管理群组. roleId=2的普通用户 无权限删除管理群组.
  199. if roleId == 2 {
  200. var gInfos []BusGroup
  201. if err = orm.Eloquent.Table(e.TableName()).Where("group_id in (?)", id).Find(&gInfos).Error; err != nil {
  202. return
  203. }
  204. for _, gInfo := range gInfos {
  205. if gInfo.GroupType == 1 {
  206. err = errors.New("管理群组, 不可删除!")
  207. return
  208. }
  209. }
  210. }
  211. if err = orm.Eloquent.Table(e.TableName()).Where("group_id in (?)", id).Delete(&BusGroup{}).Error; err != nil {
  212. return
  213. }
  214. Result = true
  215. return
  216. }