package business import ( "fmt" "net/http" "os" "strconv" "time" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/google/uuid" //"device-manage/app/admin/models" "device-manage/app/admin/models/busmodels" "device-manage/tools" "device-manage/tools/app" ) // @Summary 获取设备列表 // @Description Get JSON // @Tags 设备列表 // @Param name query string false "name" // @Param status query string false "status" // @Param deviceSn query string false "deviceSn" // @Param pageSize query int false "页条数" // @Param pageIndex query int false "页码" // @Success 200 {object} app.Response "{"code": 200, "data": [...]}" // @Router /api/v1/device/list/devicelist [get] // @Security Bearer func GetLogList(c *gin.Context) { var data busmodels.BusDeviceLog var params busmodels.QueryLogParams err := c.MustBindWith(¶ms, binding.JSON) pageSize := params.PageSize pageIndex := params.PageIndex gids := params.Gids data.LogName = params.LogName data.DeviceSn = params.DeviceSn result, count, err := data.GetPage(pageSize, pageIndex, gids) tools.HasError(err, "", -1) app.PageOK(c, result, count, pageIndex, pageSize, "") } func GetSingleDeviceLogList(c *gin.Context) { var data busmodels.BusDeviceLog var params busmodels.QueryLogParams err := c.MustBindWith(¶ms, binding.JSON) pageSize := params.PageSize pageIndex := params.PageIndex data.DeviceSn = params.DeviceSn result, count, err := data.GetSingleDevicePage(pageSize, pageIndex) tools.HasError(err, "", -1) app.PageOK(c, result, count, pageIndex, pageSize, "") } func DeleteDeviceLog(c *gin.Context) { var data busmodels.BusDeviceLog data.UpdateBy = tools.GetUserIdStr(c) IDS := tools.IdsStrToIdsIntGroup("logId", c) fmt.Println(IDS) result, err := data.BatchDelete(IDS) tools.HasError(err, "修改失败", 500) app.OK(c, result, "删除成功") } //web 端下载日志文件 func DownloadDeviceLog(c *gin.Context) { var fileInfo busmodels.BusDeviceLog fileInfo.Uid = c.Query("uid") fileDir := "files/logs/" + fileInfo.Uid[0:2] + "/" + fileInfo.Uid[2:4] finalPath := fileDir + "/" + fileInfo.Uid fmt.Println("文件路径:", finalPath) //打开文件 if _, err := os.Open(finalPath); err != nil { c.Redirect(http.StatusFound, "/404") return } deviceLog, _ := fileInfo.GetLogInfo() fmt.Println("文件名 :", deviceLog.LogName) c.Header("Content-Type", "application/octet-stream") c.Header("Content-Disposition", "attachment; filename="+deviceLog.LogName) c.Header("Content-Transfer-Encoding", "binary") c.File(finalPath) return } //设备端上传日志 func DeviceUploadLog(c *gin.Context) { // 获取上传文件 var dev busmodels.BusDevice var deviceLog busmodels.BusDeviceLog time := time.Now() file, _ := c.FormFile("file") user, _ := c.GetPostForm("user") deviceSn, _ := c.GetPostForm("deviceSn") uid := uuid.New().String() fmt.Println("user:", user) fmt.Println("deviceSn:", deviceSn) fileName := deviceSn + "-" + time.Format("20060102150405") + ".zip" destPath := "files/logs/" + uid[0:2] + "/" + uid[2:4] finalPath := destPath + "/" + uid // 上传文件到指定的路径 if _, err := os.Stat(destPath); err != nil { if err = os.MkdirAll(destPath, os.ModePerm); err != nil { fmt.Println(err) c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error()) return } } if _, err := os.Stat(finalPath); err != nil { if _, err = os.Create(finalPath); err != nil { fmt.Println(err) c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error()) return } } if err := c.SaveUploadedFile(file, finalPath); err != nil { c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error()) return } dev.DeviceSn = deviceSn devInfo, _ := dev.GetDeviceByDeviceSn() deviceLog.DeviceSn = deviceSn deviceLog.LogName = fileName deviceLog.Size = strconv.FormatInt(file.Size, 10) deviceLog.Uid = uid deviceLog.GroupId = devInfo.GroupId deviceLog.UpdateBy = user deviceLog.CreateBy = user deviceLog.UpdatedAt = time deviceLog.CreatedAt = time deviceLog.Insert() }