zigbeeService.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/util"
  6. "time"
  7. )
  8. var ZigbeeService = new(zigbeeService)
  9. type zigbeeService struct{}
  10. func (s *zigbeeService) Get(id int) (*dao.Zigbee, *util.Errors) {
  11. // 创建查询实例
  12. device := &dao.Zigbee{
  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 *zigbeeService) CreateOrUpdate(userId int64, tenantId int, req dao.Zigbee) *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. if device.IsExistedByNameAndCode() {
  41. return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  42. }
  43. if err := device.Update(); err != nil {
  44. fmt.Printf("Update err = %s \n", err.Error())
  45. return util.FailResponse(err.Error(), nil)
  46. }
  47. //todo operation record
  48. return util.SuccessResponse(util.Succeeded, nil)
  49. }
  50. func (s *zigbeeService) List(searchValue string, current, size int) ([]dao.Zigbee, *util.Errors) {
  51. device := dao.Zigbee{}
  52. if searchValue != "" {
  53. device.SN = searchValue
  54. device.Name = searchValue
  55. }
  56. offset := (current - 1) * size
  57. limit := size
  58. devices, err := device.GetDevices(offset, limit)
  59. if err != nil {
  60. return nil, util.FailResponse(err.Error(), nil)
  61. }
  62. return devices, nil
  63. }
  64. func (s *zigbeeService) Remove(userId int64, id int) *util.Errors {
  65. // 创建查询实例
  66. device := &dao.Zigbee{
  67. ID: id,
  68. IsDeleted: 1,
  69. UpdateUser: userId,
  70. UpdateTime: time.Now(),
  71. }
  72. //todo
  73. // service.transformerService.CountRelation()
  74. //todo operation record
  75. err := device.Delete()
  76. if err != nil {
  77. return util.FailResponse(err.Error(), nil)
  78. }
  79. return nil
  80. }
  81. func (s *zigbeeService) GetList(tenantId int) ([]*dao.Zigbee, *util.Errors) {
  82. device := &dao.Zigbee{
  83. TenantId: tenantId,
  84. IsDeleted: 0,
  85. }
  86. devices, err := device.GetAllDevices()
  87. if err != nil {
  88. return nil, util.FailResponse(err.Error(), nil)
  89. }
  90. return devices, nil
  91. }