zhangqian
2024-05-14 89ff3763c7eaa2db032b965596566a870dcb5030
添加文字和文字列表接口
3个文件已添加
6个文件已修改
751 ■■■■■ 已修改文件
controllers/audio.go 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/text.go 89 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 130 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 130 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 84 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/db.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/text.go 272 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
request/text.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/audio.go
@@ -127,7 +127,7 @@
        return
    }
    util.ResponseFormatList(c, code.Success, list, int(total))
    util.ResponseFormatList(c, code.Success, list, total)
}
// Process
@@ -195,7 +195,7 @@
// @Produce   application/json
// @Param     object  body request.ProcessAudio true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/delete [post]
// @Router    /api-sa/v1/audio/delete [delete]
func (slf AudioCtl) Delete(c *gin.Context) {
    var params request.ProcessAudio
    if err := c.ShouldBind(&params); err != nil {
@@ -218,7 +218,7 @@
// @Produce   application/json
// @Param     object  body request.BatchProcessAudio true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/audio/batchDelete [post]
// @Router    /api-sa/v1/audio/batchDelete [delete]
func (slf AudioCtl) BatchDelete(c *gin.Context) {
    var params request.BatchProcessAudio
    if err := c.ShouldBind(&params); err != nil {
controllers/text.go
New file
@@ -0,0 +1,89 @@
package controllers
import (
    "errors"
    "github.com/gin-gonic/gin"
    "speechAnalysis/extend/code"
    "speechAnalysis/extend/util"
    "speechAnalysis/models"
    "speechAnalysis/pkg/logx"
    "speechAnalysis/request"
)
type TextCtl struct{}
// AddText
// @Tags      文字库
// @Summary   新增文字
// @Produce   application/json
// @Param     object  body request.AddTextReq true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-sa/v1/text/add [post]
func (slf TextCtl) AddText(c *gin.Context) {
    var req request.AddTextReq
    if err := c.BindJSON(&req); err != nil {
        logx.Errorf("add text params err:%v", err)
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    text := models.Text{
        Content:          req.Content,
        LocomotiveNumber: req.LocomotiveNumber,
    }
    if err := slf.paramsCheck(text); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := models.NewTextSearch().Create(&text); err != nil {
        util.ResponseFormat(c, code.SaveFail, "添加失败,请检查是否重复")
        return
    }
    util.ResponseFormat(c, code.Success, "添加成功")
}
func (slf TextCtl) paramsCheck(text models.Text) (err error) {
    if text.Content == "" || text.LocomotiveNumber == "" {
        return errors.New("参数缺失")
    }
    _, err = models.NewTextSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First()
    if err == nil {
        return errors.New("文字重复")
    }
    return nil
}
// List
// @Tags      文字库
// @Summary   文字库列表
// @Produce   application/json
// @Param     object  query    request.GetTextList true  "参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Text}  "成功"
// @Router    /api-sa/v1/text/list [get]
func (slf TextCtl) List(c *gin.Context) {
    var params request.GetTextList
    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.NewTextSearch().
        SetPage(params.Page, params.PageSize).
        SetKeyword(params.Keyword).
        Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    util.ResponseFormatList(c, code.Success, list, total)
}
docs/docs.go
@@ -17,7 +17,7 @@
    "basePath": "{{.BasePath}}",
    "paths": {
        "/api-sa/v1/audio/batchDelete": {
            "post": {
            "delete": {
                "produces": [
                    "application/json"
                ],
@@ -77,7 +77,7 @@
            }
        },
        "/api-sa/v1/audio/delete": {
            "post": {
            "delete": {
                "produces": [
                    "application/json"
                ],
@@ -283,6 +283,90 @@
                    }
                }
            }
        },
        "/api-sa/v1/text/add": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "文字库"
                ],
                "summary": "新增文字",
                "parameters": [
                    {
                        "description": "参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddTextReq"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-sa/v1/text/list": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "文字库"
                ],
                "summary": "文字库列表",
                "parameters": [
                    {
                        "type": "string",
                        "description": "关键字",
                        "name": "keyword",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Text"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    },
    "definitions": {
@@ -401,6 +485,48 @@
                }
            }
        },
        "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"
                }
            }
        },
        "request.AddTextReq": {
            "type": "object",
            "required": [
                "content",
                "locomotiveNumber"
            ],
            "properties": {
                "content": {
                    "description": "音频名称",
                    "type": "string"
                },
                "locomotiveNumber": {
                    "description": "机车号",
                    "type": "string"
                }
            }
        },
        "request.BatchProcessAudio": {
            "type": "object",
            "required": [
docs/swagger.json
@@ -5,7 +5,7 @@
    },
    "paths": {
        "/api-sa/v1/audio/batchDelete": {
            "post": {
            "delete": {
                "produces": [
                    "application/json"
                ],
@@ -65,7 +65,7 @@
            }
        },
        "/api-sa/v1/audio/delete": {
            "post": {
            "delete": {
                "produces": [
                    "application/json"
                ],
@@ -271,6 +271,90 @@
                    }
                }
            }
        },
        "/api-sa/v1/text/add": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "文字库"
                ],
                "summary": "新增文字",
                "parameters": [
                    {
                        "description": "参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddTextReq"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                        }
                    }
                }
            }
        },
        "/api-sa/v1/text/list": {
            "get": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "文字库"
                ],
                "summary": "文字库列表",
                "parameters": [
                    {
                        "type": "string",
                        "description": "关键字",
                        "name": "keyword",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "页码",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "每页大小",
                        "name": "pageSize",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Text"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    },
    "definitions": {
@@ -389,6 +473,48 @@
                }
            }
        },
        "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"
                }
            }
        },
        "request.AddTextReq": {
            "type": "object",
            "required": [
                "content",
                "locomotiveNumber"
            ],
            "properties": {
                "content": {
                    "description": "音频名称",
                    "type": "string"
                },
                "locomotiveNumber": {
                    "description": "机车号",
                    "type": "string"
                }
            }
        },
        "request.BatchProcessAudio": {
            "type": "object",
            "required": [
docs/swagger.yaml
@@ -80,6 +80,35 @@
      updatedAt:
        type: string
    type: object
  models.Text:
    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
  request.AddTextReq:
    properties:
      content:
        description: 音频名称
        type: string
      locomotiveNumber:
        description: 机车号
        type: string
    required:
    - content
    - locomotiveNumber
    type: object
  request.BatchProcessAudio:
    properties:
      ids:
@@ -134,7 +163,7 @@
  contact: {}
paths:
  /api-sa/v1/audio/batchDelete:
    post:
    delete:
      parameters:
      - description: 参数
        in: body
@@ -172,7 +201,7 @@
      tags:
      - 音频
  /api-sa/v1/audio/delete:
    post:
    delete:
      parameters:
      - description: 参数
        in: body
@@ -299,4 +328,55 @@
      summary: 上传音频
      tags:
      - 音频
  /api-sa/v1/text/add:
    post:
      parameters:
      - description: 参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.AddTextReq'
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
      summary: 新增文字
      tags:
      - 文字库
  /api-sa/v1/text/list:
    get:
      parameters:
      - description: 关键字
        in: query
        name: keyword
        type: string
      - description: 页码
        in: query
        name: page
        type: integer
      - description: 每页大小
        in: query
        name: pageSize
        type: integer
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            allOf:
            - $ref: '#/definitions/util.ResponseList'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/models.Text'
                  type: array
              type: object
      summary: 文字库列表
      tags:
      - 文字库
swagger: "2.0"
models/db.go
@@ -75,6 +75,7 @@
    err := db.AutoMigrate(
        Audio{},
        AudioText{},
        Text{},
    )
    return err
}
models/text.go
New file
@@ -0,0 +1,272 @@
package models
import (
    "fmt"
    "gorm.io/gorm"
    "speechAnalysis/pkg/mysqlx"
)
type (
    // Text 文字
    Text 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"` // 机车号
    }
    TextSearch struct {
        Text
        Order    string
        PageNum  int
        PageSize int
        Orm      *gorm.DB
        Keyword  string
        IDs      []uint
    }
)
func (slf *Text) TableName() string {
    return "text"
}
func NewTextSearch() *TextSearch {
    return &TextSearch{Orm: mysqlx.GetDB()}
}
func (slf *TextSearch) SetOrm(tx *gorm.DB) *TextSearch {
    slf.Orm = tx
    return slf
}
func (slf *TextSearch) SetPage(page, size int) *TextSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *TextSearch) SetOrder(order string) *TextSearch {
    slf.Order = order
    return slf
}
func (slf *TextSearch) SetID(id uint) *TextSearch {
    slf.ID = id
    return slf
}
func (slf *TextSearch) SetIDs(ids []uint) *TextSearch {
    slf.IDs = ids
    return slf
}
func (slf *TextSearch) SetKeyword(kw string) *TextSearch {
    slf.Keyword = kw
    return slf
}
func (slf *TextSearch) SetLocomotiveNumber(number string) *TextSearch {
    slf.LocomotiveNumber = number
    return slf
}
func (slf *TextSearch) SetContent(content string) *TextSearch {
    slf.Content = content
    return slf
}
func (slf *TextSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if slf.Keyword != "" {
        kw := "%" + slf.Keyword + "%"
        db = db.Where("locomotive_number like ? or content like ?", kw, kw)
    }
    if slf.LocomotiveNumber != "" {
        db = db.Where("locomotive_number = ?", slf.LocomotiveNumber)
    }
    if len(slf.IDs) > 0 {
        db = db.Where("id in ?", slf.IDs)
    }
    if slf.Content != "" {
        db = db.Where("content = ?", slf.Content)
    }
    return db
}
// Create 单条插入
func (slf *TextSearch) Create(record *Text) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
// CreateBatch 批量插入
func (slf *TextSearch) CreateBatch(records []*Text) error {
    var db = slf.build()
    if err := db.Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
    return nil
}
func (slf *TextSearch) Save(record *Text) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *TextSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *TextSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *TextSearch) Delete() error {
    var db = slf.build()
    if err := db.Delete(&Text{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *TextSearch) First() (*Text, error) {
    var (
        record = new(Text)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *TextSearch) Find() ([]*Text, int, error) {
    var (
        records = make([]*Text, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, int(total), fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, int(total), fmt.Errorf("find records err: %v", err)
    }
    return records, int(total), nil
}
func (slf *TextSearch) FindNotTotal() ([]*Text, error) {
    var (
        records = make([]*Text, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *TextSearch) FindByQuery(query string, args []interface{}) ([]*Text, int64, error) {
    var (
        records = make([]*Text, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindAll 指定条件查询&不分页.
func (slf *TextSearch) FindAll(query string, args []interface{}) ([]*Text, error) {
    var (
        records = make([]*Text, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
//// InitDefaultData 初始化数据
//func (slf *TextSearch) InitDefaultData() error {
//    var (
//        db          = slf.Orm.Table(slf.TableName())
//        total int64 = 0
//    )
//    if err := db.Count(&total).Error; err != nil {
//        return err
//    }
//    if total != 0 {
//        return nil
//    }
//    Texts := make([]*Text, 0, 3)
//    Texts = append(Texts, &Text{Name: "财务部", Number: "cwb"})
//    return slf.CreateBatch(Texts)
//}
request/text.go
New file
@@ -0,0 +1,11 @@
package request
type AddTextReq struct {
    Content          string `gorm:"type:varchar(255);not null;default:'';comment:音频名称" json:"content" binding:"required"`                // 音频名称
    LocomotiveNumber string `gorm:"index;type:varchar(255);not null;default:'';comment:机车号" json:"locomotiveNumber"  binding:"required"` // 机车号
}
type GetTextList struct {
    PageInfo
    Keyword string `form:"keyword"` // 关键字
}
router/router.go
@@ -22,16 +22,26 @@
    urlPrefix := "/api-sa/v1"
    // 音频管理
    AudioCtl := new(controllers.AudioCtl)
    organizeAPI := r.Group(urlPrefix + "/audio")
    audioCtl := new(controllers.AudioCtl)
    audioAPi := r.Group(urlPrefix + "/audio")
    {
        organizeAPI.POST("upload", AudioCtl.Upload)             // 上传音频
        organizeAPI.GET("list", AudioCtl.List)                  // 音频检索
        organizeAPI.POST("process", AudioCtl.Process)           // 音频处理
        organizeAPI.POST("batchProcess", AudioCtl.BatchProcess) // 音频批量处理
        organizeAPI.POST("delete", AudioCtl.Delete)             // 音频删除
        organizeAPI.POST("batchDelete", AudioCtl.BatchDelete)   // 音频批量删除
        organizeAPI.POST("follow", AudioCtl.Follow)             // 关注/取消关注
        audioAPi.POST("upload", audioCtl.Upload)             // 上传音频
        audioAPi.GET("list", audioCtl.List)                  // 音频检索
        audioAPi.POST("process", audioCtl.Process)           // 音频处理
        audioAPi.POST("batchProcess", audioCtl.BatchProcess) // 音频批量处理
        audioAPi.DELETE("delete", audioCtl.Delete)           // 音频删除
        audioAPi.DELETE("batchDelete", audioCtl.BatchDelete) // 音频批量删除
        audioAPi.POST("follow", audioCtl.Follow)             // 关注/取消关注
    }
    // 文字库管理
    textCtl := new(controllers.TextCtl)
    textApi := r.Group(urlPrefix + "/text")
    {
        textApi.POST("add", textCtl.AddText) // 添加文字
        textApi.GET("list", textCtl.List)    // 文字列表
    }
    return r