speech_recognition_content.go 7.3 KB

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