rag_eta_report_abstract.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package rag
  2. import (
  3. "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // RagEtaReportAbstract 报告摘要
  10. type RagEtaReportAbstract struct {
  11. RagEtaReportAbstractId int `gorm:"primaryKey;column:rag_eta_report_abstract_id" description:"-"`
  12. RagEtaReportId int `gorm:"column:rag_eta_report_id" description:"ETA报告id"`
  13. Content string `gorm:"column:content" description:"摘要内容"`
  14. QuestionId int `gorm:"column:question_id" description:"提示词Id"`
  15. QuestionContent string `gorm:"column:question_content" description:"questionContent"`
  16. Version int `gorm:"column:version" description:"版本号"`
  17. Tags string `gorm:"column:tags" description:"标签"`
  18. TagsName string `gorm:"column:tags_name" description:"标签名,多个用英文逗号隔开"`
  19. VectorKey string `gorm:"column:vector_key" description:"向量key标识"`
  20. ModifyTime time.Time `gorm:"column:modify_time" description:"modifyTime"`
  21. CreateTime time.Time `gorm:"column:create_time" description:"createTime"`
  22. }
  23. // TableName get sql table name.获取数据库表名
  24. func (m *RagEtaReportAbstract) TableName() string {
  25. return "rag_eta_report_abstract"
  26. }
  27. // RagEtaReportAbstractColumns get sql column name.获取数据库列名
  28. var RagEtaReportAbstractColumns = struct {
  29. RagEtaReportAbstractId string
  30. RagEtaReportId string
  31. Content string
  32. QuestionId string
  33. QuestionContent string
  34. Version string
  35. Tags string
  36. TagsName string
  37. VectorKey string
  38. ModifyTime string
  39. CreateTime string
  40. }{
  41. RagEtaReportAbstractId: "rag_eta_report_abstract_id",
  42. RagEtaReportId: "rag_eta_report_id",
  43. Content: "content",
  44. QuestionId: "question_id",
  45. QuestionContent: "question_content",
  46. Version: "version",
  47. Tags: "tags",
  48. TagsName: "tags_name",
  49. VectorKey: "vector_key",
  50. ModifyTime: "modify_time",
  51. CreateTime: "create_time",
  52. }
  53. func (m *RagEtaReportAbstract) Create() (err error) {
  54. err = global.DbMap[utils.DbNameAI].Create(&m).Error
  55. return
  56. }
  57. func (m *RagEtaReportAbstract) Update(updateCols []string) (err error) {
  58. err = global.DbMap[utils.DbNameAI].Select(updateCols).Updates(&m).Error
  59. return
  60. }
  61. func (m *RagEtaReportAbstract) Del() (err error) {
  62. err = global.DbMap[utils.DbNameAI].Delete(&m).Error
  63. return
  64. }
  65. func (m *RagEtaReportAbstract) GetById(id int) (item *RagEtaReportAbstract, err error) {
  66. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", RagEtaReportAbstractColumns.RagEtaReportAbstractId), id).First(&item).Error
  67. return
  68. }
  69. func (m *RagEtaReportAbstract) GetByIdList(idList []int) (items []*RagEtaReportAbstract, err error) {
  70. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s in (?) ", RagEtaReportAbstractColumns.RagEtaReportAbstractId), idList).Find(&items).Error
  71. return
  72. }
  73. func (m *RagEtaReportAbstract) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*RagEtaReportAbstract, err error) {
  74. if field == "" {
  75. field = "*"
  76. }
  77. sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s order by rag_eta_report_abstract_id desc LIMIT ?,?`, field, m.TableName(), condition)
  78. pars = append(pars, startSize, pageSize)
  79. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  80. return
  81. }
  82. func (m *RagEtaReportAbstract) DelByIdList(idList []int) (err error) {
  83. if len(idList) <= 0 {
  84. return
  85. }
  86. sqlStr := fmt.Sprintf(`delete from %s where %s in (?)`, m.TableName(), RagEtaReportAbstractColumns.RagEtaReportAbstractId)
  87. err = global.DbMap[utils.DbNameAI].Exec(sqlStr, idList).Error
  88. return
  89. }
  90. // GetByRagEtaReportId
  91. // @Description: 根据报告id获取摘要
  92. // @author: Roc
  93. // @receiver m
  94. // @datetime 2025-03-07 10:00:59
  95. // @param id int
  96. // @return item *RagEtaReportAbstract
  97. // @return err error
  98. func (m *RagEtaReportAbstract) GetByRagEtaReportId(id int) (item *RagEtaReportAbstract, err error) {
  99. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", RagEtaReportAbstractColumns.RagEtaReportId), id).Order(fmt.Sprintf(`%s DESC`, RagEtaReportAbstractColumns.RagEtaReportAbstractId)).First(&item).Error
  100. return
  101. }
  102. // GetByRagEtaReportIdAndQuestionId
  103. // @Description: 根据报告id和提示词ID获取摘要
  104. // @author: Roc
  105. // @receiver m
  106. // @datetime 2025-04-17 17:39:27
  107. // @param articleId int
  108. // @param questionId int
  109. // @return item *RagEtaReportAbstract
  110. // @return err error
  111. func (m *RagEtaReportAbstract) GetByRagEtaReportIdAndQuestionId(articleId, questionId int) (item *RagEtaReportAbstract, err error) {
  112. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ? AND %s = ? ", RagEtaReportAbstractColumns.RagEtaReportId, RagEtaReportAbstractColumns.QuestionId), articleId, questionId).Order(fmt.Sprintf(`%s DESC`, RagEtaReportAbstractColumns.RagEtaReportAbstractId)).First(&item).Error
  113. return
  114. }
  115. type RagEtaReportAbstractView struct {
  116. RagEtaReportAbstractId int `gorm:"primaryKey;column:rag_eta_report_abstract_id" description:"-"`
  117. RagEtaReportId int `gorm:"column:rag_eta_report_id" description:"ETA报告id"`
  118. Abstract string `gorm:"column:abstract;type:longtext;comment:摘要内容;" description:"摘要内容"`
  119. QuestionId int `gorm:"column:question_id" description:"提示词Id"`
  120. QuestionContent string `gorm:"column:question_content" description:"questionContent"`
  121. Version int `gorm:"column:version" description:"版本号"`
  122. Tags string `gorm:"column:tags" description:"标签"`
  123. TagsName string `gorm:"column:tags_name" description:"标签名,多个用英文逗号隔开"`
  124. VectorKey string `gorm:"column:vector_key" description:"向量key标识"`
  125. ModifyTime string `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"`
  126. CreateTime string `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"`
  127. Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"`
  128. }
  129. type RagEtaReportAbstractItem struct {
  130. RagEtaReportAbstractId int `gorm:"primaryKey;column:rag_eta_report_abstract_id" description:"-"`
  131. RagEtaReportId int `gorm:"column:rag_eta_report_id" description:"ETA报告id"`
  132. Abstract string `gorm:"column:abstract;type:longtext;comment:摘要内容;" description:"摘要内容"`
  133. QuestionId int `gorm:"column:question_id" description:"提示词Id"`
  134. QuestionContent string `gorm:"column:question_content" description:"questionContent"`
  135. Version int `gorm:"column:version" description:"版本号"`
  136. Tags string `gorm:"column:tags" description:"标签"`
  137. TagsName string `gorm:"column:tags_name" description:"标签名,多个用英文逗号隔开"`
  138. VectorKey string `gorm:"column:vector_key" description:"向量key标识"`
  139. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"`
  140. CreateTime time.Time `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"`
  141. Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"`
  142. }
  143. func (m *RagEtaReportAbstractItem) ToView() RagEtaReportAbstractView {
  144. return RagEtaReportAbstractView{
  145. RagEtaReportAbstractId: m.RagEtaReportAbstractId,
  146. RagEtaReportId: m.RagEtaReportId,
  147. Abstract: m.Abstract,
  148. Version: m.Version,
  149. VectorKey: m.VectorKey,
  150. ModifyTime: utils.DateStrToDateTimeStr(m.ModifyTime),
  151. CreateTime: utils.DateStrToDateTimeStr(m.CreateTime),
  152. Title: m.Title,
  153. QuestionId: m.QuestionId,
  154. Tags: m.Tags,
  155. TagsName: m.TagsName,
  156. QuestionContent: m.QuestionContent,
  157. }
  158. }
  159. func (m *RagEtaReportAbstract) EtaReportAbstractItem(list []*RagEtaReportAbstractItem) (etaReportAbstractViewList []RagEtaReportAbstractView) {
  160. etaReportAbstractViewList = make([]RagEtaReportAbstractView, 0)
  161. for _, v := range list {
  162. etaReportAbstractViewList = append(etaReportAbstractViewList, v.ToView())
  163. }
  164. return
  165. }
  166. func (m *RagEtaReportAbstract) GetListByPlatformCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*RagEtaReportAbstractItem, err error) {
  167. if field == "" {
  168. field = "*"
  169. }
  170. sqlStr := fmt.Sprintf(`SELECT %s FROM %s AS a
  171. WHERE 1=1 %s order by a.modify_time DESC,a.rag_eta_report_abstract_id DESC LIMIT ?,?`, field, m.TableName(), condition)
  172. pars = append(pars, startSize, pageSize)
  173. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  174. return
  175. }
  176. func (m *RagEtaReportAbstract) GetCountByPlatformCondition(condition string, pars []interface{}) (total int, err error) {
  177. var intNull sql.NullInt64
  178. sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s AS a
  179. WHERE 1=1 %s`, m.TableName(), condition)
  180. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
  181. if err == nil && intNull.Valid {
  182. total = int(intNull.Int64)
  183. }
  184. return
  185. }
  186. func (m *RagEtaReportAbstract) GetPageListByPlatformCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*RagEtaReportAbstractItem, err error) {
  187. total, err = m.GetCountByPlatformCondition(condition, pars)
  188. if err != nil {
  189. return
  190. }
  191. if total > 0 {
  192. items, err = m.GetListByPlatformCondition(``, condition, pars, startSize, pageSize)
  193. }
  194. return
  195. }
  196. func (m *RagEtaReportAbstract) GetListByTagAndPlatformCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*RagEtaReportAbstractItem, err error) {
  197. if field == "" {
  198. field = "*"
  199. }
  200. sqlStr := fmt.Sprintf(`SELECT %s FROM %s AS a
  201. JOIN wechat_article AS b ON a.rag_eta_report_id=b.rag_eta_report_id
  202. JOIN wechat_platform AS c ON b.wechat_platform_id=c.wechat_platform_id
  203. JOIN wechat_platform_tag_mapping AS d ON c.wechat_platform_id=d.wechat_platform_id
  204. WHERE 1=1 AND b.is_deleted=0 %s order by a.modify_time DESC,a.rag_eta_report_abstract_id DESC LIMIT ?,?`, field, m.TableName(), condition)
  205. pars = append(pars, startSize, pageSize)
  206. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  207. return
  208. }
  209. func (m *RagEtaReportAbstract) GetCountByTagAndPlatformCondition(condition string, pars []interface{}) (total int, err error) {
  210. var intNull sql.NullInt64
  211. sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s AS a
  212. JOIN wechat_article AS b ON a.rag_eta_report_id=b.rag_eta_report_id
  213. JOIN wechat_platform AS c ON b.wechat_platform_id=c.wechat_platform_id
  214. JOIN wechat_platform_tag_mapping AS d ON c.wechat_platform_id=d.wechat_platform_id
  215. WHERE 1=1 AND b.is_deleted=0 %s`, m.TableName(), condition)
  216. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
  217. if err == nil && intNull.Valid {
  218. total = int(intNull.Int64)
  219. }
  220. return
  221. }
  222. func (m *RagEtaReportAbstract) GetPageListByTagAndPlatformCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*RagEtaReportAbstractItem, err error) {
  223. total, err = m.GetCountByTagAndPlatformCondition(condition, pars)
  224. if err != nil {
  225. return
  226. }
  227. if total > 0 {
  228. items, err = m.GetListByTagAndPlatformCondition(`a.rag_eta_report_abstract_id,a.rag_eta_report_id,a.content AS abstract,a.version,a.vector_key,a.modify_time,a.create_time,b.title,b.link,d.tag_id`, condition, pars, startSize, pageSize)
  229. }
  230. return
  231. }
  232. // DelVectorKey
  233. // @Description: 批量删除向量库
  234. // @author: Roc
  235. // @receiver m
  236. // @datetime 2025-03-12 16:47:52
  237. // @param ragEtaReportAbstractIdList []int
  238. // @return err error
  239. func (m *RagEtaReportAbstract) DelVectorKey(ragEtaReportAbstractIdList []int) (err error) {
  240. sqlStr := fmt.Sprintf(`UPDATE %s set vector_key = '' WHERE rag_eta_report_abstract_id IN (?)`, m.TableName())
  241. err = global.DbMap[utils.DbNameAI].Exec(sqlStr, ragEtaReportAbstractIdList).Error
  242. return
  243. }