gateway.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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")
  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. if req.Page != 0 || req.PageSize != 0 {
  60. db.Offset(req.PageSize * (req.Page - 1)).Limit(req.PageSize)
  61. }
  62. err = db.Debug().Find(&list).Error
  63. global.Db.Table("gateway").Where("is_deleted = 0").Count(&total)
  64. return
  65. }
  66. // AdminSet 用于后台给网关配置osn或分配用户,只有超级管理员才有权限
  67. func (gs *GatewayService) AdminSet(gw *app.Gateway) error {
  68. var m = make(map[string]any)
  69. if gw.Uid != 0 {
  70. m["uid"] = gw.Uid
  71. }
  72. if gw.Osn != "" {
  73. m["osn"] = gw.Osn
  74. }
  75. return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
  76. }
  77. // Update 由于普通用户配置网关名和描述信息
  78. func (gs *GatewayService) Update(gw *app.Gateway) error {
  79. var m = make(map[string]any)
  80. if gw.GatewayName != "" {
  81. m["gateway_name"] = gw.GatewayName
  82. }
  83. if gw.Description != "" {
  84. m["description"] = gw.Description
  85. }
  86. return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
  87. }
  88. func (gs *GatewayService) UpdateState(gw *app.Gateway) error {
  89. return global.Db.Model(&app.Gateway{}).Where("isn = ? AND is_deleted = 0", gw.Isn).Update("state", gw.State).Debug().Error
  90. }
  91. // DeleteGateways 删除网关及关联设备
  92. func (gs *GatewayService) DeleteGateways(gw app.Gateway) (err error) {
  93. devs := gs.DevIds(gw.ID)
  94. err = global.Db.Transaction(func(tx *gorm.DB) error {
  95. //删除网关
  96. if err := tx.Debug().Model(&app.Gateway{}).Where("id = ?", gw.ID).Update("is_deleted", 1).Error; err != nil {
  97. return err
  98. }
  99. if err = tx.Debug().Delete(&gw).Error; err != nil {
  100. return err
  101. }
  102. return nil
  103. })
  104. //删除摄像头
  105. cs := CameraService{}
  106. if devs.Cid != 0 {
  107. var c app.Camera
  108. c.ID = devs.Cid
  109. err = cs.DeleteCamera(c)
  110. if err != nil {
  111. logrus.Error("删除摄像头失败", err)
  112. }
  113. }
  114. //删除ip音柱
  115. if devs.Iid != 0 {
  116. err = global.Db.Model(&app.Ipcast{}).Where("id = ?", devs.Iid).Update("is_deleted", 1).Error
  117. if err != nil {
  118. logrus.Error("删除ip音柱失败", err)
  119. }
  120. }
  121. return err
  122. }
  123. // UnbindGateways 解绑网关下面的设备
  124. func (gs *GatewayService) UnbindGateways(gw app.Gateway) error {
  125. devs := gs.DevIds(gw.ID)
  126. var err error
  127. if devs.Cid != 0 {
  128. if err1 := global.Db.Model(&app.Camera{}).Where("id = ?", devs.Cid).Update("gid", 0).Error; err1 != nil {
  129. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Error("解绑失败", err)
  130. err = errors.Join(err1)
  131. }
  132. }
  133. if devs.Iid != 0 {
  134. if err2 := global.Db.Model(&app.Ipcast{}).Where("id = ?", devs.Iid).Update("gid", 0).Error; err2 != nil {
  135. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "ip音柱": devs.Iid}).Error("解绑失败", err)
  136. err = errors.Join(err2)
  137. }
  138. }
  139. if err == nil {
  140. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn, "ip音柱": devs.Iid}).Info("解绑成功")
  141. }
  142. return err
  143. }
  144. func (gs *GatewayService) DevIds(id uint) (devs app.Devices) {
  145. //查询网关及关联设备的id
  146. global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn,i.id AS iid").
  147. Table("gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN ipcast i ON g.id = i.gid").
  148. Where("g.id = ?", id).
  149. Find(&devs)
  150. return
  151. }