device.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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) CreateDevice(c *gin.Context) {
  57. var device dao.Device
  58. err := c.ShouldBindJSON(&device)
  59. if err != nil {
  60. logger.Get().Error("CreateDevice ---- " + err.Error())
  61. response.FailWithMessage("参数解析失败", c)
  62. return
  63. }
  64. err = deviceService.CreateDevice(device)
  65. if err != nil {
  66. logger.Get().Error("CreateDevice ---- " + err.Error())
  67. response.FailWithMessage("设备创建失败", c)
  68. return
  69. }
  70. response.OkWithMessage("创建成功", c)
  71. }
  72. func (da *DeviceApi) UpdateDevice(c *gin.Context) {
  73. var device dao.Device
  74. err := c.ShouldBindJSON(&device)
  75. if err != nil {
  76. logger.Get().Error("UpdateDevice ---- " + err.Error())
  77. response.FailWithMessage("参数解析失败", c)
  78. return
  79. }
  80. err = deviceService.UpdateDevice(device)
  81. if err != nil {
  82. logger.Get().Error("UpdateDevice ---- " + err.Error())
  83. response.FailWithMessage("设备更新失败", c)
  84. return
  85. }
  86. response.OkWithMessage("更新成功", c)
  87. }
  88. func (da *DeviceApi) DeleteDevice(c *gin.Context) {
  89. id, err := strconv.Atoi(c.Query("id"))
  90. if err != nil {
  91. logger.Get().Error("DeleteDevice ---- " + err.Error())
  92. response.FailWithMessage("参数错误", c)
  93. return
  94. }
  95. err = deviceService.DeleteDevice(id)
  96. if err != nil {
  97. logger.Get().Error("DeleteDevice ---- " + err.Error())
  98. response.FailWithMessage("设备删除失败", c)
  99. return
  100. }
  101. response.OkWithMessage("删除成功", c)
  102. }