switchBoxService.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 SwitchBoxService = new(switchBoxService)
  10. type switchBoxService struct{}
  11. func (s *switchBoxService) Get(id int) (*dao.SwitchBox, *utils.Errors) {
  12. // 创建查询实例
  13. device := &dao.SwitchBox{
  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 *switchBoxService) CreateOrUpdate(req dao.SwitchBox) *utils.Errors {
  23. // 创建查询实例
  24. device := req
  25. if device.TenantId == "" {
  26. device.TenantId = "000000" // todo: 使用登录态
  27. }
  28. if req.ID == 0 {
  29. device.CreateTime = time.Now()
  30. device.CreateUser = "TODO" // todo: 使用登录态
  31. if device.IsExistedBySN() {
  32. fmt.Printf("Create GetDeviceID err \n")
  33. return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  34. }
  35. if err := device.Create(); err != nil {
  36. fmt.Printf("Create err = %s \n", err.Error())
  37. return utils.FailResponse(err.Error(), nil)
  38. }
  39. return utils.SuccessResponse(utils.Succeeded, nil)
  40. }
  41. //更新
  42. device.UpdateUser = "TODO" // todo: 使用登录态
  43. device.UpdateTime = time.Now()
  44. if device.IsExistedByNameAndCode() {
  45. return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  46. }
  47. if err := device.Update(); err != nil {
  48. fmt.Printf("Update err = %s \n", err.Error())
  49. return utils.FailResponse(err.Error(), nil)
  50. }
  51. //todo operation record
  52. return utils.SuccessResponse(utils.Succeeded, nil)
  53. }
  54. func (s *switchBoxService) List(searchValue string, current, size int) ([]dao.SwitchBox, *utils.Errors) {
  55. device := dao.SwitchBox{}
  56. if searchValue != "" {
  57. device.BoxSN = searchValue
  58. device.BoxName = searchValue
  59. }
  60. offset := (current - 1) * size
  61. limit := size
  62. devices, err := device.GetDevices(offset, limit)
  63. if err != nil {
  64. return nil, utils.FailResponse(err.Error(), nil)
  65. }
  66. return devices, nil
  67. }
  68. func (s *switchBoxService) Remove(id int) *utils.Errors {
  69. // 创建查询实例
  70. device := &dao.SwitchBox{
  71. ID: id,
  72. IsDeleted: 1,
  73. UpdateUser: "TODO", // todo 使用登录态
  74. UpdateTime: time.Now(),
  75. }
  76. //todo
  77. // service.transformerService.CountRelation()
  78. //todo operation record
  79. err := device.Delete()
  80. if err != nil {
  81. return utils.FailResponse(err.Error(), nil)
  82. }
  83. return nil
  84. }
  85. func (s *switchBoxService) GetList() ([]*dao.SwitchBox, *utils.Errors) {
  86. var devices []*dao.SwitchBox
  87. err := utils.Redis.Get(getSwitchBoxListRedisKey("000000")).Scan(&devices)
  88. if err == nil {
  89. return devices, nil
  90. }
  91. device := &dao.SwitchBox{
  92. TenantId: "000000", // todo 使用登录态
  93. IsDeleted: 0,
  94. }
  95. devices, err = device.GetAllDevices()
  96. if err != nil {
  97. return nil, utils.FailResponse(err.Error(), nil)
  98. }
  99. _ = utils.Redis.Set(getSwitchBoxListRedisKey("000000"), devices, 0).Err()
  100. return devices, nil
  101. }
  102. func getSwitchBoxListRedisKey(tenantId string) string {
  103. return fmt.Sprintf(model.SwitchBoxList, tenantId)
  104. }