lampPoleGroupService.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 LampPoleGroupService = new(lampPoleGroupService)
  11. type lampPoleGroupService struct{}
  12. func (s *lampPoleGroupService) Get(id int) (*dao.LampPoleGroup, *utils.Errors) {
  13. // 创建查询实例
  14. device := &dao.LampPoleGroup{
  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 *lampPoleGroupService) CreateOrUpdate(req *dao.LampPoleGroup) *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.IsExistedByName() {
  34. fmt.Printf("Create IsExistedByName \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 *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *utils.Errors) {
  55. // 创建查询实例
  56. device := &dao.LampPoleGroup{
  57. PoleGroupName: poleGroupName,
  58. }
  59. offset := (current - 1) * size
  60. limit := size
  61. devices, err := device.GetDevices(offset, limit)
  62. if err != nil {
  63. return nil, utils.FailResponse(err.Error(), nil)
  64. }
  65. return devices, nil
  66. }
  67. func (s *lampPoleGroupService) Remove(id int) *utils.Errors {
  68. // 创建查询实例
  69. device := &dao.LampPoleGroup{
  70. ID: id,
  71. IsDeleted: 1,
  72. UpdateUser: "TODO", // todo 使用登录态
  73. UpdateTime: time.Now(),
  74. }
  75. //todo
  76. // service.lampPoleService.CountRelation()
  77. //todo operation record
  78. err := device.Delete()
  79. if err != nil {
  80. return utils.FailResponse(err.Error(), nil)
  81. }
  82. return nil
  83. }
  84. func (s *lampPoleGroupService) GetList() ([]*dao.LampPoleGroup, *utils.Errors) {
  85. var devices []*dao.LampPoleGroup
  86. err := utils.Redis.Get(getLampPoleGroupListRedisKey("000000")).Scan(&devices)
  87. if err == nil {
  88. return devices, nil
  89. }
  90. device := &dao.LampPoleGroup{
  91. TenantId: "000000", // todo 使用登录态
  92. IsDeleted: 0,
  93. }
  94. devices, err = device.GetAllDevices()
  95. if err != nil {
  96. return nil, utils.FailResponse(err.Error(), nil)
  97. }
  98. _ = utils.Redis.Set(getLampPoleGroupListRedisKey("000000"), devices, 0).Err()
  99. return devices, nil
  100. }
  101. func getLampPoleGroupListRedisKey(tenantId string) string {
  102. return fmt.Sprintf(model.LampPoleGroupList, tenantId)
  103. }
  104. func (s *lampPoleGroupService) GetFiltration() ([]*dao.LampPoleGroup, *utils.Errors) {
  105. var devices []*dao.LampPoleGroup
  106. err := utils.Redis.Get(getGroupFiltrationListRedisKey("000000")).Scan(&devices)
  107. if err == nil {
  108. return devices, nil
  109. }
  110. device := &dao.LampPoleGroup{
  111. TenantId: "000000", // todo 使用登录态
  112. IsDeleted: 0,
  113. }
  114. //todo
  115. // get t_dev_light_control group_id
  116. // groupIds := lightControl.GetGroupIds()
  117. devices, err = device.GetAllDevices()
  118. if err != nil {
  119. return nil, utils.FailResponse(err.Error(), nil)
  120. }
  121. _ = utils.Redis.Set(getGroupFiltrationListRedisKey("000000"), devices, 0).Err()
  122. return devices, nil
  123. }
  124. func getGroupFiltrationListRedisKey(tenantId string) string {
  125. return fmt.Sprintf(model.GroupFiltrationList, tenantId)
  126. }
  127. func (s *lampPoleGroupService) GetTree() ([]*dao.LampPoleGroup, *utils.Errors) {
  128. // todo use redis cache
  129. device := &dao.LampPoleGroup{
  130. TenantId: "000000", // todo 使用登录态
  131. IsDeleted: 0,
  132. }
  133. //todo lampPole getALlDevice
  134. devices, err := device.GetAllDevices()
  135. if err != nil {
  136. return nil, utils.FailResponse(err.Error(), nil)
  137. }
  138. return devices, nil
  139. }