garbageWayGroupService.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/util"
  7. "time"
  8. )
  9. var GarbageWayGroupService = new(garbageWayGroupService)
  10. type garbageWayGroupService struct{}
  11. func (s *garbageWayGroupService) Get(id int) (*dao.GarbageWayGroup, *util.Errors) {
  12. // 创建查询实例
  13. device := &dao.GarbageWayGroup{
  14. ID: id,
  15. }
  16. err := device.GetDevice()
  17. if err != nil {
  18. return nil, util.FailResponse(err.Error(), nil)
  19. }
  20. return device, nil
  21. }
  22. func (s *garbageWayGroupService) CreateOrUpdate(userId int64, tenantId int, req dao.GarbageWayGroup) *util.Errors {
  23. // 创建查询实例
  24. device := req
  25. device.TenantId = tenantId
  26. device.UpdateUser = userId
  27. device.UpdateTime = time.Now()
  28. if req.ID == 0 {
  29. device.CreateTime = time.Now()
  30. device.CreateUser = userId
  31. if device.IsExistedByName() {
  32. fmt.Printf("Create GetDeviceID err \n")
  33. return util.ParamsInvalidResponse("存在重名,请更改名称!", nil)
  34. }
  35. if err := device.Create(); err != nil {
  36. fmt.Printf("Create err = %s \n", err.Error())
  37. return util.FailResponse(err.Error(), nil)
  38. }
  39. return util.SuccessResponse(util.Succeeded, nil)
  40. }
  41. if device.IsExistedByNameAndCode() {
  42. return util.ParamsInvalidResponse("列表中存在重名,请更改垃圾桶道路名称!", nil)
  43. }
  44. if err := device.Update(); err != nil {
  45. fmt.Printf("Update err = %s \n", err.Error())
  46. return util.FailResponse(err.Error(), nil)
  47. }
  48. //todo operation record
  49. return util.SuccessResponse(util.Succeeded, nil)
  50. }
  51. func (s *garbageWayGroupService) List(searchValue string, current, size int) ([]dao.GarbageWayGroup, *util.Errors) {
  52. device := dao.GarbageWayGroup{}
  53. //if searchValue != "" {
  54. // device.TransSN = searchValue
  55. // device.TransName = searchValue
  56. //}
  57. offset := (current - 1) * size
  58. limit := size
  59. devices, err := device.GetDevices(offset, limit)
  60. if err != nil {
  61. return nil, util.FailResponse(err.Error(), nil)
  62. }
  63. return devices, nil
  64. }
  65. func (s *garbageWayGroupService) Remove(userId int64, id int) *util.Errors {
  66. // 创建查询实例
  67. device := &dao.GarbageWayGroup{
  68. ID: id,
  69. IsDeleted: 1,
  70. UpdateUser: userId,
  71. UpdateTime: time.Now(),
  72. }
  73. //todo
  74. // service.transformerService.CountRelation()
  75. //todo operation record
  76. err := device.Delete()
  77. if err != nil {
  78. return util.FailResponse(err.Error(), nil)
  79. }
  80. return nil
  81. }
  82. func (s *garbageWayGroupService) GetList(tenantId int) ([]*dao.GarbageWayGroup, *util.Errors) {
  83. var devices []*dao.GarbageWayGroup
  84. err := util.Redis.Get(getGarbageWayGroupListRedisKey("000000")).Scan(&devices)
  85. if err == nil {
  86. return devices, nil
  87. }
  88. device := &dao.GarbageWayGroup{
  89. TenantId: tenantId,
  90. IsDeleted: 0,
  91. }
  92. devices, err = device.GetAllDevices()
  93. if err != nil {
  94. return nil, util.FailResponse(err.Error(), nil)
  95. }
  96. _ = util.Redis.Set(getGarbageWayGroupListRedisKey("000000"), devices, 0).Err()
  97. return devices, nil
  98. }
  99. func getGarbageWayGroupListRedisKey(tenantId string) string {
  100. return fmt.Sprintf(model.GarbageWayGroupList, tenantId)
  101. }