speech_recognition_menu.go 9.6 KB

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