switchBoxService.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. var SwitchBoxService = new(switchBoxService)
  13. type switchBoxService struct{}
  14. func (s *switchBoxService) Get(id int) (*dao.SwitchBox, *common.Errors) {
  15. // 创建查询实例
  16. device := &dao.SwitchBox{
  17. ID: id,
  18. }
  19. err := device.GetDevice()
  20. if err != nil {
  21. return nil, common.FailResponse(err.Error(), nil)
  22. }
  23. return device, nil
  24. }
  25. func (s *switchBoxService) CreateOrUpdate(userId int, tenantId string, req dao.SwitchBox) *common.Errors {
  26. // 创建查询实例
  27. device := req
  28. device.TenantId = tenantId
  29. device.UpdateUser = userId
  30. device.UpdateTime = time.Now()
  31. if req.ID == 0 {
  32. device.CreateTime = time.Now()
  33. device.CreateUser = userId
  34. if device.IsExistedBySN() {
  35. logger.Logger.Errorf("Create GetDeviceID err \n")
  36. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", 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. cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
  43. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  44. common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
  45. return common.SuccessResponse(common.Succeeded, nil)
  46. }
  47. //更新
  48. if device.IsExistedByNameAndCode() {
  49. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  50. }
  51. if err := device.Update(); err != nil {
  52. logger.Logger.Errorf("Update err = %s \n", err.Error())
  53. return common.FailResponse(err.Error(), nil)
  54. }
  55. cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
  56. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  57. common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
  58. return common.SuccessResponse(common.Succeeded, nil)
  59. }
  60. func (s *switchBoxService) List(searchValue string, current, size int) ([]dao.SwitchBox, int64, *common.Errors) {
  61. device := dao.SwitchBox{}
  62. if searchValue != "" {
  63. device.BoxSN = searchValue
  64. device.BoxName = searchValue
  65. }
  66. offset := (current - 1) * size
  67. limit := size
  68. devices, total, err := device.GetDevices(offset, limit)
  69. if err != nil {
  70. return nil, 0, common.FailResponse(err.Error(), nil)
  71. }
  72. //关联灯杆
  73. for i, box := range devices {
  74. pole := dao.LampPole{}
  75. pole.BoxId = box.ID
  76. devices[i].CountLampPole, _ = pole.GetSwitchBoxCount()
  77. }
  78. return devices, total, nil
  79. }
  80. func (s *switchBoxService) Remove(userId int, tenantId string, id int) *common.Errors {
  81. // 创建查询实例
  82. device := &dao.SwitchBox{
  83. ID: id,
  84. IsDeleted: 1,
  85. UpdateUser: userId,
  86. UpdateTime: time.Now(),
  87. }
  88. err := device.Delete()
  89. if err != nil {
  90. return common.FailResponse(err.Error(), nil)
  91. }
  92. cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
  93. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  94. common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
  95. return nil
  96. }
  97. func (s *switchBoxService) GetList(tenantId string) ([]*dao.SwitchBox, *common.Errors) {
  98. var devices []*dao.SwitchBox
  99. err := cache.Redis.Get(getSwitchBoxListRedisKey(tenantId)).Scan(&devices)
  100. if err == nil {
  101. return devices, nil
  102. }
  103. device := &dao.SwitchBox{
  104. TenantId: tenantId,
  105. IsDeleted: 0,
  106. }
  107. devices, err = device.GetAllDevices()
  108. if err != nil {
  109. return nil, common.FailResponse(err.Error(), nil)
  110. }
  111. _ = cache.Redis.Set(getSwitchBoxListRedisKey(tenantId), devices, 0).Err()
  112. return devices, nil
  113. }
  114. func getSwitchBoxListRedisKey(tenantId string) string {
  115. return fmt.Sprintf(model.SwitchBoxList, tenantId)
  116. }
  117. func (s *switchBoxService) DeviceCount() dao.DeviceStatus {
  118. var rsp dao.DeviceStatus
  119. d := dao.SwitchBox{}
  120. rsp.Set(d.DeviceCount1())
  121. return rsp
  122. }
  123. func (s *switchBoxService) ListDevices() []dao.Device {
  124. d := dao.SwitchBox{}
  125. return d.ListDevices1()
  126. }