lampPoleGroupService.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/dao"
  5. "iot_manager_service/app/utils"
  6. "time"
  7. )
  8. // 中间件管理服务
  9. var LampPoleGroupService = new(lampPoleGroupService)
  10. type lampPoleGroupService struct{}
  11. func (s *lampPoleGroupService) Get(id int) (*dao.LampPoleGroup, *utils.Errors) {
  12. // 创建查询实例
  13. device := &dao.LampPoleGroup{
  14. ID: id,
  15. }
  16. err := device.GetDevice()
  17. if err != nil {
  18. return nil, utils.FailResponse(err.Error(), nil)
  19. }
  20. return device, nil
  21. }
  22. func (s *lampPoleGroupService) CreateOrUpdate(req *dao.LampPoleGroup) *utils.Errors {
  23. device := req
  24. if device.TenantId == "" {
  25. device.TenantId = "000000" // todo: 使用登录态
  26. }
  27. if req.ID == 0 {
  28. device.CreateTime = time.Now()
  29. device.CreateUser = "TODO" // todo: 使用登录态
  30. if device.IsExistedByName() {
  31. fmt.Printf("Create IsExistedByName \n")
  32. return utils.ParamsInvalidResponse("列表中存在重名,请更改灯杆分组名称!", nil)
  33. }
  34. if err := device.Create(); err != nil {
  35. fmt.Printf("Create err = %s \n", err.Error())
  36. return utils.FailResponse(err.Error(), nil)
  37. }
  38. return utils.SuccessResponse(utils.Succeeded, nil)
  39. }
  40. device.UpdateUser = "TODO" // todo: 使用登录态
  41. device.UpdateTime = time.Now()
  42. if device.IsExistedByNameAndCode() {
  43. fmt.Printf("Update IsExistedByNameAndCode \n")
  44. return utils.ParamsInvalidResponse("列表中存在重名,请更改灯杆分组名称!", nil)
  45. }
  46. if err := device.Update(); err != nil {
  47. fmt.Printf("Update err = %s \n", err.Error())
  48. return utils.FailResponse(err.Error(), nil)
  49. }
  50. //todo operation record
  51. return utils.SuccessResponse(utils.Succeeded, nil)
  52. }
  53. func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *utils.Errors) {
  54. // 创建查询实例
  55. device := &dao.LampPoleGroup{
  56. PoleGroupName: poleGroupName,
  57. }
  58. offset := (current - 1) * size
  59. limit := size
  60. devices, err := device.GetDevices(offset, limit)
  61. if err != nil {
  62. return nil, utils.FailResponse(err.Error(), nil)
  63. }
  64. return devices, nil
  65. }
  66. func (s *lampPoleGroupService) Remove(id int) *utils.Errors {
  67. // 创建查询实例
  68. device := &dao.LampPoleGroup{
  69. ID: id,
  70. IsDeleted: 1,
  71. UpdateUser: "TODO", // todo 使用登录态
  72. UpdateTime: time.Now(),
  73. }
  74. //todo
  75. // service.lampPoleService.CountRelation()
  76. //todo operation record
  77. err := device.Delete()
  78. if err != nil {
  79. return utils.FailResponse(err.Error(), nil)
  80. }
  81. return nil
  82. }
  83. func (s *lampPoleGroupService) GetList() ([]dao.LampPoleGroup, *utils.Errors) {
  84. // todo use redis cache
  85. device := &dao.LampPoleGroup{
  86. TenantId: "000000", // todo 使用登录态
  87. IsDeleted: 0,
  88. }
  89. devices, err := device.GetAllDevices()
  90. if err != nil {
  91. return nil, utils.FailResponse(err.Error(), nil)
  92. }
  93. return devices, nil
  94. }
  95. func (s *lampPoleGroupService) GetFiltration() ([]dao.LampPoleGroup, *utils.Errors) {
  96. // todo use redis cache
  97. device := &dao.LampPoleGroup{
  98. TenantId: "000000", // todo 使用登录态
  99. IsDeleted: 0,
  100. }
  101. //todo
  102. // get t_dev_light_control group_id
  103. // groupIds := lightControl.GetGroupIds()
  104. devices, err := device.GetAllDevices()
  105. if err != nil {
  106. return nil, utils.FailResponse(err.Error(), nil)
  107. }
  108. return devices, nil
  109. }
  110. func (s *lampPoleGroupService) GetTree() ([]dao.LampPoleGroup, *utils.Errors) {
  111. // todo use redis cache
  112. device := &dao.LampPoleGroup{
  113. TenantId: "000000", // todo 使用登录态
  114. IsDeleted: 0,
  115. }
  116. //todo lampPole getALlDevice
  117. devices, err := device.GetAllDevices()
  118. if err != nil {
  119. return nil, utils.FailResponse(err.Error(), nil)
  120. }
  121. return devices, nil
  122. }