gateway.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package app
  2. import (
  3. "errors"
  4. "github.com/sirupsen/logrus"
  5. "gorm.io/gorm"
  6. "lc-fangdaosha/global"
  7. "lc-fangdaosha/model/app"
  8. "lc-fangdaosha/model/app/request"
  9. "lc-fangdaosha/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}
  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, err error) {
  33. db := global.Db.Select("g.id,g.gateway_name,g.osn,g.description,c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url").
  34. Table("gateway g LEFT JOIN camera c ON g.id = c.gid").
  35. Where("g.is_deleted = 0")
  36. //条件查询设备,网关名,网关osn,摄像头sn
  37. if req.Uid > 0 {
  38. db.Where("g.uid = ?", req.Uid)
  39. }
  40. if req.Osn != "" {
  41. db.Where("g.osn = ?", req.Osn)
  42. }
  43. if req.CameraSn != "" {
  44. db.Where("c.sn LIKE ?", "%"+req.CameraSn)
  45. }
  46. if req.Name != "" {
  47. db.Where("gateway_name LIKE ?", "%"+req.Name+"%")
  48. }
  49. db.Offset(req.PageSize * (req.Page - 1)).Limit(req.PageSize)
  50. err = db.Debug().Find(&list).Error
  51. return
  52. }
  53. // AdminSet 用于后台给网关配置osn或分配用户,只有超级管理员才有权限
  54. func (gs *GatewayService) AdminSet(gw *app.Gateway) error {
  55. var m = make(map[string]any)
  56. if gw.Uid != 0 {
  57. m["uid"] = gw.Uid
  58. }
  59. if gw.Osn != "" {
  60. m["osn"] = gw.Osn
  61. }
  62. return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
  63. }
  64. // Update 由于普通用户配置网关名和描述信息
  65. func (gs *GatewayService) Update(gw *app.Gateway) error {
  66. var m = make(map[string]any)
  67. if gw.GatewayName != "" {
  68. m["gateway_name"] = gw.GatewayName
  69. }
  70. if gw.Description != "" {
  71. m["description"] = gw.Description
  72. }
  73. return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
  74. }
  75. // DeleteGateways 删除网关及关联设备
  76. func (gs *GatewayService) DeleteGateways(gw app.Gateway) (err error) {
  77. devs := gs.DevIds(gw.ID)
  78. err = global.Db.Transaction(func(tx *gorm.DB) error {
  79. //删除网关
  80. if err := tx.Debug().Model(&app.Gateway{}).Where("id = ?", gw.ID).Update("is_deleted", 1).Error; err != nil {
  81. return err
  82. }
  83. if err = tx.Debug().Delete(&gw).Error; err != nil {
  84. return err
  85. }
  86. return nil
  87. })
  88. //删除摄像头
  89. cs := CameraService{}
  90. if devs.Cid != 0 {
  91. var c app.Camera
  92. c.ID = devs.Cid
  93. err = cs.DeleteCamera(c)
  94. if err != nil {
  95. logrus.Error("删除摄像头失败", err)
  96. }
  97. }
  98. return err
  99. }
  100. // UnbindGateways 解绑网关下面的设备
  101. func (gs *GatewayService) UnbindGateways(gw app.Gateway) error {
  102. devs := gs.DevIds(gw.ID)
  103. var err error
  104. if devs.Cid != 0 {
  105. if err1 := global.Db.Model(&app.Camera{}).Where("id = ?", devs.Cid).Update("gid", 0).Error; err1 != nil {
  106. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Error("解绑失败", err)
  107. err = errors.Join(err1)
  108. }
  109. }
  110. if err == nil {
  111. logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Info("解绑成功")
  112. }
  113. return err
  114. }
  115. // DevIds 查询网关及关联设备的id
  116. func (gs *GatewayService) DevIds(id uint) (devs app.Devices) {
  117. global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn").
  118. Table("gateway g LEFT JOIN camera c ON g.id = c.gid").
  119. Where("g.id = ?", id).
  120. Find(&devs)
  121. return
  122. }