From de6deb8ea9db498a81dc1d841b19e7917d7e5a41 Mon Sep 17 00:00:00 2001 From: yinbangzhong <zhongbangyin@126.com> Date: 星期一, 22 七月 2024 18:57:04 +0800 Subject: [PATCH] export --- controllers/audio.go | 125 ++++++++++++++++++++++++++++++++++------- 1 files changed, 103 insertions(+), 22 deletions(-) diff --git a/controllers/audio.go b/controllers/audio.go index 570a103..ef23f56 100644 --- a/controllers/audio.go +++ b/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(¶ms); 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 澶勭悊闊抽 -- Gitblit v1.8.0