bridgeSensorService.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/dao"
  5. "iot_manager_service/app/utils"
  6. "time"
  7. )
  8. // 中间件管理服务
  9. var BridgeSensorService = new(bridgeSensorService)
  10. type bridgeSensorService struct{}
  11. func (s *bridgeSensorService) Get(id int) (*dao.BridgeSensor, *utils.Errors) {
  12. // 创建查询实例
  13. device := &dao.BridgeSensor{
  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 *bridgeSensorService) CreateOrUpdate(req dao.BridgeSensor) *utils.Errors {
  23. // 创建查询实例
  24. device := req
  25. device.UpdateUser = "TODO" // todo: 使用登录态
  26. device.UpdateTime = time.Now()
  27. if req.ID == 0 {
  28. device.CreateTime = time.Now()
  29. device.CreateUser = "TODO" // todo: 使用登录态
  30. if device.IsExistedBySN() {
  31. fmt.Printf("Create GetDeviceID err \n")
  32. return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  33. }
  34. if err := device.Create(); err != nil {
  35. fmt.Printf("Create err = %s \n", err.Error())
  36. return utils.FailResponse(err.Error(), nil)
  37. }
  38. return utils.SuccessResponse(utils.Succeeded, nil)
  39. }
  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 *bridgeSensorService) List(searchValue string, current, size int) ([]dao.BridgeSensor, *utils.Errors) {
  51. device := dao.BridgeSensor{}
  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, utils.FailResponse(err.Error(), nil)
  61. }
  62. return devices, nil
  63. }
  64. func (s *bridgeSensorService) Remove(id int) *utils.Errors {
  65. // 创建查询实例
  66. device := &dao.BridgeSensor{
  67. ID: id,
  68. IsDeleted: 1,
  69. UpdateUser: "TODO", // todo 使用登录态
  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 utils.FailResponse(err.Error(), nil)
  78. }
  79. return nil
  80. }
  81. func (s *bridgeSensorService) GetList() ([]*dao.BridgeSensor, *utils.Errors) {
  82. var devices []*dao.BridgeSensor
  83. err := utils.Redis.Get(getTransformerListRedisKey("000000")).Scan(&devices)
  84. if err == nil {
  85. return devices, nil
  86. }
  87. device := &dao.BridgeSensor{
  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(getTransformerListRedisKey("000000"), devices, 0).Err()
  96. return devices, nil
  97. }
  98. func (s *bridgeSensorService) Enable(id, status int) *utils.Errors {
  99. // 创建查询实例
  100. device := &dao.BridgeSensor{
  101. ID: id,
  102. IsEnable: status,
  103. }
  104. err := device.UpdateEnable()
  105. if err != nil {
  106. return utils.FailResponse(err.Error(), nil)
  107. }
  108. return nil
  109. }