ai_pormpt.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package ai
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/ai_summary"
  7. "eta_gn/eta_api/models/system"
  8. aiSummaryService "eta_gn/eta_api/services/ai_summary"
  9. "eta_gn/eta_api/utils"
  10. "time"
  11. )
  12. // @router /prompt/list [get]
  13. func (this *AiController) AiPromptList() {
  14. br := new(models.BaseResponse).Init()
  15. defer func() {
  16. this.Data["json"] = br
  17. this.ServeJSON()
  18. }()
  19. list, err := ai_summary.GetAiPromptList(this.SysUser.AdminId, 0)
  20. if err != nil {
  21. br.Msg = "获取失败"
  22. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  23. return
  24. }
  25. br.Ret = 200
  26. br.Success = true
  27. br.Msg = "获取成功"
  28. br.Data = list
  29. }
  30. // @router /prompt/groups [get]
  31. func (this *AiController) PromptGroupList() {
  32. br := new(models.BaseResponse).Init()
  33. defer func() {
  34. this.Data["json"] = br
  35. this.ServeJSON()
  36. }()
  37. list, err := ai_summary.GetAiPromptShareList()
  38. if err != nil {
  39. br.Msg = err.Error()
  40. return
  41. }
  42. resp := make([]*ai_summary.RespGroupListItem, 0)
  43. publicAdminIdList := make([]int, 0)
  44. publicPromptListMap := make(map[int][]*ai_summary.AiPromptItem)
  45. systemAdminMap := make(map[int]*system.Admin)
  46. for _, v := range list {
  47. publicPromptList, ok := publicPromptListMap[v.SysAdminId]
  48. if !ok {
  49. publicPromptList = make([]*ai_summary.AiPromptItem, 0)
  50. publicAdminIdList = append(publicAdminIdList, v.SysAdminId)
  51. }
  52. systemAdminMap[v.SysAdminId] = &system.Admin{
  53. AdminId: v.SysAdminId,
  54. RealName: v.SysAdminName,
  55. }
  56. publicPromptList = append(publicPromptList, v)
  57. publicPromptListMap[v.SysAdminId] = publicPromptList
  58. }
  59. for _, v := range publicAdminIdList {
  60. systemAdmin, ok := systemAdminMap[v]
  61. if !ok {
  62. continue
  63. }
  64. respGroupPptNameListItemList, ok := publicPromptListMap[v]
  65. if !ok {
  66. respGroupPptNameListItemList = make([]*ai_summary.AiPromptItem, 0)
  67. }
  68. tmpRespGroupListItem := &ai_summary.RespGroupListItem{
  69. GroupId: int64(systemAdmin.AdminId),
  70. GroupName: systemAdmin.RealName,
  71. AdminId: systemAdmin.AdminId,
  72. IsShare: 1,
  73. PromptList: respGroupPptNameListItemList,
  74. }
  75. resp = append(resp, tmpRespGroupListItem)
  76. }
  77. br.Ret = 200
  78. br.Success = true
  79. br.Msg = "查询成功"
  80. br.Data = resp
  81. return
  82. }
  83. // @router /prompt/add [post]
  84. func (this *AiController) AddPrompt() {
  85. br := new(models.BaseResponse).Init()
  86. defer func() {
  87. this.Data["json"] = br
  88. this.ServeJSON()
  89. }()
  90. var req ai_summary.AiPromptAddReq
  91. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  92. if err != nil {
  93. br.Msg = "参数解析异常!"
  94. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  95. return
  96. }
  97. if req.Title == "" {
  98. br.Msg = "标题不能为空"
  99. return
  100. }
  101. var msg string
  102. maxSort, err := ai_summary.GetAiPromptMaxSort(this.SysUser.AdminId)
  103. prompt := &ai_summary.AiPrompt{
  104. CreateTime: time.Now(),
  105. ModifyTime: time.Now(),
  106. PromptContent: req.PromptContent,
  107. SysAdminId: this.SysUser.AdminId,
  108. SysAdminName: this.SysUser.RealName,
  109. Title: req.Title,
  110. Sort: maxSort + 1,
  111. }
  112. _, err = ai_summary.AddPropmt(prompt)
  113. if err != nil {
  114. br.Msg = "新增失败"
  115. br.ErrMsg = "新增失败,Err:" + err.Error()
  116. return
  117. }
  118. msg = "新增成功"
  119. br.Ret = 200
  120. br.Success = true
  121. br.Msg = msg
  122. }
  123. // @router /prompt/edit [post]
  124. func (this *AiController) EditPrompt() {
  125. br := new(models.BaseResponse).Init()
  126. defer func() {
  127. this.Data["json"] = br
  128. this.ServeJSON()
  129. }()
  130. var req ai_summary.AiPromptEditReq
  131. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  132. if err != nil {
  133. br.Msg = "参数解析异常!"
  134. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  135. return
  136. }
  137. if req.Title == "" {
  138. br.Msg = "标题不能为空"
  139. return
  140. }
  141. promptItem := &ai_summary.AiPrompt{
  142. AiPromptId: req.AiPromptId,
  143. PromptContent: req.PromptContent,
  144. Title: req.Title,
  145. CreateTime: time.Time{},
  146. ModifyTime: time.Now(),
  147. }
  148. err = promptItem.Update([]string{"ModifyTime", "PromptContent", "Title"})
  149. if err != nil {
  150. br.Msg = "编辑失败"
  151. br.ErrMsg = "编辑失败,Err:" + err.Error()
  152. return
  153. }
  154. br.Ret = 200
  155. br.Success = true
  156. br.Msg = "编辑成功"
  157. br.IsAddLog = true
  158. }
  159. // @router /prompt/delete [post]
  160. func (this *AiController) DeletePrompt() {
  161. br := new(models.BaseResponse).Init()
  162. defer func() {
  163. this.Data["json"] = br
  164. this.ServeJSON()
  165. }()
  166. var req ai_summary.DeleteAipromptReq
  167. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  168. if err != nil {
  169. br.Msg = "参数解析异常!"
  170. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  171. return
  172. }
  173. if req.AiPromptId <= 0 {
  174. br.Msg = "参数错误"
  175. return
  176. }
  177. promptInfo, err := ai_summary.GetAiPromptById(req.AiPromptId)
  178. if err != nil {
  179. br.Msg = "提示词不存在"
  180. return
  181. }
  182. if promptInfo.SysAdminId != this.SysUser.AdminId {
  183. br.Msg = "无权删除"
  184. return
  185. }
  186. err = ai_summary.DelAiPromptyId(req.AiPromptId)
  187. if err != nil {
  188. br.Msg = "删除失败"
  189. br.ErrMsg = "删除失败,Err:" + err.Error()
  190. return
  191. }
  192. br.Ret = 200
  193. br.Success = true
  194. br.IsAddLog = true
  195. br.Msg = "删除成功"
  196. }
  197. // @router /prompt/detail [get]
  198. func (this *AiController) DetailPrompt() {
  199. br := new(models.BaseResponse).Init()
  200. defer func() {
  201. if br.ErrMsg == "" {
  202. br.IsSendEmail = false
  203. }
  204. this.Data["json"] = br
  205. this.ServeJSON()
  206. }()
  207. sysUser := this.SysUser
  208. if sysUser == nil {
  209. br.Msg = "请登录"
  210. br.ErrMsg = "请登录,SysUser Is Empty"
  211. br.Ret = 408
  212. return
  213. }
  214. aiPromptId, _ := this.GetInt("AiPromptId")
  215. promptInfo, err := ai_summary.GetAiPromptById(aiPromptId)
  216. if err != nil {
  217. br.Msg = "提示词不存在"
  218. return
  219. }
  220. br.Ret = 200
  221. br.Success = true
  222. br.Msg = "获取成功"
  223. br.Data = promptInfo
  224. }
  225. // @router /prompt/move [post]
  226. func (this *AiController) MoveAiPrompt() {
  227. br := new(models.BaseResponse).Init()
  228. defer func() {
  229. this.Data["json"] = br
  230. this.ServeJSON()
  231. }()
  232. var req ai_summary.ReqMovePrompt
  233. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  234. if err != nil {
  235. br.Msg = "参数解析异常!"
  236. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  237. return
  238. }
  239. if req.AiPromptId <= 0 {
  240. br.Msg = "请输入提示词ID"
  241. return
  242. }
  243. prompt, err := ai_summary.GetAiPromptById(req.AiPromptId)
  244. if err != nil {
  245. if utils.IsErrNoRow(err) {
  246. br.Msg = "当前提示词不存在"
  247. br.ErrMsg = "当前提示词不存在"
  248. return
  249. }
  250. br.Msg = "提示词查询出错"
  251. br.ErrMsg = "提示词查询出错:" + err.Error()
  252. return
  253. }
  254. var updateStr []string
  255. var currentSort, prevSort, nextSort int
  256. currentSort = prompt.Sort
  257. var prevPrompt *ai_summary.AiPrompt
  258. var nextPrompt *ai_summary.AiPrompt
  259. if req.PrevAiPromptId > 0 {
  260. prevPrompt, err = ai_summary.GetAiPromptById(req.PrevAiPromptId)
  261. if err != nil {
  262. if utils.IsErrNoRow(err) {
  263. err = errors.New("目录下的提示词不存在")
  264. return
  265. }
  266. err = errors.New("目录下的提示词查询出错:" + err.Error())
  267. return
  268. }
  269. prevSort = prevPrompt.Sort
  270. }
  271. if req.NextAiPromptId > 0 {
  272. nextPrompt, err = ai_summary.GetAiPromptById(req.NextAiPromptId)
  273. if err != nil {
  274. if utils.IsErrNoRow(err) {
  275. err = errors.New("目录下的提示词不存在")
  276. return
  277. }
  278. err = errors.New("目录下的提示词查询出错:" + err.Error())
  279. return
  280. }
  281. nextSort = nextPrompt.Sort
  282. }
  283. updateStr = append(updateStr, "sort")
  284. if prevSort >= currentSort {
  285. err = ai_summary.MoveDownPromptBySort(this.SysUser.AdminId, prevSort, currentSort)
  286. if err != nil {
  287. err = errors.New("向下移动提示词出错:" + err.Error())
  288. return
  289. }
  290. prompt.Sort = prevSort
  291. } else if nextSort <= currentSort && nextSort != 0 {
  292. err = ai_summary.MoveUpPromptBySort(this.SysUser.AdminId, nextSort, currentSort)
  293. if err != nil {
  294. err = errors.New("向上移动提示词出错:" + err.Error())
  295. return
  296. }
  297. prompt.Sort = nextSort
  298. }
  299. err = prompt.Update(updateStr)
  300. if err != nil {
  301. err = errors.New("移动提示词出错:" + err.Error())
  302. return
  303. }
  304. br.Ret = 200
  305. br.Success = true
  306. br.Msg = "移动成功"
  307. }
  308. // @router /prompt/share [post]
  309. func (this *AiController) SharePrompt() {
  310. br := new(models.BaseResponse).Init()
  311. defer func() {
  312. this.Data["json"] = br
  313. this.ServeJSON()
  314. }()
  315. var req ai_summary.DeleteAipromptReq
  316. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  317. if err != nil {
  318. br.Msg = "参数解析异常!"
  319. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  320. return
  321. }
  322. if req.AiPromptId <= 0 {
  323. br.Msg = "请输入提示词序号"
  324. return
  325. }
  326. err = aiSummaryService.SharePrompt(req.AiPromptId, this.SysUser.AdminId)
  327. if err != nil {
  328. br.Msg = err.Error()
  329. return
  330. }
  331. br.Ret = 200
  332. br.Success = true
  333. br.Msg = "操作成功"
  334. return
  335. }