rag_eta_report_abstract.go 12 KB

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