onDemandGroupService.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "strconv"
  10. "time"
  11. )
  12. // 中间件管理服务
  13. var OnDemandGroupService = new(onDemandGroupService)
  14. type onDemandGroupService struct{}
  15. func (s *onDemandGroupService) Get(id int) (*dao.OnDemandGroup, *common.Errors) {
  16. // 创建查询实例
  17. device := &dao.OnDemandGroup{
  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 *onDemandGroupService) CreateOrUpdate(userId int64, tenantId int, req *dao.OnDemandGroup) *common.Errors {
  27. device := req
  28. device.TenantId = tenantId
  29. device.UpdateUser = userId
  30. device.UpdateTime = time.Now()
  31. if device.ID == 0 {
  32. device.CreateTime = time.Now()
  33. device.CreateUser = userId
  34. if device.IsExistedBySN() {
  35. fmt.Printf("Create IsExistedBySN \n")
  36. return common.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  37. }
  38. fmt.Printf("device = %+v \n", device)
  39. if err := device.Create(); err != nil {
  40. fmt.Printf("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.PoleGroupName), common.OperationSuccess)
  45. return common.SuccessResponse(common.Succeeded, nil)
  46. }
  47. if err := device.Update(); err != nil {
  48. fmt.Printf("Update err = %s \n", err.Error())
  49. return common.FailResponse(err.Error(), nil)
  50. }
  51. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  52. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
  53. return common.SuccessResponse(common.Succeeded, nil)
  54. }
  55. func (s *onDemandGroupService) List(searchValue string, current, size int) ([]dao.OnDemandGroup, *common.Errors) {
  56. device := dao.OnDemandGroup{}
  57. if searchValue != "" {
  58. device.PoleGroupName = searchValue
  59. }
  60. offset := (current - 1) * size
  61. limit := size
  62. devices, err := device.GetDevices(offset, limit)
  63. if err != nil {
  64. return nil, common.FailResponse(err.Error(), nil)
  65. }
  66. return devices, nil
  67. }
  68. func (s *onDemandGroupService) Remove(userId int64, tenantId int, id int) *common.Errors {
  69. // 创建查询实例
  70. device := &dao.OnDemandGroup{
  71. ID: id,
  72. IsDeleted: 1,
  73. UpdateUser: userId,
  74. UpdateTime: time.Now(),
  75. }
  76. err := device.Delete()
  77. if err != nil {
  78. return common.FailResponse(err.Error(), nil)
  79. }
  80. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  81. common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
  82. return nil
  83. }
  84. func (s *onDemandGroupService) GetList(tenantId int) ([]*dao.OnDemandGroup, *common.Errors) {
  85. var devices []*dao.OnDemandGroup
  86. err := cache.Redis.Get(getOnDemandGroupListRedisKey(tenantId)).Scan(&devices)
  87. if err == nil {
  88. return devices, nil
  89. }
  90. device := &dao.OnDemandGroup{
  91. TenantId: tenantId,
  92. IsDeleted: 0,
  93. }
  94. devices, err = device.GetAllDevices()
  95. if err != nil {
  96. return nil, common.FailResponse(err.Error(), nil)
  97. }
  98. _ = cache.Redis.Set(getOnDemandGroupListRedisKey(tenantId), devices, 0).Err()
  99. return devices, nil
  100. }
  101. func getOnDemandGroupListRedisKey(tenantId int) string {
  102. return fmt.Sprintf(model.OnDemandGroupList, tenantId)
  103. }
  104. func (s *onDemandGroupService) GroupNumberList(id int) ([]model.RspOnDemandGroupNumber, *common.Errors) {
  105. // 创建查询实例
  106. device := &dao.OnDemandGroup{
  107. ID: id,
  108. }
  109. err := device.GetDevice()
  110. if err != nil {
  111. return nil, common.FailResponse(err.Error(), nil)
  112. }
  113. var list []model.RspOnDemandGroupNumber
  114. for i := 1; i <= device.LampPoleCount; i++ {
  115. list = append(list, model.RspOnDemandGroupNumber{Name: strconv.Itoa(i), Value: i})
  116. }
  117. return list, nil
  118. }