wechat_article.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 WechatArticle struct {
  10. WechatArticleId int `gorm:"column:wechat_article_id;type:int(10) UNSIGNED;primaryKey;not null;" description:""`
  11. WechatPlatformId int `gorm:"column:wechat_platform_id;type:int(11);comment:归属公众号id;default:0;" description:"归属公众号id"`
  12. FakeId string `gorm:"column:fake_id;type:varchar(255);comment:公众号唯一id;" description:"公众号唯一id"`
  13. Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"`
  14. Link string `gorm:"column:link;type:varchar(255);comment:链接;" description:"链接"`
  15. CoverUrl string `gorm:"column:cover_url;type:varchar(255);comment:公众号封面;" description:"公众号封面"`
  16. Description string `gorm:"column:description;type:varchar(255);comment:描述;" description:"描述"`
  17. Content string `gorm:"column:content;type:longtext;comment:报告详情;" description:"报告详情"`
  18. TextContent string `gorm:"column:text_content;type:text;comment:文本内容;" description:"文本内容"`
  19. Abstract string `gorm:"column:abstract;type:text;comment:摘要;" description:"摘要"`
  20. Country string `gorm:"column:country;type:varchar(255);comment:国家;" description:"国家"`
  21. Province string `gorm:"column:province;type:varchar(255);comment:省;" description:"省"`
  22. City string `gorm:"column:city;type:varchar(255);comment:市;" description:"市"`
  23. ArticleCreateTime time.Time `gorm:"column:article_create_time;type:datetime;comment:报告创建时间;default:NULL;" description:"报告创建时间"`
  24. VectorKey string `gorm:"column:vector_key;type:varchar(255);comment:向量key标识;" description:"向量key标识"`
  25. IsDeleted int `gorm:"column:is_deleted;type:tinyint(4);comment:是否删除,0:未删除,1: 已删除;default:0;" description:"是否删除,0:未删除,1: 已删除"`
  26. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;" description:"修改时间"`
  27. CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:入库时间;default:NULL;" description:"入库时间"`
  28. }
  29. // TableName get sql table name.获取数据库表名
  30. func (m *WechatArticle) TableName() string {
  31. return "wechat_article"
  32. }
  33. // WechatArticleColumns get sql column name.获取数据库列名
  34. var WechatArticleColumns = struct {
  35. WechatArticleID string
  36. WechatPlatformID string
  37. FakeID string
  38. Title string
  39. Link string
  40. CoverURL string
  41. Description string
  42. Content string
  43. TextContent string
  44. Abstract string
  45. Country string
  46. Province string
  47. City string
  48. ArticleCreateTime string
  49. IsDeleted string
  50. ModifyTime string
  51. CreateTime string
  52. }{
  53. WechatArticleID: "wechat_article_id",
  54. WechatPlatformID: "wechat_platform_id",
  55. FakeID: "fake_id",
  56. Title: "title",
  57. Link: "link",
  58. CoverURL: "cover_url",
  59. Description: "description",
  60. Content: "content",
  61. TextContent: "text_content",
  62. Abstract: "abstract",
  63. Country: "country",
  64. Province: "province",
  65. City: "city",
  66. ArticleCreateTime: "article_create_time",
  67. IsDeleted: "is_deleted",
  68. ModifyTime: "modify_time",
  69. CreateTime: "create_time",
  70. }
  71. type WechatArticleView struct {
  72. WechatArticleId int `gorm:"column:wechat_article_id;type:int(10) UNSIGNED;primaryKey;not null;" description:""`
  73. WechatPlatformId int `gorm:"column:wechat_platform_id;type:int(11);comment:归属公众号id;default:0;" description:"归属公众号id"`
  74. FakeId string `gorm:"column:fake_id;type:varchar(255);comment:公众号唯一id;" description:"公众号唯一id"`
  75. Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"`
  76. Link string `gorm:"column:link;type:varchar(255);comment:链接;" description:"链接"`
  77. CoverUrl string `gorm:"column:cover_url;type:varchar(255);comment:公众号封面;" description:"公众号封面"`
  78. Description string `gorm:"column:description;type:varchar(255);comment:描述;" description:"描述"`
  79. Content string `gorm:"column:content;type:longtext;comment:报告详情;" description:"报告详情"`
  80. TextContent string `gorm:"column:text_content;type:text;comment:文本内容;" description:"文本内容"`
  81. Abstract string `gorm:"column:abstract;type:text;comment:摘要;" description:"摘要"`
  82. Country string `gorm:"column:country;type:varchar(255);comment:国家;" description:"国家"`
  83. Province string `gorm:"column:province;type:varchar(255);comment:省;" description:"省"`
  84. City string `gorm:"column:city;type:varchar(255);comment:市;" description:"市"`
  85. ArticleCreateTime string `gorm:"column:article_create_time;type:datetime;comment:报告创建时间;default:NULL;" description:"报告创建时间"`
  86. ModifyTime string `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;" description:"修改时间"`
  87. CreateTime string `gorm:"column:create_time;type:datetime;comment:入库时间;default:NULL;" description:"入库时间"`
  88. WechatPlatformName string `gorm:"column:title;type:varchar(255);comment:标题;" description:"微信公众号名称"`
  89. WechatPlatformRoundHeadImg string `gorm:"column:round_head_img;type:varchar(255);comment:头像;" description:"微信公众号头像"`
  90. }
  91. func (m *WechatArticle) ToView() WechatArticleView {
  92. var articleCreateTime, modifyTime, createTime string
  93. if !m.ArticleCreateTime.IsZero() {
  94. articleCreateTime = m.ArticleCreateTime.Format(utils.FormatDateTime)
  95. }
  96. if !m.CreateTime.IsZero() {
  97. createTime = m.CreateTime.Format(utils.FormatDateTime)
  98. }
  99. if !m.ModifyTime.IsZero() {
  100. modifyTime = m.ModifyTime.Format(utils.FormatDateTime)
  101. }
  102. return WechatArticleView{
  103. WechatArticleId: m.WechatArticleId,
  104. WechatPlatformId: m.WechatPlatformId,
  105. FakeId: m.FakeId,
  106. Title: m.Title,
  107. Link: m.Link,
  108. CoverUrl: m.CoverUrl,
  109. Description: m.Description,
  110. Content: m.Content,
  111. TextContent: m.TextContent,
  112. Abstract: m.Abstract,
  113. Country: m.Country,
  114. Province: m.Province,
  115. City: m.City,
  116. ArticleCreateTime: articleCreateTime,
  117. ModifyTime: modifyTime,
  118. CreateTime: createTime,
  119. WechatPlatformName: "",
  120. WechatPlatformRoundHeadImg: "",
  121. }
  122. }
  123. func (m *WechatArticle) ListToViewList(list []*WechatArticle) (wechatArticleViewList []WechatArticleView) {
  124. wechatArticleViewList = make([]WechatArticleView, 0)
  125. for _, v := range list {
  126. wechatArticleViewList = append(wechatArticleViewList, v.ToView())
  127. }
  128. return
  129. }
  130. func (m *WechatArticle) Create() (err error) {
  131. err = global.DbMap[utils.DbNameAI].Create(&m).Error
  132. return
  133. }
  134. func (m *WechatArticle) Update(updateCols []string) (err error) {
  135. err = global.DbMap[utils.DbNameAI].Select(updateCols).Updates(&m).Error
  136. return
  137. }
  138. func (m *WechatArticle) GetById(id int) (item *WechatArticle, err error) {
  139. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", WechatArticleColumns.WechatArticleID), id).First(&item).Error
  140. return
  141. }
  142. func (m *WechatArticle) GetByLink(link string) (item *WechatArticle, err error) {
  143. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", WechatArticleColumns.Link), link).First(&item).Error
  144. return
  145. }
  146. func (m *WechatArticle) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*WechatArticle, err error) {
  147. if field == "" {
  148. field = "*"
  149. }
  150. sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 AND is_deleted=0 %s order by article_create_time desc,wechat_article_id desc LIMIT ?,?`, field, m.TableName(), condition)
  151. pars = append(pars, startSize, pageSize)
  152. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  153. return
  154. }
  155. func (m *WechatArticle) GetCountByCondition(condition string, pars []interface{}) (total int, err error) {
  156. var intNull sql.NullInt64
  157. sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s WHERE 1=1 AND is_deleted=0 %s`, m.TableName(), condition)
  158. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
  159. if err == nil && intNull.Valid {
  160. total = int(intNull.Int64)
  161. }
  162. return
  163. }
  164. func (m *WechatArticle) GetPageListByCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*WechatArticle, err error) {
  165. total, err = m.GetCountByCondition(condition, pars)
  166. if err != nil {
  167. return
  168. }
  169. if total > 0 {
  170. items, err = m.GetListByCondition(`wechat_article_id,wechat_platform_id,fake_id,title,link,cover_url,description,country,province,city,article_create_time,modify_time,create_time`, condition, pars, startSize, pageSize)
  171. }
  172. return
  173. }
  174. type WechatArticleAndPlatform struct {
  175. WechatArticleId int `gorm:"column:wechat_article_id;type:int(10) UNSIGNED;primaryKey;not null;" description:""`
  176. WechatPlatformId int `gorm:"column:wechat_platform_id;type:int(11);comment:归属公众号id;default:0;" description:"归属公众号id"`
  177. FakeId string `gorm:"column:fake_id;type:varchar(255);comment:公众号唯一id;" description:"公众号唯一id"`
  178. Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"`
  179. Link string `gorm:"column:link;type:varchar(255);comment:链接;" description:"链接"`
  180. CoverUrl string `gorm:"column:cover_url;type:varchar(255);comment:公众号封面;" description:"公众号封面"`
  181. Description string `gorm:"column:description;type:varchar(255);comment:描述;" description:"描述"`
  182. Content string `gorm:"column:content;type:longtext;comment:报告详情;" description:"报告详情"`
  183. TextContent string `gorm:"column:text_content;type:text;comment:文本内容;" description:"文本内容"`
  184. Abstract string `gorm:"column:abstract;type:text;comment:摘要;" description:"摘要"`
  185. Country string `gorm:"column:country;type:varchar(255);comment:国家;" description:"国家"`
  186. Province string `gorm:"column:province;type:varchar(255);comment:省;" description:"省"`
  187. City string `gorm:"column:city;type:varchar(255);comment:市;" description:"市"`
  188. ArticleCreateTime time.Time `gorm:"column:article_create_time;type:datetime;comment:报告创建时间;default:NULL;" description:"报告创建时间"`
  189. IsDeleted int `gorm:"column:is_deleted;type:tinyint(4);comment:是否删除,0:未删除,1: 已删除;default:0;" description:"是否删除,0:未删除,1: 已删除"`
  190. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;" description:"修改时间"`
  191. CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:入库时间;default:NULL;" description:"入库时间"`
  192. Nickname string `gorm:"column:nickname;type:varchar(255);comment:公众号名称;" description:"nickname"` // 公众号名称
  193. Alias string `gorm:"column:alias;type:varchar(255);comment:别名;" description:"alias"` // 别名
  194. RoundHeadImg string `gorm:"column:round_head_img;type:varchar(255);comment:头像;" description:"round_head_img"` // 头像
  195. }
  196. func (m *WechatArticleAndPlatform) ToView() WechatArticleView {
  197. var articleCreateTime, modifyTime, createTime string
  198. if !m.ArticleCreateTime.IsZero() {
  199. articleCreateTime = m.ArticleCreateTime.Format(utils.FormatDateTime)
  200. }
  201. if !m.CreateTime.IsZero() {
  202. createTime = m.CreateTime.Format(utils.FormatDateTime)
  203. }
  204. if !m.ModifyTime.IsZero() {
  205. modifyTime = m.ModifyTime.Format(utils.FormatDateTime)
  206. }
  207. return WechatArticleView{
  208. WechatArticleId: m.WechatArticleId,
  209. WechatPlatformId: m.WechatPlatformId,
  210. FakeId: m.FakeId,
  211. Title: m.Title,
  212. Link: m.Link,
  213. CoverUrl: m.CoverUrl,
  214. Description: m.Description,
  215. Content: m.Content,
  216. TextContent: m.TextContent,
  217. Abstract: m.Abstract,
  218. Country: m.Country,
  219. Province: m.Province,
  220. City: m.City,
  221. ArticleCreateTime: articleCreateTime,
  222. ModifyTime: modifyTime,
  223. CreateTime: createTime,
  224. WechatPlatformName: m.Nickname,
  225. WechatPlatformRoundHeadImg: m.RoundHeadImg,
  226. }
  227. }
  228. func (m *WechatArticle) ArticleAndPlatformListToViewList(list []*WechatArticleAndPlatform) (wechatArticleViewList []WechatArticleView) {
  229. wechatArticleViewList = make([]WechatArticleView, 0)
  230. for _, v := range list {
  231. wechatArticleViewList = append(wechatArticleViewList, v.ToView())
  232. }
  233. return
  234. }
  235. func (m *WechatArticle) GetListByPlatformCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*WechatArticleAndPlatform, err error) {
  236. if field == "" {
  237. field = "*"
  238. }
  239. sqlStr := fmt.Sprintf(`SELECT %s FROM %s AS a
  240. JOIN wechat_platform AS b ON a.wechat_platform_id=b.wechat_platform_id
  241. WHERE 1=1 AND a.is_deleted=0 %s order by a.article_create_time DESC,a.wechat_article_id DESC LIMIT ?,?`, field, m.TableName(), condition)
  242. pars = append(pars, startSize, pageSize)
  243. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  244. return
  245. }
  246. func (m *WechatArticle) GetCountByPlatformCondition(condition string, pars []interface{}) (total int, err error) {
  247. var intNull sql.NullInt64
  248. sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s AS a
  249. JOIN wechat_platform AS b ON a.wechat_platform_id=b.wechat_platform_id
  250. WHERE 1=1 AND a.is_deleted=0 %s`, m.TableName(), condition)
  251. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
  252. if err == nil && intNull.Valid {
  253. total = int(intNull.Int64)
  254. }
  255. return
  256. }
  257. func (m *WechatArticle) GetPageListByPlatformCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*WechatArticleAndPlatform, err error) {
  258. total, err = m.GetCountByPlatformCondition(condition, pars)
  259. if err != nil {
  260. return
  261. }
  262. if total > 0 {
  263. items, err = m.GetListByPlatformCondition(`a.wechat_article_id,a.wechat_platform_id,a.fake_id,a.title,a.link,a.cover_url,a.description,a.country,a.province,a.city,a.article_create_time,a.modify_time,a.create_time,b.nickname,b.round_head_img`, condition, pars, startSize, pageSize)
  264. }
  265. return
  266. }