speech_recognition.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package speech_recognition
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. SpeechRecognitionFileRemoveFlag = 1 // 文件删除标记
  12. SpeechRecognitionStateWait = 1
  13. SpeechRecognitionStateSuccess = 2
  14. SpeechRecognitionStateFail = 3
  15. )
  16. type SpeechRecognition struct {
  17. SpeechRecognitionId int `gorm:"primaryKey;column:speech_recognition_id;type:int(10) unsigned;not null"` // 语音识别Id
  18. UniqueCode string `gorm:"column:unique_code;type:varchar(64);not null;default:''"` // 唯一编码
  19. FileName string `gorm:"column:file_name;type:varchar(128);not null;default:''"` // 文件名称
  20. ResourceUrl string `gorm:"column:resource_url;type:varchar(255);not null;default:''"` // 文件路径
  21. MenuId int `gorm:"index:idx_menu_id;column:menu_id;type:int(10) unsigned;not null;default:0"` // 目录Id
  22. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人Id
  23. SysUserName string `gorm:"column:sys_user_name;type:varchar(128);not null;default:''"` // 创建人姓名
  24. State int `gorm:"column:state;type:tinyint(4) unsigned;not null;default:0"` // 状态:1-待转换;2-转换完成;3-转换失败
  25. Abstract string `gorm:"column:abstract;type:text"` // 摘要,取前几段内容
  26. Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 目录下的排序
  27. FileState int `gorm:"column:file_state;type:tinyint(4) unsigned;not null;default:0"` // 文件删除状态:0-正常;1-删除(该字段作为软删标识)
  28. FileSecond int `gorm:"column:file_second;type:int(10) unsigned;not null;default:0"` // 文件时长(秒)
  29. FileSize int `gorm:"column:file_size;type:int(10) unsigned;not null;default:0"` // 文件大小(byte)
  30. ConvertRemark string `gorm:"column:convert_remark;type:varchar(255);not null;default:''"` // 转写备注-失败原因
  31. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  32. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  33. }
  34. var SpeechRecognitionCols = struct {
  35. SpeechRecognitionId string
  36. UniqueCode string
  37. FileName string
  38. ResourceUrl string
  39. MenuId string
  40. MenuPath string
  41. SysUserId string
  42. SysUserName string
  43. State string
  44. Abstract string
  45. Sort string
  46. FileState string
  47. FileSecond string
  48. FileSize string
  49. ConvertRemark string
  50. CreateTime string
  51. ModifyTime string
  52. }{
  53. SpeechRecognitionId: "speech_recognition_id",
  54. UniqueCode: "unique_code",
  55. FileName: "file_name",
  56. ResourceUrl: "resource_url",
  57. MenuId: "menu_id",
  58. MenuPath: "menu_path",
  59. SysUserId: "sys_user_id",
  60. SysUserName: "sys_user_name",
  61. State: "state",
  62. Abstract: "abstract",
  63. Sort: "sort",
  64. FileState: "file_state",
  65. FileSecond: "file_second",
  66. FileSize: "file_size",
  67. ConvertRemark: "convert_remark",
  68. CreateTime: "create_time",
  69. ModifyTime: "modify_time",
  70. }
  71. func (m *SpeechRecognition) TableName() string {
  72. return "speech_recognition"
  73. }
  74. func (m *SpeechRecognition) PrimaryId() string {
  75. return SpeechRecognitionCols.SpeechRecognitionId
  76. }
  77. func (m *SpeechRecognition) Create() (err error) {
  78. err = global.DEFAULT_DmSQL.Create(m).Error
  79. return
  80. }
  81. func (m *SpeechRecognition) CreateMulti(items []*SpeechRecognition) (err error) {
  82. if len(items) == 0 {
  83. return
  84. }
  85. err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
  86. return
  87. }
  88. func (m *SpeechRecognition) Update(cols []string) (err error) {
  89. err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
  90. return
  91. }
  92. func (m *SpeechRecognition) Del() (err error) {
  93. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  94. err = global.DEFAULT_DmSQL.Exec(sql, m.SpeechRecognitionId).Error
  95. return
  96. }
  97. func (m *SpeechRecognition) MultiDel(menuIds []int) (err error) {
  98. if len(menuIds) == 0 {
  99. return
  100. }
  101. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  102. err = global.DEFAULT_DmSQL.Exec(sql, menuIds).Error
  103. return
  104. }
  105. func (m *SpeechRecognition) GetItemById(id int) (item *SpeechRecognition, err error) {
  106. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  107. err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error
  108. return
  109. }
  110. func (m *SpeechRecognition) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognition, err error) {
  111. order := ``
  112. if orderRule != "" {
  113. order = ` ORDER BY ` + orderRule
  114. }
  115. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  116. err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error
  117. return
  118. }
  119. func (m *SpeechRecognition) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  120. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  121. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  122. return
  123. }
  124. func (m *SpeechRecognition) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognition, err error) {
  125. fields := strings.Join(fieldArr, ",")
  126. if len(fieldArr) == 0 {
  127. fields = `*`
  128. }
  129. order := `ORDER BY create_time DESC`
  130. if orderRule != "" {
  131. order = ` ORDER BY ` + orderRule
  132. }
  133. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  134. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  135. return
  136. }
  137. func (m *SpeechRecognition) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognition, err error) {
  138. fields := strings.Join(fieldArr, ",")
  139. if len(fieldArr) == 0 {
  140. fields = `*`
  141. }
  142. order := `ORDER BY create_time DESC`
  143. if orderRule != "" {
  144. order = ` ORDER BY ` + orderRule
  145. }
  146. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  147. pars = append(pars, startSize)
  148. pars = append(pars, pageSize)
  149. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  150. return
  151. }
  152. // SpeechRecognitionItem 语音识别信息
  153. type SpeechRecognitionItem struct {
  154. SpeechRecognitionId int
  155. UniqueCode string `description:"唯一编码"`
  156. FileName string `description:"文件名称"`
  157. ResourceUrl string `description:"文件路径"`
  158. MenuId int `description:"目录ID"`
  159. SysUserId int `description:"创建人ID"`
  160. SysUserName string `description:"创建人姓名"`
  161. State int `description:"状态:1-待转换;2-转换完成;3-转换失败"`
  162. Abstract string `description:"摘要,取前几段内容"`
  163. Sort int `description:"目录下的排序"`
  164. ConvertRemark string `description:"转写备注-失败原因"`
  165. CreateTime string `description:"创建时间"`
  166. ModifyTime string `description:"修改时间"`
  167. }
  168. func FormatSpeechRecognition2Item(origin *SpeechRecognition) (item *SpeechRecognitionItem) {
  169. if origin == nil {
  170. return
  171. }
  172. item = new(SpeechRecognitionItem)
  173. item.SpeechRecognitionId = origin.SpeechRecognitionId
  174. item.UniqueCode = origin.UniqueCode
  175. item.FileName = origin.FileName
  176. item.ResourceUrl = origin.ResourceUrl
  177. if origin.FileState == SpeechRecognitionFileRemoveFlag {
  178. item.ResourceUrl = ""
  179. }
  180. item.MenuId = origin.MenuId
  181. item.SysUserId = origin.SysUserId
  182. item.SysUserName = origin.SysUserName
  183. item.State = origin.State
  184. item.Abstract = origin.Abstract
  185. item.Sort = origin.Sort
  186. item.ConvertRemark = origin.ConvertRemark
  187. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  188. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  189. return
  190. }
  191. // SpeechRecognitionSaveReq 保存请求体
  192. type SpeechRecognitionSaveReq struct {
  193. SpeechRecognitionId int `description:"语音识别ID"`
  194. FileName string `description:"文件名称"`
  195. TagIds []int `description:"标签IDs"`
  196. Contents []SpeechRecognitionSaveContentReq `description:"保存内容"`
  197. }
  198. // SpeechRecognitionSaveContentReq 保存内容
  199. type SpeechRecognitionSaveContentReq struct {
  200. SpeechRecognitionContentId int `description:"语音识别内容ID"`
  201. Content string `description:"段落内容"`
  202. }
  203. // SpeechRecognitionRenameReq 重命名
  204. type SpeechRecognitionRenameReq struct {
  205. SpeechRecognitionId int `description:"语音识别ID"`
  206. FileName string `description:"文件名称"`
  207. }
  208. // SpeechRecognitionRemoveReq 删除
  209. type SpeechRecognitionRemoveReq struct {
  210. SpeechRecognitionId int `description:"语音识别ID"`
  211. }
  212. // SpeechRecognitionRemoveFileReq 删除文件
  213. type SpeechRecognitionRemoveFileReq struct {
  214. SpeechRecognitionId int `description:"语音识别ID"`
  215. }
  216. // SpeechRecognitionSaveTagReq 保存标签
  217. type SpeechRecognitionSaveTagReq struct {
  218. SpeechRecognitionId int `description:"语音识别ID"`
  219. TagIds []int `description:"标签IDs"`
  220. }
  221. // SpeechRecognitionConvertReq 批量转写
  222. type SpeechRecognitionConvertReq struct {
  223. MenuId int `description:"目录ID"`
  224. Files []SpeechRecognitionConvertFiles `description:"转写文件"`
  225. }
  226. // SpeechRecognitionConvertFiles 批量转写文件
  227. type SpeechRecognitionConvertFiles struct {
  228. FileName string `description:"文件名称"`
  229. ResourceUrl string `description:"文件地址"`
  230. FileSecond int `description:"文件时长(秒)"`
  231. FileSize int `description:"文件大小(byte)"`
  232. }
  233. // UpdateSpeechAndApiLog 更新语音识别及API记录
  234. func UpdateSpeechAndApiLog(speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
  235. if speechItem == nil || apiLogItem == nil {
  236. err = fmt.Errorf("speechItem nil or apiLogItem nil")
  237. return
  238. }
  239. tx := global.DEFAULT_DmSQL.Begin()
  240. defer func() {
  241. if err != nil {
  242. _ = tx.Rollback()
  243. return
  244. }
  245. _ = tx.Commit()
  246. }()
  247. e := tx.Select(speechCols).Updates(speechItem).Error
  248. if e != nil {
  249. err = fmt.Errorf("update speech err: %v", e)
  250. return
  251. }
  252. e = tx.Select(logCols).Updates(apiLogItem).Error
  253. if e != nil {
  254. err = fmt.Errorf("update api err: %v", e)
  255. return
  256. }
  257. return
  258. }
  259. // CreateContentAndUpdateSpeechAndApiLog 新增语音识别内容并更新语音识别及API记录
  260. func CreateContentAndUpdateSpeechAndApiLog(contents []*SpeechRecognitionContent, speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
  261. if speechItem == nil || apiLogItem == nil {
  262. err = fmt.Errorf("speechItem nil or apiLogItem nil")
  263. return
  264. }
  265. tx := global.DEFAULT_DmSQL.Begin()
  266. defer func() {
  267. if err != nil {
  268. _ = tx.Rollback()
  269. return
  270. }
  271. _ = tx.Commit()
  272. }()
  273. e := tx.Select(speechCols).Updates(speechItem).Error
  274. if e != nil {
  275. err = fmt.Errorf("update speech err: %v", e)
  276. return
  277. }
  278. e = tx.Select(logCols).Updates(apiLogItem).Error
  279. if e != nil {
  280. err = fmt.Errorf("update api err: %v", e)
  281. return
  282. }
  283. if len(contents) > 0 {
  284. e = tx.CreateInBatches(contents, utils.MultiAddNum).Error
  285. if e != nil {
  286. err = fmt.Errorf("insert multi contents err: %s", e.Error())
  287. return
  288. }
  289. }
  290. return
  291. }
  292. // SpeechRecognitionDetailItem 语音识别详情信息
  293. type SpeechRecognitionDetailItem struct {
  294. SpeechRecognitionId int
  295. UniqueCode string `description:"唯一编码"`
  296. FileName string `description:"文件名称"`
  297. FileExt string `description:"文件后缀名"`
  298. ResourceUrl string `description:"文件地址"`
  299. MenuId int `description:"目录ID"`
  300. MenuPath []*SpeechRecognitionMenuItem `description:"目录全路径, 多级并列为一个数组"`
  301. SysUserId int `description:"创建人ID"`
  302. SysUserName string `description:"创建人姓名"`
  303. Abstract string `description:"摘要,取前几段内容"`
  304. Sort int `description:"目录下的排序"`
  305. FileSecond string `description:"文件时长(HH:MM:SS/MM:SS)"`
  306. FileSize string `description:"文件大小(MB)"`
  307. CreateTime string `description:"创建时间"`
  308. ModifyTime string `description:"修改时间"`
  309. Contents []*SpeechRecognitionContentItem `description:"语音识别内容"`
  310. Tags []*SpeechRecognitionDetailTag `description:"标签"`
  311. }
  312. func FormatSpeechRecognition2DetailItem(origin *SpeechRecognition, contents []*SpeechRecognitionContentItem, tags []*SpeechRecognitionDetailTag, menuPath []*SpeechRecognitionMenuItem) (item *SpeechRecognitionDetailItem) {
  313. if origin == nil {
  314. return
  315. }
  316. item = new(SpeechRecognitionDetailItem)
  317. item.SpeechRecognitionId = origin.SpeechRecognitionId
  318. item.UniqueCode = origin.UniqueCode
  319. item.FileName = origin.FileName
  320. item.ResourceUrl = origin.ResourceUrl
  321. if item.ResourceUrl != "" {
  322. pointArr := strings.Split(item.ResourceUrl, ".")
  323. if len(pointArr) > 0 {
  324. item.FileExt = pointArr[len(pointArr)-1]
  325. }
  326. }
  327. item.ResourceUrl = origin.ResourceUrl
  328. if origin.FileState == SpeechRecognitionFileRemoveFlag {
  329. item.ResourceUrl = ""
  330. }
  331. item.MenuId = origin.MenuId
  332. item.MenuPath = menuPath
  333. item.SysUserId = origin.SysUserId
  334. item.SysUserName = origin.SysUserName
  335. item.Abstract = origin.Abstract
  336. item.Sort = origin.Sort
  337. item.FileSecond = utils.SecondsToHHMMSS(origin.FileSecond)
  338. item.FileSize = fmt.Sprintf("%.2fM", utils.ByteToMB(origin.FileSize))
  339. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  340. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  341. item.Contents = contents
  342. item.Tags = tags
  343. return
  344. }
  345. // SpeechRecognitionDetailTag 语音识别详情标签
  346. type SpeechRecognitionDetailTag struct {
  347. TagId int `description:"标签ID" gorm:"tag_id"`
  348. TagName string `description:"标签名称" gorm:"tag_name"`
  349. }
  350. // GetSpeechRecognitionTagBySpeechId 获取语音识别标签
  351. func GetSpeechRecognitionTagBySpeechId(speechId int) (items []*SpeechRecognitionDetailTag, err error) {
  352. sql := `SELECT a.speech_recognition_tag_id AS tag_id, a.tag_name FROM speech_recognition_tag AS a
  353. JOIN speech_recognition_tag_mapping AS b ON a.speech_recognition_tag_id = b.tag_id
  354. WHERE b.speech_recognition_id = ?`
  355. err = global.DEFAULT_DmSQL.Raw(sql, speechId).Find(&items).Error
  356. return
  357. }
  358. // SpeechRecognitionMappingTags 语音识别标签
  359. type SpeechRecognitionMappingTags struct {
  360. SpeechRecognitionId int `description:"语音识别ID" gorm:"speech_recognition_id"`
  361. TagId int `description:"标签ID" gorm:"tag_id"`
  362. TagName string `description:"标签名称" gorm:"tag_name"`
  363. }
  364. // GetSpeechRecognitionTagsBySpeechIds 根据语音识别IDs获取标签
  365. func GetSpeechRecognitionTagsBySpeechIds(speechIds []int) (items []*SpeechRecognitionMappingTags, err error) {
  366. if len(speechIds) == 0 {
  367. return
  368. }
  369. sql := fmt.Sprintf(`SELECT a.speech_recognition_id, a.tag_id, b.tag_name FROM speech_recognition_tag_mapping AS a
  370. JOIN speech_recognition_tag AS b ON a.tag_id = b.speech_recognition_tag_id
  371. WHERE a.speech_recognition_id IN (%s)`, utils.GetOrmInReplace(len(speechIds)))
  372. err = global.DEFAULT_DmSQL.Raw(sql, speechIds).Find(&items).Error
  373. return
  374. }
  375. // SpeechRecognitionListReq 语音识别列表请求体
  376. type SpeechRecognitionListReq struct {
  377. PageSize int `form:"PageSize"`
  378. CurrentIndex int `form:"CurrentIndex"`
  379. FileName string `form:"FileName" description:"文件名称"`
  380. StartTime string `form:"StartTime" description:"开始时间"`
  381. EndTime string `form:"EndTime" description:"结束时间"`
  382. CreateUserId string `form:"CreateUserId" description:"创建人IDs"`
  383. TagId int `form:"TagId" description:"标签ID"`
  384. TagIds string `form:"TagIds" description:"标签IDs, 英文逗号分隔"`
  385. MenuId int `form:"MenuId" description:"目录ID"`
  386. IsTagMenu bool `form:"IsTagMenu" description:"是否为标签目录"`
  387. }
  388. // SpeechRecognitionListResp 语音识别列表响应体
  389. type SpeechRecognitionListResp struct {
  390. List []*SpeechRecognitionDetailItem
  391. Paging *paging.PagingItem `description:"分页数据"`
  392. }
  393. // SpeechRecognitionContentExportReq 导出内容
  394. type SpeechRecognitionContentExportReq struct {
  395. SpeechRecognitionId int `description:"语音识别ID"`
  396. ExportType int `description:"导出类型:1-txt;2-doc;3-pdf"`
  397. Timestamp bool `description:"是否显示时间戳"`
  398. }
  399. // UpdateSortByMenuId 根据分类ID更新排序
  400. func (m *SpeechRecognition) UpdateSortByMenuId(menuId, nowSort int, prevSpeechId int, updateSort string) (err error) {
  401. sql := fmt.Sprintf(`UPDATE %s SET %s = %s WHERE %s = ? `, m.TableName(), SpeechRecognitionCols.Sort, updateSort, SpeechRecognitionCols.MenuId)
  402. if prevSpeechId > 0 {
  403. sql += fmt.Sprintf(` AND (%s > ? OR (%s > %d AND %s = %d))`, SpeechRecognitionCols.Sort, SpeechRecognitionCols.SpeechRecognitionId, prevSpeechId, SpeechRecognitionCols.Sort, nowSort)
  404. } else {
  405. sql += fmt.Sprintf(` AND %s > ?`, SpeechRecognitionCols.Sort)
  406. }
  407. err = global.DEFAULT_DmSQL.Exec(sql, menuId, nowSort).Error
  408. return
  409. }
  410. // GetMaxSortByMenuId 获取分类下最大Sort
  411. func (m *SpeechRecognition) GetMaxSortByMenuId(menuId int) (sort int, err error) {
  412. sql := fmt.Sprintf(`SELECT COALESCE(MAX(sort),0) AS sort FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionCols.MenuId)
  413. err = global.DEFAULT_DmSQL.Raw(sql, menuId).Scan(&sort).Error
  414. return
  415. }
  416. // GetFirstByMenuId 获取目录下排序第一的数据
  417. func (m *SpeechRecognition) GetFirstByMenuId(menuId int) (item *SpeechRecognition, err error) {
  418. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? ORDER BY %s ASC, %s ASC LIMIT 1`, m.TableName(), SpeechRecognitionCols.MenuId, SpeechRecognitionCols.Sort, SpeechRecognitionCols.SpeechRecognitionId)
  419. err = global.DEFAULT_DmSQL.Raw(sql, menuId).First(&item).Error
  420. return
  421. }
  422. // SpeechRecognitionConvertCheckNameReq 校验文件重名请求体
  423. type SpeechRecognitionConvertCheckNameReq struct {
  424. FileName string `description:"文件名称"`
  425. }
  426. // SpeechSave 更新内容、摘要及标签
  427. func (m *SpeechRecognition) SpeechSave(speechItem *SpeechRecognition, speechCols []string, contents []SpeechRecognitionSaveContentReq, tagMappings []*SpeechRecognitionTagMapping) (err error) {
  428. if speechItem == nil {
  429. err = fmt.Errorf("speech nil")
  430. return
  431. }
  432. tx := global.DEFAULT_DmSQL.Begin()
  433. defer func() {
  434. if err != nil {
  435. _ = tx.Rollback()
  436. return
  437. }
  438. _ = tx.Commit()
  439. }()
  440. // 转写文件
  441. if len(speechCols) > 0 {
  442. e := tx.Select(speechCols).Updates(speechItem).Error
  443. if e != nil {
  444. err = fmt.Errorf("update speech err: %s", e.Error())
  445. return
  446. }
  447. }
  448. // 转写内容
  449. if len(contents) > 0 {
  450. sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = 1, %s = NOW() WHERE %s = ?`, "speech_recognition_content", SpeechRecognitionContentCols.Content, SpeechRecognitionContentCols.IsUpdate, SpeechRecognitionContentCols.ModifyTime, SpeechRecognitionContentCols.SpeechRecognitionContentId)
  451. for _, v := range contents {
  452. e := tx.Exec(sql, v.Content, v.SpeechRecognitionContentId).Error
  453. if e != nil {
  454. err = fmt.Errorf("update exec err: %s", e.Error())
  455. return
  456. }
  457. }
  458. }
  459. // 标签
  460. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, "speech_recognition_tag_mapping", SpeechRecognitionTagMappingCols.SpeechRecognitionId)
  461. e := tx.Exec(sql, speechItem.SpeechRecognitionId).Error
  462. if e != nil {
  463. err = fmt.Errorf("remove tag mappings err: %s", e.Error())
  464. return
  465. }
  466. if len(tagMappings) > 0 {
  467. e = tx.CreateInBatches(tagMappings, utils.MultiAddNum).Error
  468. if e != nil {
  469. err = fmt.Errorf("insert tag mappings err: %s", e.Error())
  470. return
  471. }
  472. }
  473. return
  474. }