From c40d07e760598c0aae4353f7a1e2d6b01747e83f Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 12 六月 2024 11:54:54 +0800
Subject: [PATCH] 文件下载&状态筛选

---
 controllers/audio.go |  188 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 184 insertions(+), 4 deletions(-)

diff --git a/controllers/audio.go b/controllers/audio.go
index 94b0117..7c58dd9 100644
--- a/controllers/audio.go
+++ b/controllers/audio.go
@@ -2,8 +2,11 @@
 
 import (
 	"errors"
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"gorm.io/gorm"
+	"io"
+	"os"
 	"path"
 	"speechAnalysis/constvar"
 	"speechAnalysis/extend/code"
@@ -73,7 +76,7 @@
 		LocomotiveNumber: arr[0],
 		TrainNumber:      arr[1],
 		DriverNumber:     arr[2],
-		StationNumber:    arr[3],
+		Station:          arr[3],
 		OccurrenceAt:     t,
 		IsFollowed:       0,
 	}
@@ -82,6 +85,31 @@
 		util.ResponseFormat(c, code.SaveFail, "涓婁紶澶辫触")
 		return
 	}
+	go func() {
+
+		var trainInfoNames = []string{arr[0], arr[1], arr[3]}
+
+		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, "娣诲姞鎴愬姛")
 }
@@ -92,6 +120,39 @@
 		return errors.New("鏂囦欢鏍煎紡閿欒")
 	}
 	return nil
+}
+
+// TrainInfoList
+// @Tags      闊抽
+// @Summary   鑾峰彇鐏溅淇℃伅
+// @Produce   application/json
+// @Param     object  query    request.GetTrainInfoList true  "鍙傛暟"
+// @Success   200   {object}  util.ResponseList{data=[]models.TrainInfo}  "鎴愬姛"
+// @Router    /api-sa/v1/audio/trainInfoList [get]
+func (slf AudioCtl) TrainInfoList(c *gin.Context) {
+	var params request.GetTrainInfoList
+	if err := c.ShouldBindQuery(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+
+	if !params.PageInfo.Check() {
+		util.ResponseFormat(c, code.RequestParamError, "鍒嗛〉鍙傛暟閿欒")
+		return
+	}
+
+	list, total, err := models.NewTrainInfoSearch().
+		SetPage(params.Page, params.PageSize).
+		SetClass(params.Class).
+		SetParentId(params.ParentID).
+		Find()
+
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+
+	util.ResponseFormatList(c, code.Success, list, total)
 }
 
 // List
@@ -119,7 +180,11 @@
 		SetLocomotiveNumber(params.LocomotiveNumber).
 		SetTrainNumber(params.TrainNumber).
 		SetDriverNumber(params.DriverNumber).
-		SetStationNumber(params.StationNumber).
+		SetStation(params.StationNumber).
+		SetBeginTime(params.BeginTime).
+		SetEndTime(params.EndTime).
+		SetIsFollowed(params.IsFollowed).
+		SetAudioStatusList(params.StatusList).
 		Find()
 
 	if err != nil {
@@ -151,6 +216,81 @@
 	}
 
 	util.ResponseFormat(c, code.UpdateSuccess, "鎴愬姛")
+}
+
+// AudioInfo
+// @Tags      闊抽
+// @Summary   闊抽璇︽儏锛屽惈瑙f瀽缁撴灉
+// @Produce   application/json
+// @Param     object  query request.ProcessAudio true  "鍙傛暟"
+// @Success   200 {object} util.Response{data=models.Audio} "鎴愬姛"
+// @Router    /api-sa/v1/audio/info [get]
+func (slf AudioCtl) AudioInfo(c *gin.Context) {
+	var params request.ProcessAudio
+	if err := c.ShouldBindQuery(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+
+	audio, err := models.NewAudioSearch().SetID(params.ID).First()
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "璇锋眰澶辫触")
+		return
+	}
+	audioText, err := models.NewAudioTextSearch().SetAudioID(audio.ID).First()
+	if err == nil {
+		audio.AudioText = audioText.AudioText
+	}
+
+	util.ResponseFormat(c, code.UpdateSuccess, audio)
+}
+
+// AudioDownload
+// @Tags      闊抽
+// @Summary   闊抽涓嬭浇
+// @Produce   application/json
+// @Param     object  query request.ProcessAudio true  "鍙傛暟"
+// @Success   200 {object} util.Response{data=models.Audio} "鎴愬姛"
+// @Router    /api-sa/v1/audio/download [get]
+func (slf AudioCtl) AudioDownload(c *gin.Context) {
+	var params request.ProcessAudio
+	if err := c.ShouldBindQuery(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+
+	audio, err := models.NewAudioSearch().SetID(params.ID).First()
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
+		return
+	}
+
+	if audio.FilePath == "" {
+		util.ResponseFormat(c, code.InternalError, "鏌ヨ澶辫触")
+		return
+	}
+
+	file, err := os.Open(audio.FilePath)
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鏂囦欢鎵撳紑澶辫触")
+		return
+	}
+	defer file.Close()
+
+	fileInfo, err := file.Stat()
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鑾峰彇鏂囦欢淇℃伅澶辫触")
+		return
+	}
+
+	c.Header("Content-Disposition", "inline; filename="+audio.Name) // 鍦ㄦ祻瑙堝櫒涓洿鎺ユ墦寮�
+	c.Header("Content-Length", fmt.Sprint(fileInfo.Size()))
+	c.Header("Content-Type", "audio/mpeg") // 璁剧疆闊抽鏂囦欢绫诲瀷
+
+	if _, err := io.Copy(c.Writer, file); err != nil {
+		util.ResponseFormat(c, code.InternalError, "鏂囦欢浼犺緭澶辫触")
+		return
+	}
 }
 
 // BatchProcess
@@ -203,11 +343,29 @@
 		return
 	}
 
-	err := service.DeleteAudio(params.ID)
+	audio, err := models.NewAudioSearch().SetID(params.ID).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "闊抽涓嶅瓨鍦�")
+		return
+	}
+
+	if audio.AudioStatus == constvar.AudioStatusProcessing || audio.AudioStatus == constvar.AudioStatusFinish {
+		util.ResponseFormat(c, code.RequestParamError, "闊抽姝e湪澶勭悊鎴栬�呭鐞嗗畬鎴愶紝涓嶅彲鍒犻櫎")
+		return
+	}
+
+	err = service.DeleteAudio(params.ID)
 	if err != nil {
 		util.ResponseFormat(c, code.InternalError, err.Error())
 		return
 	}
+
+	go func() {
+		err = os.Remove(audio.FilePath)
+		if err != nil {
+			logx.Warnf("remove file err:%v, file:%v", err, audio.FilePath)
+		}
+	}()
 
 	util.ResponseFormat(c, code.DeleteSuccess, "鎴愬姛")
 }
@@ -226,12 +384,34 @@
 		return
 	}
 
-	err := service.BatchDeleteAudio(params.IDs)
+	audioList, err := models.NewAudioSearch().SetIDs(params.IDs).FindNotTotal()
+	if err != nil {
+		util.ResponseFormat(c, code.InternalError, "鍐呴儴閿欒")
+		return
+	}
+
+	for _, audio := range audioList {
+		if audio.AudioStatus == constvar.AudioStatusProcessing || audio.AudioStatus == constvar.AudioStatusFinish {
+			util.ResponseFormat(c, code.RequestParamError, "闊抽姝e湪澶勭悊鎴栬�呭鐞嗗畬鎴愶紝涓嶅彲鍒犻櫎")
+			return
+		}
+	}
+
+	err = service.BatchDeleteAudio(params.IDs)
 	if err != nil {
 		util.ResponseFormat(c, code.InternalError, err.Error())
 		return
 	}
 
+	go func() {
+		for _, audio := range audioList {
+			err = os.Remove(audio.FilePath)
+			if err != nil {
+				logx.Warnf("remove file err:%v, file:%v", err, audio.FilePath)
+			}
+		}
+	}()
+
 	util.ResponseFormat(c, code.DeleteSuccess, "鎴愬姛")
 }
 

--
Gitblit v1.8.0