speech_recognition.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  191. return
  192. }
  193. // SpeechRecognitionItem 语音识别信息
  194. type SpeechRecognitionItem struct {
  195. SpeechRecognitionId int
  196. UniqueCode string `description:"唯一编码"`
  197. FileName string `description:"文件名称"`
  198. ResourceUrl string `description:"文件路径"`
  199. MenuId int `description:"目录ID"`
  200. SysUserId int `description:"创建人ID"`
  201. SysUserName string `description:"创建人姓名"`
  202. State int `description:"状态:1-待转换;2-转换完成;3-转换失败"`
  203. Abstract string `description:"摘要,取前几段内容"`
  204. Sort int `description:"目录下的排序"`
  205. ConvertRemark string `description:"转写备注-失败原因"`
  206. CreateTime string `description:"创建时间"`
  207. ModifyTime string `description:"修改时间"`
  208. }
  209. func FormatSpeechRecognition2Item(origin *SpeechRecognition) (item *SpeechRecognitionItem) {
  210. if origin == nil {
  211. return
  212. }
  213. item = new(SpeechRecognitionItem)
  214. item.SpeechRecognitionId = origin.SpeechRecognitionId
  215. item.UniqueCode = origin.UniqueCode
  216. item.FileName = origin.FileName
  217. item.ResourceUrl = origin.ResourceUrl
  218. if origin.FileState == SpeechRecognitionFileRemoveFlag {
  219. item.ResourceUrl = ""
  220. }
  221. item.MenuId = origin.MenuId
  222. item.SysUserId = origin.SysUserId
  223. item.SysUserName = origin.SysUserName
  224. item.State = origin.State
  225. item.Abstract = origin.Abstract
  226. item.Sort = origin.Sort
  227. item.ConvertRemark = origin.ConvertRemark
  228. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  229. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  230. return
  231. }
  232. // SpeechRecognitionSaveReq 保存请求体
  233. type SpeechRecognitionSaveReq struct {
  234. SpeechRecognitionId int `description:"语音识别ID"`
  235. FileName string `description:"文件名称"`
  236. TagIds []int `description:"标签IDs"`
  237. Contents []SpeechRecognitionSaveContentReq `description:"保存内容"`
  238. }
  239. // SpeechRecognitionSaveContentReq 保存内容
  240. type SpeechRecognitionSaveContentReq struct {
  241. SpeechRecognitionContentId int `description:"语音识别内容ID"`
  242. Content string `description:"段落内容"`
  243. }
  244. // SpeechRecognitionRenameReq 重命名
  245. type SpeechRecognitionRenameReq struct {
  246. SpeechRecognitionId int `description:"语音识别ID"`
  247. FileName string `description:"文件名称"`
  248. }
  249. // SpeechRecognitionRemoveReq 删除
  250. type SpeechRecognitionRemoveReq struct {
  251. SpeechRecognitionId int `description:"语音识别ID"`
  252. }
  253. // SpeechRecognitionRemoveFileReq 删除文件
  254. type SpeechRecognitionRemoveFileReq struct {
  255. SpeechRecognitionId int `description:"语音识别ID"`
  256. }
  257. // SpeechRecognitionSaveTagReq 保存标签
  258. type SpeechRecognitionSaveTagReq struct {
  259. SpeechRecognitionId int `description:"语音识别ID"`
  260. TagIds []int `description:"标签IDs"`
  261. }
  262. // SpeechRecognitionConvertReq 批量转写
  263. type SpeechRecognitionConvertReq struct {
  264. MenuId int `description:"目录ID"`
  265. Files []SpeechRecognitionConvertFiles `description:"转写文件"`
  266. }
  267. // SpeechRecognitionConvertFiles 批量转写文件
  268. type SpeechRecognitionConvertFiles struct {
  269. FileName string `description:"文件名称"`
  270. ResourceUrl string `description:"文件地址"`
  271. FileSecond int `description:"文件时长(秒)"`
  272. FileSize int `description:"文件大小(byte)"`
  273. }
  274. // UpdateSpeechAndApiLog 更新语音识别及API记录
  275. func UpdateSpeechAndApiLog(speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
  276. if speechItem == nil || apiLogItem == nil {
  277. err = fmt.Errorf("speechItem nil or apiLogItem nil")
  278. return
  279. }
  280. //o := orm.NewOrm()
  281. //tx, err := o.Begin()
  282. //if err != nil {
  283. // return
  284. //}
  285. //defer func() {
  286. // if err != nil {
  287. // _ = tx.Rollback()
  288. // } else {
  289. // _ = tx.Commit()
  290. // }
  291. //}()
  292. //
  293. //_, e := tx.Update(speechItem, speechCols...)
  294. //if e != nil {
  295. // err = fmt.Errorf("update speech err: %s", e.Error())
  296. // return
  297. //}
  298. //_, e = tx.Update(apiLogItem, logCols...)
  299. //if e != nil {
  300. // err = fmt.Errorf("update api log err: %s", e.Error())
  301. // return
  302. //}
  303. tx := global.DEFAULT_DmSQL.Begin()
  304. defer func() {
  305. if err != nil {
  306. _ = tx.Rollback()
  307. return
  308. }
  309. _ = tx.Commit()
  310. }()
  311. e := tx.Select(speechCols).Updates(speechItem).Error
  312. if e != nil {
  313. err = fmt.Errorf("update speech err: %v", e)
  314. return
  315. }
  316. e = tx.Select(logCols).Updates(apiLogItem).Error
  317. if e != nil {
  318. err = fmt.Errorf("update api err: %v", e)
  319. return
  320. }
  321. return
  322. }
  323. // CreateContentAndUpdateSpeechAndApiLog 新增语音识别内容并更新语音识别及API记录
  324. func CreateContentAndUpdateSpeechAndApiLog(contents []*SpeechRecognitionContent, speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
  325. if speechItem == nil || apiLogItem == nil {
  326. err = fmt.Errorf("speechItem nil or apiLogItem nil")
  327. return
  328. }
  329. //o := orm.NewOrm()
  330. //tx, err := o.Begin()
  331. //if err != nil {
  332. // return
  333. //}
  334. //defer func() {
  335. // if err != nil {
  336. // _ = tx.Rollback()
  337. // } else {
  338. // _ = tx.Commit()
  339. // }
  340. //}()
  341. //
  342. //_, e := tx.Update(speechItem, speechCols...)
  343. //if e != nil {
  344. // err = fmt.Errorf("update speech err: %s", e.Error())
  345. // return
  346. //}
  347. //_, e = tx.Update(apiLogItem, logCols...)
  348. //if e != nil {
  349. // err = fmt.Errorf("update api log err: %s", e.Error())
  350. // return
  351. //}
  352. //if len(contents) > 0 {
  353. // _, e = tx.InsertMulti(len(contents), contents)
  354. // if e != nil {
  355. // err = fmt.Errorf("insert multi contents err: %s", e.Error())
  356. // return
  357. // }
  358. //}
  359. tx := global.DEFAULT_DmSQL.Begin()
  360. defer func() {
  361. if err != nil {
  362. _ = tx.Rollback()
  363. return
  364. }
  365. _ = tx.Commit()
  366. }()
  367. e := tx.Select(speechCols).Updates(speechItem).Error
  368. if e != nil {
  369. err = fmt.Errorf("update speech err: %v", e)
  370. return
  371. }
  372. e = tx.Select(logCols).Updates(apiLogItem).Error
  373. if e != nil {
  374. err = fmt.Errorf("update api err: %v", e)
  375. return
  376. }
  377. if len(contents) > 0 {
  378. e = tx.CreateInBatches(contents, utils.MultiAddNum).Error
  379. if e != nil {
  380. err = fmt.Errorf("insert multi contents err: %s", e.Error())
  381. return
  382. }
  383. }
  384. return
  385. }
  386. // SpeechRecognitionDetailItem 语音识别详情信息
  387. type SpeechRecognitionDetailItem struct {
  388. SpeechRecognitionId int
  389. UniqueCode string `description:"唯一编码"`
  390. FileName string `description:"文件名称"`
  391. FileExt string `description:"文件后缀名"`
  392. ResourceUrl string `description:"文件地址"`
  393. MenuId int `description:"目录ID"`
  394. MenuPath []*SpeechRecognitionMenuItem `description:"目录全路径, 多级并列为一个数组"`
  395. SysUserId int `description:"创建人ID"`
  396. SysUserName string `description:"创建人姓名"`
  397. Abstract string `description:"摘要,取前几段内容"`
  398. Sort int `description:"目录下的排序"`
  399. FileSecond string `description:"文件时长(HH:MM:SS/MM:SS)"`
  400. FileSize string `description:"文件大小(MB)"`
  401. CreateTime string `description:"创建时间"`
  402. ModifyTime string `description:"修改时间"`
  403. Contents []*SpeechRecognitionContentItem `description:"语音识别内容"`
  404. Tags []*SpeechRecognitionDetailTag `description:"标签"`
  405. }
  406. func FormatSpeechRecognition2DetailItem(origin *SpeechRecognition, contents []*SpeechRecognitionContentItem, tags []*SpeechRecognitionDetailTag, menuPath []*SpeechRecognitionMenuItem) (item *SpeechRecognitionDetailItem) {
  407. if origin == nil {
  408. return
  409. }
  410. item = new(SpeechRecognitionDetailItem)
  411. item.SpeechRecognitionId = origin.SpeechRecognitionId
  412. item.UniqueCode = origin.UniqueCode
  413. item.FileName = origin.FileName
  414. item.ResourceUrl = origin.ResourceUrl
  415. if item.ResourceUrl != "" {
  416. pointArr := strings.Split(item.ResourceUrl, ".")
  417. if len(pointArr) > 0 {
  418. item.FileExt = pointArr[len(pointArr)-1]
  419. }
  420. }
  421. item.ResourceUrl = origin.ResourceUrl
  422. if origin.FileState == SpeechRecognitionFileRemoveFlag {
  423. item.ResourceUrl = ""
  424. }
  425. item.MenuId = origin.MenuId
  426. item.MenuPath = menuPath
  427. item.SysUserId = origin.SysUserId
  428. item.SysUserName = origin.SysUserName
  429. item.Abstract = origin.Abstract
  430. item.Sort = origin.Sort
  431. item.FileSecond = utils.SecondsToHHMMSS(origin.FileSecond)
  432. item.FileSize = fmt.Sprintf("%.2fM", utils.ByteToMB(origin.FileSize))
  433. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  434. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  435. item.Contents = contents
  436. item.Tags = tags
  437. return
  438. }
  439. // SpeechRecognitionDetailTag 语音识别详情标签
  440. type SpeechRecognitionDetailTag struct {
  441. TagId int `description:"标签ID" gorm:"tag_id"`
  442. TagName string `description:"标签名称" gorm:"tag_name"`
  443. }
  444. // GetSpeechRecognitionTagBySpeechId 获取语音识别标签
  445. func GetSpeechRecognitionTagBySpeechId(speechId int) (items []*SpeechRecognitionDetailTag, err error) {
  446. // o := orm.NewOrm()
  447. sql := `SELECT a.speech_recognition_tag_id AS tag_id, a.tag_name FROM speech_recognition_tag AS a
  448. JOIN speech_recognition_tag_mapping AS b ON a.speech_recognition_tag_id = b.tag_id
  449. WHERE b.speech_recognition_id = ?`
  450. //_, err = o.Raw(sql, speechId).QueryRows(&items)
  451. err = global.DEFAULT_DmSQL.Raw(sql, speechId).Find(&items).Error
  452. return
  453. }
  454. // SpeechRecognitionMappingTags 语音识别标签
  455. type SpeechRecognitionMappingTags struct {
  456. SpeechRecognitionId int `description:"语音识别ID" gorm:"speech_recognition_id"`
  457. TagId int `description:"标签ID" gorm:"tag_id"`
  458. TagName string `description:"标签名称" gorm:"tag_name"`
  459. }
  460. // GetSpeechRecognitionTagsBySpeechIds 根据语音识别IDs获取标签
  461. func GetSpeechRecognitionTagsBySpeechIds(speechIds []int) (items []*SpeechRecognitionMappingTags, err error) {
  462. if len(speechIds) == 0 {
  463. return
  464. }
  465. // o := orm.NewOrm()
  466. sql := fmt.Sprintf(`SELECT a.speech_recognition_id, a.tag_id, b.tag_name FROM speech_recognition_tag_mapping AS a
  467. JOIN speech_recognition_tag AS b ON a.tag_id = b.speech_recognition_tag_id
  468. WHERE a.speech_recognition_id IN (%s)`, utils.GetOrmInReplace(len(speechIds)))
  469. //_, err = o.Raw(sql, speechIds).QueryRows(&items)
  470. err = global.DEFAULT_DmSQL.Raw(sql, speechIds).Find(&items).Error
  471. return
  472. }
  473. // SpeechRecognitionListReq 语音识别列表请求体
  474. type SpeechRecognitionListReq struct {
  475. PageSize int `form:"PageSize"`
  476. CurrentIndex int `form:"CurrentIndex"`
  477. FileName string `form:"FileName" description:"文件名称"`
  478. StartTime string `form:"StartTime" description:"开始时间"`
  479. EndTime string `form:"EndTime" description:"结束时间"`
  480. CreateUserId string `form:"CreateUserId" description:"创建人IDs"`
  481. TagId int `form:"TagId" description:"标签ID"`
  482. TagIds string `form:"TagIds" description:"标签IDs, 英文逗号分隔"`
  483. MenuId int `form:"MenuId" description:"目录ID"`
  484. IsTagMenu bool `form:"IsTagMenu" description:"是否为标签目录"`
  485. }
  486. // SpeechRecognitionListResp 语音识别列表响应体
  487. type SpeechRecognitionListResp struct {
  488. List []*SpeechRecognitionDetailItem
  489. Paging *paging.PagingItem `description:"分页数据"`
  490. }
  491. // SpeechRecognitionContentExportReq 导出内容
  492. type SpeechRecognitionContentExportReq struct {
  493. SpeechRecognitionId int `description:"语音识别ID"`
  494. ExportType int `description:"导出类型:1-txt;2-doc;3-pdf"`
  495. Timestamp bool `description:"是否显示时间戳"`
  496. }
  497. // UpdateSortByMenuId 根据分类ID更新排序
  498. func (m *SpeechRecognition) UpdateSortByMenuId(menuId, nowSort int, prevSpeechId int, updateSort string) (err error) {
  499. // o := orm.NewOrm()
  500. sql := fmt.Sprintf(`UPDATE %s SET %s = %s WHERE %s = ? `, m.TableName(), SpeechRecognitionCols.Sort, updateSort, SpeechRecognitionCols.MenuId)
  501. if prevSpeechId > 0 {
  502. sql += fmt.Sprintf(` AND (%s > ? OR (%s > %d AND %s = %d))`, SpeechRecognitionCols.Sort, SpeechRecognitionCols.SpeechRecognitionId, prevSpeechId, SpeechRecognitionCols.Sort, nowSort)
  503. } else {
  504. sql += fmt.Sprintf(` AND %s > ?`, SpeechRecognitionCols.Sort)
  505. }
  506. //_, err = o.Raw(sql, menuId, nowSort).Exec()
  507. err = global.DEFAULT_DmSQL.Exec(sql, menuId, nowSort).Error
  508. return
  509. }
  510. // GetMaxSortByMenuId 获取分类下最大Sort
  511. func (m *SpeechRecognition) GetMaxSortByMenuId(menuId int) (sort int, err error) {
  512. // o := orm.NewOrm()
  513. sql := fmt.Sprintf(`SELECT MAX(sort) AS sort FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionCols.MenuId)
  514. //err = o.Raw(sql, menuId).QueryRow(&sort)
  515. err = global.DEFAULT_DmSQL.Raw(sql, menuId).Scan(&sort).Error
  516. return
  517. }
  518. // GetFirstByMenuId 获取目录下排序第一的数据
  519. func (m *SpeechRecognition) GetFirstByMenuId(menuId int) (item *SpeechRecognition, err error) {
  520. // o := orm.NewOrm()
  521. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? ORDER BY %s ASC, %s ASC LIMIT 1`, m.TableName(), SpeechRecognitionCols.MenuId, SpeechRecognitionCols.Sort, SpeechRecognitionCols.SpeechRecognitionId)
  522. //err = o.Raw(sql, menuId).QueryRow(&item)
  523. err = global.DEFAULT_DmSQL.Raw(sql, menuId).First(&item).Error
  524. return
  525. }
  526. // SpeechRecognitionConvertCheckNameReq 校验文件重名请求体
  527. type SpeechRecognitionConvertCheckNameReq struct {
  528. FileName string `description:"文件名称"`
  529. }
  530. // SpeechSave 更新内容、摘要及标签
  531. func (m *SpeechRecognition) SpeechSave(speechItem *SpeechRecognition, speechCols []string, contents []SpeechRecognitionSaveContentReq, tagMappings []*SpeechRecognitionTagMapping) (err error) {
  532. if speechItem == nil {
  533. err = fmt.Errorf("speech nil")
  534. return
  535. }
  536. //o := orm.NewOrm()
  537. //tx, e := o.Begin()
  538. //if e != nil {
  539. // err = fmt.Errorf("transaction begin err: %s", e.Error())
  540. // return
  541. //}
  542. //defer func() {
  543. // if err != nil {
  544. // _ = tx.Rollback()
  545. // return
  546. // }
  547. // _ = tx.Commit()
  548. //}()
  549. //
  550. //// 转写文件
  551. //if len(speechCols) > 0 {
  552. // _, e = tx.Update(speechItem, speechCols...)
  553. // if e != nil {
  554. // err = fmt.Errorf("update speech err: %s", e.Error())
  555. // return
  556. // }
  557. //}
  558. //
  559. //// 转写内容
  560. //if len(contents) > 0 {
  561. // sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = 1, %s = NOW() WHERE %s = ?`, "speech_recognition_content", SpeechRecognitionContentCols.Content, SpeechRecognitionContentCols.IsUpdate, SpeechRecognitionContentCols.ModifyTime, SpeechRecognitionContentCols.SpeechRecognitionContentId)
  562. // p, e := tx.Raw(sql).Prepare()
  563. // if e != nil {
  564. // err = fmt.Errorf("update prepare err: %s", e.Error())
  565. // return
  566. // }
  567. // defer func() {
  568. // _ = p.Close()
  569. // }()
  570. // for _, v := range contents {
  571. // _, e = p.Exec(v.Content, v.SpeechRecognitionContentId)
  572. // if e != nil {
  573. // err = fmt.Errorf("update exec err: %s", e.Error())
  574. // return
  575. // }
  576. // }
  577. //}
  578. //
  579. //// 标签
  580. //sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, "speech_recognition_tag_mapping", SpeechRecognitionTagMappingCols.SpeechRecognitionId)
  581. //_, e = tx.Raw(sql, speechItem.SpeechRecognitionId).Exec()
  582. //if e != nil {
  583. // err = fmt.Errorf("remove tag mappings err: %s", e.Error())
  584. // return
  585. //}
  586. //if len(tagMappings) > 0 {
  587. // _, e = tx.InsertMulti(len(tagMappings), tagMappings)
  588. // if e != nil {
  589. // err = fmt.Errorf("insert tag mappings err: %s", e.Error())
  590. // return
  591. // }
  592. //}
  593. tx := global.DEFAULT_DmSQL.Begin()
  594. defer func() {
  595. if err != nil {
  596. _ = tx.Rollback()
  597. return
  598. }
  599. _ = tx.Commit()
  600. }()
  601. // 转写文件
  602. if len(speechCols) > 0 {
  603. e := tx.Select(speechCols).Updates(speechItem).Error
  604. if e != nil {
  605. err = fmt.Errorf("update speech err: %s", e.Error())
  606. return
  607. }
  608. }
  609. // 转写内容
  610. if len(contents) > 0 {
  611. sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = 1, %s = NOW() WHERE %s = ?`, "speech_recognition_content", SpeechRecognitionContentCols.Content, SpeechRecognitionContentCols.IsUpdate, SpeechRecognitionContentCols.ModifyTime, SpeechRecognitionContentCols.SpeechRecognitionContentId)
  612. for _, v := range contents {
  613. e := tx.Exec(sql, v.Content, v.SpeechRecognitionContentId).Error
  614. if e != nil {
  615. err = fmt.Errorf("update exec err: %s", e.Error())
  616. return
  617. }
  618. }
  619. }
  620. // 标签
  621. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, "speech_recognition_tag_mapping", SpeechRecognitionTagMappingCols.SpeechRecognitionId)
  622. e := tx.Exec(sql, speechItem.SpeechRecognitionId).Error
  623. if e != nil {
  624. err = fmt.Errorf("remove tag mappings err: %s", e.Error())
  625. return
  626. }
  627. if len(tagMappings) > 0 {
  628. e = tx.CreateInBatches(tagMappings, utils.MultiAddNum).Error
  629. if e != nil {
  630. err = fmt.Errorf("insert tag mappings err: %s", e.Error())
  631. return
  632. }
  633. }
  634. return
  635. }