dev_screens.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package devices
  2. import (
  3. "gorm.io/gorm"
  4. "server/global"
  5. "server/model/common/request"
  6. "time"
  7. )
  8. type Screens struct {
  9. ID int `gorm:"primarykey" json:"ID"` //主键ID
  10. ScreensName string `gorm:"type:varchar(64)" json:"screensName"` //设备名称
  11. ScreensCode string `gorm:"type:varchar(64)" json:"screensCode"` //设备编码
  12. Sn string `gorm:"type:varchar(64)" json:"sn"` //设备sn
  13. Network int `gorm:"type:int" json:"network"` //组网方式 0代表网关 1代表4G模块
  14. GatewayId int `gorm:"type:int" json:"gatewayId"` //所属网关
  15. ScreensBrand string `gorm:"type:varchar(60)" json:"screensBrand"` //屏幕品牌
  16. ScreensModel string `gorm:"type:varchar(60)" json:"screensModel"` //屏幕型号
  17. IPAddress string `gorm:"type:varchar(40)" json:"ipAddress"` //IP地址
  18. InstallTime time.Time `gorm:"type:date" json:"installTime"` //安装时间
  19. Shunt int `gorm:"type:int" json:"shunt"` //所属分路 1主路,2支路
  20. ShuntDescribe string `gorm:"type:varchar(60)" json:"shuntDescribe"` //分路描述
  21. ResolutionId int `gorm:"type:int" json:"resolutionId"` //分辨率
  22. ScreensSizeId int `gorm:"type:int" json:"screensSizeId"` //信息屏尺寸
  23. IsDeleted gorm.DeletedAt `gorm:"default:null" json:"isDeleted"` //是否删除
  24. Status int `gorm:"type:int;default:0" json:"status"` //在线状态 0=离线,1=在线
  25. PlayStatus int `gorm:"type:int;default:0" json:"playStatus"` //播放状态
  26. DisplayStatus int `gorm:"type:int;default:0" json:"displayStatus"` //显示模板状态
  27. SourceStatus int `gorm:"type:int;default:0" json:"sourceStatus"` //素材状态
  28. GateWay GateWay `gorm:"foreignkey:GatewayId"`
  29. Resolution Resolution `gorm:"foreignkey:ResolutionId"`
  30. ScreensSize ScreensSize `gorm:"foreignkey:ScreensSizeId"`
  31. }
  32. func (Screens) TableName() string {
  33. return "dev_screens"
  34. }
  35. func (s Screens) DelScreens(id int) error {
  36. err := global.GVA_DB.Model(&s).Where("id = ?", id).Delete(&s).Error
  37. return err
  38. }
  39. func (s Screens) IsExistedByCode() error {
  40. err := global.GVA_DB.Model(&Screens{}).Where("screens_code = ?", s.ScreensCode).First(&s).Error
  41. return err
  42. }
  43. func (s Screens) AddScreens() error {
  44. err := global.GVA_DB.Model(&Screens{}).Create(&s).Error
  45. return err
  46. }
  47. func (s Screens) UpdateScreens() error {
  48. err := global.GVA_DB.Model(&s).
  49. Select("screens_name", "screens_code", "sn", "network", "gateway_id", "screens_brand", "screens_model", "ip_address", "install_time", "shunt", "shunt_describe", "resolution_id", "screens_size_id").
  50. Where("id=?", s.ID).
  51. Updates(map[string]interface{}{
  52. "screens_name": s.ScreensName,
  53. "screens_code": s.ScreensCode,
  54. "sn": s.Sn,
  55. "network": s.Network,
  56. "gateway_id": s.GatewayId,
  57. "screens_brand": s.ScreensBrand,
  58. "screens_model": s.ScreensModel,
  59. "ip_address": s.IPAddress,
  60. "install_time": s.InstallTime,
  61. "shunt": s.Shunt,
  62. "shunt_describe": s.ShuntDescribe,
  63. "resolution_id": s.ResolutionId,
  64. "screens_size_id": s.ScreensSizeId,
  65. }).Error
  66. return err
  67. }
  68. func QueryScreensInfoList(limit, offset int, keyword, shunt string) (screensList []Screens, total int64, err error) {
  69. db := global.GVA_DB.Model(&Screens{})
  70. if keyword != "" {
  71. db.Where("screens_name like ? or screens_code like ?", "%"+keyword+"%", "%"+keyword+"%")
  72. }
  73. if shunt != "" {
  74. db.Where("shunt = ?", shunt)
  75. }
  76. err = db.Count(&total).Error
  77. if err != nil {
  78. return
  79. }
  80. err = db.Limit(limit).Offset(offset).Preload("GateWay").Preload("Resolution").Preload("ScreensSize").Find(&screensList).Error
  81. return
  82. }
  83. func QueryScreensToDetail(id int) (screensList []Screens, err error) {
  84. db := global.GVA_DB.Model(&Screens{})
  85. err = db.Where("gateway_id = ?", id).Find(&screensList).Error
  86. return
  87. }
  88. func UpdateScreensStatusByGatewayId(id, status int) error {
  89. var screens Screens
  90. err := global.GVA_DB.Model(&screens).
  91. Select("status", "play_status", "display_status", "source_status").
  92. Where("gateway_id = ?", id).
  93. Updates(map[string]interface{}{
  94. "status": status,
  95. "play_status": status,
  96. "display_status": status,
  97. "source_status": status,
  98. }).Error
  99. return err
  100. }
  101. func UpdateScreensStatus(code string, sta request.DeviceStatus) error {
  102. var screens Screens
  103. return global.GVA_DB.Model(&screens).Debug().Select("status", "play_status", "display_status", "source_status").
  104. Where("screens_code = ?", code).Updates(map[string]interface{}{
  105. "status": sta.Status,
  106. "play_status": sta.PlayStatus,
  107. "display_status": sta.DisplayStatus,
  108. "source_status": sta.SourceStatus,
  109. }).Error
  110. }
  111. // 根据sn查询设备是否存在
  112. func QueryScreenBySn(sn string) (screens Screens, err error) {
  113. err = global.GVA_DB.Model(&screens).Where("sn = ?", sn).First(&screens).Error
  114. if err != nil {
  115. return Screens{}, err
  116. }
  117. return screens, nil
  118. }