switchBoxService.go 2.6 KB

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