alarmService.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/util"
  7. "time"
  8. )
  9. // 中间件管理服务
  10. var AlarmService = new(alarmService)
  11. type alarmService struct{}
  12. func (s *alarmService) Get(id int) (*dao.Alarm, *util.Errors) {
  13. // 创建查询实例
  14. device := &dao.Alarm{
  15. ID: id,
  16. }
  17. err := device.GetDevice()
  18. if err != nil {
  19. return nil, util.FailResponse(err.Error(), nil)
  20. }
  21. return device, nil
  22. }
  23. func (s *alarmService) CreateOrUpdate(userId int64, tenantId int, req dao.Alarm) *util.Errors {
  24. device := req
  25. device.TenantId = tenantId
  26. device.UpdateUser = userId
  27. device.UpdateTime = time.Now()
  28. if req.ID == 0 {
  29. device.CreateTime = time.Now()
  30. device.CreateUser = userId
  31. if device.IsExistedBySN() {
  32. fmt.Printf("Create IsExistedBySN \n")
  33. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  34. }
  35. if err := device.Create(); err != nil {
  36. fmt.Printf("Create err = %s \n", err.Error())
  37. return util.FailResponse(err.Error(), nil)
  38. }
  39. return util.SuccessResponse(util.Succeeded, nil)
  40. }
  41. if device.IsExistedByNameAndCode() {
  42. fmt.Printf("Update IsExistedByNameAndCode \n")
  43. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  44. }
  45. if err := device.Update(); err != nil {
  46. fmt.Printf("Update err = %s \n", err.Error())
  47. return util.FailResponse(err.Error(), nil)
  48. }
  49. //todo operation record
  50. return util.SuccessResponse(util.Succeeded, nil)
  51. }
  52. func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, *util.Errors) {
  53. // 创建查询实例
  54. device := &dao.Alarm{}
  55. offset := (current - 1) * size
  56. limit := size
  57. devices, err := device.GetDevices(offset, limit)
  58. if err != nil {
  59. return nil, util.FailResponse(err.Error(), nil)
  60. }
  61. return devices, nil
  62. }
  63. func (s *alarmService) Remove(userId int64, id int) *util.Errors {
  64. // 创建查询实例
  65. device := &dao.Alarm{
  66. ID: id,
  67. IsDeleted: 1,
  68. UpdateUser: userId,
  69. UpdateTime: time.Now(),
  70. }
  71. //todo
  72. // service.lampPoleService.CountRelation()
  73. //todo operation record
  74. err := device.Delete()
  75. if err != nil {
  76. return util.FailResponse(err.Error(), nil)
  77. }
  78. return nil
  79. }
  80. func (s *alarmService) GetList(tenantId int) ([]*dao.Alarm, *util.Errors) {
  81. var devices []*dao.Alarm
  82. err := util.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices)
  83. if err == nil {
  84. return devices, nil
  85. }
  86. device := &dao.Alarm{
  87. TenantId: tenantId,
  88. IsDeleted: 0,
  89. }
  90. devices, err = device.GetAllDevices()
  91. if err != nil {
  92. return nil, util.FailResponse(err.Error(), nil)
  93. }
  94. _ = util.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err()
  95. return devices, nil
  96. }
  97. func getAlarmListRedisKey(tenantId int) string {
  98. return fmt.Sprintf(model.AlarmList, tenantId)
  99. }