switchBoxService.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 int64, tenantId int, 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, *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, err := device.GetDevices(offset, limit)
  69. if err != nil {
  70. return nil, common.FailResponse(err.Error(), nil)
  71. }
  72. return devices, nil
  73. }
  74. func (s *switchBoxService) Remove(userId int64, tenantId int, id int) *common.Errors {
  75. // 创建查询实例
  76. device := &dao.SwitchBox{
  77. ID: id,
  78. IsDeleted: 1,
  79. UpdateUser: userId,
  80. UpdateTime: time.Now(),
  81. }
  82. err := device.Delete()
  83. if err != nil {
  84. return common.FailResponse(err.Error(), nil)
  85. }
  86. cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
  87. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  88. common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
  89. return nil
  90. }
  91. func (s *switchBoxService) GetList(tenantId int) ([]*dao.SwitchBox, *common.Errors) {
  92. var devices []*dao.SwitchBox
  93. err := cache.Redis.Get(getSwitchBoxListRedisKey(tenantId)).Scan(&devices)
  94. if err == nil {
  95. return devices, nil
  96. }
  97. device := &dao.SwitchBox{
  98. TenantId: tenantId,
  99. IsDeleted: 0,
  100. }
  101. devices, err = device.GetAllDevices()
  102. if err != nil {
  103. return nil, common.FailResponse(err.Error(), nil)
  104. }
  105. _ = cache.Redis.Set(getSwitchBoxListRedisKey(tenantId), devices, 0).Err()
  106. return devices, nil
  107. }
  108. func getSwitchBoxListRedisKey(tenantId int) string {
  109. return fmt.Sprintf(model.SwitchBoxList, tenantId)
  110. }