appdeviceconfig.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package busmodels
  2. import (
  3. "device-manage/app/admin/models"
  4. orm "device-manage/common/global"
  5. "errors"
  6. "strconv"
  7. )
  8. type BusAppDeviceBind struct {
  9. BindId int `json:"bindId" gorm:"primary_key;AUTO_INCREMENT"`
  10. AppId int `json:"appId"`
  11. DeviceId int `json:"deviceId"`
  12. UpdateBy string `json:"updateBy" gorm:"size:128;"`
  13. CreateBy string `json:"createBy" gorm:"size:128;"`
  14. BaseModel
  15. }
  16. type AppDeviceBindedInfo struct {
  17. BusAppDeviceBind
  18. DeviceSn string `json:"deviceSn" gorm:"size:128;"`
  19. GroupName string `json:"groupName" gorm:"size:128;"`
  20. }
  21. type QueryBindedDeviceParams struct {
  22. DeviceSn string `json:"deviceSn" 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 AppDeviceData struct {
  29. AppId int `json:"appId"`
  30. DevIds []int `json:"deviceId"`
  31. }
  32. func (BusAppDeviceBind) TableName() string {
  33. return "bus_app_device_bind"
  34. }
  35. func (e *BusAppDeviceBind) GetBindedPage(pageSize int, pageIndex int, deviceSn string, gids []int) ([]AppDeviceBindedInfo, int, error) {
  36. var doc []AppDeviceBindedInfo
  37. var devices []BusDevice
  38. tableBind := orm.Eloquent.Table(e.TableName())
  39. tableDevice := orm.Eloquent.Table("bus_device")
  40. tableUser := orm.Eloquent.Table("sys_user")
  41. //根据设备序列号得到所有的设备id
  42. if deviceSn != "" {
  43. tableDevice = tableDevice.Where("device_sn = ?", deviceSn)
  44. }
  45. //根据群组过滤目标设备
  46. if len(gids) > 0 {
  47. tableDevice = tableDevice.Where("group_id in (?)", gids)
  48. }
  49. if err := tableDevice.Find(&devices).Error; err != nil {
  50. return nil, 0, err
  51. }
  52. devids := make([]int, len(devices))
  53. for _, devid := range devices {
  54. devids = append(devids, devid.DeviceId)
  55. }
  56. //根据设备序列号查出来的id 过滤当前应用绑定的设备
  57. if e.AppId != 0 {
  58. tableBind = tableBind.Where("app_id = ?", e.AppId)
  59. }
  60. var count int64
  61. if err := tableBind.Where("device_id in (?)", devids).Order("bind_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 BusGroup
  66. var device BusDevice
  67. tableGroup := orm.Eloquent.Table("bus_group")
  68. tableDevice = orm.Eloquent.Table("bus_device")
  69. user := models.SysUser{}
  70. id, _ := strconv.Atoi(v.CreateBy)
  71. if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
  72. doc[k].CreateBy = user.Username
  73. }
  74. if err := tableDevice.Where("device_id = ?", v.DeviceId).Find(&device).Error; err == nil {
  75. doc[k].DeviceSn = device.DeviceSn
  76. if err := tableGroup.Where("group_id = ?", device.GroupId).Find(&group).Error; err == nil {
  77. doc[k].GroupName = group.GroupName
  78. }
  79. }
  80. }
  81. return doc, int(count), nil
  82. }
  83. func (e *BusAppDeviceBind) GetUnBindedPage(pageSize int, pageIndex int, deviceSn string, gids []int) ([]DeviceInfo, int, error) {
  84. var bindedDevices []BusAppDeviceBind
  85. var doc []DeviceInfo
  86. tableBind := orm.Eloquent.Table(e.TableName())
  87. tableDevice := orm.Eloquent.Table("bus_device")
  88. tableUser := orm.Eloquent.Table("sys_user")
  89. if deviceSn != "" {
  90. tableDevice = tableDevice.Where("device_sn like ?", "%"+deviceSn+"%")
  91. }
  92. if e.AppId != 0 {
  93. tableBind = tableBind.Where("app_id = ?", e.AppId)
  94. }
  95. if err := tableBind.Find(&bindedDevices).Error; err != nil {
  96. return nil, 0, err
  97. }
  98. //只查找在当前用户拥有的群组下的设备.
  99. if len(gids) > 0 {
  100. tableDevice = tableDevice.Where("group_id in (?)", gids)
  101. }
  102. var count int64
  103. if len(bindedDevices) > 0 {
  104. devids := make([]int, len(bindedDevices))
  105. for _, devid := range bindedDevices {
  106. devids = append(devids, devid.DeviceId)
  107. }
  108. if err := tableDevice.Where("device_id not in (?)", devids).Order("device_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Order("created_at ASC").Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  109. return nil, 0, err
  110. }
  111. } else {
  112. if err := tableDevice.Order("device_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  113. return nil, 0, err
  114. }
  115. }
  116. for k, v := range doc {
  117. var group BusGroup
  118. tableGroup := orm.Eloquent.Table("bus_group")
  119. user := models.SysUser{}
  120. id, _ := strconv.Atoi(v.CreateBy)
  121. if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
  122. doc[k].CreateBy = user.Username
  123. }
  124. if err := tableGroup.Where("group_id = ?", v.GroupId).Find(&group).Error; err == nil {
  125. doc[k].GroupName = group.GroupName
  126. }
  127. }
  128. return doc, int(count), nil
  129. }
  130. func (e *BusAppDeviceBind) Insert(bindData AppDeviceData) (id []int, err error) {
  131. //添加数据
  132. var count int64
  133. id = make([]int, len(bindData.DevIds))
  134. for _, devid := range bindData.DevIds {
  135. e.BindId = 0
  136. e.DeviceId = devid
  137. e.AppId = bindData.AppId
  138. orm.Eloquent.Table(e.TableName()).Where("app_id = ?", e.AppId).Where("device_id = ?", e.DeviceId).Count(&count)
  139. if count > 0 {
  140. err = errors.New("device 已存在!")
  141. id = append(id, devid)
  142. } else {
  143. if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
  144. id = append(id, devid)
  145. }
  146. }
  147. }
  148. return
  149. }
  150. func (e *BusAppDeviceBind) Delete(id int) (success bool, err error) {
  151. if err = orm.Eloquent.Table(e.TableName()).Where("bind_id = ?", id).Delete(&BusAppDeviceBind{}).Error; err != nil {
  152. success = false
  153. return
  154. }
  155. success = true
  156. return
  157. }
  158. func (e *BusAppDeviceBind) BatchDelete(id []int) (Result bool, err error) {
  159. if err = orm.Eloquent.Table(e.TableName()).Where("bind_id in (?)", id).Delete(&BusAppDeviceBind{}).Error; err != nil {
  160. return
  161. }
  162. Result = true
  163. return
  164. }