garbageService.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 GarbageService = new(garbageService)
  13. type garbageService struct{}
  14. func (s *garbageService) Get(id int) (*dao.Garbage, *common.Errors) {
  15. // 创建查询实例
  16. device := &dao.Garbage{
  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 *garbageService) CreateOrUpdate(userId int, tenantId string, req dao.Garbage) *common.Errors {
  26. // 创建查询实例
  27. device := req
  28. device.UpdateUser = userId
  29. device.TenantId = tenantId
  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. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  43. common.DeviceTypeGarbage, common.GetDeviceObject(device.ID, device.DeviceName), common.OperationSuccess)
  44. return common.SuccessResponse(common.Succeeded, nil)
  45. }
  46. if device.IsExistedByNameAndCode() {
  47. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  48. }
  49. if err := device.Update(); err != nil {
  50. logger.Logger.Errorf("Update err = %s \n", err.Error())
  51. return common.FailResponse(err.Error(), nil)
  52. }
  53. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  54. common.DeviceTypeGarbage, common.GetDeviceObject(device.ID, device.DeviceName), common.OperationSuccess)
  55. return common.SuccessResponse(common.Succeeded, nil)
  56. }
  57. func (s *garbageService) List(searchValue string, current, size int) ([]dao.Garbage, int64, *common.Errors) {
  58. device := dao.Garbage{}
  59. if searchValue != "" {
  60. device.DeviceSN = searchValue
  61. device.DeviceName = searchValue
  62. }
  63. offset := (current - 1) * size
  64. limit := size
  65. devices, total, err := device.GetDevices(offset, limit)
  66. if err != nil {
  67. return nil, 0, common.FailResponse(err.Error(), nil)
  68. }
  69. return devices, total, nil
  70. }
  71. func (s *garbageService) Remove(userId int, tenantId string, id int) *common.Errors {
  72. // 创建查询实例
  73. device := &dao.Garbage{
  74. ID: id,
  75. IsDeleted: 1,
  76. UpdateUser: userId,
  77. UpdateTime: time.Now(),
  78. }
  79. err := device.Delete()
  80. if err != nil {
  81. return common.FailResponse(err.Error(), nil)
  82. }
  83. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  84. common.DeviceTypeGarbage, common.GetDeviceObject(device.ID, device.DeviceName), common.OperationSuccess)
  85. return nil
  86. }
  87. func (s *garbageService) GetList(tenantId string) ([]*dao.Garbage, *common.Errors) {
  88. var devices []*dao.Garbage
  89. err := cache.Redis.Get(getGarbageListRedisKey(tenantId)).Scan(&devices)
  90. if err == nil {
  91. return devices, nil
  92. }
  93. device := &dao.Garbage{
  94. TenantId: tenantId,
  95. IsDeleted: 0,
  96. }
  97. devices, err = device.GetAllDevices()
  98. if err != nil {
  99. return nil, common.FailResponse(err.Error(), nil)
  100. }
  101. _ = cache.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err()
  102. return devices, nil
  103. }
  104. func getGarbageListRedisKey(tenantId string) string {
  105. return fmt.Sprintf(model.GarbageList, tenantId)
  106. }
  107. func (s *garbageService) GetAll() []*dao.Garbage {
  108. device := &dao.Garbage{}
  109. devices := device.GetAll()
  110. return devices
  111. }