lampPoleGroupService.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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"
  8. "time"
  9. )
  10. // 中间件管理服务
  11. var LampPoleGroupService = new(lampPoleGroupService)
  12. type lampPoleGroupService struct{}
  13. func (s *lampPoleGroupService) Get(id int) (*dao.LampPoleGroup, *util.Errors) {
  14. // 创建查询实例
  15. device := &dao.LampPoleGroup{
  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 *lampPoleGroupService) CreateOrUpdate(userId int64, tenantId int, req *dao.LampPoleGroup) *util.Errors {
  25. device := req
  26. device.TenantId = tenantId
  27. device.UpdateUser = userId
  28. device.UpdateTime = time.Now()
  29. if req.ID == 0 {
  30. device.CreateTime = time.Now()
  31. device.CreateUser = userId
  32. if device.IsExistedByName() {
  33. fmt.Printf("Create IsExistedByName \n")
  34. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  35. }
  36. if err := device.Create(); err != nil {
  37. fmt.Printf("Create err = %s \n", err.Error())
  38. return util.FailResponse(err.Error(), nil)
  39. }
  40. service.OperationHisService.Save(userId, tenantId, util.OperationCreate, util.ModuleTypeDevice,
  41. util.DeviceTypeLampPoleGroup, util.GetDeviceObject(device.ID, device.PoleGroupName), util.OperationSuccess)
  42. return util.SuccessResponse(util.Succeeded, nil)
  43. }
  44. if device.IsExistedByNameAndCode() {
  45. fmt.Printf("Update IsExistedByNameAndCode \n")
  46. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  47. }
  48. if err := device.Update(); err != nil {
  49. fmt.Printf("Update err = %s \n", err.Error())
  50. return util.FailResponse(err.Error(), nil)
  51. }
  52. service.OperationHisService.Save(userId, tenantId, util.OperationUpdate, util.ModuleTypeDevice,
  53. util.DeviceTypeLampPoleGroup, util.GetDeviceObject(device.ID, device.PoleGroupName), util.OperationSuccess)
  54. return util.SuccessResponse(util.Succeeded, nil)
  55. }
  56. func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *util.Errors) {
  57. // 创建查询实例
  58. device := &dao.LampPoleGroup{
  59. PoleGroupName: poleGroupName,
  60. }
  61. offset := (current - 1) * size
  62. limit := size
  63. devices, err := device.GetDevices(offset, limit)
  64. if err != nil {
  65. return nil, util.FailResponse(err.Error(), nil)
  66. }
  67. return devices, nil
  68. }
  69. func (s *lampPoleGroupService) Remove(userId int64, tenantId int, id int, name string) *util.Errors {
  70. device := &dao.LampPoleGroup{
  71. ID: id,
  72. IsDeleted: 1,
  73. UpdateUser: userId,
  74. UpdateTime: time.Now(),
  75. }
  76. //todo
  77. // service.lampPoleService.CountRelation()
  78. err := device.Delete()
  79. if err != nil {
  80. service.OperationHisService.Save(userId, tenantId, util.OperationRemove, util.ModuleTypeDevice,
  81. util.DeviceTypeLampPoleGroup, util.GetDeviceObject(device.ID, device.PoleGroupName), util.OperationFail)
  82. return util.FailResponse(err.Error(), nil)
  83. }
  84. service.OperationHisService.Save(userId, tenantId, util.OperationRemove, util.ModuleTypeDevice,
  85. util.DeviceTypeLampPoleGroup, util.GetDeviceObject(device.ID, device.PoleGroupName), util.OperationSuccess)
  86. return nil
  87. }
  88. func (s *lampPoleGroupService) GetList(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) {
  89. var devices []*dao.LampPoleGroup
  90. err := util.Redis.Get(getLampPoleGroupListRedisKey(tenantId)).Scan(&devices)
  91. if err == nil {
  92. return devices, nil
  93. }
  94. device := &dao.LampPoleGroup{
  95. TenantId: tenantId,
  96. IsDeleted: 0,
  97. }
  98. devices, err = device.GetAllDevices()
  99. if err != nil {
  100. return nil, util.FailResponse(err.Error(), nil)
  101. }
  102. _ = util.Redis.Set(getLampPoleGroupListRedisKey(tenantId), devices, 0).Err()
  103. return devices, nil
  104. }
  105. func getLampPoleGroupListRedisKey(tenantId int) string {
  106. return fmt.Sprintf(model.LampPoleGroupList, tenantId)
  107. }
  108. func (s *lampPoleGroupService) GetFiltration(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) {
  109. var devices []*dao.LampPoleGroup
  110. err := util.Redis.Get(getGroupFiltrationListRedisKey(tenantId)).Scan(&devices)
  111. if err == nil {
  112. return devices, nil
  113. }
  114. device := &dao.LampPoleGroup{
  115. TenantId: tenantId,
  116. IsDeleted: 0,
  117. }
  118. //todo
  119. // get device_light_control group_id
  120. // groupIds := lightControl.GetGroupIds()
  121. devices, err = device.GetAllDevices()
  122. if err != nil {
  123. return nil, util.FailResponse(err.Error(), nil)
  124. }
  125. _ = util.Redis.Set(getGroupFiltrationListRedisKey(tenantId), devices, 0).Err()
  126. return devices, nil
  127. }
  128. func getGroupFiltrationListRedisKey(tenantId int) string {
  129. return fmt.Sprintf(model.GroupFiltrationList, tenantId)
  130. }
  131. func (s *lampPoleGroupService) GetTree(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) {
  132. // todo use redis cache
  133. device := &dao.LampPoleGroup{
  134. TenantId: tenantId,
  135. IsDeleted: 0,
  136. }
  137. //todo lampPole getALlDevice
  138. devices, err := device.GetAllDevices()
  139. if err != nil {
  140. return nil, util.FailResponse(err.Error(), nil)
  141. }
  142. return devices, nil
  143. }