alarmService.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return device, nil
  25. }
  26. func (s *alarmService) CreateOrUpdate(userId int64, tenantId int, req dao.Alarm) *common.Errors {
  27. device := req
  28. device.TenantId = tenantId
  29. device.UpdateUser = userId
  30. device.UpdateTime = time.Now()
  31. if req.ID == 0 {
  32. device.CreateTime = time.Now()
  33. device.CreateUser = userId
  34. if device.IsExistedBySN() {
  35. logger.Logger.Errorf("Create IsExistedBySN \n")
  36. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  37. }
  38. if err := device.Create(); err != nil {
  39. logger.Logger.Errorf("Create err = %s \n", err.Error())
  40. return common.FailResponse(err.Error(), nil)
  41. }
  42. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  43. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  44. return common.SuccessResponse(common.Succeeded, nil)
  45. }
  46. if device.IsExistedByNameAndCode() {
  47. logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
  48. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  49. }
  50. if err := device.Update(); err != nil {
  51. logger.Logger.Errorf("Update err = %s \n", err.Error())
  52. return common.FailResponse(err.Error(), nil)
  53. }
  54. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  55. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  56. return common.SuccessResponse(common.Succeeded, nil)
  57. }
  58. func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, *common.Errors) {
  59. // 创建查询实例
  60. device := &dao.Alarm{}
  61. offset := (current - 1) * size
  62. limit := size
  63. devices, err := device.GetDevices(offset, limit)
  64. if err != nil {
  65. return nil, common.FailResponse(err.Error(), nil)
  66. }
  67. return devices, nil
  68. }
  69. func (s *alarmService) Remove(userId int64, tenantId int, id int) *common.Errors {
  70. // 创建查询实例
  71. device := &dao.Alarm{
  72. ID: id,
  73. IsDeleted: 1,
  74. UpdateUser: userId,
  75. UpdateTime: time.Now(),
  76. }
  77. err := device.Delete()
  78. if err != nil {
  79. return common.FailResponse(err.Error(), nil)
  80. }
  81. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  82. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  83. return nil
  84. }
  85. func (s *alarmService) GetList(tenantId int) ([]*dao.Alarm, *common.Errors) {
  86. var devices []*dao.Alarm
  87. err := cache.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices)
  88. if err == nil {
  89. return devices, nil
  90. }
  91. device := &dao.Alarm{
  92. TenantId: tenantId,
  93. IsDeleted: 0,
  94. }
  95. devices, err = device.GetAllDevices()
  96. if err != nil {
  97. return nil, common.FailResponse(err.Error(), nil)
  98. }
  99. _ = cache.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err()
  100. return devices, nil
  101. }
  102. func getAlarmListRedisKey(tenantId int) string {
  103. return fmt.Sprintf(model.AlarmList, tenantId)
  104. }