OnDemandGroupService.go 3.3 KB

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