speech_recognition_content.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package speech_recognition
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. type SpeechRecognitionContent struct {
  10. SpeechRecognitionContentId int `gorm:"primaryKey;column:speech_recognition_content_id;type:int(10) unsigned;not null"`
  11. SpeechRecognitionId int `gorm:"index:idx_speech_id;column:speech_recognition_id;type:int(10) unsigned;not null;default:0"` // 语音识别Id
  12. Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 段落排序
  13. Content string `gorm:"column:content;type:text"` // 段落内容
  14. StartMs int `gorm:"column:start_ms;type:int(10) unsigned;not null;default:0"` // 单句开始时间(毫秒)
  15. EndMs int `gorm:"column:end_ms;type:int(10) unsigned;not null;default:0"` // 单句结束时间(毫秒)
  16. IsUpdate int `gorm:"column:is_update;type:tinyint(4) unsigned;not null;default:0"` // 是否手动修改过:0-否;1-是
  17. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  18. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  19. }
  20. var SpeechRecognitionContentCols = struct {
  21. SpeechRecognitionContentId string
  22. SpeechRecognitionId string
  23. Sort string
  24. Content string
  25. StartMs string
  26. EndMs string
  27. IsUpdate string
  28. CreateTime string
  29. ModifyTime string
  30. }{
  31. SpeechRecognitionContentId: "speech_recognition_content_id",
  32. SpeechRecognitionId: "speech_recognition_id",
  33. Sort: "sort",
  34. Content: "content",
  35. StartMs: "start_ms",
  36. EndMs: "end_ms",
  37. IsUpdate: "is_update",
  38. CreateTime: "create_time",
  39. ModifyTime: "modify_time",
  40. }
  41. func (m *SpeechRecognitionContent) TableName() string {
  42. return "speech_recognition_content"
  43. }
  44. func (m *SpeechRecognitionContent) PrimaryId() string {
  45. return SpeechRecognitionContentCols.SpeechRecognitionContentId
  46. }
  47. func (m *SpeechRecognitionContent) Create() (err error) {
  48. err = global.DEFAULT_DmSQL.Create(m).Error
  49. return
  50. }
  51. func (m *SpeechRecognitionContent) CreateMulti(items []*SpeechRecognitionContent) (err error) {
  52. if len(items) == 0 {
  53. return
  54. }
  55. err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
  56. return
  57. }
  58. func (m *SpeechRecognitionContent) Update(cols []string) (err error) {
  59. err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
  60. return
  61. }
  62. func (m *SpeechRecognitionContent) Del() (err error) {
  63. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  64. err = global.DEFAULT_DmSQL.Exec(sql, m.SpeechRecognitionContentId).Error
  65. return
  66. }
  67. func (m *SpeechRecognitionContent) MultiDel(menuIds []int) (err error) {
  68. if len(menuIds) == 0 {
  69. return
  70. }
  71. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  72. err = global.DEFAULT_DmSQL.Exec(sql, menuIds).Error
  73. return
  74. }
  75. func (m *SpeechRecognitionContent) GetItemById(id int) (item *SpeechRecognitionContent, err error) {
  76. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  77. err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error
  78. return
  79. }
  80. func (m *SpeechRecognitionContent) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognitionContent, err error) {
  81. order := ``
  82. if orderRule != "" {
  83. order = ` ORDER BY ` + orderRule
  84. }
  85. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  86. err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error
  87. return
  88. }
  89. func (m *SpeechRecognitionContent) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  90. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  91. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  92. return
  93. }
  94. func (m *SpeechRecognitionContent) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognitionContent, err error) {
  95. fields := strings.Join(fieldArr, ",")
  96. if len(fieldArr) == 0 {
  97. fields = `*`
  98. }
  99. order := `ORDER BY create_time DESC`
  100. if orderRule != "" {
  101. order = ` ORDER BY ` + orderRule
  102. }
  103. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  104. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  105. return
  106. }
  107. func (m *SpeechRecognitionContent) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognitionContent, err error) {
  108. fields := strings.Join(fieldArr, ",")
  109. if len(fieldArr) == 0 {
  110. fields = `*`
  111. }
  112. order := `ORDER BY create_time DESC`
  113. if orderRule != "" {
  114. order = ` ORDER BY ` + orderRule
  115. }
  116. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  117. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  118. return
  119. }
  120. type SpeechRecognitionContentItem struct {
  121. SpeechRecognitionContentId int
  122. SpeechRecognitionId int `description:"语音识别ID"`
  123. Sort int `description:"段落排序"`
  124. Content string `description:"段落内容"`
  125. StartMs int `description:"单句开始时间(毫秒)"`
  126. EndMs int `description:"单句结束时间(毫秒)"`
  127. StartTime string `description:"开始时间, 格式HH:MM:SS"`
  128. CreateTime string `description:"创建时间"`
  129. ModifyTime string `description:"修改时间"`
  130. HideTimestamp bool `description:"前端用的, 默认false就行"`
  131. IsHover bool `description:"前端用的, 默认false就行"`
  132. }
  133. func FormatSpeechRecognitionContent2Item(origin *SpeechRecognitionContent) (item *SpeechRecognitionContentItem) {
  134. if origin == nil {
  135. return
  136. }
  137. item = new(SpeechRecognitionContentItem)
  138. item.SpeechRecognitionContentId = origin.SpeechRecognitionContentId
  139. item.SpeechRecognitionId = origin.SpeechRecognitionId
  140. item.Sort = origin.Sort
  141. item.Content = origin.Content
  142. item.StartMs = origin.StartMs
  143. item.EndMs = origin.EndMs
  144. item.StartTime = utils.MillisecondsToHHMMSS(origin.StartMs)
  145. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  146. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  147. return
  148. }
  149. // BatchUpdateContents 批量更新语音识别内容
  150. func (m *SpeechRecognitionContent) BatchUpdateContents(contents []SpeechRecognitionSaveContentReq) (err error) {
  151. if len(contents) == 0 {
  152. return
  153. }
  154. sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = 1, %s = NOW() WHERE %s = ?`, m.TableName(), SpeechRecognitionContentCols.Content, SpeechRecognitionContentCols.IsUpdate, SpeechRecognitionContentCols.ModifyTime, SpeechRecognitionContentCols.SpeechRecognitionContentId)
  155. for _, v := range contents {
  156. err = global.DEFAULT_DmSQL.Exec(sql, v.Content, v.SpeechRecognitionContentId).Error
  157. if err != nil {
  158. return
  159. }
  160. }
  161. return
  162. }
  163. // ClearContentBySpeechId 清除转写文件内容
  164. func (m *SpeechRecognitionContent) ClearContentBySpeechId(speechId int) (err error) {
  165. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionContentCols.SpeechRecognitionId)
  166. err = global.DEFAULT_DmSQL.Exec(sql, speechId).Error
  167. return
  168. }