lampPoleService.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "time"
  8. )
  9. // 中间件管理服务
  10. var LampPoleService = new(lampPoleService)
  11. type lampPoleService struct{}
  12. func (s *lampPoleService) Get(id int) (*dao.LampPole, *util.Errors) {
  13. // 创建查询实例
  14. device := &dao.LampPole{
  15. ID: id,
  16. }
  17. err := device.GetDevice()
  18. if err != nil {
  19. return nil, util.FailResponse(err.Error(), nil)
  20. }
  21. //todo runstate 需要使用各个设备的状态来处理
  22. return device, nil
  23. }
  24. func (s *lampPoleService) GetRelevanceDetail(id int) (*model.LampPoleDetail, *util.Errors) {
  25. // 创建查询实例
  26. device := &dao.LampPole{
  27. ID: id,
  28. }
  29. err := device.GetDevice()
  30. if err != nil {
  31. return nil, util.FailResponse(err.Error(), nil)
  32. }
  33. //todo get gateway ipBroadcast lightcontroller... list
  34. return &model.LampPoleDetail{
  35. LampPole: *device,
  36. AlarmTerminalList: nil,
  37. CameraList: nil,
  38. CaptureUnitList: nil,
  39. GatewayList: nil,
  40. InfoBoardList: nil,
  41. IpBroadcastList: nil,
  42. LightControlList: nil,
  43. SensorList: nil,
  44. ZigbeeList: nil,
  45. }, nil
  46. }
  47. func (s *lampPoleService) CreateOrUpdate(userId int64, tenantId int, req *dao.LampPole) *util.Errors {
  48. device := req
  49. device.TenantId = tenantId
  50. device.CoordType = 1
  51. device.UpdateUser = userId
  52. device.UpdateTime = time.Now()
  53. if device.ID == 0 {
  54. device.CreateTime = time.Now()
  55. device.CreateUser = userId
  56. if device.IsExistedBySN() {
  57. fmt.Printf("Create IsExistedBySN \n")
  58. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  59. }
  60. fmt.Printf("device = %+v \n", device)
  61. if err := device.Create(); err != nil {
  62. fmt.Printf("Create err = %s \n", err.Error())
  63. return util.FailResponse(err.Error(), nil)
  64. }
  65. return util.SuccessResponse(util.Succeeded, nil)
  66. }
  67. if err := device.Update(); err != nil {
  68. fmt.Printf("Update err = %s \n", err.Error())
  69. return util.FailResponse(err.Error(), nil)
  70. }
  71. //todo operation record
  72. return util.SuccessResponse(util.Succeeded, nil)
  73. }
  74. func (s *lampPoleService) List(searchValue, groupId, boxId string, current, size int) ([]dao.LampPole, *util.Errors) {
  75. device := dao.LampPole{}
  76. if searchValue != "" {
  77. device.PoleSN = searchValue
  78. }
  79. device.GroupId = util.StringToInt(groupId)
  80. device.BoxId = util.StringToInt(boxId)
  81. offset := (current - 1) * size
  82. limit := size
  83. devices, err := device.GetDevices(offset, limit)
  84. if err != nil {
  85. return nil, util.FailResponse(err.Error(), nil)
  86. }
  87. return devices, nil
  88. }
  89. func (s *lampPoleService) Remove(userId int64, id int) *util.Errors {
  90. // 创建查询实例
  91. device := &dao.LampPole{
  92. ID: id,
  93. IsDeleted: 1,
  94. UpdateUser: userId,
  95. UpdateTime: time.Now(),
  96. }
  97. //todo
  98. // service.lampPoleService.CountRelation()
  99. //todo operation record
  100. err := device.Delete()
  101. if err != nil {
  102. return util.FailResponse(err.Error(), nil)
  103. }
  104. return nil
  105. }
  106. func (s *lampPoleService) GetList(tenantId int) ([]*dao.LampPole, *util.Errors) {
  107. device := &dao.LampPole{
  108. TenantId: tenantId,
  109. IsDeleted: 0,
  110. }
  111. devices, err := device.GetAllDevices()
  112. for _, device := range devices {
  113. device.PoleName = device.PoleName + "(" + device.PoleSN + ")"
  114. }
  115. if err != nil {
  116. return nil, util.FailResponse(err.Error(), nil)
  117. }
  118. return devices, nil
  119. }
  120. func (s *lampPoleService) GetOne(id int) (*dao.LampPole, *util.Errors) {
  121. device := &dao.LampPole{
  122. ID: id,
  123. }
  124. err := device.GetDevice()
  125. if err != nil {
  126. return nil, util.FailResponse(err.Error(), nil)
  127. }
  128. return device, nil
  129. }
  130. func (s *lampPoleService) CountLampPole(groupId int) int {
  131. device := &dao.LampPole{
  132. GroupId: groupId,
  133. }
  134. return device.Count()
  135. }