speech_recognition_menu.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // SpeechRecognitionMenuItem 目录
  125. type SpeechRecognitionMenuItem struct {
  126. UniqueCode string `description:"唯一编码"`
  127. MenuId int `description:"目录ID"`
  128. MenuName string `description:"目录名称"`
  129. ParentId int `description:"父级ID"`
  130. Level int `description:"目录层级"`
  131. Sort int `description:"排序"`
  132. CreateTime string `description:"创建时间"`
  133. Children []*SpeechRecognitionMenuItem `gorm:"-" description:"子目录"`
  134. }
  135. // SpeechRecognitionMenuNodeItem 语音识别目录节点
  136. type SpeechRecognitionMenuNodeItem struct {
  137. UniqueCode string `description:"唯一编码"`
  138. NodeType int `description:"节点类型:0-目录;1-语音识别"`
  139. MenuId int `description:"目录ID"`
  140. MenuName string `description:"目录名称"`
  141. SpeechRecognitionId int `description:"语音识别ID"`
  142. SpeechRecognitionName string `description:"语音识别名称"`
  143. ParentId int `description:"父级ID"`
  144. Level int `description:"目录层级"`
  145. Sort int `description:"排序"`
  146. CreateTime string `description:"创建时间"`
  147. Children []*SpeechRecognitionMenuNodeItem `gorm:"-" description:"子节点"`
  148. }
  149. // SpeechRecognitionMenuAddReq 新增语音识别目录
  150. type SpeechRecognitionMenuAddReq struct {
  151. ParentId int `description:"父级ID"`
  152. MenuName string `description:"目录名称"`
  153. }
  154. // SpeechRecognitionMenuEditReq 编辑语音识别目录
  155. type SpeechRecognitionMenuEditReq struct {
  156. MenuId int `description:"目录ID"`
  157. MenuName string `description:"目录名称"`
  158. }
  159. // SpeechRecognitionMenuRemoveReq 删除语音识别目录
  160. type SpeechRecognitionMenuRemoveReq struct {
  161. MenuId int `description:"目录ID"`
  162. }
  163. // SpeechRecognitionMenuRemoveCheckResp 删除语音识别目录-响应体
  164. type SpeechRecognitionMenuRemoveCheckResp struct {
  165. CheckResult int `description:"校验结果:0-可删除;1-关联内容;2-关联空目录"`
  166. Tips string `description:"提示信息"`
  167. }
  168. // SpeechRecognitionMenuMoveReq 移动目录请求体
  169. type SpeechRecognitionMenuMoveReq struct {
  170. MenuId int `description:"目录ID"`
  171. ParentMenuId int `description:"父级目录ID"`
  172. PrevMenuId int `description:"上一个兄弟节点目录ID"`
  173. NextMenuId int `description:"下一个兄弟节点目录ID"`
  174. SpeechId int `description:"语音识别ID, 大于0则表示移动语音识别"`
  175. PrevSpeechId int `description:"上一个语音识别ID"`
  176. NextSpeechId int `description:"下一个语音识别ID"`
  177. }
  178. // UpdateSortByParentId 根据父级ID更新排序
  179. func (m *SpeechRecognitionMenu) UpdateSortByParentId(parentId, menuId, nowSort int, updateSort string) (err error) {
  180. sql := fmt.Sprintf(`UPDATE %s SET %s = %s WHERE %s = ? AND %s > ?`, m.TableName(), SpeechRecognitionMenuCols.Sort, updateSort, SpeechRecognitionMenuCols.ParentId, SpeechRecognitionMenuCols.Sort)
  181. if menuId > 0 {
  182. sql += fmt.Sprintf(` OR (%s > %d AND %s = %d)`, SpeechRecognitionMenuCols.SpeechRecognitionMenuId, menuId, SpeechRecognitionMenuCols.Sort, nowSort)
  183. }
  184. err = global.DEFAULT_DmSQL.Exec(sql, parentId, nowSort).Error
  185. return
  186. }
  187. // GetMaxSortByParentId 获取父级分类下最大Sort
  188. func (m *SpeechRecognitionMenu) GetMaxSortByParentId(parentId int) (sort int, err error) {
  189. sql := fmt.Sprintf(`SELECT COALESCE(MAX(sort),0) AS sort FROM %s WHERE %s = ?`, m.TableName(), SpeechRecognitionMenuCols.ParentId)
  190. err = global.DEFAULT_DmSQL.Raw(sql, parentId).Scan(&sort).Error
  191. return
  192. }
  193. // GetFirstByParentId 获取父级目录下排序第一的目录
  194. func (m *SpeechRecognitionMenu) GetFirstByParentId(parentId int) (item *SpeechRecognitionMenu, err error) {
  195. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? ORDER BY %s ASC, %s ASC LIMIT 1`, m.TableName(), SpeechRecognitionMenuCols.ParentId, SpeechRecognitionMenuCols.Sort, SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
  196. err = global.DEFAULT_DmSQL.Raw(sql, parentId).First(&item).Error
  197. return
  198. }
  199. // UpdateChildByParentMenuId 通过父级目录ID更新子目录
  200. func (m *SpeechRecognitionMenu) UpdateChildByParentMenuId(menuIds []int, rootId int, levelStep int) (err error) {
  201. if len(menuIds) == 0 {
  202. return
  203. }
  204. var pars []interface{}
  205. pars = append(pars, rootId, levelStep)
  206. pars = append(pars, menuIds)
  207. 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)))
  208. err = global.DEFAULT_DmSQL.Exec(sql, pars...).Error
  209. return
  210. }