garbageWayGroupService.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 GarbageWayGroupService = new(garbageWayGroupService)
  13. type garbageWayGroupService struct{}
  14. func (s *garbageWayGroupService) Get(id int) (*dao.GarbageWayGroup, *common.Errors) {
  15. // 创建查询实例
  16. device := &dao.GarbageWayGroup{
  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 *garbageWayGroupService) CreateOrUpdate(userId int, tenantId string, req dao.GarbageWayGroup) *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.IsExistedByName() {
  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.DeviceTypeGarbageGroup, common.GetDeviceObject(device.ID, device.GroupName), 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.DeviceTypeGarbageGroup, common.GetDeviceObject(device.ID, device.GroupName), common.OperationSuccess)
  55. return common.SuccessResponse(common.Succeeded, nil)
  56. }
  57. func (s *garbageWayGroupService) List(searchValue string, current, size int) ([]dao.GarbageWayGroup, int64, *common.Errors) {
  58. device := dao.GarbageWayGroup{}
  59. if searchValue != "" {
  60. device.GroupName = searchValue
  61. }
  62. offset := (current - 1) * size
  63. limit := size
  64. devices, total, err := device.GetDevices(offset, limit)
  65. if err != nil {
  66. return nil, 0, common.FailResponse(err.Error(), nil)
  67. }
  68. return devices, total, nil
  69. }
  70. func (s *garbageWayGroupService) Remove(userId int, tenantId string, id int) *common.Errors {
  71. // 创建查询实例
  72. device := &dao.GarbageWayGroup{
  73. ID: id,
  74. IsDeleted: 1,
  75. UpdateUser: userId,
  76. UpdateTime: time.Now(),
  77. }
  78. err := device.Delete()
  79. if err != nil {
  80. return common.FailResponse(err.Error(), nil)
  81. }
  82. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  83. common.DeviceTypeGarbageGroup, common.GetDeviceObject(device.ID, device.GroupName), common.OperationSuccess)
  84. return nil
  85. }
  86. func (s *garbageWayGroupService) GetList(tenantId string) ([]*dao.GarbageWayGroup, *common.Errors) {
  87. var devices []*dao.GarbageWayGroup
  88. err := cache.Redis.Get(getGarbageWayGroupListRedisKey("000000")).Scan(&devices)
  89. if err == nil {
  90. return devices, nil
  91. }
  92. device := &dao.GarbageWayGroup{
  93. TenantId: tenantId,
  94. IsDeleted: 0,
  95. }
  96. devices, err = device.GetAllDevices()
  97. if err != nil {
  98. return nil, common.FailResponse(err.Error(), nil)
  99. }
  100. _ = cache.Redis.Set(getGarbageWayGroupListRedisKey("000000"), devices, 0).Err()
  101. return devices, nil
  102. }
  103. func getGarbageWayGroupListRedisKey(tenantId string) string {
  104. return fmt.Sprintf(model.GarbageWayGroupList, tenantId)
  105. }