lampPoleGroupService.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/util"
  7. "strings"
  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. var build strings.Builder
  26. device := req
  27. device.TenantId = tenantId
  28. device.UpdateUser = userId
  29. device.UpdateTime = time.Now()
  30. if req.ID == 0 {
  31. device.CreateTime = time.Now()
  32. device.CreateUser = userId
  33. build.WriteString("新增(灯杆分组)【" + device.PoleGroupName + "】")
  34. if device.IsExistedByName() {
  35. fmt.Printf("Create IsExistedByName \n")
  36. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  37. }
  38. if err := device.Create(); err != nil {
  39. fmt.Printf("Create err = %s \n", err.Error())
  40. build.WriteString("失败")
  41. OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String())
  42. return util.FailResponse(err.Error(), nil)
  43. }
  44. build.WriteString("成功")
  45. OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String())
  46. return util.SuccessResponse(util.Succeeded, nil)
  47. }
  48. if device.IsExistedByNameAndCode() {
  49. fmt.Printf("Update IsExistedByNameAndCode \n")
  50. return util.ParamsInvalidResponse(model.RepeatedName, nil)
  51. }
  52. build.WriteString("修改(灯杆分组)【" + device.PoleGroupName + "】")
  53. if err := device.Update(); err != nil {
  54. fmt.Printf("Update err = %s \n", err.Error())
  55. build.WriteString("失败")
  56. OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String())
  57. return util.FailResponse(err.Error(), nil)
  58. }
  59. build.WriteString("成功")
  60. OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String())
  61. //todo operation record
  62. return util.SuccessResponse(util.Succeeded, nil)
  63. }
  64. func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *util.Errors) {
  65. // 创建查询实例
  66. device := &dao.LampPoleGroup{
  67. PoleGroupName: poleGroupName,
  68. }
  69. offset := (current - 1) * size
  70. limit := size
  71. devices, err := device.GetDevices(offset, limit)
  72. if err != nil {
  73. return nil, util.FailResponse(err.Error(), nil)
  74. }
  75. return devices, nil
  76. }
  77. func (s *lampPoleGroupService) Remove(userId int64, tenantId int, id int, name string) *util.Errors {
  78. var build strings.Builder
  79. device := &dao.LampPoleGroup{
  80. ID: id,
  81. IsDeleted: 1,
  82. UpdateUser: userId,
  83. UpdateTime: time.Now(),
  84. }
  85. //todo
  86. // service.lampPoleService.CountRelation()
  87. build.WriteString("删除(灯杆分组)【" + name + "】")
  88. err := device.Delete()
  89. if err != nil {
  90. build.WriteString("失败")
  91. OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String())
  92. return util.FailResponse(err.Error(), nil)
  93. }
  94. build.WriteString("成功")
  95. OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String())
  96. return nil
  97. }
  98. func (s *lampPoleGroupService) GetList(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) {
  99. var devices []*dao.LampPoleGroup
  100. err := util.Redis.Get(getLampPoleGroupListRedisKey(tenantId)).Scan(&devices)
  101. if err == nil {
  102. return devices, nil
  103. }
  104. device := &dao.LampPoleGroup{
  105. TenantId: tenantId,
  106. IsDeleted: 0,
  107. }
  108. devices, err = device.GetAllDevices()
  109. if err != nil {
  110. return nil, util.FailResponse(err.Error(), nil)
  111. }
  112. _ = util.Redis.Set(getLampPoleGroupListRedisKey(tenantId), devices, 0).Err()
  113. return devices, nil
  114. }
  115. func getLampPoleGroupListRedisKey(tenantId int) string {
  116. return fmt.Sprintf(model.LampPoleGroupList, tenantId)
  117. }
  118. func (s *lampPoleGroupService) GetFiltration(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) {
  119. var devices []*dao.LampPoleGroup
  120. err := util.Redis.Get(getGroupFiltrationListRedisKey(tenantId)).Scan(&devices)
  121. if err == nil {
  122. return devices, nil
  123. }
  124. device := &dao.LampPoleGroup{
  125. TenantId: tenantId,
  126. IsDeleted: 0,
  127. }
  128. //todo
  129. // get t_dev_light_control group_id
  130. // groupIds := lightControl.GetGroupIds()
  131. devices, err = device.GetAllDevices()
  132. if err != nil {
  133. return nil, util.FailResponse(err.Error(), nil)
  134. }
  135. _ = util.Redis.Set(getGroupFiltrationListRedisKey(tenantId), devices, 0).Err()
  136. return devices, nil
  137. }
  138. func getGroupFiltrationListRedisKey(tenantId int) string {
  139. return fmt.Sprintf(model.GroupFiltrationList, tenantId)
  140. }
  141. func (s *lampPoleGroupService) GetTree(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) {
  142. // todo use redis cache
  143. device := &dao.LampPoleGroup{
  144. TenantId: tenantId,
  145. IsDeleted: 0,
  146. }
  147. //todo lampPole getALlDevice
  148. devices, err := device.GetAllDevices()
  149. if err != nil {
  150. return nil, util.FailResponse(err.Error(), nil)
  151. }
  152. return devices, nil
  153. }