alarmService.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 int64, tenantId int, 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. fmt.Printf("device = %v", device)
  52. if err := device.Update(); err != nil {
  53. logger.Logger.Errorf("Update err = %s \n", err.Error())
  54. return common.FailResponse(err.Error(), nil)
  55. }
  56. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  57. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  58. return common.SuccessResponse(common.Succeeded, nil)
  59. }
  60. func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, int64, *common.Errors) {
  61. // 创建查询实例
  62. device := &dao.Alarm{}
  63. offset := (current - 1) * size
  64. limit := size
  65. devices, total, err := device.GetDevices(offset, limit)
  66. if err != nil {
  67. return nil, 0, common.FailResponse(err.Error(), nil)
  68. }
  69. for i, alarm := range devices {
  70. devices[i].CountRel = int(dao.AlarmTerminal{}.GetTerminalCount(alarm.ID))
  71. }
  72. return devices, total, nil
  73. }
  74. func (s *alarmService) Remove(userId int64, tenantId int, id int) *common.Errors {
  75. // 创建查询实例
  76. device := &dao.Alarm{
  77. ID: id,
  78. IsDeleted: 1,
  79. UpdateUser: userId,
  80. UpdateTime: time.Now(),
  81. }
  82. err := device.Delete()
  83. if err != nil {
  84. return common.FailResponse(err.Error(), nil)
  85. }
  86. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  87. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
  88. return nil
  89. }
  90. func (s *alarmService) GetList(tenantId int) ([]*dao.Alarm, *common.Errors) {
  91. var devices []*dao.Alarm
  92. err := cache.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices)
  93. if err == nil {
  94. return devices, nil
  95. }
  96. device := &dao.Alarm{
  97. TenantId: tenantId,
  98. IsDeleted: 0,
  99. }
  100. devices, err = device.GetAllDevices()
  101. if err != nil {
  102. return nil, common.FailResponse(err.Error(), nil)
  103. }
  104. _ = cache.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err()
  105. return devices, nil
  106. }
  107. func getAlarmListRedisKey(tenantId int) string {
  108. return fmt.Sprintf(model.AlarmList, tenantId)
  109. }