switchBoxService.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/util"
  6. "time"
  7. )
  8. var SwitchBoxService = new(switchBoxService)
  9. type switchBoxService struct{}
  10. func (s *switchBoxService) Get(id int) (*dao.SwitchBox, *util.Errors) {
  11. // 创建查询实例
  12. device := &dao.SwitchBox{
  13. ID: id,
  14. }
  15. err := device.GetDevice()
  16. if err != nil {
  17. return nil, util.FailResponse(err.Error(), nil)
  18. }
  19. return device, nil
  20. }
  21. func (s *switchBoxService) CreateOrUpdate(userId int64, tenantId int, req dao.SwitchBox) *util.Errors {
  22. // 创建查询实例
  23. device := req
  24. device.TenantId = tenantId
  25. device.UpdateUser = userId
  26. device.UpdateTime = time.Now()
  27. if req.ID == 0 {
  28. device.CreateTime = time.Now()
  29. device.CreateUser = userId
  30. if device.IsExistedBySN() {
  31. fmt.Printf("Create GetDeviceID err \n")
  32. return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  33. }
  34. if err := device.Create(); err != nil {
  35. fmt.Printf("Create err = %s \n", err.Error())
  36. return util.FailResponse(err.Error(), nil)
  37. }
  38. return util.SuccessResponse(util.Succeeded, nil)
  39. }
  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 *switchBoxService) List(searchValue string, current, size int) ([]dao.SwitchBox, *util.Errors) {
  52. device := dao.SwitchBox{}
  53. if searchValue != "" {
  54. device.BoxSN = searchValue
  55. device.BoxName = 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 *switchBoxService) Remove(userId int64, id int) *util.Errors {
  66. // 创建查询实例
  67. device := &dao.SwitchBox{
  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 *switchBoxService) GetList(tenantId int) ([]*dao.SwitchBox, *util.Errors) {
  83. // todo use redis cache
  84. device := &dao.SwitchBox{
  85. TenantId: tenantId,
  86. IsDeleted: 0,
  87. }
  88. devices, err := device.GetAllDevices()
  89. if err != nil {
  90. return nil, util.FailResponse(err.Error(), nil)
  91. }
  92. return devices, nil
  93. }