speech_recognition_menu.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. const (
  10. SpeechRecognitionMenuNodeTypeDefault = 0
  11. SpeechRecognitionMenuNodeTypeSpeech = 1
  12. )
  13. // SpeechRecognitionMenu 语音识别-目录表
  14. type SpeechRecognitionMenu struct {
  15. SpeechRecognitionMenuId int `orm:"column(speech_recognition_menu_id);pk"`
  16. UniqueCode string `description:"唯一编码"`
  17. MenuName string `description:"目录名称"`
  18. ParentId int `description:"父级ID"`
  19. Level int `description:"目录层级"`
  20. Sort int `description:"排序"`
  21. RootId int `description:"顶级ID"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. var SpeechRecognitionMenuCols = struct {
  26. SpeechRecognitionMenuId string
  27. UniqueCode string
  28. MenuName string
  29. ParentId string
  30. Level string
  31. Sort string
  32. RootId string
  33. CreateTime string
  34. ModifyTime string
  35. }{
  36. SpeechRecognitionMenuId: "speech_recognition_menu_id",
  37. UniqueCode: "unique_code",
  38. MenuName: "menu_name",
  39. ParentId: "parent_id",
  40. Level: "level",
  41. Sort: "sort",
  42. RootId: "root_id",
  43. CreateTime: "create_time",
  44. ModifyTime: "modify_time",
  45. }
  46. func (m *SpeechRecognitionMenu) TableName() string {
  47. return "speech_recognition_menu"
  48. }
  49. func (m *SpeechRecognitionMenu) PrimaryId() string {
  50. return SpeechRecognitionMenuCols.SpeechRecognitionMenuId
  51. }
  52. func (m *SpeechRecognitionMenu) Create() (err error) {
  53. o := orm.NewOrm()
  54. id, err := o.Insert(m)
  55. if err != nil {
  56. return
  57. }
  58. m.SpeechRecognitionMenuId = int(id)
  59. return
  60. }
  61. func (m *SpeechRecognitionMenu) CreateMulti(items []*SpeechRecognitionMenu) (err error) {
  62. if len(items) == 0 {
  63. return
  64. }
  65. o := orm.NewOrm()
  66. _, err = o.InsertMulti(len(items), items)
  67. return
  68. }
  69. func (m *SpeechRecognitionMenu) Update(cols []string) (err error) {
  70. o := orm.NewOrm()
  71. _, err = o.Update(m, cols...)
  72. return
  73. }
  74. func (m *SpeechRecognitionMenu) Del() (err error) {
  75. o := orm.NewOrm()
  76. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  77. _, err = o.Raw(sql, m.SpeechRecognitionMenuId).Exec()
  78. return
  79. }
  80. func (m *SpeechRecognitionMenu) MultiDel(menuIds []int) (err error) {
  81. if len(menuIds) == 0 {
  82. return
  83. }
  84. o := orm.NewOrm()
  85. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  86. _, err = o.Raw(sql, menuIds).Exec()
  87. return
  88. }
  89. func (m *SpeechRecognitionMenu) GetItemById(id int) (item *SpeechRecognitionMenu, err error) {
  90. o := orm.NewOrm()
  91. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  92. err = o.Raw(sql, id).QueryRow(&item)
  93. return
  94. }
  95. func (m *SpeechRecognitionMenu) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SpeechRecognitionMenu, err error) {
  96. o := orm.NewOrm()
  97. order := ``
  98. if orderRule != "" {
  99. order = ` ORDER BY ` + orderRule
  100. }
  101. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  102. err = o.Raw(sql, pars).QueryRow(&item)
  103. return
  104. }
  105. func (m *SpeechRecognitionMenu) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  106. o := orm.NewOrm()
  107. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  108. err = o.Raw(sql, pars).QueryRow(&count)
  109. return
  110. }
  111. func (m *SpeechRecognitionMenu) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SpeechRecognitionMenu, err error) {
  112. o := orm.NewOrm()
  113. fields := strings.Join(fieldArr, ",")
  114. if len(fieldArr) == 0 {
  115. fields = `*`
  116. }
  117. order := `ORDER BY create_time DESC`
  118. if orderRule != "" {
  119. order = ` ORDER BY ` + orderRule
  120. }
  121. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  122. _, err = o.Raw(sql, pars).QueryRows(&items)
  123. return
  124. }
  125. func (m *SpeechRecognitionMenu) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SpeechRecognitionMenu, err error) {
  126. o := orm.NewOrm()
  127. fields := strings.Join(fieldArr, ",")
  128. if len(fieldArr) == 0 {
  129. fields = `*`
  130. }
  131. order := `ORDER BY create_time DESC`
  132. if orderRule != "" {
  133. order = ` ORDER BY ` + orderRule
  134. }
  135. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  136. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  137. return
  138. }
  139. // SpeechRecognitionMenuItem 目录
  140. type SpeechRecognitionMenuItem struct {
  141. UniqueCode string `description:"唯一编码"`
  142. MenuId int `description:"目录ID"`
  143. MenuName string `description:"目录名称"`
  144. ParentId int `description:"父级ID"`
  145. Level int `description:"目录层级"`
  146. Sort int `description:"排序"`
  147. CreateTime string `description:"创建时间"`
  148. Children []*SpeechRecognitionMenuItem `description:"子目录"`
  149. }
  150. // SpeechRecognitionMenuNodeItem 语音识别目录节点
  151. type SpeechRecognitionMenuNodeItem struct {
  152. UniqueCode string `description:"唯一编码"`
  153. NodeType int `description:"节点类型:0-目录;1-语音识别"`
  154. MenuId int `description:"目录ID"`
  155. MenuName string `description:"目录名称"`
  156. SpeechRecognitionId int `description:"语音识别ID"`
  157. SpeechRecognitionName string `description:"语音识别名称"`
  158. ParentId int `description:"父级ID"`
  159. Level int `description:"目录层级"`
  160. Sort int `description:"排序"`
  161. CreateTime string `description:"创建时间"`
  162. Children []*SpeechRecognitionMenuNodeItem `description:"子节点"`
  163. }
  164. // SpeechRecognitionMenuAddReq 新增语音识别目录
  165. type SpeechRecognitionMenuAddReq struct {
  166. ParentId int `description:"父级ID"`
  167. MenuName string `description:"目录名称"`
  168. }
  169. // SpeechRecognitionMenuEditReq 编辑语音识别目录
  170. type SpeechRecognitionMenuEditReq struct {
  171. MenuId int `description:"目录ID"`
  172. MenuName string `description:"目录名称"`
  173. }
  174. // SpeechRecognitionMenuRemoveReq 删除语音识别目录
  175. type SpeechRecognitionMenuRemoveReq struct {
  176. MenuId int `description:"目录ID"`
  177. }
  178. // SpeechRecognitionMenuRemoveCheckResp 删除语音识别目录-响应体
  179. type SpeechRecognitionMenuRemoveCheckResp struct {
  180. CheckResult int `description:"校验结果:0-可删除;1-关联内容;2-关联空目录"`
  181. Tips string `description:"提示信息"`
  182. }
  183. // SpeechRecognitionMenuMoveReq 移动目录请求体
  184. type SpeechRecognitionMenuMoveReq struct {
  185. MenuId int `description:"目录ID"`
  186. ParentMenuId int `description:"父级目录ID"`
  187. PrevMenuId int `description:"上一个兄弟节点目录ID"`
  188. NextMenuId int `description:"下一个兄弟节点目录ID"`
  189. SpeechId int `description:"语音识别ID, 大于0则表示移动语音识别"`
  190. PrevSpeechId int `description:"上一个语音识别ID"`
  191. NextSpeechId int `description:"下一个语音识别ID"`
  192. }
  193. // UpdateSortByParentId 根据父级ID更新排序
  194. func (m *SpeechRecognitionMenu) UpdateSortByParentId(parentId, menuId, nowSort int, updateSort string) (err error) {
  195. o := orm.NewOrm()
  196. sql := fmt.Sprintf(`UPDATE %s SET %s = %s WHERE %s = ? AND %s > ?`, m.TableName(), SpeechRecognitionMenuCols.Sort, updateSort, SpeechRecognitionMenuCols.ParentId, SpeechRecognitionMenuCols.Sort)
  197. if menuId > 0 {
  198. sql += fmt.Sprintf(` OR (%s > %d AND %s = %d)`, SpeechRecognitionMenuCols.SpeechRecognitionMenuId, menuId, SpeechRecognitionMenuCols.Sort, nowSort)
  199. }
  200. _, err = o.Raw(sql, parentId, nowSort).Exec()
  201. return
  202. }
  203. // GetMaxSortByParentId 获取父级分类下最大Sort
  204. func (m *SpeechRecognitionMenu) GetMaxSortByParentId(parentId int) (sort int, err error) {
  205. o := orm.NewOrm()
  206. sql := fmt.Sprintf(`SELECT MAX(sort) AS sort FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionMenuCols.ParentId)
  207. err = o.Raw(sql, parentId).QueryRow(&sort)
  208. return
  209. }
  210. // GetFirstByParentId 获取父级目录下排序第一的目录
  211. func (m *SpeechRecognitionMenu) GetFirstByParentId(parentId int) (item *SpeechRecognitionMenu, err error) {
  212. o := orm.NewOrm()
  213. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? ORDER BY %s ASC, %s ASC LIMIT 1`, m.TableName(), SpeechRecognitionMenuCols.ParentId, SpeechRecognitionMenuCols.Sort, SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
  214. err = o.Raw(sql, parentId).QueryRow(&item)
  215. return
  216. }
  217. // UpdateChildByParentMenuId 通过父级目录ID更新子目录
  218. func (m *SpeechRecognitionMenu) UpdateChildByParentMenuId(menuIds []int, rootId int, levelStep int) (err error) {
  219. if len(menuIds) == 0 {
  220. return
  221. }
  222. o := orm.NewOrm()
  223. var pars []interface{}
  224. pars = append(pars, rootId, levelStep)
  225. pars = append(pars, menuIds)
  226. sql := fmt.Sprintf(`UPDATE %s SET %s = ?, %s = %s + ? WHERE %s IN (%s)`, m.TableName(), SpeechRecognitionMenuCols.RootId, SpeechRecognitionMenuCols.Level, SpeechRecognitionMenuCols.Level, SpeechRecognitionMenuCols.SpeechRecognitionMenuId, utils.GetOrmInReplace(len(menuIds)))
  227. _, err = o.Raw(sql, pars).Exec()
  228. return
  229. }