infoBoardService.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/device/model"
  5. "iot_manager_service/app/system/service"
  6. "iot_manager_service/util/cache"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "time"
  10. )
  11. var InfoBoardService = new(infoBoardService)
  12. type infoBoardService struct{}
  13. func (s *infoBoardService) Get(id int) (*model.InfoBoardDetail, *common.Errors) {
  14. // 创建查询实例
  15. device := &dao.InfoBoard{
  16. ID: id,
  17. }
  18. err := device.GetDevice()
  19. if err != nil {
  20. return nil, common.FailResponse(err.Error(), nil)
  21. }
  22. endTime, state := cache.GetDeviceState(device.Sn)
  23. return &model.InfoBoardDetail{InfoBoard: *device,
  24. RunState: state,
  25. NetworkState: state,
  26. EndLineTime: endTime,
  27. }, nil
  28. }
  29. func (s *infoBoardService) CreateOrUpdate(userId int64, tenantId int, req dao.InfoBoard) *common.Errors {
  30. // 创建查询实例
  31. device := req
  32. device.TenantId = tenantId
  33. device.UpdateUser = userId
  34. device.UpdateTime = time.Now()
  35. if req.ID == 0 {
  36. device.CreateTime = time.Now()
  37. device.CreateUser = userId
  38. if device.IsExistedBySN() {
  39. logger.Logger.Errorf("Create GetDeviceID err \n")
  40. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  41. }
  42. if err := device.Create(); err != nil {
  43. logger.Logger.Errorf("Create err = %s \n", err.Error())
  44. return common.FailResponse(err.Error(), nil)
  45. }
  46. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  47. common.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
  48. return common.SuccessResponse(common.Succeeded, nil)
  49. }
  50. if device.IsExistedByNameAndCode() {
  51. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  52. }
  53. if err := device.Update(); err != nil {
  54. logger.Logger.Errorf("Update err = %s \n", err.Error())
  55. return common.FailResponse(err.Error(), nil)
  56. }
  57. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  58. common.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
  59. return common.SuccessResponse(common.Succeeded, nil)
  60. }
  61. func (s *infoBoardService) List(searchValue string, current, size int) ([]model.InfoBoardDetail, int64, *common.Errors) {
  62. device := dao.InfoBoard{}
  63. if searchValue != "" {
  64. device.Sn = searchValue
  65. device.InfoName = searchValue
  66. }
  67. offset := (current - 1) * size
  68. limit := size
  69. devices, total, err := device.GetDevices(offset, limit)
  70. if err != nil {
  71. return nil, 0, common.FailResponse(err.Error(), nil)
  72. }
  73. var details []model.InfoBoardDetail
  74. for _, d := range devices {
  75. endTime, state := cache.GetDeviceState(d.Sn)
  76. details = append(details, model.InfoBoardDetail{
  77. InfoBoard: d,
  78. RunState: state,
  79. NetworkState: state,
  80. EndLineTime: endTime,
  81. })
  82. }
  83. return details, total, nil
  84. }
  85. func (s *infoBoardService) Remove(userId int64, tenantId int, id int) *common.Errors {
  86. // 创建查询实例
  87. device := &dao.InfoBoard{
  88. ID: id,
  89. IsDeleted: 1,
  90. UpdateUser: userId,
  91. UpdateTime: time.Now(),
  92. }
  93. err := device.Delete()
  94. if err != nil {
  95. return common.FailResponse(err.Error(), nil)
  96. }
  97. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  98. common.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
  99. return nil
  100. }
  101. func (s *infoBoardService) GetByGateway(gatewayId int) []dao.InfoBoard {
  102. device := &dao.InfoBoard{
  103. GatewayId: gatewayId,
  104. }
  105. return device.GetDevicesByGateway()
  106. }
  107. func (s *infoBoardService) GetByLampPole(lampPoleId int) []dao.InfoBoard {
  108. device := &dao.InfoBoard{
  109. LampPoleId: lampPoleId,
  110. }
  111. return device.GetDevicesByLampPole()
  112. }
  113. func (s *infoBoardService) GetByResolution(tenantId, resolution int) []dao.InfoBoard {
  114. device := &dao.InfoBoard{
  115. Resolution: resolution,
  116. TenantId: tenantId,
  117. }
  118. return device.GetDevicesByResolution()
  119. }
  120. func (s *infoBoardService) GetByIds(tenantId int, ids string) []dao.InfoBoard {
  121. device := &dao.InfoBoard{
  122. TenantId: tenantId,
  123. }
  124. return device.GetDevicesByIds(ids)
  125. }