yinbangzhong
2024-07-22 de6deb8ea9db498a81dc1d841b19e7917d7e5a41
controllers/audio.go
@@ -3,6 +3,7 @@
import (
   "errors"
   "fmt"
   "github.com/360EntSecGroup-Skylar/excelize/v2"
   "github.com/gin-gonic/gin"
   "gorm.io/gorm"
   "io"
@@ -113,31 +114,31 @@
      util.ResponseFormat(c, code.SaveFail, "上传失败")
      return
   } else {
      go func() {
         var trainInfoNames = []string{audio.LocomotiveNumber, audio.TrainNumber, audio.Station}
         var (
            info   *models.TrainInfo
            err    error
            parent models.TrainInfo
         )
         for i := 0; i < 3; i++ {
            name := trainInfoNames[i]
            class := constvar.Class(i + 1)
            info, err = models.NewTrainInfoSearch().SetName(name).SetClass(class).First()
            if err == gorm.ErrRecordNotFound {
               info = &models.TrainInfo{
                  Name:     name,
                  Class:    class,
                  ParentID: parent.ID,
               }
               _ = models.NewTrainInfoSearch().Create(info)
            }
            parent = *info
         }
      }()
      util.ResponseFormat(c, code.Success, "上传成功")
      return
   }
   go func() {
      var trainInfoNames = []string{audio.LocomotiveNumber, audio.TrainNumber, audio.Station}
      var (
         info   *models.TrainInfo
         err    error
         parent models.TrainInfo
      )
      for i := 0; i < 3; i++ {
         name := trainInfoNames[i]
         class := constvar.Class(i + 1)
         info, err = models.NewTrainInfoSearch().SetName(name).SetClass(class).First()
         if err == gorm.ErrRecordNotFound {
            info = &models.TrainInfo{
               Name:     name,
               Class:    class,
               ParentID: parent.ID,
            }
            _ = models.NewTrainInfoSearch().Create(info)
         }
         parent = *info
      }
   }()
}
func (slf AudioCtl) ParamsCheck(filename string) (err error) {
@@ -222,6 +223,86 @@
   util.ResponseFormatList(c, code.Success, list, total)
}
// Export
// @Tags      音频检索结果到处
// @Summary   音频检索结果到处
// @Produce   application/json
// @Param     object  query    request.GetAudioList true  "参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Audio}  "成功"
// @Router    /api-sa/v1/audio/export [get]
func (slf AudioCtl) Export(c *gin.Context) {
   var params request.GetAudioList
   if err := c.ShouldBindQuery(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   list, _, err := models.NewAudioSearch().
      SetKeyword(params.Keyword).
      SetLocomotiveNumber(params.LocomotiveNumber).
      SetTrainNumber(params.TrainNumber).
      SetDriverNumber(params.DriverNumber).
      SetStation(params.StationNumber).
      SetBeginTime(params.BeginTime).
      SetEndTime(params.EndTime).
      SetIsFollowed(params.IsFollowed).
      SetAudioStatusList(params.StatusList).
      SetOrder("created_at desc").
      Find()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "查找失败")
      return
   }
   sheet := "auto_result"
   f := excelize.NewFile()
   index := f.NewSheet(sheet)
   //设置f的列名,车号,车次,司机号,车站号,日期
   f.SetCellValue(sheet, "A1", "机车号")
   f.SetCellValue(sheet, "B1", "车次")
   f.SetCellValue(sheet, "C1", "司机号")
   f.SetCellValue(sheet, "D1", "车站号")
   f.SetCellValue(sheet, "E1", "日期")
   f.SetCellValue(sheet, "F1", "分数")
   // 将数据写入工作表
   for i, auto := range list {
      row := i + 2
      cell1, _ := excelize.CoordinatesToCellName(1, row)
      f.SetCellValue(sheet, cell1, auto.LocomotiveNumber)
      cell2, _ := excelize.CoordinatesToCellName(2, row)
      f.SetCellValue(sheet, cell2, auto.TrainNumber)
      cell3, _ := excelize.CoordinatesToCellName(3, row)
      f.SetCellValue(sheet, cell3, auto.DriverNumber)
      cell4, _ := excelize.CoordinatesToCellName(4, row)
      f.SetCellValue(sheet, cell4, auto.Station)
      cell5, _ := excelize.CoordinatesToCellName(5, row)
      f.SetCellValue(sheet, cell5, auto.OccurrenceAt)
      cell6, _ := excelize.CoordinatesToCellName(6, row)
      f.SetCellValue(sheet, cell6, auto.Score)
   }
   // 设置默认打开的工作表
   f.SetActiveSheet(index)
   //// 保存文件
   //if err := f.SaveAs("result.xlsx"); err != nil {
   //   fmt.Println(err)
   //}
   // Set the headers for the file download
   c.Header("Content-Description", "File Transfer")
   c.Header("Content-Disposition", "attachment; filename=result.xlsx")
   c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
   // Send the file to the client
   if err := f.Write(c.Writer); err != nil {
      util.ResponseFormat(c, code.InternalError, err.Error())
      return
   }
   util.ResponseFormat(c, code.Success, "success")
}
// Process
// @Tags      音频
// @Summary   处理音频