123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- package llm
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/rag"
- "eta/eta_api/models/rag/request"
- "eta/eta_api/models/rag/response"
- "eta/eta_api/services"
- "eta/eta_api/services/elastic"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strings"
- "time"
- )
- // QuestionController
- // @Description: 问题库管理
- type QuestionController struct {
- controllers.BaseAuthController
- }
- // List
- // @Title 列表
- // @Description 列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param KeyWord query string true "搜索关键词"
- // @Success 200 {object} []*rag.QuestionListListResp
- // @router /question/list [get]
- func (c *QuestionController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- return
- }
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- keyWord := c.GetString("KeyWord")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var total int
- viewList := make([]rag.QuestionView, 0)
- if keyWord == `` {
- var condition string
- var pars []interface{}
- if keyWord != "" {
- condition += fmt.Sprintf(` AND %s like ?`, rag.QuestionColumns.QuestionContent)
- pars = append(pars, `%`+keyWord+`%`)
- }
- obj := new(rag.Question)
- tmpTotal, list, err := obj.GetPageListByCondition(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- total = tmpTotal
- if list != nil && len(list) > 0 {
- viewList = list[0].ListToViewList(list)
- }
- } else {
- sortMap := map[string]string{
- //"ArticleCreateTime": "desc",
- //"WechatArticleId": "desc",
- }
- tmpTotal, list, err := elastic.RagQuestionEsSearch(keyWord, startSize, pageSize, sortMap)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- total = int(tmpTotal)
- if list != nil && len(list) > 0 {
- viewList = list[0].ToViewList(list)
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := response.QuestionListListResp{
- List: viewList,
- Paging: page,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // TitleList
- // @Title 标题列表
- // @Description 标题列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param KeyWord query string true "搜索关键词"
- // @Success 200 {object} []*rag.QuestionListListResp
- // @router /question/title/list [get]
- func (c *QuestionController) TitleList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- return
- }
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- keyWord := c.GetString("KeyWord")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var total int
- viewList := make([]rag.QuestionView, 0)
- if keyWord == `` {
- var condition string
- var pars []interface{}
- if keyWord != "" {
- condition += fmt.Sprintf(` AND %s like ?`, rag.QuestionColumns.QuestionContent)
- pars = append(pars, `%`+keyWord+`%`)
- }
- obj := new(rag.Question)
- tmpTotal, list, err := obj.GetTitlePageListByCondition(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- total = tmpTotal
- if list != nil && len(list) > 0 {
- viewList = list[0].ListToViewList(list)
- }
- } else {
- sortMap := map[string]string{
- //"ArticleCreateTime": "desc",
- //"WechatArticleId": "desc",
- }
- tmpTotal, list, err := elastic.RagQuestionEsSearch(keyWord, startSize, pageSize, sortMap)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- total = int(tmpTotal)
- if list != nil && len(list) > 0 {
- viewList = list[0].ToViewList(list)
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp := response.QuestionListListResp{
- List: viewList,
- Paging: page,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Detail
- // @Title 列表
- // @Description 列表
- // @Param QuestionId query int true "问题id"
- // @Success 200 {object} []*rag.QuestionListListResp
- // @router /question/detail [get]
- func (c *QuestionController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- return
- }
- questionId, _ := c.GetInt("QuestionId")
- if questionId <= 0 {
- br.Msg = "问题id不能为空"
- br.ErrMsg = "问题id不能为空"
- return
- }
- obj := new(rag.Question)
- questionItem, err := obj.GetByID(questionId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- br.Data = questionItem.ToView()
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // Add
- // @Title 新增问题
- // @Description 新增问题
- // @Param request body request.AddQuestionReq true "type json string"
- // @Success 200 Ret=200 新增成功
- // @router /question/add [post]
- func (c *QuestionController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.AddQuestionReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.Content = strings.TrimSpace(req.Content)
- if req.Content == "" {
- br.Msg = "请输入问题"
- br.IsSendEmail = false
- return
- }
- //obj := rag.Question{}
- //_, err = obj.GetByCondition(` AND question_content = ? `, []interface{}{req.Content})
- //if err == nil {
- // br.Msg = "问题已入库,请不要重复添加"
- // br.IsSendEmail = false
- // return
- //}
- title := utils.GetFirstNChars(req.Content, 20)
- item := &rag.Question{
- QuestionId: 0,
- QuestionTitle: title,
- QuestionContent: req.Content,
- Sort: 0,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = item.Create()
- if err != nil {
- br.Msg = "添加失败"
- br.ErrMsg = "添加失败,Err:" + err.Error()
- return
- }
- // 新增/编辑ES数据
- go services.AddOrEditEsRagQuestion(item.QuestionId)
- br.Ret = 200
- br.Success = true
- br.Msg = `添加成功`
- }
- // Edit
- // @Title 编辑问题
- // @Description 编辑问题
- // @Param request body request.EditQuestionReq true "type json string"
- // @Success 200 Ret=200 新增成功
- // @router /question/edit [post]
- func (c *QuestionController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.EditQuestionReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.QuestionId <= 0 {
- br.Msg = "问题id不能为空"
- br.IsSendEmail = false
- return
- }
- req.Content = strings.TrimSpace(req.Content)
- if req.Content == "" {
- br.Msg = "请输入问题"
- br.IsSendEmail = false
- return
- }
- obj := rag.Question{}
- item, err := obj.GetByID(req.QuestionId)
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
- if utils.IsErrNoRow(err) {
- br.Msg = "问题不存在"
- br.IsSendEmail = false
- }
- return
- }
- item.QuestionTitle = utils.GetFirstNChars(req.Content, 20)
- item.QuestionContent = req.Content
- item.ModifyTime = time.Now()
- err = item.Update([]string{"question_title", "question_content", "modify_time"})
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改失败,Err:" + err.Error()
- return
- }
- // 新增/编辑ES数据
- go services.AddOrEditEsRagQuestion(item.QuestionId)
- br.Ret = 200
- br.Success = true
- br.Msg = `添加成功`
- }
- // Del
- // @Title 删除问题
- // @Description 删除问题
- // @Param request body request.EditQuestionReq true "type json string"
- // @Success 200 Ret=200 新增成功
- // @router /question/del [post]
- func (c *QuestionController) Del() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- var req request.EditQuestionReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.QuestionId <= 0 {
- br.Msg = "问题id不能为空"
- br.IsSendEmail = false
- return
- }
- obj := rag.Question{}
- item, err := obj.GetByID(req.QuestionId)
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
- if utils.IsErrNoRow(err) {
- br.Msg = "问题不存在"
- br.IsSendEmail = false
- }
- return
- }
- err = item.Del()
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + err.Error()
- return
- }
- // 删除ES数据
- go services.DelEsRagQuestion(item.QuestionId)
- br.Ret = 200
- br.Success = true
- br.Msg = `删除成功`
- }
- //func init() {
- // // 问题加到es
- // {
- // obj := rag.Question{}
- // list, _ := obj.GetListByCondition(``, ` `, []interface{}{}, 0, 10000)
- // total := len(list)
- // for k, item := range list {
- // fmt.Println(k, "/", total)
- // services.AddOrEditEsRagQuestion(item.QuestionId)
- // }
- //
- // fmt.Println("结束了")
- // }
- //}
|