123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- package speech_recognition
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strings"
- "time"
- )
- const (
- SpeechRecognitionFileRemoveFlag = 1 // 文件删除标记
- SpeechRecognitionStateWait = 1
- SpeechRecognitionStateSuccess = 2
- SpeechRecognitionStateFail = 3
- )
- type SpeechRecognition struct {
- SpeechRecognitionId int `gorm:"primaryKey;column:speech_recognition_id;type:int(10) unsigned;not null"` // 语音识别Id
- UniqueCode string `gorm:"column:unique_code;type:varchar(64);not null;default:''"` // 唯一编码
- FileName string `gorm:"column:file_name;type:varchar(128);not null;default:''"` // 文件名称
- ResourceUrl string `gorm:"column:resource_url;type:varchar(255);not null;default:''"` // 文件路径
- MenuId int `gorm:"index:idx_menu_id;column:menu_id;type:int(10) unsigned;not null;default:0"` // 目录Id
- SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人Id
- SysUserName string `gorm:"column:sys_user_name;type:varchar(128);not null;default:''"` // 创建人姓名
- State int `gorm:"column:state;type:tinyint(4) unsigned;not null;default:0"` // 状态:1-待转换;2-转换完成;3-转换失败
- Abstract string `gorm:"column:abstract;type:text"` // 摘要,取前几段内容
- Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 目录下的排序
- FileState int `gorm:"column:file_state;type:tinyint(4) unsigned;not null;default:0"` // 文件删除状态:0-正常;1-删除(该字段作为软删标识)
- FileSecond int `gorm:"column:file_second;type:int(10) unsigned;not null;default:0"` // 文件时长(秒)
- FileSize int `gorm:"column:file_size;type:int(10) unsigned;not null;default:0"` // 文件大小(byte)
- ConvertRemark string `gorm:"column:convert_remark;type:varchar(255);not null;default:''"` // 转写备注-失败原因
- CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
- ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
- }
- var SpeechRecognitionCols = struct {
- SpeechRecognitionId string
- UniqueCode string
- FileName string
- ResourceUrl string
- MenuId string
- MenuPath string
- SysUserId string
- SysUserName string
- State string
- Abstract string
- Sort string
- FileState string
- FileSecond string
- FileSize string
- ConvertRemark string
- CreateTime string
- ModifyTime string
- }{
- SpeechRecognitionId: "speech_recognition_id",
- UniqueCode: "unique_code",
- FileName: "file_name",
- ResourceUrl: "resource_url",
- MenuId: "menu_id",
- MenuPath: "menu_path",
- SysUserId: "sys_user_id",
- SysUserName: "sys_user_name",
- State: "state",
- Abstract: "abstract",
- Sort: "sort",
- FileState: "file_state",
- FileSecond: "file_second",
- FileSize: "file_size",
- ConvertRemark: "convert_remark",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- func (m *SpeechRecognition) TableName() string {
- return "speech_recognition"
- }
- func (m *SpeechRecognition) PrimaryId() string {
- return SpeechRecognitionCols.SpeechRecognitionId
- }
- func (m *SpeechRecognition) Create() (err error) {
- err = global.DEFAULT_DmSQL.Create(m).Error
- return
- }
- func (m *SpeechRecognition) CreateMulti(items []*SpeechRecognition) (err error) {
- if len(items) == 0 {
- return
- }
- err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
- return
- }
- func (m *SpeechRecognition) Update(cols []string) (err error) {
- err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
- return
- }
- func (m *SpeechRecognition) Del() (err error) {
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- err = global.DEFAULT_DmSQL.Exec(sql, m.SpeechRecognitionId).Error
- return
- }
- func (m *SpeechRecognition) MultiDel(menuIds []int) (err error) {
- if len(menuIds) == 0 {
- return
- }
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
- err = global.DEFAULT_DmSQL.Exec(sql, menuIds).Error
- return
- }
- func (m *SpeechRecognition) GetItemById(id int) (item *SpeechRecognition, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error
- return
- }
- func (m *SpeechRecognition) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognition, err error) {
- order := ``
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
- err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error
- return
- }
- func (m *SpeechRecognition) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
- return
- }
- func (m *SpeechRecognition) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognition, err error) {
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
- return
- }
- func (m *SpeechRecognition) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognition, err error) {
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
- pars = append(pars, startSize)
- pars = append(pars, pageSize)
- err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
- return
- }
- // SpeechRecognitionItem 语音识别信息
- type SpeechRecognitionItem struct {
- SpeechRecognitionId int
- UniqueCode string `description:"唯一编码"`
- FileName string `description:"文件名称"`
- ResourceUrl string `description:"文件路径"`
- MenuId int `description:"目录ID"`
- SysUserId int `description:"创建人ID"`
- SysUserName string `description:"创建人姓名"`
- State int `description:"状态:1-待转换;2-转换完成;3-转换失败"`
- Abstract string `description:"摘要,取前几段内容"`
- Sort int `description:"目录下的排序"`
- ConvertRemark string `description:"转写备注-失败原因"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"修改时间"`
- }
- func FormatSpeechRecognition2Item(origin *SpeechRecognition) (item *SpeechRecognitionItem) {
- if origin == nil {
- return
- }
- item = new(SpeechRecognitionItem)
- item.SpeechRecognitionId = origin.SpeechRecognitionId
- item.UniqueCode = origin.UniqueCode
- item.FileName = origin.FileName
- item.ResourceUrl = origin.ResourceUrl
- if origin.FileState == SpeechRecognitionFileRemoveFlag {
- item.ResourceUrl = ""
- }
- item.MenuId = origin.MenuId
- item.SysUserId = origin.SysUserId
- item.SysUserName = origin.SysUserName
- item.State = origin.State
- item.Abstract = origin.Abstract
- item.Sort = origin.Sort
- item.ConvertRemark = origin.ConvertRemark
- item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
- item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
- return
- }
- // SpeechRecognitionSaveReq 保存请求体
- type SpeechRecognitionSaveReq struct {
- SpeechRecognitionId int `description:"语音识别ID"`
- FileName string `description:"文件名称"`
- TagIds []int `description:"标签IDs"`
- Contents []SpeechRecognitionSaveContentReq `description:"保存内容"`
- }
- // SpeechRecognitionSaveContentReq 保存内容
- type SpeechRecognitionSaveContentReq struct {
- SpeechRecognitionContentId int `description:"语音识别内容ID"`
- Content string `description:"段落内容"`
- }
- // SpeechRecognitionRenameReq 重命名
- type SpeechRecognitionRenameReq struct {
- SpeechRecognitionId int `description:"语音识别ID"`
- FileName string `description:"文件名称"`
- }
- // SpeechRecognitionRemoveReq 删除
- type SpeechRecognitionRemoveReq struct {
- SpeechRecognitionId int `description:"语音识别ID"`
- }
- // SpeechRecognitionRemoveFileReq 删除文件
- type SpeechRecognitionRemoveFileReq struct {
- SpeechRecognitionId int `description:"语音识别ID"`
- }
- // SpeechRecognitionSaveTagReq 保存标签
- type SpeechRecognitionSaveTagReq struct {
- SpeechRecognitionId int `description:"语音识别ID"`
- TagIds []int `description:"标签IDs"`
- }
- // SpeechRecognitionConvertReq 批量转写
- type SpeechRecognitionConvertReq struct {
- MenuId int `description:"目录ID"`
- Files []SpeechRecognitionConvertFiles `description:"转写文件"`
- }
- // SpeechRecognitionConvertFiles 批量转写文件
- type SpeechRecognitionConvertFiles struct {
- FileName string `description:"文件名称"`
- ResourceUrl string `description:"文件地址"`
- FileSecond int `description:"文件时长(秒)"`
- FileSize int `description:"文件大小(byte)"`
- }
- // UpdateSpeechAndApiLog 更新语音识别及API记录
- func UpdateSpeechAndApiLog(speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
- if speechItem == nil || apiLogItem == nil {
- err = fmt.Errorf("speechItem nil or apiLogItem nil")
- return
- }
- tx := global.DEFAULT_DmSQL.Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- e := tx.Select(speechCols).Updates(speechItem).Error
- if e != nil {
- err = fmt.Errorf("update speech err: %v", e)
- return
- }
- e = tx.Select(logCols).Updates(apiLogItem).Error
- if e != nil {
- err = fmt.Errorf("update api err: %v", e)
- return
- }
- return
- }
- // CreateContentAndUpdateSpeechAndApiLog 新增语音识别内容并更新语音识别及API记录
- func CreateContentAndUpdateSpeechAndApiLog(contents []*SpeechRecognitionContent, speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
- if speechItem == nil || apiLogItem == nil {
- err = fmt.Errorf("speechItem nil or apiLogItem nil")
- return
- }
- tx := global.DEFAULT_DmSQL.Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- e := tx.Select(speechCols).Updates(speechItem).Error
- if e != nil {
- err = fmt.Errorf("update speech err: %v", e)
- return
- }
- e = tx.Select(logCols).Updates(apiLogItem).Error
- if e != nil {
- err = fmt.Errorf("update api err: %v", e)
- return
- }
- if len(contents) > 0 {
- e = tx.CreateInBatches(contents, utils.MultiAddNum).Error
- if e != nil {
- err = fmt.Errorf("insert multi contents err: %s", e.Error())
- return
- }
- }
- return
- }
- // SpeechRecognitionDetailItem 语音识别详情信息
- type SpeechRecognitionDetailItem struct {
- SpeechRecognitionId int
- UniqueCode string `description:"唯一编码"`
- FileName string `description:"文件名称"`
- FileExt string `description:"文件后缀名"`
- ResourceUrl string `description:"文件地址"`
- MenuId int `description:"目录ID"`
- MenuPath []*SpeechRecognitionMenuItem `description:"目录全路径, 多级并列为一个数组"`
- SysUserId int `description:"创建人ID"`
- SysUserName string `description:"创建人姓名"`
- Abstract string `description:"摘要,取前几段内容"`
- Sort int `description:"目录下的排序"`
- FileSecond string `description:"文件时长(HH:MM:SS/MM:SS)"`
- FileSize string `description:"文件大小(MB)"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"修改时间"`
- Contents []*SpeechRecognitionContentItem `description:"语音识别内容"`
- Tags []*SpeechRecognitionDetailTag `description:"标签"`
- }
- func FormatSpeechRecognition2DetailItem(origin *SpeechRecognition, contents []*SpeechRecognitionContentItem, tags []*SpeechRecognitionDetailTag, menuPath []*SpeechRecognitionMenuItem) (item *SpeechRecognitionDetailItem) {
- if origin == nil {
- return
- }
- item = new(SpeechRecognitionDetailItem)
- item.SpeechRecognitionId = origin.SpeechRecognitionId
- item.UniqueCode = origin.UniqueCode
- item.FileName = origin.FileName
- item.ResourceUrl = origin.ResourceUrl
- if item.ResourceUrl != "" {
- pointArr := strings.Split(item.ResourceUrl, ".")
- if len(pointArr) > 0 {
- item.FileExt = pointArr[len(pointArr)-1]
- }
- }
- item.ResourceUrl = origin.ResourceUrl
- if origin.FileState == SpeechRecognitionFileRemoveFlag {
- item.ResourceUrl = ""
- }
- item.MenuId = origin.MenuId
- item.MenuPath = menuPath
- item.SysUserId = origin.SysUserId
- item.SysUserName = origin.SysUserName
- item.Abstract = origin.Abstract
- item.Sort = origin.Sort
- item.FileSecond = utils.SecondsToHHMMSS(origin.FileSecond)
- item.FileSize = fmt.Sprintf("%.2fM", utils.ByteToMB(origin.FileSize))
- item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
- item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
- item.Contents = contents
- item.Tags = tags
- return
- }
- // SpeechRecognitionDetailTag 语音识别详情标签
- type SpeechRecognitionDetailTag struct {
- TagId int `description:"标签ID" gorm:"tag_id"`
- TagName string `description:"标签名称" gorm:"tag_name"`
- }
- // GetSpeechRecognitionTagBySpeechId 获取语音识别标签
- func GetSpeechRecognitionTagBySpeechId(speechId int) (items []*SpeechRecognitionDetailTag, err error) {
- sql := `SELECT a.speech_recognition_tag_id AS tag_id, a.tag_name FROM speech_recognition_tag AS a
- JOIN speech_recognition_tag_mapping AS b ON a.speech_recognition_tag_id = b.tag_id
- WHERE b.speech_recognition_id = ?`
- err = global.DEFAULT_DmSQL.Raw(sql, speechId).Find(&items).Error
- return
- }
- // SpeechRecognitionMappingTags 语音识别标签
- type SpeechRecognitionMappingTags struct {
- SpeechRecognitionId int `description:"语音识别ID" gorm:"speech_recognition_id"`
- TagId int `description:"标签ID" gorm:"tag_id"`
- TagName string `description:"标签名称" gorm:"tag_name"`
- }
- // GetSpeechRecognitionTagsBySpeechIds 根据语音识别IDs获取标签
- func GetSpeechRecognitionTagsBySpeechIds(speechIds []int) (items []*SpeechRecognitionMappingTags, err error) {
- if len(speechIds) == 0 {
- return
- }
- sql := fmt.Sprintf(`SELECT a.speech_recognition_id, a.tag_id, b.tag_name FROM speech_recognition_tag_mapping AS a
- JOIN speech_recognition_tag AS b ON a.tag_id = b.speech_recognition_tag_id
- WHERE a.speech_recognition_id IN (%s)`, utils.GetOrmInReplace(len(speechIds)))
- err = global.DEFAULT_DmSQL.Raw(sql, speechIds).Find(&items).Error
- return
- }
- // SpeechRecognitionListReq 语音识别列表请求体
- type SpeechRecognitionListReq struct {
- PageSize int `form:"PageSize"`
- CurrentIndex int `form:"CurrentIndex"`
- FileName string `form:"FileName" description:"文件名称"`
- StartTime string `form:"StartTime" description:"开始时间"`
- EndTime string `form:"EndTime" description:"结束时间"`
- CreateUserId string `form:"CreateUserId" description:"创建人IDs"`
- TagId int `form:"TagId" description:"标签ID"`
- TagIds string `form:"TagIds" description:"标签IDs, 英文逗号分隔"`
- MenuId int `form:"MenuId" description:"目录ID"`
- IsTagMenu bool `form:"IsTagMenu" description:"是否为标签目录"`
- }
- // SpeechRecognitionListResp 语音识别列表响应体
- type SpeechRecognitionListResp struct {
- List []*SpeechRecognitionDetailItem
- Paging *paging.PagingItem `description:"分页数据"`
- }
- // SpeechRecognitionContentExportReq 导出内容
- type SpeechRecognitionContentExportReq struct {
- SpeechRecognitionId int `description:"语音识别ID"`
- ExportType int `description:"导出类型:1-txt;2-doc;3-pdf"`
- Timestamp bool `description:"是否显示时间戳"`
- }
- // UpdateSortByMenuId 根据分类ID更新排序
- func (m *SpeechRecognition) UpdateSortByMenuId(menuId, nowSort int, prevSpeechId int, updateSort string) (err error) {
- sql := fmt.Sprintf(`UPDATE %s SET %s = %s WHERE %s = ? `, m.TableName(), SpeechRecognitionCols.Sort, updateSort, SpeechRecognitionCols.MenuId)
- if prevSpeechId > 0 {
- sql += fmt.Sprintf(` AND (%s > ? OR (%s > %d AND %s = %d))`, SpeechRecognitionCols.Sort, SpeechRecognitionCols.SpeechRecognitionId, prevSpeechId, SpeechRecognitionCols.Sort, nowSort)
- } else {
- sql += fmt.Sprintf(` AND %s > ?`, SpeechRecognitionCols.Sort)
- }
- err = global.DEFAULT_DmSQL.Exec(sql, menuId, nowSort).Error
- return
- }
- // GetMaxSortByMenuId 获取分类下最大Sort
- func (m *SpeechRecognition) GetMaxSortByMenuId(menuId int) (sort int, err error) {
- sql := fmt.Sprintf(`SELECT COALESCE(MAX(sort),0) AS sort FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionCols.MenuId)
- err = global.DEFAULT_DmSQL.Raw(sql, menuId).Scan(&sort).Error
- return
- }
- // GetFirstByMenuId 获取目录下排序第一的数据
- func (m *SpeechRecognition) GetFirstByMenuId(menuId int) (item *SpeechRecognition, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? ORDER BY %s ASC, %s ASC LIMIT 1`, m.TableName(), SpeechRecognitionCols.MenuId, SpeechRecognitionCols.Sort, SpeechRecognitionCols.SpeechRecognitionId)
- err = global.DEFAULT_DmSQL.Raw(sql, menuId).First(&item).Error
- return
- }
- // SpeechRecognitionConvertCheckNameReq 校验文件重名请求体
- type SpeechRecognitionConvertCheckNameReq struct {
- FileName string `description:"文件名称"`
- }
- // SpeechSave 更新内容、摘要及标签
- func (m *SpeechRecognition) SpeechSave(speechItem *SpeechRecognition, speechCols []string, contents []SpeechRecognitionSaveContentReq, tagMappings []*SpeechRecognitionTagMapping) (err error) {
- if speechItem == nil {
- err = fmt.Errorf("speech nil")
- return
- }
- tx := global.DEFAULT_DmSQL.Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- // 转写文件
- if len(speechCols) > 0 {
- e := tx.Select(speechCols).Updates(speechItem).Error
- if e != nil {
- err = fmt.Errorf("update speech err: %s", e.Error())
- return
- }
- }
- // 转写内容
- if len(contents) > 0 {
- sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = 1, %s = NOW() WHERE %s = ?`, "speech_recognition_content", SpeechRecognitionContentCols.Content, SpeechRecognitionContentCols.IsUpdate, SpeechRecognitionContentCols.ModifyTime, SpeechRecognitionContentCols.SpeechRecognitionContentId)
- for _, v := range contents {
- e := tx.Exec(sql, v.Content, v.SpeechRecognitionContentId).Error
- if e != nil {
- err = fmt.Errorf("update exec err: %s", e.Error())
- return
- }
- }
- }
- // 标签
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, "speech_recognition_tag_mapping", SpeechRecognitionTagMappingCols.SpeechRecognitionId)
- e := tx.Exec(sql, speechItem.SpeechRecognitionId).Error
- if e != nil {
- err = fmt.Errorf("remove tag mappings err: %s", e.Error())
- return
- }
- if len(tagMappings) > 0 {
- e = tx.CreateInBatches(tagMappings, utils.MultiAddNum).Error
- if e != nil {
- err = fmt.Errorf("insert tag mappings err: %s", e.Error())
- return
- }
- }
- return
- }
|