gateway.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package app
  2. import (
  3. "errors"
  4. "github.com/sirupsen/logrus"
  5. "gorm.io/gorm"
  6. "lcfns/global"
  7. "lcfns/model/app"
  8. "lcfns/model/app/request"
  9. "lcfns/model/app/response"
  10. )
  11. var ExistError = errors.New("已存在")
  12. type GatewayService struct {
  13. }
  14. func (gs *GatewayService) CreateIfNotExist(isn string) (*app.Gateway, error) {
  15. var gate *app.Gateway
  16. err := global.Db.Select("id, isn").Model(&app.Gateway{}).Where("isn = ? AND is_deleted = 0", isn).Scan(&gate).Debug().Error
  17. if err != nil {
  18. return nil, err
  19. }
  20. if gate != nil {
  21. return gate, ExistError
  22. }
  23. var gateway = &app.Gateway{Isn: isn, State: 1}
  24. err = global.Db.Create(gateway).Debug().Error
  25. if err != nil {
  26. return nil, err
  27. }
  28. logrus.Infof("网关[%s]注册成功", isn)
  29. return gateway, nil
  30. }
  31. // List 该用户下所有设备数据
  32. func (gs *GatewayService) List(req request.GatewayRequest) (list []response.Gateways, total int64, err error) {
  33. //SELECT g.id,g.name AS gateway_name,g.osn,
  34. // c.`name` AS camera_name, RIGHT(c.sn,9) AS sn,stream_id,stream_url,
  35. // l.device_id AS ls_id,l.`status` AS ls_status
  36. //FROM gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN loudspeaker l ON g.id = l.gid ;
  37. db := global.Db.Select("g.id,g.uid,g.gateway_name,g.isn,g.osn,g.state AS gateway_state,g.description," +
  38. "c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url,c.state AS camera_state," +
  39. //"i.device_id AS ipcast_id," +
  40. "i.state AS ipcast_state").
  41. Table("gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN ipcast i ON g.id = i.gid").
  42. Where("g.is_deleted = 0 AND c.deleted_at IS NULL AND i.is_deleted = 0")
  43. //条件查询设备,网关名,网关osn,摄像头sn,ip音柱devid,
  44. if req.Uid > 1 {
  45. db.Where("g.uid = ?", req.Uid)
  46. }
  47. if req.Osn != "" {
  48. db.Where("g.osn = ?", req.Osn)
  49. }
  50. if req.IpcastId != 0 {
  51. db.Where("i.ipcast_id = ?", req.IpcastId)
  52. }
  53. if req.CameraSn != "" {
  54. db.Where("c.sn LIKE ?", "%"+req.CameraSn)
  55. }
  56. if req.Name != "" {
  57. db.Where("gateway_name LIKE ?", "%"+req.Name+"%")
  58. }
  59. db.Count(&total)
  60. if req.Page != 0 || req.PageSize != 0 {
  61. db.Offset(req.PageSize * (req.Page - 1)).Limit(req.PageSize)
  62. }
  63. err = db.Debug().Find(&list).Error
  64. //global.Db.Table("gateway").Where("is_deleted = 0").Count(&total)
  65. return
  66. }
  67. // AdminSet 用于后台给网关配置osn或分配用户,只有超级管理员才有权限
  68. func (gs *GatewayService) AdminSet(gw *app.Gateway) error {
  69. var m = make(map[string]any)
  70. if gw.Uid != 0 {
  71. m["uid"] = gw.Uid
  72. }
  73. if gw.Osn != "" {
  74. m["osn"] = gw.Osn
  75. }
  76. return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
  77. }
  78. // Update 由于普通用户配置网关名和描述信息
  79. func (gs *GatewayService) Update(gw *app.Gateway) error {
  80. var m = make(map[string]any)
  81. if gw.GatewayName != "" {
  82. m["gateway_name"] = gw.GatewayName
  83. }
  84. if gw.Description != "" {
  85. m["description"] = gw.Description
  86. }
  87. return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
  88. }
  89. func (gs *GatewayService) UpdateState(gw *app.Gateway) error {
  90. return global.Db.Model(&app.Gateway{}).Where("isn = ? AND is_deleted = 0", gw.Isn).Update("state", gw.State).Debug().Error
  91. }
  92. // DeleteGateways 删除网关及关联设备
  93. func (gs *GatewayService) DeleteGateways(ids *request.Ids) (err error) {
  94. for _, id := range ids.IDS {
  95. devs := gs.DevIds(uint(id))
  96. err = global.Db.Transaction(func(tx *gorm.DB) error {
  97. //删除网关
  98. if err := tx.Debug().Model(&app.Gateway{}).Where("id = ?", id).Update("is_deleted", 1).Error; err != nil {
  99. return err
  100. }
  101. if err = tx.Debug().Model(&app.Gateway{}).Delete("id = ?", id).Error; err != nil {
  102. return err
  103. }
  104. return nil
  105. })
  106. //删除摄像头
  107. cs := CameraService{}
  108. if devs.Cid != 0 {
  109. var c app.Camera
  110. c.ID = devs.Cid
  111. err = cs.DeleteCamera(c)
  112. if err != nil {
  113. logrus.Error("删除摄像头失败", err)
  114. }
  115. }
  116. //删除ip音柱
  117. if devs.Iid != 0 {
  118. err = global.Db.Model(&app.Ipcast{}).Where("id = ?", devs.Iid).Update("is_deleted", 1).Error
  119. if err != nil {
  120. logrus.Error("删除ip音柱失败", err)
  121. }
  122. }
  123. }
  124. return err
  125. }
  126. // UnbindGateways 解绑网关下面的设备
  127. func (gs *GatewayService) UnbindGateways(gw app.Gateway) error {
  128. devs := gs.DevIds(gw.ID)
  129. var err error
  130. if devs.Cid != 0 {
  131. if err1 := global.Db.Model(&app.Camera{}).Where("id = ?", devs.Cid).Update("gid", 0).Error; err1 != nil {
  132. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Error("解绑失败", err)
  133. err = err1
  134. }
  135. }
  136. if devs.Iid != 0 {
  137. if err2 := global.Db.Model(&app.Ipcast{}).Where("id = ?", devs.Iid).Update("gid", 0).Error; err2 != nil {
  138. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "ip音柱": devs.Iid}).Error("解绑失败", err)
  139. err = err2
  140. }
  141. }
  142. if err == nil {
  143. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn, "ip音柱": devs.Iid}).Info("解绑成功")
  144. }
  145. return err
  146. }
  147. func (gs *GatewayService) DevIds(id uint) (devs app.Devices) {
  148. //查询网关及关联设备的id
  149. global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn,i.id AS iid").
  150. Table("gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN ipcast i ON g.id = i.gid").
  151. Where("g.id = ?", id).
  152. Find(&devs)
  153. return
  154. }