speech_recognition.go 26 KB

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