alarmService.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util/cache"
  8. "iot_manager_service/util/common"
  9. "iot_manager_service/util/logger"
  10. "time"
  11. )
  12. // 中间件管理服务
  13. var AlarmService = new(alarmService)
  14. type alarmService struct{}
  15. func (s *alarmService) Get(id int) (*dao.Alarm, *common.Errors) {
  16. // 创建查询实例
  17. device := &dao.Alarm{
  18. ID: id,
  19. }
  20. err := device.GetDevice()
  21. if err != nil {
  22. return nil, common.FailResponse(err.Error(), nil)
  23. }
  24. device.CountRel = int(dao.AlarmTerminal{}.GetTerminalCount(device.ID))
  25. return device, nil
  26. }
  27. func (s *alarmService) CreateOrUpdate(userId int, tenantId string, req dao.Alarm) *common.Errors {
  28. device := req
  29. device.TenantId = tenantId
  30. device.UpdateUser = userId
  31. device.UpdateTime = time.Now()
  32. if req.ID == 0 {
  33. device.CreateTime = time.Now()
  34. device.CreateUser = userId
  35. if device.IsExistedBySN() {
  36. logger.Logger.Errorf("Create IsExistedBySN \n")
  37. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  38. }
  39. if err := device.Create(); err != nil {
  40. logger.Logger.Errorf("Create err = %s \n", err.Error())
  41. return common.FailResponse(err.Error(), nil)
  42. }
  43. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  44. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  45. return common.SuccessResponse(common.Succeeded, nil)
  46. }
  47. if device.IsExistedByNameAndCode() {
  48. logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
  49. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  50. }
  51. if err := device.Update(); err != nil {
  52. logger.Logger.Errorf("Update err = %s \n", err.Error())
  53. return common.FailResponse(err.Error(), nil)
  54. }
  55. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  56. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  57. return common.SuccessResponse(common.Succeeded, nil)
  58. }
  59. func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, int64, *common.Errors) {
  60. // 创建查询实例
  61. device := &dao.Alarm{}
  62. offset := (current - 1) * size
  63. limit := size
  64. devices, total, err := device.GetDevices(offset, limit)
  65. if err != nil {
  66. return nil, 0, common.FailResponse(err.Error(), nil)
  67. }
  68. for i, alarm := range devices {
  69. devices[i].CountRel = int(dao.AlarmTerminal{}.GetTerminalCount(alarm.ID))
  70. }
  71. return devices, total, nil
  72. }
  73. func (s *alarmService) Remove(userId int, tenantId string, id int) *common.Errors {
  74. // 创建查询实例
  75. device := &dao.Alarm{
  76. ID: id,
  77. IsDeleted: 1,
  78. UpdateUser: userId,
  79. UpdateTime: time.Now(),
  80. }
  81. err := device.Delete()
  82. if err != nil {
  83. return common.FailResponse(err.Error(), nil)
  84. }
  85. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  86. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  87. return nil
  88. }
  89. func (s *alarmService) GetList(tenantId string) ([]*dao.Alarm, *common.Errors) {
  90. var devices []*dao.Alarm
  91. err := cache.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices)
  92. if err == nil {
  93. return devices, nil
  94. }
  95. device := &dao.Alarm{
  96. TenantId: tenantId,
  97. IsDeleted: 0,
  98. }
  99. devices, err = device.GetAllDevices()
  100. if err != nil {
  101. return nil, common.FailResponse(err.Error(), nil)
  102. }
  103. _ = cache.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err()
  104. return devices, nil
  105. }
  106. func getAlarmListRedisKey(tenantId string) string {
  107. return fmt.Sprintf(model.AlarmList, tenantId)
  108. }
  109. func (s *alarmService) DeviceCount() dao.DeviceStatus {
  110. var rsp dao.DeviceStatus
  111. d := dao.Alarm{}
  112. rsp.Set(d.DeviceCount1())
  113. return rsp
  114. }
  115. func (s *alarmService) ListDevices() []dao.Device {
  116. d := dao.Alarm{}
  117. return d.ListDevices1()
  118. }