alarmTerminalService.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. // 中间件管理服务
  12. var AlarmTerminalService = new(alarmTerminalService)
  13. type alarmTerminalService struct{}
  14. func (s *alarmTerminalService) Get(id int) (*model.AlarmTerminalDetail, *common.Errors) {
  15. // 创建查询实例
  16. device := &dao.AlarmTerminal{
  17. ID: id,
  18. }
  19. err := device.GetDevice()
  20. if err != nil {
  21. return nil, common.FailResponse(err.Error(), nil)
  22. }
  23. endTime, state := cache.GetDeviceState(device.TerminalSN)
  24. return &model.AlarmTerminalDetail{
  25. AlarmTerminal: *device,
  26. RunState: state,
  27. NetworkState: state,
  28. EndLineTime: endTime,
  29. }, nil
  30. }
  31. func (s *alarmTerminalService) CreateOrUpdate(userId int, tenantId string, req dao.AlarmTerminal) *common.Errors {
  32. device := req
  33. device.TenantId = tenantId
  34. device.UpdateUser = userId
  35. device.UpdateTime = time.Now()
  36. if req.ID == 0 {
  37. device.CreateTime = time.Now()
  38. device.CreateUser = userId
  39. if device.IsExistedBySN() {
  40. logger.Logger.Errorf("Create IsExistedBySN \n")
  41. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  42. }
  43. if err := device.Create(); err != nil {
  44. logger.Logger.Errorf("Create err = %s \n", err.Error())
  45. return common.FailResponse(err.Error(), nil)
  46. }
  47. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  48. common.DeviceTypeAlarmTerminal, common.GetDeviceObject(device.ID, device.TerminalName), common.OperationSuccess)
  49. return common.SuccessResponse(common.Succeeded, nil)
  50. }
  51. if device.IsExistedByNameAndCode() {
  52. logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
  53. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  54. }
  55. if err := device.Update(); err != nil {
  56. logger.Logger.Errorf("Update err = %s \n", err.Error())
  57. return common.FailResponse(err.Error(), nil)
  58. }
  59. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  60. common.DeviceTypeAlarmTerminal, common.GetDeviceObject(device.ID, device.TerminalName), common.OperationSuccess)
  61. return common.SuccessResponse(common.Succeeded, nil)
  62. }
  63. func (s *alarmTerminalService) List(poleGroupName string, current, size int) ([]model.AlarmTerminalDetail, int64, *common.Errors) {
  64. // 创建查询实例
  65. device := &dao.AlarmTerminal{}
  66. offset := (current - 1) * size
  67. limit := size
  68. devices, total, err := device.GetDevices(offset, limit)
  69. if err != nil {
  70. return nil, 0, common.FailResponse(err.Error(), nil)
  71. }
  72. var details []model.AlarmTerminalDetail
  73. for _, d := range devices {
  74. endTime, state := cache.GetDeviceState(d.TerminalSN)
  75. details = append(details, model.AlarmTerminalDetail{
  76. AlarmTerminal: d,
  77. RunState: state,
  78. NetworkState: state,
  79. EndLineTime: endTime,
  80. })
  81. }
  82. return details, total, nil
  83. }
  84. func (s *alarmTerminalService) Remove(userId int, tenantId string, id int) *common.Errors {
  85. // 创建查询实例
  86. device := &dao.AlarmTerminal{
  87. ID: id,
  88. IsDeleted: 1,
  89. UpdateUser: userId,
  90. UpdateTime: time.Now(),
  91. }
  92. err := device.Delete()
  93. if err != nil {
  94. return common.FailResponse(err.Error(), nil)
  95. }
  96. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  97. common.DeviceTypeAlarmTerminal, common.GetDeviceObject(device.ID, device.TerminalName), common.OperationSuccess)
  98. return nil
  99. }
  100. func (s *alarmTerminalService) GetByGateway(id int) []dao.AlarmTerminal {
  101. // 创建查询实例
  102. device := &dao.AlarmTerminal{
  103. GatewayId: id,
  104. }
  105. return device.GetDevicesByGateway()
  106. }
  107. func (s *alarmTerminalService) GetByLampPole(id int) []dao.AlarmTerminal {
  108. // 创建查询实例
  109. device := &dao.AlarmTerminal{
  110. LampPoleId: id,
  111. }
  112. return device.GetDevicesByLampPole()
  113. }
  114. func (s *alarmTerminalService) DeviceCount() dao.DeviceStatus {
  115. var rsp dao.DeviceStatus
  116. d := dao.AlarmTerminal{}
  117. rsp.Set(d.DeviceCount1())
  118. return rsp
  119. }
  120. func (s *alarmTerminalService) ListDevices() []dao.Device {
  121. d := dao.AlarmTerminal{}
  122. return d.ListDevices1()
  123. }