article_history_record_newpv.go 8.5 KB

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