lampPoleGroupService.go 5.2 KB

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