question.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package llm
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/rag"
  7. "eta/eta_api/models/rag/request"
  8. "eta/eta_api/models/rag/response"
  9. "eta/eta_api/services"
  10. "eta/eta_api/services/elastic"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "github.com/rdlucklib/rdluck_tools/paging"
  14. "strings"
  15. "time"
  16. )
  17. // QuestionController
  18. // @Description: 问题库管理
  19. type QuestionController struct {
  20. controllers.BaseAuthController
  21. }
  22. // List
  23. // @Title 列表
  24. // @Description 列表
  25. // @Param PageSize query int true "每页数据条数"
  26. // @Param CurrentIndex query int true "当前页页码,从1开始"
  27. // @Param KeyWord query string true "搜索关键词"
  28. // @Success 200 {object} []*rag.QuestionListListResp
  29. // @router /question/list [get]
  30. func (c *QuestionController) List() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. c.Data["json"] = br
  34. c.ServeJSON()
  35. }()
  36. sysUser := c.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. return
  41. }
  42. pageSize, _ := c.GetInt("PageSize")
  43. currentIndex, _ := c.GetInt("CurrentIndex")
  44. keyWord := c.GetString("KeyWord")
  45. var startSize int
  46. if pageSize <= 0 {
  47. pageSize = utils.PageSize20
  48. }
  49. if currentIndex <= 0 {
  50. currentIndex = 1
  51. }
  52. startSize = utils.StartIndex(currentIndex, pageSize)
  53. var total int
  54. viewList := make([]rag.QuestionView, 0)
  55. if keyWord == `` {
  56. var condition string
  57. var pars []interface{}
  58. if keyWord != "" {
  59. condition += fmt.Sprintf(` AND %s like ?`, rag.QuestionColumns.QuestionContent)
  60. pars = append(pars, `%`+keyWord+`%`)
  61. }
  62. obj := new(rag.Question)
  63. tmpTotal, list, err := obj.GetPageListByCondition(condition, pars, startSize, pageSize)
  64. if err != nil {
  65. br.Msg = "获取失败"
  66. br.ErrMsg = "获取失败,Err:" + err.Error()
  67. return
  68. }
  69. total = tmpTotal
  70. if list != nil && len(list) > 0 {
  71. viewList = list[0].ListToViewList(list)
  72. }
  73. } else {
  74. sortMap := map[string]string{
  75. //"ArticleCreateTime": "desc",
  76. //"WechatArticleId": "desc",
  77. }
  78. tmpTotal, list, err := elastic.RagQuestionEsSearch(keyWord, startSize, pageSize, sortMap)
  79. if err != nil {
  80. br.Msg = "获取失败"
  81. br.ErrMsg = "获取失败,Err:" + err.Error()
  82. return
  83. }
  84. total = int(tmpTotal)
  85. if list != nil && len(list) > 0 {
  86. viewList = list[0].ToViewList(list)
  87. }
  88. }
  89. page := paging.GetPaging(currentIndex, pageSize, total)
  90. resp := response.QuestionListListResp{
  91. List: viewList,
  92. Paging: page,
  93. }
  94. br.Ret = 200
  95. br.Success = true
  96. br.Msg = "获取成功"
  97. br.Data = resp
  98. }
  99. // TitleList
  100. // @Title 标题列表
  101. // @Description 标题列表
  102. // @Param PageSize query int true "每页数据条数"
  103. // @Param CurrentIndex query int true "当前页页码,从1开始"
  104. // @Param KeyWord query string true "搜索关键词"
  105. // @Success 200 {object} []*rag.QuestionListListResp
  106. // @router /question/title/list [get]
  107. func (c *QuestionController) TitleList() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. c.Data["json"] = br
  111. c.ServeJSON()
  112. }()
  113. sysUser := c.SysUser
  114. if sysUser == nil {
  115. br.Msg = "请登录"
  116. br.ErrMsg = "请登录,SysUser Is Empty"
  117. return
  118. }
  119. pageSize, _ := c.GetInt("PageSize")
  120. currentIndex, _ := c.GetInt("CurrentIndex")
  121. keyWord := c.GetString("KeyWord")
  122. var startSize int
  123. if pageSize <= 0 {
  124. pageSize = utils.PageSize20
  125. }
  126. if currentIndex <= 0 {
  127. currentIndex = 1
  128. }
  129. startSize = utils.StartIndex(currentIndex, pageSize)
  130. var total int
  131. viewList := make([]rag.QuestionView, 0)
  132. if keyWord == `` {
  133. var condition string
  134. var pars []interface{}
  135. if keyWord != "" {
  136. condition += fmt.Sprintf(` AND %s like ?`, rag.QuestionColumns.QuestionContent)
  137. pars = append(pars, `%`+keyWord+`%`)
  138. }
  139. obj := new(rag.Question)
  140. tmpTotal, list, err := obj.GetTitlePageListByCondition(condition, pars, startSize, pageSize)
  141. if err != nil {
  142. br.Msg = "获取失败"
  143. br.ErrMsg = "获取失败,Err:" + err.Error()
  144. return
  145. }
  146. total = tmpTotal
  147. if list != nil && len(list) > 0 {
  148. viewList = list[0].ListToViewList(list)
  149. }
  150. } else {
  151. sortMap := map[string]string{
  152. //"ArticleCreateTime": "desc",
  153. //"WechatArticleId": "desc",
  154. }
  155. tmpTotal, list, err := elastic.RagQuestionEsSearch(keyWord, startSize, pageSize, sortMap)
  156. if err != nil {
  157. br.Msg = "获取失败"
  158. br.ErrMsg = "获取失败,Err:" + err.Error()
  159. return
  160. }
  161. total = int(tmpTotal)
  162. if list != nil && len(list) > 0 {
  163. viewList = list[0].ToViewList(list)
  164. }
  165. }
  166. page := paging.GetPaging(currentIndex, pageSize, total)
  167. resp := response.QuestionListListResp{
  168. List: viewList,
  169. Paging: page,
  170. }
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "获取成功"
  174. br.Data = resp
  175. }
  176. // Detail
  177. // @Title 列表
  178. // @Description 列表
  179. // @Param QuestionId query int true "问题id"
  180. // @Success 200 {object} []*rag.QuestionListListResp
  181. // @router /question/detail [get]
  182. func (c *QuestionController) Detail() {
  183. br := new(models.BaseResponse).Init()
  184. defer func() {
  185. c.Data["json"] = br
  186. c.ServeJSON()
  187. }()
  188. sysUser := c.SysUser
  189. if sysUser == nil {
  190. br.Msg = "请登录"
  191. br.ErrMsg = "请登录,SysUser Is Empty"
  192. return
  193. }
  194. questionId, _ := c.GetInt("QuestionId")
  195. if questionId <= 0 {
  196. br.Msg = "问题id不能为空"
  197. br.ErrMsg = "问题id不能为空"
  198. return
  199. }
  200. obj := new(rag.Question)
  201. questionItem, err := obj.GetByID(questionId)
  202. if err != nil {
  203. br.Msg = "获取失败"
  204. br.ErrMsg = "获取失败,Err:" + err.Error()
  205. return
  206. }
  207. br.Data = questionItem.ToView()
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. }
  212. // Add
  213. // @Title 新增问题
  214. // @Description 新增问题
  215. // @Param request body request.AddQuestionReq true "type json string"
  216. // @Success 200 Ret=200 新增成功
  217. // @router /question/add [post]
  218. func (c *QuestionController) Add() {
  219. br := new(models.BaseResponse).Init()
  220. defer func() {
  221. c.Data["json"] = br
  222. c.ServeJSON()
  223. }()
  224. var req request.AddQuestionReq
  225. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  226. if err != nil {
  227. br.Msg = "参数解析异常!"
  228. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  229. return
  230. }
  231. req.Content = strings.TrimSpace(req.Content)
  232. if req.Content == "" {
  233. br.Msg = "请输入问题"
  234. br.IsSendEmail = false
  235. return
  236. }
  237. //obj := rag.Question{}
  238. //_, err = obj.GetByCondition(` AND question_content = ? `, []interface{}{req.Content})
  239. //if err == nil {
  240. // br.Msg = "问题已入库,请不要重复添加"
  241. // br.IsSendEmail = false
  242. // return
  243. //}
  244. title := utils.GetFirstNChars(req.Content, 20)
  245. item := &rag.Question{
  246. QuestionId: 0,
  247. QuestionTitle: title,
  248. QuestionContent: req.Content,
  249. Sort: 0,
  250. ModifyTime: time.Now(),
  251. CreateTime: time.Now(),
  252. }
  253. err = item.Create()
  254. if err != nil {
  255. br.Msg = "添加失败"
  256. br.ErrMsg = "添加失败,Err:" + err.Error()
  257. return
  258. }
  259. // 新增/编辑ES数据
  260. go services.AddOrEditEsRagQuestion(item.QuestionId)
  261. br.Ret = 200
  262. br.Success = true
  263. br.Msg = `添加成功`
  264. }
  265. // Edit
  266. // @Title 编辑问题
  267. // @Description 编辑问题
  268. // @Param request body request.EditQuestionReq true "type json string"
  269. // @Success 200 Ret=200 新增成功
  270. // @router /question/edit [post]
  271. func (c *QuestionController) Edit() {
  272. br := new(models.BaseResponse).Init()
  273. defer func() {
  274. c.Data["json"] = br
  275. c.ServeJSON()
  276. }()
  277. var req request.EditQuestionReq
  278. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  279. if err != nil {
  280. br.Msg = "参数解析异常!"
  281. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  282. return
  283. }
  284. if req.QuestionId <= 0 {
  285. br.Msg = "问题id不能为空"
  286. br.IsSendEmail = false
  287. return
  288. }
  289. req.Content = strings.TrimSpace(req.Content)
  290. if req.Content == "" {
  291. br.Msg = "请输入问题"
  292. br.IsSendEmail = false
  293. return
  294. }
  295. obj := rag.Question{}
  296. item, err := obj.GetByID(req.QuestionId)
  297. if err != nil {
  298. br.Msg = "修改失败"
  299. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  300. if utils.IsErrNoRow(err) {
  301. br.Msg = "问题不存在"
  302. br.IsSendEmail = false
  303. }
  304. return
  305. }
  306. item.QuestionTitle = utils.GetFirstNChars(req.Content, 20)
  307. item.QuestionContent = req.Content
  308. item.ModifyTime = time.Now()
  309. err = item.Update([]string{"question_title", "question_content", "modify_time"})
  310. if err != nil {
  311. br.Msg = "修改失败"
  312. br.ErrMsg = "修改失败,Err:" + err.Error()
  313. return
  314. }
  315. // 新增/编辑ES数据
  316. go services.AddOrEditEsRagQuestion(item.QuestionId)
  317. br.Ret = 200
  318. br.Success = true
  319. br.Msg = `添加成功`
  320. }
  321. // Del
  322. // @Title 删除问题
  323. // @Description 删除问题
  324. // @Param request body request.EditQuestionReq true "type json string"
  325. // @Success 200 Ret=200 新增成功
  326. // @router /question/del [post]
  327. func (c *QuestionController) Del() {
  328. br := new(models.BaseResponse).Init()
  329. defer func() {
  330. c.Data["json"] = br
  331. c.ServeJSON()
  332. }()
  333. var req request.EditQuestionReq
  334. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  335. if err != nil {
  336. br.Msg = "参数解析异常!"
  337. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  338. return
  339. }
  340. if req.QuestionId <= 0 {
  341. br.Msg = "问题id不能为空"
  342. br.IsSendEmail = false
  343. return
  344. }
  345. obj := rag.Question{}
  346. item, err := obj.GetByID(req.QuestionId)
  347. if err != nil {
  348. br.Msg = "修改失败"
  349. br.ErrMsg = "修改失败,查找问题失败,Err:" + err.Error()
  350. if utils.IsErrNoRow(err) {
  351. br.Msg = "问题不存在"
  352. br.IsSendEmail = false
  353. }
  354. return
  355. }
  356. err = item.Del()
  357. if err != nil {
  358. br.Msg = "删除失败"
  359. br.ErrMsg = "删除失败,Err:" + err.Error()
  360. return
  361. }
  362. // 删除ES数据
  363. go services.DelEsRagQuestion(item.QuestionId)
  364. br.Ret = 200
  365. br.Success = true
  366. br.Msg = `删除成功`
  367. }
  368. //func init() {
  369. // // 问题加到es
  370. // {
  371. // obj := rag.Question{}
  372. // list, _ := obj.GetListByCondition(``, ` `, []interface{}{}, 0, 10000)
  373. // total := len(list)
  374. // for k, item := range list {
  375. // fmt.Println(k, "/", total)
  376. // services.AddOrEditEsRagQuestion(item.QuestionId)
  377. // }
  378. //
  379. // fmt.Println("结束了")
  380. // }
  381. //}