device_bind_camera.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package camera
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "smart-site-management/errors"
  10. "smart-site-management/model"
  11. "smart-site-management/pb/v1"
  12. )
  13. func DeviceBindCamera(ctx context.Context, req *v1.DeviceBindCameraRequest) (reply *v1.DeviceBindCameraReply, err error) {
  14. reply = &v1.DeviceBindCameraReply{}
  15. if req.DeviceId < 1 || req.ChannelId < 1 {
  16. return nil, errors.ParamsError
  17. }
  18. p := model.TDevice{}
  19. // 判断是否已绑定摄像头
  20. where := map[string]interface{}{
  21. "device_code !=":model.DeviceTypeVedio,
  22. "vss_channel_id":req.ChannelId,
  23. }
  24. count, err := p.Count(database.DB(), where)
  25. if err != nil {
  26. logger.Error("DeviceBindCamera",
  27. zap.String("err", err.Error()))
  28. return nil, errors.DataBaseError
  29. }
  30. if count > 0 {
  31. return nil, errors.DeviceExistError
  32. }
  33. pc := model.Vsschanneltbl{}
  34. // 判断摄像头是否存在
  35. where = map[string]interface{}{
  36. "ID":req.ChannelId,
  37. }
  38. count, err = pc.Count(database.DB(), where, nil)
  39. if err != nil {
  40. logger.Error("DeviceBindCamera",
  41. zap.String("err", err.Error()))
  42. return nil, errors.DataBaseError
  43. }
  44. if count == 0 {
  45. return nil, errors.NoRecordError
  46. }
  47. // 判断设备是否存在或可用
  48. where = map[string]interface{}{
  49. "id":req.DeviceId,
  50. "verify_status":1,
  51. }
  52. err = p.Find(database.DB(), where)
  53. if err != nil {
  54. logger.Error("DeviceBindCamera",
  55. zap.String("err", err.Error()))
  56. return nil, errors.DataBaseError
  57. }
  58. if p.Id == 0 {
  59. return nil, errors.NoRecordError
  60. }
  61. if p.DeviceCode == model.DeviceTypeVedio {
  62. return nil, errors.CameraBindError
  63. }
  64. values := map[string]interface{}{
  65. "vss_channel_id":req.ChannelId,
  66. }
  67. err = p.Update(database.DB(), where, values)
  68. if err != nil {
  69. logger.Error("DeviceBindCamera",
  70. zap.String("err", err.Error()))
  71. return nil, errors.DataBaseError
  72. }
  73. return reply, nil
  74. }