speech_recognition_content.go 9.6 KB

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