zhangqian
2024-05-16 3eb829393c22b975b9ca7e0e022150d4cafa9bd1
增加音频详情接口,返回解析的文字
13个文件已修改
416 ■■■■ 已修改文件
conf/config.yaml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/audio.go 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/text.go 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 107 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 107 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 70 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/audio.go 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/db.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/text.go 80 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/train.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
request/audio.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
response/audio.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.yaml
@@ -1,5 +1,5 @@
web:
  port: 8102
  port: 8203
  host: 192.168.20.119
db:
  dsn: root:c++java123@tcp(192.168.20.119:3306)/speech_analysis?charset=utf8&parseTime=True&loc=Local
controllers/audio.go
@@ -211,6 +211,33 @@
    util.ResponseFormat(c, code.UpdateSuccess, "成功")
}
// AudioInfo
// @Tags      音频
// @Summary   音频详情,含解析结果
// @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)
}
// BatchProcess
// @Tags      音频
// @Summary   批量处理音频
controllers/text.go
@@ -27,7 +27,7 @@
        return
    }
    text := models.Text{
    text := models.Word{
        Content:          req.Content,
        LocomotiveNumber: req.LocomotiveNumber,
    }
@@ -37,7 +37,7 @@
        return
    }
    if err := models.NewTextSearch().Create(&text); err != nil {
    if err := models.NewWordSearch().Create(&text); err != nil {
        util.ResponseFormat(c, code.SaveFail, "添加失败,请检查是否重复")
        return
    }
@@ -45,11 +45,11 @@
    util.ResponseFormat(c, code.Success, "添加成功")
}
func (slf TextCtl) paramsCheck(text models.Text) (err error) {
func (slf TextCtl) paramsCheck(text models.Word) (err error) {
    if text.Content == "" || text.LocomotiveNumber == "" {
        return errors.New("参数缺失")
    }
    _, err = models.NewTextSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First()
    _, err = models.NewWordSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First()
    if err == nil {
        return errors.New("文字重复")
    }
@@ -61,7 +61,7 @@
// @Summary   文字库列表
// @Produce   application/json
// @Param     object  query    request.GetTextList true  "参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Text}  "成功"
// @Success   200   {object}  util.ResponseList{data=[]models.Word}  "成功"
// @Router    /api-sa/v1/text/list [get]
func (slf TextCtl) List(c *gin.Context) {
    var params request.GetTextList
@@ -75,7 +75,7 @@
        return
    }
    list, total, err := models.NewTextSearch().
    list, total, err := models.NewWordSearch().
        SetPage(params.Page, params.PageSize).
        SetKeyword(params.Keyword).
        Find()
docs/docs.go
@@ -148,6 +148,45 @@
                }
            }
        },
        "/api-sa/v1/audio/info": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "音频"
                ],
                "summary": "音频详情,含解析结果",
                "parameters": [
                    {
                        "type": "integer",
                        "name": "id",
                        "in": "query",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.Response"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "$ref": "#/definitions/models.Audio"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api-sa/v1/audio/list": {
            "get": {
                "produces": [
@@ -432,7 +471,7 @@
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Text"
                                                "$ref": "#/definitions/models.Word"
                                            }
                                        }
                                    }
@@ -525,6 +564,10 @@
                        }
                    ]
                },
                "audioText": {
                    "description": "解析出的文本",
                    "type": "string"
                },
                "createdAt": {
                    "type": "string"
                },
@@ -575,31 +618,13 @@
                },
                "updatedAt": {
                    "type": "string"
                },
                "words": {
                    "description": "匹配到的文字数组",
                    "type": "array",
                    "items": {
                        "type": "string"
                }
            }
        },
        "models.Text": {
            "type": "object",
            "properties": {
                "content": {
                    "description": "音频名称",
                    "type": "string"
                },
                "createdAt": {
                    "type": "string"
                },
                "deletedAt": {
                    "$ref": "#/definitions/gorm.DeletedAt"
                },
                "id": {
                    "type": "integer"
                },
                "locomotiveNumber": {
                    "description": "机车号",
                    "type": "string"
                },
                "updatedAt": {
                    "type": "string"
                }
            }
        },
@@ -607,7 +632,7 @@
            "type": "object",
            "properties": {
                "class": {
                    "description": "分类",
                    "description": "分类  1 机车 2车次 3 车站",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.Class"
@@ -630,6 +655,31 @@
                "parentID": {
                    "description": "上级id",
                    "type": "integer"
                },
                "updatedAt": {
                    "type": "string"
                }
            }
        },
        "models.Word": {
            "type": "object",
            "properties": {
                "content": {
                    "description": "文字",
                    "type": "string"
                },
                "createdAt": {
                    "type": "string"
                },
                "deletedAt": {
                    "$ref": "#/definitions/gorm.DeletedAt"
                },
                "id": {
                    "type": "integer"
                },
                "locomotiveNumber": {
                    "description": "机车号",
                    "type": "string"
                },
                "updatedAt": {
                    "type": "string"
@@ -693,8 +743,13 @@
            "type": "object",
            "properties": {
                "followStatus": {
                    "description": "1 已关注 2未关注",
                    "allOf": [
                        {
                    "$ref": "#/definitions/constvar.BoolType"
                }
                    ]
                }
            }
        },
        "util.Response": {
docs/swagger.json
@@ -136,6 +136,45 @@
                }
            }
        },
        "/api-sa/v1/audio/info": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "音频"
                ],
                "summary": "音频详情,含解析结果",
                "parameters": [
                    {
                        "type": "integer",
                        "name": "id",
                        "in": "query",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.Response"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "$ref": "#/definitions/models.Audio"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api-sa/v1/audio/list": {
            "get": {
                "produces": [
@@ -420,7 +459,7 @@
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Text"
                                                "$ref": "#/definitions/models.Word"
                                            }
                                        }
                                    }
@@ -513,6 +552,10 @@
                        }
                    ]
                },
                "audioText": {
                    "description": "解析出的文本",
                    "type": "string"
                },
                "createdAt": {
                    "type": "string"
                },
@@ -563,31 +606,13 @@
                },
                "updatedAt": {
                    "type": "string"
                },
                "words": {
                    "description": "匹配到的文字数组",
                    "type": "array",
                    "items": {
                        "type": "string"
                }
            }
        },
        "models.Text": {
            "type": "object",
            "properties": {
                "content": {
                    "description": "音频名称",
                    "type": "string"
                },
                "createdAt": {
                    "type": "string"
                },
                "deletedAt": {
                    "$ref": "#/definitions/gorm.DeletedAt"
                },
                "id": {
                    "type": "integer"
                },
                "locomotiveNumber": {
                    "description": "机车号",
                    "type": "string"
                },
                "updatedAt": {
                    "type": "string"
                }
            }
        },
@@ -595,7 +620,7 @@
            "type": "object",
            "properties": {
                "class": {
                    "description": "分类",
                    "description": "分类  1 机车 2车次 3 车站",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.Class"
@@ -618,6 +643,31 @@
                "parentID": {
                    "description": "上级id",
                    "type": "integer"
                },
                "updatedAt": {
                    "type": "string"
                }
            }
        },
        "models.Word": {
            "type": "object",
            "properties": {
                "content": {
                    "description": "文字",
                    "type": "string"
                },
                "createdAt": {
                    "type": "string"
                },
                "deletedAt": {
                    "$ref": "#/definitions/gorm.DeletedAt"
                },
                "id": {
                    "type": "integer"
                },
                "locomotiveNumber": {
                    "description": "机车号",
                    "type": "string"
                },
                "updatedAt": {
                    "type": "string"
@@ -681,8 +731,13 @@
            "type": "object",
            "properties": {
                "followStatus": {
                    "description": "1 已关注 2未关注",
                    "allOf": [
                        {
                    "$ref": "#/definitions/constvar.BoolType"
                }
                    ]
                }
            }
        },
        "util.Response": {
docs/swagger.yaml
@@ -58,6 +58,9 @@
        allOf:
        - $ref: '#/definitions/constvar.AudioStatus'
        description: 音频状态
      audioText:
        description: 解析出的文本
        type: string
      createdAt:
        type: string
      deletedAt:
@@ -93,30 +96,18 @@
        type: string
      updatedAt:
        type: string
    type: object
  models.Text:
    properties:
      content:
        description: 音频名称
      words:
        description: 匹配到的文字数组
        items:
        type: string
      createdAt:
        type: string
      deletedAt:
        $ref: '#/definitions/gorm.DeletedAt'
      id:
        type: integer
      locomotiveNumber:
        description: 机车号
        type: string
      updatedAt:
        type: string
        type: array
    type: object
  models.TrainInfo:
    properties:
      class:
        allOf:
        - $ref: '#/definitions/constvar.Class'
        description: 分类
        description: 分类  1 机车 2车次 3 车站
      createdAt:
        type: string
      deletedAt:
@@ -129,6 +120,23 @@
      parentID:
        description: 上级id
        type: integer
      updatedAt:
        type: string
    type: object
  models.Word:
    properties:
      content:
        description: 文字
        type: string
      createdAt:
        type: string
      deletedAt:
        $ref: '#/definitions/gorm.DeletedAt'
      id:
        type: integer
      locomotiveNumber:
        description: 机车号
        type: string
      updatedAt:
        type: string
    type: object
@@ -170,7 +178,9 @@
  response.FollowResp:
    properties:
      followStatus:
        $ref: '#/definitions/constvar.BoolType'
        allOf:
        - $ref: '#/definitions/constvar.BoolType'
        description: 1 已关注 2未关注
    type: object
  util.Response:
    properties:
@@ -276,6 +286,28 @@
                  $ref: '#/definitions/response.FollowResp'
              type: object
      summary: 关注/取消关注
      tags:
      - 音频
  /api-sa/v1/audio/info:
    get:
      parameters:
      - in: query
        name: id
        required: true
        type: integer
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            allOf:
            - $ref: '#/definitions/util.Response'
            - properties:
                data:
                  $ref: '#/definitions/models.Audio'
              type: object
      summary: 音频详情,含解析结果
      tags:
      - 音频
  /api-sa/v1/audio/list:
@@ -456,7 +488,7 @@
            - properties:
                data:
                  items:
                    $ref: '#/definitions/models.Text'
                    $ref: '#/definitions/models.Word'
                  type: array
              type: object
      summary: 文字库列表
models/audio.go
@@ -24,6 +24,8 @@
        OccurrenceTime   string               `json:"occurrenceTime" gorm:"-"`
        IsFollowed       constvar.BoolType    `gorm:"type:tinyint;not null;default:2;comment:是否关注"` //是否关注 1关注 2未关注
        Score            float64              `json:"score"`                                        // 置信度
        Words            []string             `json:"words" gorm:"-"`                               //匹配到的文字数组
        AudioText        string               `json:"audioText" gorm:"-"`                           //解析出的文本
    }
    AudioSearch struct {
models/db.go
@@ -75,7 +75,7 @@
    err := db.AutoMigrate(
        Audio{},
        AudioText{},
        Text{},
        Word{},
        TrainInfo{},
    )
    return err
models/text.go
@@ -7,15 +7,15 @@
)
type (
    // Text 文字
    Text struct {
    // Word 文字
    Word struct {
        gorm.Model
        Content          string `gorm:"uniqueIndex:locomotive_number_text;type:varchar(255);not null;default:'';comment:音频名称" json:"content"`         // 音频名称
        LocomotiveNumber string `gorm:"uniqueIndex:locomotive_number_text;type:varchar(255);not null;default:'';comment:机车号" json:"locomotiveNumber"` // 机车号
        Content          string `gorm:"uniqueIndex:locomotive_number_Word;type:varchar(255);not null;default:'';comment:音频名称" json:"content"`         // 文字
        LocomotiveNumber string `gorm:"uniqueIndex:locomotive_number_Word;type:varchar(255);not null;default:'';comment:机车号" json:"locomotiveNumber"` // 机车号
    }
    TextSearch struct {
        Text
    WordSearch struct {
        Word
        Order    string
        PageNum  int
        PageSize int
@@ -25,55 +25,55 @@
    }
)
func (slf *Text) TableName() string {
    return "text"
func (slf *Word) TableName() string {
    return "word"
}
func NewTextSearch() *TextSearch {
    return &TextSearch{Orm: mysqlx.GetDB()}
func NewWordSearch() *WordSearch {
    return &WordSearch{Orm: mysqlx.GetDB()}
}
func (slf *TextSearch) SetOrm(tx *gorm.DB) *TextSearch {
func (slf *WordSearch) SetOrm(tx *gorm.DB) *WordSearch {
    slf.Orm = tx
    return slf
}
func (slf *TextSearch) SetPage(page, size int) *TextSearch {
func (slf *WordSearch) SetPage(page, size int) *WordSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *TextSearch) SetOrder(order string) *TextSearch {
func (slf *WordSearch) SetOrder(order string) *WordSearch {
    slf.Order = order
    return slf
}
func (slf *TextSearch) SetID(id uint) *TextSearch {
func (slf *WordSearch) SetID(id uint) *WordSearch {
    slf.ID = id
    return slf
}
func (slf *TextSearch) SetIDs(ids []uint) *TextSearch {
func (slf *WordSearch) SetIDs(ids []uint) *WordSearch {
    slf.IDs = ids
    return slf
}
func (slf *TextSearch) SetKeyword(kw string) *TextSearch {
func (slf *WordSearch) SetKeyword(kw string) *WordSearch {
    slf.Keyword = kw
    return slf
}
func (slf *TextSearch) SetLocomotiveNumber(number string) *TextSearch {
func (slf *WordSearch) SetLocomotiveNumber(number string) *WordSearch {
    slf.LocomotiveNumber = number
    return slf
}
func (slf *TextSearch) SetContent(content string) *TextSearch {
func (slf *WordSearch) SetContent(content string) *WordSearch {
    slf.Content = content
    return slf
}
func (slf *TextSearch) build() *gorm.DB {
func (slf *WordSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
@@ -105,7 +105,7 @@
}
// Create 单条插入
func (slf *TextSearch) Create(record *Text) error {
func (slf *WordSearch) Create(record *Word) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
@@ -116,7 +116,7 @@
}
// CreateBatch 批量插入
func (slf *TextSearch) CreateBatch(records []*Text) error {
func (slf *WordSearch) CreateBatch(records []*Word) error {
    var db = slf.build()
    if err := db.Create(&records).Error; err != nil {
@@ -126,7 +126,7 @@
    return nil
}
func (slf *TextSearch) Save(record *Text) error {
func (slf *WordSearch) Save(record *Word) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
@@ -136,7 +136,7 @@
    return nil
}
func (slf *TextSearch) UpdateByMap(upMap map[string]interface{}) error {
func (slf *WordSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
@@ -148,7 +148,7 @@
    return nil
}
func (slf *TextSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
func (slf *WordSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
@@ -160,19 +160,19 @@
    return nil
}
func (slf *TextSearch) Delete() error {
func (slf *WordSearch) Delete() error {
    var db = slf.build()
    if err := db.Delete(&Text{}).Error; err != nil {
    if err := db.Delete(&Word{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *TextSearch) First() (*Text, error) {
func (slf *WordSearch) First() (*Word, error) {
    var (
        record = new(Text)
        record = new(Word)
        db     = slf.build()
    )
@@ -183,9 +183,9 @@
    return record, nil
}
func (slf *TextSearch) Find() ([]*Text, int, error) {
func (slf *WordSearch) Find() ([]*Word, int, error) {
    var (
        records = make([]*Text, 0)
        records = make([]*Word, 0)
        total   int64
        db      = slf.build()
    )
@@ -203,9 +203,9 @@
    return records, int(total), nil
}
func (slf *TextSearch) FindNotTotal() ([]*Text, error) {
func (slf *WordSearch) FindNotTotal() ([]*Word, error) {
    var (
        records = make([]*Text, 0)
        records = make([]*Word, 0)
        db      = slf.build()
    )
@@ -220,9 +220,9 @@
}
// FindByQuery 指定条件查询.
func (slf *TextSearch) FindByQuery(query string, args []interface{}) ([]*Text, int64, error) {
func (slf *WordSearch) FindByQuery(query string, args []interface{}) ([]*Word, int64, error) {
    var (
        records = make([]*Text, 0)
        records = make([]*Word, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
@@ -241,9 +241,9 @@
}
// FindAll 指定条件查询&不分页.
func (slf *TextSearch) FindAll(query string, args []interface{}) ([]*Text, error) {
func (slf *WordSearch) FindAll(query string, args []interface{}) ([]*Word, error) {
    var (
        records = make([]*Text, 0)
        records = make([]*Word, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
@@ -255,7 +255,7 @@
}
//// InitDefaultData 初始化数据
//func (slf *TextSearch) InitDefaultData() error {
//func (slf *WordSearch) InitDefaultData() error {
//    var (
//        db          = slf.Orm.Table(slf.TableName())
//        total int64 = 0
@@ -266,7 +266,7 @@
//    if total != 0 {
//        return nil
//    }
//    Texts := make([]*Text, 0, 3)
//    Texts = append(Texts, &Text{Name: "财务部", Number: "cwb"})
//    return slf.CreateBatch(Texts)
//    Words := make([]*Word, 0, 3)
//    Words = append(Words, &Word{Name: "财务部", Number: "cwb"})
//    return slf.CreateBatch(Words)
//}
models/train.go
@@ -12,7 +12,7 @@
    TrainInfo struct {
        gorm.Model
        Name     string         `gorm:"type:varchar(255);not null;default:'';comment:名称" json:"name"` //名称
        Class    constvar.Class `gorm:"type:tinyint;not null;default:0;comment:分类" json:"class"`      //分类
        Class    constvar.Class `gorm:"type:tinyint;not null;default:0;comment:分类" json:"class"`      //分类  1 机车 2车次 3 车站
        ParentID uint           `gorm:"type:int;not null;default 0; comment:上级id" json:"parentID"`    //上级id
    }
request/audio.go
@@ -12,7 +12,7 @@
}
type ProcessAudio struct {
    ID uint `json:"id" binding:"required"`
    ID uint `json:"id" form:"id" binding:"required"`
}
type BatchProcessAudio struct {
response/audio.go
@@ -3,5 +3,5 @@
import "speechAnalysis/constvar"
type FollowResp struct {
    FollowStatus constvar.BoolType `json:"followStatus"`
    FollowStatus constvar.BoolType `json:"followStatus"` //1 已关注 2未关注
}
router/router.go
@@ -27,6 +27,7 @@
    {
        audioAPi.POST("upload", audioCtl.Upload)              // 上传音频
        audioAPi.GET("list", audioCtl.List)                   // 音频检索
        audioAPi.GET("info", audioCtl.AudioInfo)              // 音频详情
        audioAPi.GET("trainInfoList", audioCtl.TrainInfoList) // 火车信息列表
        audioAPi.POST("process", audioCtl.Process)            // 音频处理
        audioAPi.POST("batchProcess", audioCtl.BatchProcess)  // 音频批量处理