garbageService.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/dao"
  5. "iot_manager_service/app/model"
  6. "iot_manager_service/app/utils"
  7. "time"
  8. )
  9. var GarbageService = new(garbageService)
  10. type garbageService struct{}
  11. func (s *garbageService) Get(id int) (*dao.Garbage, *utils.Errors) {
  12. // 创建查询实例
  13. device := &dao.Garbage{
  14. ID: id,
  15. }
  16. err := device.GetDevice()
  17. if err != nil {
  18. return nil, utils.FailResponse(err.Error(), nil)
  19. }
  20. return device, nil
  21. }
  22. func (s *garbageService) CreateOrUpdate(req dao.Garbage) *utils.Errors {
  23. // 创建查询实例
  24. device := req
  25. if req.ID == 0 {
  26. device.CreateTime = time.Now()
  27. device.CreateUser = "TODO" // todo: 使用登录态
  28. if device.IsExistedBySN() {
  29. fmt.Printf("Create GetDeviceID err \n")
  30. return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  31. }
  32. if err := device.Create(); err != nil {
  33. fmt.Printf("Create err = %s \n", err.Error())
  34. return utils.FailResponse(err.Error(), nil)
  35. }
  36. return utils.SuccessResponse(utils.Succeeded, nil)
  37. }
  38. device.UpdateUser = "TODO" // todo: 使用登录态
  39. device.UpdateTime = time.Now()
  40. if device.IsExistedByNameAndCode() {
  41. return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  42. }
  43. if err := device.Update(); err != nil {
  44. fmt.Printf("Update err = %s \n", err.Error())
  45. return utils.FailResponse(err.Error(), nil)
  46. }
  47. //todo operation record
  48. return utils.SuccessResponse(utils.Succeeded, nil)
  49. }
  50. func (s *garbageService) List(searchValue string, current, size int) ([]dao.Garbage, *utils.Errors) {
  51. device := dao.Garbage{}
  52. if searchValue != "" {
  53. device.DeviceSN = searchValue
  54. device.DeviceName = searchValue
  55. }
  56. offset := (current - 1) * size
  57. limit := size
  58. devices, err := device.GetDevices(offset, limit)
  59. if err != nil {
  60. return nil, utils.FailResponse(err.Error(), nil)
  61. }
  62. return devices, nil
  63. }
  64. func (s *garbageService) Remove(id int) *utils.Errors {
  65. // 创建查询实例
  66. device := &dao.Garbage{
  67. ID: id,
  68. IsDeleted: 1,
  69. UpdateUser: "TODO", // todo 使用登录态
  70. UpdateTime: time.Now(),
  71. }
  72. //todo
  73. // service.garbageWayService.CountRelation()
  74. //todo operation record
  75. err := device.Delete()
  76. if err != nil {
  77. return utils.FailResponse(err.Error(), nil)
  78. }
  79. return nil
  80. }
  81. func (s *garbageService) GetList() ([]*dao.Garbage, *utils.Errors) {
  82. var devices []*dao.Garbage
  83. err := utils.Redis.Get(getGarbageListRedisKey("000000")).Scan(&devices)
  84. if err == nil {
  85. return devices, nil
  86. }
  87. device := &dao.Garbage{
  88. TenantId: "000000", // todo 使用登录态
  89. IsDeleted: 0,
  90. }
  91. devices, err = device.GetAllDevices()
  92. if err != nil {
  93. return nil, utils.FailResponse(err.Error(), nil)
  94. }
  95. _ = utils.Redis.Set(getAlarmListRedisKey("000000"), devices, 0).Err()
  96. return devices, nil
  97. }
  98. func getGarbageListRedisKey(tenantId string) string {
  99. return fmt.Sprintf(model.GarbageList, tenantId)
  100. }