device.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package admin
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "server/dao"
  5. "server/model/common/request"
  6. "server/model/common/response"
  7. "server/utils/logger"
  8. "strconv"
  9. )
  10. type DeviceApi struct{}
  11. func (da *DeviceApi) QueryAllDevices(c *gin.Context) {
  12. devices, err := deviceService.QueryAllDevices()
  13. if err != nil {
  14. logger.Get().Error("QueryAllDevices ---- " + err.Error())
  15. response.FailWithMessage("设备获取设备", c)
  16. return
  17. }
  18. response.OkWithData(devices, c)
  19. }
  20. func (da *DeviceApi) QueryDeviceList(c *gin.Context) {
  21. var info request.DeviceSearch
  22. err := c.ShouldBindJSON(&info)
  23. if err != nil {
  24. logger.Get().Error("QueryDeviceList ---- " + err.Error())
  25. response.FailWithMessage("参数解析失败", c)
  26. return
  27. }
  28. list, total, err := deviceService.QueryDeviceList(info)
  29. if err != nil {
  30. logger.Get().Error("QueryDeviceList ---- " + err.Error())
  31. response.FailWithMessage("设备获取列表错误", c)
  32. return
  33. }
  34. response.OkWithDetailed(response.PageResult{
  35. List: list,
  36. Total: total,
  37. Page: info.PageInfo.Page,
  38. PageSize: info.PageInfo.PageSize,
  39. }, "获取成功", c)
  40. }
  41. func (da *DeviceApi) GenerateDeviceFile(c *gin.Context) {
  42. id, err := strconv.Atoi(c.Query("id"))
  43. if err != nil {
  44. logger.Get().Error("GenerateDeviceFile ---- " + err.Error())
  45. response.FailWithMessage("参数解析失败", c)
  46. return
  47. }
  48. data, err := deviceService.GenerateDeviceFile(id)
  49. if err != nil {
  50. logger.Get().Error("GenerateDeviceFile ---- " + err.Error())
  51. response.FailWithMessage("设备文件生成失败", c)
  52. return
  53. }
  54. response.OkWithData(data, c)
  55. }
  56. func (da *DeviceApi) DeviceSwitch(c *gin.Context) {
  57. var info request.SwitchTunnel
  58. err := c.ShouldBindJSON(&info)
  59. if err != nil {
  60. logger.Get().Error("DeviceSwitch ---- " + err.Error())
  61. response.FailWithMessage("参数解析失败", c)
  62. return
  63. }
  64. err = deviceService.DeviceSwitch(info)
  65. if err != nil {
  66. logger.Get().Error("DeviceSwitch ---- " + err.Error())
  67. response.FailWithMessage("设备状态切换失败", c)
  68. return
  69. }
  70. response.Ok(c)
  71. }
  72. func (da *DeviceApi) CreateDevice(c *gin.Context) {
  73. var device dao.Device
  74. err := c.ShouldBindJSON(&device)
  75. if err != nil {
  76. logger.Get().Error("CreateDevice ---- " + err.Error())
  77. response.FailWithMessage("参数解析失败", c)
  78. return
  79. }
  80. err = deviceService.CreateDevice(device)
  81. if err != nil {
  82. logger.Get().Error("CreateDevice ---- " + err.Error())
  83. response.FailWithMessage("设备创建失败", c)
  84. return
  85. }
  86. response.OkWithMessage("创建成功", c)
  87. }
  88. func (da *DeviceApi) UpdateDevice(c *gin.Context) {
  89. var device dao.Device
  90. err := c.ShouldBindJSON(&device)
  91. if err != nil {
  92. logger.Get().Error("UpdateDevice ---- " + err.Error())
  93. response.FailWithMessage("参数解析失败", c)
  94. return
  95. }
  96. err = deviceService.UpdateDevice(device)
  97. if err != nil {
  98. logger.Get().Error("UpdateDevice ---- " + err.Error())
  99. response.FailWithMessage("设备更新失败", c)
  100. return
  101. }
  102. response.OkWithMessage("更新成功", c)
  103. }
  104. func (da *DeviceApi) DeleteDevice(c *gin.Context) {
  105. id, err := strconv.Atoi(c.Query("id"))
  106. if err != nil {
  107. logger.Get().Error("DeleteDevice ---- " + err.Error())
  108. response.FailWithMessage("参数错误", c)
  109. return
  110. }
  111. err = deviceService.DeleteDevice(id)
  112. if err != nil {
  113. logger.Get().Error("DeleteDevice ---- " + err.Error())
  114. response.FailWithMessage("设备删除失败", c)
  115. return
  116. }
  117. response.OkWithMessage("删除成功", c)
  118. }