speech_recognition.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. package speech_recognition
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  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. FileName string `description:"文件名称"`
  20. ResourceUrl string `description:"文件路径"`
  21. MenuId int `description:"目录ID"`
  22. MenuPath string `description:"所属目录位置,例:/一级目录ID/二级目录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. var SpeechRecognitionCols = struct {
  36. SpeechRecognitionId 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. FileName: "file_name",
  55. ResourceUrl: "resource_url",
  56. MenuId: "menu_id",
  57. MenuPath: "menu_path",
  58. SysUserId: "sys_user_id",
  59. SysUserName: "sys_user_name",
  60. State: "state",
  61. Abstract: "abstract",
  62. Sort: "sort",
  63. FileState: "file_state",
  64. FileSecond: "file_second",
  65. FileSize: "file_size",
  66. ConvertRemark: "convert_remark",
  67. CreateTime: "create_time",
  68. ModifyTime: "modify_time",
  69. }
  70. func (m *SpeechRecognition) TableName() string {
  71. return "speech_recognition"
  72. }
  73. func (m *SpeechRecognition) PrimaryId() string {
  74. return SpeechRecognitionCols.SpeechRecognitionId
  75. }
  76. func (m *SpeechRecognition) Create() (err error) {
  77. o := orm.NewOrm()
  78. id, err := o.Insert(m)
  79. if err != nil {
  80. return
  81. }
  82. m.SpeechRecognitionId = int(id)
  83. return
  84. }
  85. func (m *SpeechRecognition) CreateMulti(items []*SpeechRecognition) (err error) {
  86. if len(items) == 0 {
  87. return
  88. }
  89. o := orm.NewOrm()
  90. _, err = o.InsertMulti(len(items), items)
  91. return
  92. }
  93. func (m *SpeechRecognition) Update(cols []string) (err error) {
  94. o := orm.NewOrm()
  95. _, err = o.Update(m, cols...)
  96. return
  97. }
  98. func (m *SpeechRecognition) Del() (err error) {
  99. o := orm.NewOrm()
  100. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  101. _, err = o.Raw(sql, m.SpeechRecognitionId).Exec()
  102. return
  103. }
  104. func (m *SpeechRecognition) MultiDel(menuIds []int) (err error) {
  105. if len(menuIds) == 0 {
  106. return
  107. }
  108. o := orm.NewOrm()
  109. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  110. _, err = o.Raw(sql, menuIds).Exec()
  111. return
  112. }
  113. func (m *SpeechRecognition) GetItemById(id int) (item *SpeechRecognition, err error) {
  114. o := orm.NewOrm()
  115. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  116. err = o.Raw(sql, id).QueryRow(&item)
  117. return
  118. }
  119. func (m *SpeechRecognition) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognition, err error) {
  120. o := orm.NewOrm()
  121. order := ``
  122. if orderRule != "" {
  123. order = ` ORDER BY ` + orderRule
  124. }
  125. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  126. err = o.Raw(sql, pars).QueryRow(&item)
  127. return
  128. }
  129. func (m *SpeechRecognition) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  130. o := orm.NewOrm()
  131. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  132. err = o.Raw(sql, pars).QueryRow(&count)
  133. return
  134. }
  135. func (m *SpeechRecognition) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognition, err error) {
  136. o := orm.NewOrm()
  137. fields := strings.Join(fieldArr, ",")
  138. if len(fieldArr) == 0 {
  139. fields = `*`
  140. }
  141. order := `ORDER BY create_time DESC`
  142. if orderRule != "" {
  143. order = ` ORDER BY ` + orderRule
  144. }
  145. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  146. _, err = o.Raw(sql, pars).QueryRows(&items)
  147. return
  148. }
  149. func (m *SpeechRecognition) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognition, err error) {
  150. o := orm.NewOrm()
  151. fields := strings.Join(fieldArr, ",")
  152. if len(fieldArr) == 0 {
  153. fields = `*`
  154. }
  155. order := `ORDER BY create_time DESC`
  156. if orderRule != "" {
  157. order = ` ORDER BY ` + orderRule
  158. }
  159. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  160. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  161. return
  162. }
  163. // SpeechRecognitionItem 语音识别信息
  164. type SpeechRecognitionItem struct {
  165. SpeechRecognitionId int
  166. FileName string `description:"文件名称"`
  167. ResourceUrl string `description:"文件路径"`
  168. MenuId int `description:"目录ID"`
  169. MenuPath string `description:"所属目录位置,例:/一级目录ID/二级目录ID"`
  170. SysUserId int `description:"创建人ID"`
  171. SysUserName string `description:"创建人姓名"`
  172. State int `description:"状态:1-待转换;2-转换完成;3-转换失败"`
  173. Abstract string `description:"摘要,取前几段内容"`
  174. Sort int `description:"目录下的排序"`
  175. ConvertRemark string `description:"转写备注-失败原因"`
  176. CreateTime string `description:"创建时间"`
  177. ModifyTime string `description:"修改时间"`
  178. }
  179. func FormatSpeechRecognition2Item(origin *SpeechRecognition) (item *SpeechRecognitionItem) {
  180. if origin == nil {
  181. return
  182. }
  183. item = new(SpeechRecognitionItem)
  184. item.SpeechRecognitionId = origin.SpeechRecognitionId
  185. item.FileName = origin.FileName
  186. item.ResourceUrl = origin.ResourceUrl
  187. if origin.FileState == SpeechRecognitionFileRemoveFlag {
  188. item.ResourceUrl = ""
  189. }
  190. item.MenuId = origin.MenuId
  191. item.MenuPath = origin.MenuPath
  192. item.SysUserId = origin.SysUserId
  193. item.SysUserName = origin.SysUserName
  194. item.State = origin.State
  195. item.Abstract = origin.Abstract
  196. item.Sort = origin.Sort
  197. item.ConvertRemark = origin.ConvertRemark
  198. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  199. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  200. return
  201. }
  202. // SpeechRecognitionSaveReq 保存请求体
  203. type SpeechRecognitionSaveReq struct {
  204. SpeechRecognitionId int `description:"语音识别ID"`
  205. Contents []SpeechRecognitionSaveContentReq `description:"保存内容"`
  206. }
  207. // SpeechRecognitionSaveContentReq 保存内容
  208. type SpeechRecognitionSaveContentReq struct {
  209. SpeechRecognitionContentId int `description:"语音识别内容ID"`
  210. Content string `description:"段落内容"`
  211. }
  212. // SpeechRecognitionRenameReq 重命名
  213. type SpeechRecognitionRenameReq struct {
  214. SpeechRecognitionId int `description:"语音识别ID"`
  215. FileName string `description:"文件名称"`
  216. }
  217. // SpeechRecognitionRemoveReq 删除
  218. type SpeechRecognitionRemoveReq struct {
  219. SpeechRecognitionId int `description:"语音识别ID"`
  220. }
  221. // SpeechRecognitionRemoveFileReq 删除文件
  222. type SpeechRecognitionRemoveFileReq struct {
  223. SpeechRecognitionId int `description:"语音识别ID"`
  224. }
  225. // SpeechRecognitionSaveTagReq 保存标签
  226. type SpeechRecognitionSaveTagReq struct {
  227. SpeechRecognitionId int `description:"语音识别ID"`
  228. TagIds []int `description:"标签IDs"`
  229. }
  230. // SpeechRecognitionConvertReq 批量转写
  231. type SpeechRecognitionConvertReq struct {
  232. MenuId int `description:"目录ID"`
  233. Files []SpeechRecognitionConvertFiles `description:"转写文件"`
  234. }
  235. // SpeechRecognitionConvertFiles 批量转写文件
  236. type SpeechRecognitionConvertFiles struct {
  237. FileName string `description:"文件名称"`
  238. ResourceUrl string `description:"文件地址"`
  239. }
  240. // UpdateSpeechAndApiLog 更新语音识别及API记录
  241. func UpdateSpeechAndApiLog(speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
  242. if speechItem == nil || apiLogItem == nil {
  243. err = fmt.Errorf("speechItem nil or apiLogItem nil")
  244. return
  245. }
  246. o := orm.NewOrm()
  247. tx, err := o.Begin()
  248. if err != nil {
  249. return
  250. }
  251. defer func() {
  252. if err != nil {
  253. _ = tx.Rollback()
  254. } else {
  255. _ = tx.Commit()
  256. }
  257. }()
  258. _, e := tx.Update(speechItem, speechCols...)
  259. if e != nil {
  260. err = fmt.Errorf("update speech err: %s", e.Error())
  261. return
  262. }
  263. _, e = tx.Update(apiLogItem, logCols...)
  264. if e != nil {
  265. err = fmt.Errorf("update api log err: %s", e.Error())
  266. return
  267. }
  268. return
  269. }
  270. // CreateContentAndUpdateSpeechAndApiLog 新增语音识别内容并更新语音识别及API记录
  271. func CreateContentAndUpdateSpeechAndApiLog(contents []*SpeechRecognitionContent, speechItem *SpeechRecognition, speechCols []string, apiLogItem *SpeechRecognitionApiLog, logCols []string) (err error) {
  272. if speechItem == nil || apiLogItem == nil {
  273. err = fmt.Errorf("speechItem nil or apiLogItem nil")
  274. return
  275. }
  276. o := orm.NewOrm()
  277. tx, err := o.Begin()
  278. if err != nil {
  279. return
  280. }
  281. defer func() {
  282. if err != nil {
  283. _ = tx.Rollback()
  284. } else {
  285. _ = tx.Commit()
  286. }
  287. }()
  288. _, e := tx.Update(speechItem, speechCols...)
  289. if e != nil {
  290. err = fmt.Errorf("update speech err: %s", e.Error())
  291. return
  292. }
  293. _, e = tx.Update(apiLogItem, logCols...)
  294. if e != nil {
  295. err = fmt.Errorf("update api log err: %s", e.Error())
  296. return
  297. }
  298. if len(contents) > 0 {
  299. _, e = tx.InsertMulti(len(contents), contents)
  300. if e != nil {
  301. err = fmt.Errorf("insert multi contents err: %s", e.Error())
  302. return
  303. }
  304. }
  305. return
  306. }
  307. // SpeechRecognitionDetailItem 语音识别详情信息
  308. type SpeechRecognitionDetailItem struct {
  309. SpeechRecognitionId int
  310. FileName string `description:"文件名称"`
  311. ResourceUrl string `description:"文件路径"`
  312. MenuId int `description:"目录ID"`
  313. SysUserId int `description:"创建人ID"`
  314. SysUserName string `description:"创建人姓名"`
  315. Abstract string `description:"摘要,取前几段内容"`
  316. Sort int `description:"目录下的排序"`
  317. FileSecond int `description:"文件时长(秒)"`
  318. FileSize int `description:"文件大小(byte)"`
  319. CreateTime string `description:"创建时间"`
  320. ModifyTime string `description:"修改时间"`
  321. Contents []*SpeechRecognitionContentItem `description:"语音识别内容"`
  322. Tags []*SpeechRecognitionDetailTag `description:"标签"`
  323. }
  324. func FormatSpeechRecognition2DetailItem(origin *SpeechRecognition, contents []*SpeechRecognitionContentItem, tags []*SpeechRecognitionDetailTag) (item *SpeechRecognitionDetailItem) {
  325. if origin == nil {
  326. return
  327. }
  328. item = new(SpeechRecognitionDetailItem)
  329. item.SpeechRecognitionId = origin.SpeechRecognitionId
  330. item.FileName = origin.FileName
  331. item.ResourceUrl = origin.ResourceUrl
  332. if origin.FileState == SpeechRecognitionFileRemoveFlag {
  333. item.ResourceUrl = ""
  334. }
  335. item.MenuId = origin.MenuId
  336. item.SysUserId = origin.SysUserId
  337. item.SysUserName = origin.SysUserName
  338. item.Abstract = origin.Abstract
  339. item.Sort = origin.Sort
  340. item.FileSecond = origin.FileSecond
  341. item.FileSize = origin.FileSize
  342. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  343. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  344. item.Contents = contents
  345. item.Tags = tags
  346. return
  347. }
  348. // SpeechRecognitionDetailTag 语音识别详情标签
  349. type SpeechRecognitionDetailTag struct {
  350. TagId int `description:"标签ID"`
  351. TagName string `description:"标签名称"`
  352. }
  353. // GetSpeechRecognitionTagBySpeechId 获取语音识别标签
  354. func GetSpeechRecognitionTagBySpeechId(speechId int) (items []*SpeechRecognitionDetailTag, err error) {
  355. o := orm.NewOrm()
  356. sql := `SELECT a.speech_recognition_tag_id AS tag_id, a.tag_name FROM speech_recognition_tag AS a
  357. JOIN speech_recognition_tag_mapping AS b ON a.speech_recognition_tag_id = b.tag_id
  358. WHERE b.speech_recognition_id = ?`
  359. _, err = o.Raw(sql, speechId).QueryRows(&items)
  360. return
  361. }
  362. // SpeechRecognitionMappingTags 语音识别标签
  363. type SpeechRecognitionMappingTags struct {
  364. SpeechRecognitionId int `description:"语音识别ID"`
  365. TagId int `description:"标签ID"`
  366. TagName string `description:"标签名称"`
  367. }
  368. // GetSpeechRecognitionTagsBySpeechIds 根据语音识别IDs获取标签
  369. func GetSpeechRecognitionTagsBySpeechIds(speechIds []int) (items []*SpeechRecognitionMappingTags, err error) {
  370. if len(speechIds) == 0 {
  371. return
  372. }
  373. o := orm.NewOrm()
  374. sql := fmt.Sprintf(`SELECT a.speech_recognition_id, a.tag_id, b.tag_name FROM speech_recognition_tag_mapping AS a
  375. JOIN speech_recognition_tag AS b ON a.tag_id = b.speech_recognition_tag_id
  376. WHERE a.speech_recognition_id IN (%s)`, utils.GetOrmInReplace(len(speechIds)))
  377. _, err = o.Raw(sql, speechIds).QueryRows(&items)
  378. return
  379. }
  380. // SpeechRecognitionListReq 语音识别列表请求体
  381. type SpeechRecognitionListReq struct {
  382. PageSize int `form:"PageSize"`
  383. CurrentIndex int `form:"CurrentIndex"`
  384. FileName string `form:"FileName" description:"文件名称"`
  385. StartTime string `form:"StartTime" description:"开始时间"`
  386. EndTime string `form:"EndTime" description:"结束时间"`
  387. CreateUserId int `form:"CreateUserId" description:"创建人ID"`
  388. TagId int `form:"TagId" description:"标签ID"`
  389. }
  390. // SpeechRecognitionListResp 语音识别列表响应体
  391. type SpeechRecognitionListResp struct {
  392. List []*SpeechRecognitionDetailItem
  393. Paging *paging.PagingItem `description:"分页数据"`
  394. }