onDemandGroupService.go 3.2 KB

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