wechat_article_abstract.go 14 KB

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