ai.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package aimod
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type AiChatTopic struct {
  7. AiChatTopicId int `orm:"column(ai_chat_topic_id);pk"`
  8. TopicName string
  9. SysUserId int
  10. SysUserRealName string
  11. CreateTime time.Time
  12. ModifyTime time.Time
  13. }
  14. type AiChat struct {
  15. AiChatId int `orm:"column(ai_chat_id);pk"`
  16. AiChatTopicId int
  17. Ask string
  18. AskUuid string
  19. Answer string
  20. Model string
  21. SysUserId int
  22. SysUserRealName string
  23. CreateTime time.Time
  24. ModifyTime time.Time
  25. }
  26. type ChatReq struct {
  27. AiChatTopicId int `description:"主题id"`
  28. Ask string `description:"提问"`
  29. }
  30. func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
  31. sql := `SELECT * FROM ai_chat WHERE ask_uuid=?`
  32. o := orm.NewOrmUsingDB("ai")
  33. err = o.Raw(sql, askUuid).QueryRow(&item)
  34. return
  35. }
  36. type ChatResp struct {
  37. AiChatTopicId int `description:"主题id"`
  38. Ask string `description:"提问"`
  39. Answer string `description:"回答"`
  40. Model string
  41. }
  42. // AddAiChatTopic 新增主题
  43. func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) {
  44. o := orm.NewOrmUsingDB("ai")
  45. lastId, err = o.Insert(item)
  46. return
  47. }
  48. // AddAiChat 新增聊天
  49. func AddAiChat(item *AiChat) (lastId int64, err error) {
  50. o := orm.NewOrmUsingDB("ai")
  51. lastId, err = o.Insert(item)
  52. return
  53. }
  54. type AiChatTopicView struct {
  55. AiChatTopicId int `description:"主题id"`
  56. TopicName string `description:"主题名称"`
  57. CreateTime string `description:"创建时间"`
  58. ModifyTime string `description:"修改时间"`
  59. }
  60. func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
  61. sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
  62. o := orm.NewOrmUsingDB("ai")
  63. _, err = o.Raw(sql, sysUserId).QueryRows(&item)
  64. return
  65. }
  66. type AiChatTopicListResp struct {
  67. List []*AiChatTopicView
  68. }
  69. type AiChatView struct {
  70. AiChatId int `description:"记录id"`
  71. AiChatTopicId int `description:"主题id"`
  72. Ask string `description:"提问"`
  73. Answer string `description:"答案"`
  74. Model string
  75. CreateTime string `description:"创建时间"`
  76. ModifyTime string `description:"修改时间"`
  77. }
  78. func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
  79. sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
  80. o := orm.NewOrmUsingDB("ai")
  81. _, err = o.Raw(sql, aiChatTopicId).QueryRows(&item)
  82. return
  83. }
  84. type AiChatDetailResp struct {
  85. List []*AiChatView
  86. }
  87. type TopicDeleteReq struct {
  88. AiChatTopicId int `description:"主题id"`
  89. }
  90. func DeleteTopic(topicId int) (err error) {
  91. o := orm.NewOrmUsingDB("ai")
  92. tx, err := o.Begin()
  93. defer func() {
  94. if err != nil {
  95. tx.Rollback()
  96. } else {
  97. tx.Commit()
  98. }
  99. }()
  100. sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
  101. _, err = tx.Raw(sql, topicId).Exec()
  102. if err != nil {
  103. return err
  104. }
  105. sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
  106. _, err = tx.Raw(sql, topicId).Exec()
  107. if err != nil {
  108. return err
  109. }
  110. return err
  111. }
  112. type TopicEditReq struct {
  113. AiChatTopicId int `description:"主题id"`
  114. TopicName string `description:"主题名称"`
  115. }
  116. func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
  117. sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
  118. o := orm.NewOrmUsingDB("ai")
  119. err = o.Raw(sql, topicName).QueryRow(&item)
  120. return
  121. }
  122. func EditTopic(topicId int, topicName string) (err error) {
  123. o := orm.NewOrmUsingDB("ai")
  124. sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
  125. _, err = o.Raw(sql, topicName, topicId).Exec()
  126. return err
  127. }