alarmService.go 2.8 KB

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