multiscreenService.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package service
  2. import (
  3. deviceDao "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/security/model"
  6. "iot_manager_service/app/system/dao"
  7. "log"
  8. "strconv"
  9. "strings"
  10. )
  11. var MultiscreenService = new(multiscreenService)
  12. type multiscreenService struct{}
  13. func (s multiscreenService) List(claims *middleware.Claims) ([]model.Placement, error) {
  14. securitySixScreen := s.getSlot(claims.UserId)
  15. idsSplit := strings.Split(securitySixScreen, ",")
  16. var ids []string
  17. for _, id := range idsSplit {
  18. idInt, _ := strconv.Atoi(id)
  19. if idInt < 1 {
  20. continue
  21. }
  22. ids = append(ids, strconv.Itoa(idInt))
  23. }
  24. //devices := deviceDao.CameraDevice{}.GetDevicesByIds(strings.Join(ids, ","))
  25. var list []model.Placement
  26. for i, id := range idsSplit {
  27. idInt, _ := strconv.Atoi(id)
  28. placement := model.Placement{}
  29. placement.ID = idInt
  30. placement.Slot = i + 1
  31. if idInt != -1 {
  32. device := deviceDao.CameraDevice{ID: idInt}
  33. err := device.GetDevice()
  34. if err != nil {
  35. continue
  36. }
  37. placement.Name = device.DeviceName
  38. placement.Sn = device.DeviceSN
  39. placement.Url = device.MonitorAddress
  40. }
  41. list = append(list, placement)
  42. }
  43. return list, nil
  44. }
  45. // Save 保存 多屏监控位置
  46. func (s multiscreenService) Save(claims *middleware.Claims, site, id int) error {
  47. securitySixScreen := s.getSlot(claims.UserId)
  48. idsSplit := strings.Split(securitySixScreen, ",")
  49. ids := ""
  50. for i := range idsSplit {
  51. if i == (site - 1) {
  52. idsSplit[i] = strconv.Itoa(id)
  53. }
  54. ids += idsSplit[i]
  55. if i < (len(idsSplit) - 1) {
  56. ids += ","
  57. }
  58. }
  59. user := &dao.User{ID: claims.UserId}
  60. user.SecuritySixScreen = ids
  61. err := user.Update()
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func (s multiscreenService) getSlot(userId int) string {
  68. user := &dao.User{ID: userId}
  69. err := user.GetUser()
  70. if err != nil {
  71. log.Fatalln(err)
  72. panic("用户取失败了")
  73. }
  74. ids := ""
  75. if user.SecuritySixScreen == "" {
  76. ids = "-1,-1,-1,-1,-1,-1"
  77. } else {
  78. ids = user.SecuritySixScreen
  79. }
  80. return ids
  81. }