log.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package business
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gin-gonic/gin/binding"
  10. "github.com/google/uuid"
  11. //"device-manage/app/admin/models"
  12. "device-manage/app/admin/models/busmodels"
  13. "device-manage/tools"
  14. "device-manage/tools/app"
  15. )
  16. // @Summary 获取设备列表
  17. // @Description Get JSON
  18. // @Tags 设备列表
  19. // @Param name query string false "name"
  20. // @Param status query string false "status"
  21. // @Param deviceSn query string false "deviceSn"
  22. // @Param pageSize query int false "页条数"
  23. // @Param pageIndex query int false "页码"
  24. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  25. // @Router /api/v1/device/list/devicelist [get]
  26. // @Security Bearer
  27. func GetLogList(c *gin.Context) {
  28. var data busmodels.BusDeviceLog
  29. var params busmodels.QueryLogParams
  30. err := c.MustBindWith(&params, binding.JSON)
  31. pageSize := params.PageSize
  32. pageIndex := params.PageIndex
  33. gids := params.Gids
  34. data.LogName = params.LogName
  35. data.DeviceSn = params.DeviceSn
  36. result, count, err := data.GetPage(pageSize, pageIndex, gids)
  37. tools.HasError(err, "", -1)
  38. app.PageOK(c, result, count, pageIndex, pageSize, "")
  39. }
  40. func GetSingleDeviceLogList(c *gin.Context) {
  41. var data busmodels.BusDeviceLog
  42. var params busmodels.QueryLogParams
  43. err := c.MustBindWith(&params, binding.JSON)
  44. pageSize := params.PageSize
  45. pageIndex := params.PageIndex
  46. data.DeviceSn = params.DeviceSn
  47. result, count, err := data.GetSingleDevicePage(pageSize, pageIndex)
  48. tools.HasError(err, "", -1)
  49. app.PageOK(c, result, count, pageIndex, pageSize, "")
  50. }
  51. func DeleteDeviceLog(c *gin.Context) {
  52. var data busmodels.BusDeviceLog
  53. data.UpdateBy = tools.GetUserIdStr(c)
  54. IDS := tools.IdsStrToIdsIntGroup("logId", c)
  55. fmt.Println(IDS)
  56. result, err := data.BatchDelete(IDS)
  57. tools.HasError(err, "修改失败", 500)
  58. app.OK(c, result, "删除成功")
  59. }
  60. //web 端下载日志文件
  61. func DownloadDeviceLog(c *gin.Context) {
  62. var fileInfo busmodels.BusDeviceLog
  63. fileInfo.Uid = c.Query("uid")
  64. fileDir := "files/logs/" + fileInfo.Uid[0:2] + "/" + fileInfo.Uid[2:4]
  65. finalPath := fileDir + "/" + fileInfo.Uid
  66. fmt.Println("文件路径:", finalPath)
  67. //打开文件
  68. if _, err := os.Open(finalPath); err != nil {
  69. c.Redirect(http.StatusFound, "/404")
  70. return
  71. }
  72. deviceLog, _ := fileInfo.GetLogInfo()
  73. fmt.Println("文件名 :", deviceLog.LogName)
  74. c.Header("Content-Type", "application/octet-stream")
  75. c.Header("Content-Disposition", "attachment; filename="+deviceLog.LogName)
  76. c.Header("Content-Transfer-Encoding", "binary")
  77. c.File(finalPath)
  78. return
  79. }
  80. //设备端上传日志
  81. func DeviceUploadLog(c *gin.Context) {
  82. // 获取上传文件
  83. var dev busmodels.BusDevice
  84. var deviceLog busmodels.BusDeviceLog
  85. time := time.Now()
  86. file, _ := c.FormFile("file")
  87. user, _ := c.GetPostForm("user")
  88. deviceSn, _ := c.GetPostForm("deviceSn")
  89. uid := uuid.New().String()
  90. fmt.Println("user:", user)
  91. fmt.Println("deviceSn:", deviceSn)
  92. fileName := deviceSn + "-" + time.Format("20060102150405") + ".zip"
  93. destPath := "files/logs/" + uid[0:2] + "/" + uid[2:4]
  94. finalPath := destPath + "/" + uid
  95. // 上传文件到指定的路径
  96. if _, err := os.Stat(destPath); err != nil {
  97. if err = os.MkdirAll(destPath, os.ModePerm); err != nil {
  98. fmt.Println(err)
  99. c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error())
  100. return
  101. }
  102. }
  103. if _, err := os.Stat(finalPath); err != nil {
  104. if _, err = os.Create(finalPath); err != nil {
  105. fmt.Println(err)
  106. c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error())
  107. return
  108. }
  109. }
  110. if err := c.SaveUploadedFile(file, finalPath); err != nil {
  111. c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error())
  112. return
  113. }
  114. dev.DeviceSn = deviceSn
  115. devInfo, _ := dev.GetDeviceByDeviceSn()
  116. deviceLog.DeviceSn = deviceSn
  117. deviceLog.LogName = fileName
  118. deviceLog.Size = strconv.FormatInt(file.Size, 10)
  119. deviceLog.Uid = uid
  120. deviceLog.GroupId = devInfo.GroupId
  121. deviceLog.UpdateBy = user
  122. deviceLog.CreateBy = user
  123. deviceLog.UpdatedAt = time
  124. deviceLog.CreatedAt = time
  125. deviceLog.Insert()
  126. }