lightControlService.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/dao"
  5. "iot_manager_service/app/model"
  6. "iot_manager_service/app/utils"
  7. "time"
  8. )
  9. // 中间件管理服务
  10. var LightControlService = new(lightControlService)
  11. type lightControlService struct{}
  12. func (s *lightControlService) Get(id int) (*dao.LightControl, *utils.Errors) {
  13. // 创建查询实例
  14. device := &dao.LightControl{
  15. ID: id,
  16. }
  17. err := device.GetDevice()
  18. if err != nil {
  19. return nil, utils.FailResponse(err.Error(), nil)
  20. }
  21. //todo runstate 需要使用各个设备的状态来处理
  22. return device, nil
  23. }
  24. func (s *lightControlService) GetRelevanceDetail(id int) (*model.LightControlDetail, *utils.Errors) {
  25. // 创建查询实例
  26. device := &dao.LightControl{
  27. ID: id,
  28. }
  29. err := device.GetDevice()
  30. if err != nil {
  31. return nil, utils.FailResponse(err.Error(), nil)
  32. }
  33. //todo get gateway ipBroadcast lightcontroller... list
  34. return &model.LightControlDetail{
  35. LightControl: *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 *lightControlService) CreateOrUpdate(req *dao.LightControl) *utils.Errors {
  48. device := req
  49. if device.TenantId == "" {
  50. device.TenantId = "000000" // todo: 使用登录态
  51. }
  52. device.UpdateUser = "TODO" // todo: 使用登录态
  53. device.UpdateTime = time.Now()
  54. if device.ID == 0 {
  55. device.CreateTime = time.Now()
  56. device.CreateUser = "TODO" // todo: 使用登录态
  57. if device.IsExistedBySN() {
  58. fmt.Printf("Create IsExistedBySN \n")
  59. return utils.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  60. }
  61. fmt.Printf("device = %+v \n", device)
  62. if err := device.Create(); err != nil {
  63. fmt.Printf("Create err = %s \n", err.Error())
  64. return utils.FailResponse(err.Error(), nil)
  65. }
  66. return utils.SuccessResponse(utils.Succeeded, nil)
  67. }
  68. if err := device.Update(); err != nil {
  69. fmt.Printf("Update err = %s \n", err.Error())
  70. return utils.FailResponse(err.Error(), nil)
  71. }
  72. //todo operation record
  73. return utils.SuccessResponse(utils.Succeeded, nil)
  74. }
  75. func (s *lightControlService) List(searchValue, groupId, boxId string, current, size int) ([]dao.LightControl, *utils.Errors) {
  76. device := dao.LightControl{}
  77. if searchValue != "" {
  78. device.SN = searchValue
  79. }
  80. device.GroupId = utils.StringToInt(groupId)
  81. offset := (current - 1) * size
  82. limit := size
  83. devices, err := device.GetDevices(offset, limit)
  84. if err != nil {
  85. return nil, utils.FailResponse(err.Error(), nil)
  86. }
  87. return devices, nil
  88. }
  89. func (s *lightControlService) Remove(id int) *utils.Errors {
  90. // 创建查询实例
  91. device := &dao.LightControl{
  92. ID: id,
  93. IsDeleted: 1,
  94. UpdateUser: "TODO", // todo 使用登录态
  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 utils.FailResponse(err.Error(), nil)
  103. }
  104. return nil
  105. }
  106. func (s *lightControlService) GetList() ([]*dao.LightControl, *utils.Errors) {
  107. // todo use redis cache
  108. device := &dao.LightControl{
  109. TenantId: "000000", // todo 使用登录态
  110. IsDeleted: 0,
  111. }
  112. devices, err := device.GetAllDevices()
  113. if err != nil {
  114. return nil, utils.FailResponse(err.Error(), nil)
  115. }
  116. return devices, nil
  117. }
  118. func (s *lightControlService) GetOne(id int) (*dao.LightControl, *utils.Errors) {
  119. device := &dao.LightControl{
  120. ID: id,
  121. }
  122. err := device.GetDevice()
  123. if err != nil {
  124. return nil, utils.FailResponse(err.Error(), nil)
  125. }
  126. return device, nil
  127. }