transformerService.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 TransformerService = new(transformerService)
  13. type transformerService struct{}
  14. func (s *transformerService) Get(id int) (*dao.Transformer, *common.Errors) {
  15. // 创建查询实例
  16. device := &dao.Transformer{
  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 *transformerService) CreateOrUpdate(userId int, tenantId string, req dao.Transformer) *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. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  43. common.DeviceTypeTransformer, common.GetDeviceObject(device.ID, device.TransName), 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.DeviceTypeTransformer, common.GetDeviceObject(device.ID, device.TransName), common.OperationSuccess)
  55. return common.SuccessResponse(common.Succeeded, nil)
  56. }
  57. func (s *transformerService) List(searchValue string, current, size int) ([]dao.Transformer, int64, *common.Errors) {
  58. device := dao.Transformer{}
  59. if searchValue != "" {
  60. device.TransSN = searchValue
  61. device.TransName = 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. for i, transformer := range devices {
  70. switchBox := dao.SwitchBox{TransformerId: transformer.ID}
  71. devices[i].CountNum, _ = switchBox.GetDeviceCount()
  72. }
  73. return devices, total, nil
  74. }
  75. func (s *transformerService) Remove(userId int, tenantId string, id int) *common.Errors {
  76. // 创建查询实例
  77. device := &dao.Transformer{
  78. ID: id,
  79. IsDeleted: 1,
  80. UpdateUser: userId,
  81. UpdateTime: time.Now(),
  82. }
  83. err := device.Delete()
  84. if err != nil {
  85. return common.FailResponse(err.Error(), nil)
  86. }
  87. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  88. common.DeviceTypeTransformer, common.GetDeviceObject(device.ID, device.TransName), common.OperationSuccess)
  89. return nil
  90. }
  91. func (s *transformerService) GetList(tenantId string) ([]*dao.Transformer, *common.Errors) {
  92. var devices []*dao.Transformer
  93. err := cache.Redis.Get(getTransformerListRedisKey(tenantId)).Scan(&devices)
  94. if err == nil {
  95. return devices, nil
  96. }
  97. device := &dao.Transformer{
  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(getTransformerListRedisKey(tenantId), devices, 0).Err()
  106. return devices, nil
  107. }
  108. func getTransformerListRedisKey(tenantId string) string {
  109. return fmt.Sprintf(model.TransformerList, tenantId)
  110. }