lampPoleGroupService.go 5.6 KB

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