article_history_record_newpv.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_clpt/utils"
  6. "strconv"
  7. "time"
  8. )
  9. type CygxArticleHistoryRecordNewpv struct {
  10. Id int `orm:"column(id);pk"`
  11. ArticleId int
  12. UserId int
  13. CreateTime time.Time
  14. ModifyTime time.Time
  15. Mobile string `description:"手机号"`
  16. Email string `description:"邮箱"`
  17. CompanyId int `description:"公司id"`
  18. CompanyName string `description:"公司名称"`
  19. StopTime int `description:"停留时间"`
  20. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  21. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  22. RegisterPlatform int `description:"来源 1小程序,2:网页"`
  23. }
  24. type CygxArticleHistoryRecord struct {
  25. Id int `orm:"column(id);pk"`
  26. ArticleId int
  27. UserId int
  28. CreateTime time.Time
  29. Mobile string `description:"手机号"`
  30. Email string `description:"邮箱"`
  31. CompanyId int `description:"公司id"`
  32. CompanyName string `description:"公司名称"`
  33. ModifyTime time.Time `description:"修改时间"`
  34. StopTime int `description:"停留时间"`
  35. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  36. RegisterPlatform int `description:"来源 1小程序,2:网页"`
  37. }
  38. // 添加阅读记录信息
  39. func AddCygxArticleViewRecordNewpv(item *CygxArticleHistoryRecordNewpv) (lastId int64, err error) {
  40. o, err := orm.NewOrm().Begin()
  41. if err != nil {
  42. return
  43. }
  44. defer func() {
  45. fmt.Println(err)
  46. if err == nil {
  47. o.Commit()
  48. } else {
  49. o.Rollback()
  50. }
  51. }()
  52. lastId, err = o.Insert(item)
  53. if err != nil {
  54. return
  55. }
  56. historyRecord := new(CygxArticleHistoryRecord)
  57. historyRecord.UserId = item.UserId
  58. historyRecord.ArticleId = item.ArticleId
  59. historyRecord.CreateTime = time.Now()
  60. historyRecord.Mobile = item.Mobile
  61. historyRecord.Email = item.Email
  62. historyRecord.CompanyId = item.CompanyId
  63. historyRecord.CompanyName = item.CompanyName
  64. lastId, err = o.Insert(historyRecord)
  65. if err != nil {
  66. return
  67. }
  68. //写入记录到总的统计表
  69. record := new(CygxArticleHistoryRecordAll)
  70. record.UserId = item.UserId
  71. record.ArticleId = item.ArticleId
  72. record.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  73. record.ModifyTime = item.ModifyTime
  74. record.Mobile = item.Mobile
  75. record.Email = item.Email
  76. record.CompanyId = item.CompanyId
  77. record.CompanyName = item.CompanyName
  78. record.StopTime = item.StopTime
  79. record.OutType = item.OutType
  80. record.Source = item.Source
  81. record.Platfor = 1
  82. lastId, err = o.Insert(record)
  83. if err != nil {
  84. return
  85. }
  86. // 软删除当天策略平台的文章阅读记录
  87. if item.Mobile != "" {
  88. sql := `UPDATE cygx_article_history_record_all
  89. SET is_del = 1
  90. WHERE
  91. article_id = ?
  92. AND mobile = ?
  93. AND platfor = 2
  94. AND create_time >= date(NOW()) `
  95. _, err = o.Raw(sql, record.ArticleId, record.Mobile).Exec()
  96. }
  97. return
  98. }
  99. type CygxArticleHistoryRecordAll struct {
  100. Id int `orm:"column(id);pk"`
  101. ArticleId int
  102. UserId int
  103. CreateTime string
  104. ModifyTime time.Time
  105. Mobile string `description:"手机号"`
  106. Email string `description:"邮箱"`
  107. CompanyId int `description:"公司id"`
  108. CompanyName string `description:"公司名称"`
  109. StopTime int `description:"停留时间"`
  110. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  111. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  112. RealName string `description:"用户实际名称"`
  113. CreateDateApi time.Time `description:"同步创建时间"`
  114. CelueHistoryId int `description:"策略平台记录的ID"`
  115. Platfor int `description:"PV阅读记录来源,1:查研观向,2:策略平台"`
  116. IsDel int `description:"是否删除"`
  117. RegisterPlatform int `description:"来源 1小程序,2:网页"`
  118. }
  119. type EsUserInteraction struct {
  120. Id int `description:"主键ID"`
  121. ArticleId int `description:"文章id"`
  122. ArticleType int `description:"文章类型 1:查研观向, 2:策略平台"`
  123. Title string `description:"标题"`
  124. PublishDate string `description:"发布时间"`
  125. CreateTime string `description:"创建时间"`
  126. StopTime string `description:"阅读停留时间"`
  127. RealName string `description:"姓名"`
  128. CompanyName string `description:"公司名称"`
  129. CompanyId int `description:"公司ID"`
  130. SellerName string `description:"所属销售"`
  131. SellerId int `description:"所属销售ID"`
  132. Mobile string `description:"手机号"`
  133. Email string `description:"邮箱"`
  134. UserId int `description:"用户ID"`
  135. UserArticleHistoryNum int `description:"用户阅读数量"`
  136. CompanyArticleHistoryNum int `description:"机构阅读数量"`
  137. }
  138. // GetArticleHistoryRecord 或用户阅读过的文章ID
  139. func GetArticleHistoryRecord(condition string, pars []interface{}) (item []*CygxArticleHistoryRecord, err error) {
  140. if condition == "" {
  141. return
  142. }
  143. o := orm.NewOrm()
  144. sql := `SELECT *
  145. FROM
  146. cygx_article_history_record
  147. WHERE 1 = 1 ` + condition + ` GROUP BY article_id`
  148. _, err = o.Raw(sql, pars).QueryRows(&item)
  149. return
  150. }
  151. // 把十分钟之内的阅读记录进行累加
  152. func UpdateCygxArticleViewRecordNewpv(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  153. o, err := orm.NewOrm().Begin()
  154. if err != nil {
  155. return
  156. }
  157. defer func() {
  158. fmt.Println(err)
  159. if err == nil {
  160. o.Commit()
  161. } else {
  162. o.Rollback()
  163. }
  164. }()
  165. sql := `UPDATE cygx_article_history_record_newpv
  166. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  167. WHERE
  168. article_id = ?
  169. AND user_id = ?
  170. AND out_type = 2
  171. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  172. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  173. // 修改总表的停留时间
  174. sql = `UPDATE cygx_article_history_record_all
  175. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  176. WHERE
  177. article_id = ?
  178. AND user_id = ?
  179. AND out_type = 2
  180. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  181. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  182. return
  183. }
  184. // 列表
  185. func GetCygxArticleHistoryRecordNewpvList(condition string, pars []interface{}) (items []*CygxArticleHistoryRecordNewpv, err error) {
  186. o := orm.NewOrm()
  187. sql := `SELECT * FROM cygx_article_history_record_newpv as art WHERE 1= 1 `
  188. if condition != "" {
  189. sql += condition
  190. }
  191. _, err = o.Raw(sql, pars).QueryRows(&items)
  192. return
  193. }
  194. type ListPvUvResp struct {
  195. ArticleId int `description:"文章ID"`
  196. Pv int `description:"pv"`
  197. Uv int `description:"pv"`
  198. }
  199. // 列表
  200. func GetCygxArticleHistoryRecordNewpvListPv(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  201. o := orm.NewOrm()
  202. sql := `SELECT
  203. COUNT( 1 ) AS pv,
  204. article_id
  205. FROM
  206. cygx_article_history_record_newpv WHERE 1 = 1 `
  207. if condition != "" {
  208. sql += condition
  209. }
  210. sql += ` GROUP BY article_id `
  211. _, err = o.Raw(sql, pars).QueryRows(&items)
  212. return
  213. }
  214. // 查研PV列表
  215. func GetCygxArticleHistoryRecordNewpvListPvCy(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  216. o := orm.NewOrm()
  217. sql := `SELECT
  218. COUNT( 1 ) AS pv,
  219. article_id
  220. FROM
  221. cygx_article_history_record_all WHERE 1 = 1 AND platfor = 1 `
  222. if condition != "" {
  223. sql += condition
  224. }
  225. sql += ` GROUP BY article_id `
  226. _, err = o.Raw(sql, pars).QueryRows(&items)
  227. return
  228. }
  229. // 策略PV列表
  230. func GetCygxArticleHistoryRecordNewpvListPvCl(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  231. o := orm.NewOrm()
  232. sql := `SELECT
  233. COUNT( 1 ) AS pv,
  234. article_id
  235. FROM
  236. cygx_article_history_record_all WHERE 1 = 1 AND platfor = 2 AND is_del = 0 `
  237. if condition != "" {
  238. sql += condition
  239. }
  240. sql += ` GROUP BY article_id `
  241. _, err = o.Raw(sql, pars).QueryRows(&items)
  242. return
  243. }