lampPoleGroupService.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. } else {
  46. if err := device.Update(); err != nil {
  47. logger.Logger.Errorf("Update err = %s \n", err.Error())
  48. return common.FailResponse(err.Error(), nil)
  49. }
  50. }
  51. if device.IsExistedByNameAndCode() {
  52. logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
  53. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  54. }
  55. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  56. common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
  57. return common.SuccessResponse(common.Succeeded, nil)
  58. }
  59. func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *common.Errors) {
  60. // 创建查询实例
  61. device := &dao.LampPoleGroup{
  62. PoleGroupName: poleGroupName,
  63. }
  64. offset := (current - 1) * size
  65. limit := size
  66. devices, err := device.GetDevices(offset, limit)
  67. if err != nil {
  68. return nil, common.FailResponse(err.Error(), nil)
  69. }
  70. for i, group := range devices {
  71. poleGroup := dao.LampPole{}
  72. poleGroup.GroupId = group.ID
  73. devices[i].CountLampPole, _ = poleGroup.GetGroupIdCount()
  74. }
  75. return devices, nil
  76. }
  77. func (s *lampPoleGroupService) Remove(userId int64, tenantId int, id int, name string) *common.Errors {
  78. device := &dao.LampPoleGroup{
  79. ID: id,
  80. IsDeleted: 1,
  81. UpdateUser: userId,
  82. UpdateTime: common.Time(time.Now()),
  83. }
  84. fmt.Printf("device = %v", device)
  85. //todo
  86. // service.lampPoleService.CountRelation()
  87. err := device.Delete()
  88. if err != nil {
  89. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  90. common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationFail)
  91. return common.FailResponse(err.Error(), nil)
  92. }
  93. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  94. common.DeviceTypeLampPoleGroup, common.GetDeviceObject(device.ID, device.PoleGroupName), common.OperationSuccess)
  95. return nil
  96. }
  97. func (s *lampPoleGroupService) GetList(tenantId int) ([]*dao.LampPoleGroup, *common.Errors) {
  98. var devices []*dao.LampPoleGroup
  99. err := cache.Redis.Get(getLampPoleGroupListRedisKey(tenantId)).Scan(&devices)
  100. if err == nil {
  101. return devices, nil
  102. }
  103. device := &dao.LampPoleGroup{
  104. TenantId: tenantId,
  105. IsDeleted: 0,
  106. }
  107. devices, err = device.GetAllDevices()
  108. if err != nil {
  109. return nil, common.FailResponse(err.Error(), nil)
  110. }
  111. _ = cache.Redis.Set(getLampPoleGroupListRedisKey(tenantId), devices, 0).Err()
  112. return devices, nil
  113. }
  114. func getLampPoleGroupListRedisKey(tenantId int) string {
  115. return fmt.Sprintf(model.LampPoleGroupList, tenantId)
  116. }
  117. func (s *lampPoleGroupService) GetFiltration(tenantId int) ([]*dao.LampPoleGroup, *common.Errors) {
  118. var devices []*dao.LampPoleGroup
  119. err := cache.Redis.Get(getGroupFiltrationListRedisKey(tenantId)).Scan(&devices)
  120. if err == nil {
  121. return devices, nil
  122. }
  123. device := &dao.LampPoleGroup{
  124. TenantId: tenantId,
  125. IsDeleted: 0,
  126. }
  127. //todo
  128. // get device_light_control group_id
  129. // groupIds := lightControl.GetGroupIds()
  130. devices, err = device.GetAllDevices()
  131. if err != nil {
  132. return nil, common.FailResponse(err.Error(), nil)
  133. }
  134. _ = cache.Redis.Set(getGroupFiltrationListRedisKey(tenantId), devices, 0).Err()
  135. return devices, nil
  136. }
  137. func getGroupFiltrationListRedisKey(tenantId int) string {
  138. return fmt.Sprintf(model.GroupFiltrationList, tenantId)
  139. }
  140. func (s *lampPoleGroupService) GetTree(tenantId int) ([]*dao.LampPoleGroup, *common.Errors) {
  141. // todo use redis cache
  142. device := &dao.LampPoleGroup{
  143. TenantId: tenantId,
  144. IsDeleted: 0,
  145. }
  146. //todo lampPole getALlDevice
  147. devices, err := device.GetAllDevices()
  148. if err != nil {
  149. return nil, common.FailResponse(err.Error(), nil)
  150. }
  151. return devices, nil
  152. }
  153. func (s *lampPoleGroupService) GetByIds(tenantId int, ids []int) []*dao.LampPoleGroup {
  154. device := &dao.LampPoleGroup{
  155. TenantId: tenantId,
  156. IsDeleted: 0,
  157. }
  158. return device.BatchGet(ids)
  159. }