123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- package speech_recognition
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/speech_recognition"
- "eta/eta_api/utils"
- "fmt"
- "strings"
- "time"
- )
- type SpeechRecognitionMenuController struct {
- controllers.BaseAuthController
- }
- // Add
- // @Title 新增分类
- // @Description 新增分类
- // @Param request body speech_recognition.SpeechRecognitionMenuAddReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /menu/add [post]
- func (this *SpeechRecognitionMenuController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionMenuAddReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- req.MenuName = strings.TrimSpace(req.MenuName)
- if req.MenuName == "" {
- br.Msg = "请输入目录名称"
- return
- }
- // 校验同级目录是否有重名
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- {
- cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.MenuName, req.ParentId)
- exists, e := menuOb.GetItemByCondition(cond, pars, "")
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取同名目录失败, Err: " + e.Error()
- return
- }
- if exists != nil && exists.SpeechRecognitionMenuId > 0 {
- br.Msg = "分类名称已存在,请重新输入"
- return
- }
- }
- // 获取目录层级
- level := 1
- {
- if req.ParentId > 0 {
- parentMenu, e := menuOb.GetItemById(req.ParentId)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取父级目录失败, Err: " + e.Error()
- return
- }
- level += parentMenu.Level
- }
- }
- menuOb.MenuName = req.MenuName
- menuOb.ParentId = req.ParentId
- menuOb.Level = level
- menuOb.CreateTime = time.Now().Local()
- menuOb.ModifyTime = time.Now().Local()
- e := menuOb.Create()
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "新增目录失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Edit
- // @Title 编辑分类
- // @Description 编辑分类
- // @Param request body speech_recognition.SpeechRecognitionMenuEditReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /menu/edit [post]
- func (this *SpeechRecognitionMenuController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionMenuEditReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.MenuId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
- return
- }
- req.MenuName = strings.TrimSpace(req.MenuName)
- if req.MenuName == "" {
- br.Msg = "请输入目录名称"
- return
- }
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- menuItem, e := menuOb.GetItemById(req.MenuId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "目录不存在,请刷新页面"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录失败, Err: " + e.Error()
- return
- }
- // 校验同级目录是否有重名
- {
- cond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ParentId, speech_recognition.SpeechRecognitionMenuCols.SpeechRecognitionMenuId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.MenuName, menuItem.ParentId, req.MenuId)
- exists, e := menuOb.GetItemByCondition(cond, pars, "")
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取同名目录失败, Err: " + e.Error()
- return
- }
- if exists != nil && exists.SpeechRecognitionMenuId > 0 {
- br.Msg = "分类名称已存在,请重新输入"
- return
- }
- }
- menuItem.MenuName = req.MenuName
- menuItem.ModifyTime = time.Now().Local()
- updateCols := []string{speech_recognition.SpeechRecognitionMenuCols.MenuName, speech_recognition.SpeechRecognitionMenuCols.ModifyTime}
- if e = menuItem.Update(updateCols); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "更新目录失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Remove
- // @Title 删除分类
- // @Description 删除分类
- // @Param request body speech_recognition.SpeechRecognitionMenuRemoveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /menu/remove [post]
- func (this *SpeechRecognitionMenuController) Remove() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req speech_recognition.SpeechRecognitionMenuRemoveReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数解析失败, Err: " + e.Error()
- return
- }
- if req.MenuId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, MenuId: %d", req.MenuId)
- return
- }
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- menuItem, e := menuOb.GetItemById(req.MenuId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录失败, Err: " + e.Error()
- return
- }
- // 校验目录下是否有内容
- {
- speechOb := new(speech_recognition.SpeechRecognition)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionCols.MenuId)
- pars := make([]interface{}, 0)
- pars = append(pars, req.MenuId)
- count, e := speechOb.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录下的语音识别数失败, Err: " + e.Error()
- return
- }
- if count > 0 {
- br.Msg = "该分类关联转写文件,删除失败!"
- return
- }
- cond = fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.ParentId)
- pars = make([]interface{}, 0)
- pars = append(pars, req.MenuId)
- count, e = menuOb.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取下级目录数失败, Err: " + e.Error()
- return
- }
- if count > 0 {
- br.Msg = "该分类含下级目录,删除失败!"
- return
- }
- }
- if e = menuItem.Del(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "删除目录失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // Tree
- // @Title 目录树
- // @Description 目录树
- // @Param ParentId query int false "父级ID"
- // @Success 200 {object} speech_recognition.SpeechRecognitionMenuNodeItem
- // @router /menu/tree [get]
- func (this *SpeechRecognitionMenuController) Tree() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 前端采用懒加载, 所以只查询目录及当前目录下的语音识别
- parentId, _ := this.GetInt("ParentId")
- menus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
- {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionMenuCols.ParentId)
- pars := make([]interface{}, 0)
- pars = append(pars, parentId)
- list, e := menuOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", speech_recognition.SpeechRecognitionMenuCols.Sort))
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
- return
- }
- menus = list
- }
- resp := make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
- if len(menus) == 0 {
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- menuIds := make([]int, 0)
- for _, m := range menus {
- menuIds = append(menuIds, m.SpeechRecognitionMenuId)
- }
- // 子目录
- childMenus := make([]*speech_recognition.SpeechRecognitionMenu, 0)
- {
- menuOb := new(speech_recognition.SpeechRecognitionMenu)
- cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionMenuCols.ParentId, utils.GetOrmInReplace(len(menuIds)))
- pars := make([]interface{}, 0)
- pars = append(pars, menuIds)
- list, e := menuOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", speech_recognition.SpeechRecognitionMenuCols.Sort, speech_recognition.SpeechRecognitionMenuCols.ParentId))
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取子目录列表失败, Err: " + e.Error()
- return
- }
- childMenus = list
- }
- menuChildren := make(map[int][]*speech_recognition.SpeechRecognitionMenuNodeItem)
- for _, m := range childMenus {
- if menuChildren[m.ParentId] == nil {
- menuChildren[m.ParentId] = make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
- }
- menuChildren[m.ParentId] = append(menuChildren[m.ParentId], &speech_recognition.SpeechRecognitionMenuNodeItem{
- NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeDefault,
- MenuId: m.SpeechRecognitionMenuId,
- MenuName: m.MenuName,
- ParentId: m.ParentId,
- Sort: m.Sort,
- CreateTime: utils.TimeTransferString(utils.FormatDateTime, m.CreateTime),
- })
- }
- // 目录下的语音识别
- speeches := make([]*speech_recognition.SpeechRecognition, 0)
- {
- speechOb := new(speech_recognition.SpeechRecognition)
- cond := fmt.Sprintf(` AND %s IN (%s)`, speech_recognition.SpeechRecognitionCols.MenuId, utils.GetOrmInReplace(len(menuIds)))
- pars := make([]interface{}, 0)
- pars = append(pars, menuIds)
- list, e := speechOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", speech_recognition.SpeechRecognitionCols.Sort, speech_recognition.SpeechRecognitionCols.CreateTime))
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取语音识别列表失败, Err: " + e.Error()
- return
- }
- speeches = list
- }
- menuSpeeches := make(map[int][]*speech_recognition.SpeechRecognitionMenuNodeItem)
- for _, s := range speeches {
- if menuSpeeches[s.MenuId] == nil {
- menuSpeeches[s.MenuId] = make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0)
- }
- menuSpeeches[s.MenuId] = append(menuSpeeches[s.MenuId], &speech_recognition.SpeechRecognitionMenuNodeItem{
- NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeSpeech,
- SpeechRecognitionId: s.SpeechRecognitionId,
- SpeechRecognitionName: s.FileName,
- ParentId: s.MenuId,
- Sort: s.Sort,
- CreateTime: utils.TimeTransferString(utils.FormatDateTime, s.CreateTime),
- })
- }
- for _, m := range menus {
- t := &speech_recognition.SpeechRecognitionMenuNodeItem{
- NodeType: speech_recognition.SpeechRecognitionMenuNodeTypeDefault,
- MenuId: m.SpeechRecognitionMenuId,
- MenuName: m.MenuName,
- ParentId: m.ParentId,
- Sort: m.Sort,
- CreateTime: utils.TimeTransferString(utils.FormatDateTime, m.CreateTime),
- Children: make([]*speech_recognition.SpeechRecognitionMenuNodeItem, 0),
- }
- if menuSpeeches[m.SpeechRecognitionMenuId] != nil {
- t.Children = append(t.Children, menuSpeeches[m.SpeechRecognitionMenuId]...)
- }
- if menuChildren[m.SpeechRecognitionMenuId] != nil {
- t.Children = append(t.Children, menuChildren[m.SpeechRecognitionMenuId]...)
- }
- resp = append(resp, t)
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
|