article_history_record_newpv.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_cygx/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. }
  23. // 添加阅读记录信息
  24. func AddCygxArticleViewRecordNewpv(item *CygxArticleHistoryRecordNewpv) (lastId int64, err error) {
  25. o, err := orm.NewOrm().Begin()
  26. if err != nil {
  27. return
  28. }
  29. defer func() {
  30. fmt.Println(err)
  31. if err == nil {
  32. o.Commit()
  33. } else {
  34. o.Rollback()
  35. }
  36. }()
  37. lastId, err = o.Insert(item)
  38. //写入记录到总的统计表
  39. record := new(CygxArticleHistoryRecordAll)
  40. record.UserId = item.UserId
  41. record.ArticleId = item.ArticleId
  42. record.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  43. record.ModifyTime = item.ModifyTime
  44. record.Mobile = item.Mobile
  45. record.Email = item.Email
  46. record.CompanyId = item.CompanyId
  47. record.CompanyName = item.CompanyName
  48. record.StopTime = item.StopTime
  49. record.OutType = item.OutType
  50. record.Source = item.Source
  51. record.Platfor = 1
  52. lastId, err = o.Insert(record)
  53. // 软删除当天策略平台的文章阅读记录
  54. if item.Mobile != "" {
  55. sql := `UPDATE cygx_article_history_record_all
  56. SET is_del = 1
  57. WHERE
  58. article_id = ?
  59. AND mobile = ?
  60. AND platfor = 2
  61. AND create_time >= date(NOW()) `
  62. _, err = o.Raw(sql, record.ArticleId, record.Mobile).Exec()
  63. }
  64. return
  65. }
  66. // 获取最新的一条阅读记录
  67. func GetNewArticleHistoryRecordNewpv(uid, articleId int, modifytime string) (item *AddStopTimeNewRep, err error) {
  68. o := orm.NewOrm()
  69. sql := `SELECT * FROM cygx_article_history_record_newpv WHERE user_id = ? AND article_id = ? AND modify_time <='` + modifytime + `' ORDER BY id DESC LIMIT 1;`
  70. err = o.Raw(sql, uid, articleId).QueryRow(&item)
  71. return
  72. }
  73. // 把十分钟之内的阅读记录进行累加
  74. func UpdateCygxArticleViewRecordNewpv(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  75. o, err := orm.NewOrm().Begin()
  76. if err != nil {
  77. return
  78. }
  79. defer func() {
  80. fmt.Println(err)
  81. if err == nil {
  82. o.Commit()
  83. } else {
  84. o.Rollback()
  85. }
  86. }()
  87. sql := `UPDATE cygx_article_history_record_newpv
  88. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  89. WHERE
  90. article_id = ?
  91. AND user_id = ?
  92. AND out_type = 2
  93. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  94. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  95. // 修改总表的停留时间
  96. sql = `UPDATE cygx_article_history_record_all
  97. SET modify_time = NOW(), stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  98. WHERE
  99. article_id = ?
  100. AND user_id = ?
  101. AND out_type = 2
  102. AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
  103. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId).Exec()
  104. return
  105. }
  106. // 把十分钟之内的阅读记录进行累加
  107. func UpdateCygxArticleViewRecordNewpvList(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
  108. o := orm.NewOrm()
  109. sql := `UPDATE cygx_article_history_record_newpv
  110. SET stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
  111. WHERE
  112. article_id = ?
  113. AND user_id = ?
  114. AND modify_time = ?
  115. AND id = ?`
  116. _, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId, itemRep.ModifyTime, itemRep.Id).Exec()
  117. return
  118. }
  119. type CygxArticleHistoryRecordAll struct {
  120. Id int `orm:"column(id);pk"`
  121. ArticleId int
  122. UserId int
  123. CreateTime string
  124. ModifyTime time.Time
  125. Mobile string `description:"手机号"`
  126. Email string `description:"邮箱"`
  127. CompanyId int `description:"公司id"`
  128. CompanyName string `description:"公司名称"`
  129. StopTime int `description:"停留时间"`
  130. OutType int `description:"退出方式,1正常退出,2强制关闭"`
  131. Source string `description:"来源,MOBILE:手机端,PC:电脑端"`
  132. RealName string `description:"用户实际名称"`
  133. CreateDateApi time.Time `description:"同步创建时间"`
  134. CelueHistoryId int `description:"策略平台记录的ID"`
  135. Platfor int `description:"PV阅读记录来源,1:查研观向,2:策略平台"`
  136. IsDel int `description:"是否删除"`
  137. }
  138. // 获取当天总表的阅读记录
  139. func GetArticleHistoryRecordAllList() (items []*CygxArticleHistoryRecordNewpv, err error) {
  140. o := orm.NewOrm()
  141. sql := ` SELECT * FROM cygx_article_history_record_all WHERE create_time >= date(NOW())
  142. AND mobile <> ''
  143. AND platfor = 1
  144. GROUP BY mobile,article_id `
  145. _, err = o.Raw(sql).QueryRows(&items)
  146. return
  147. }
  148. // 获取列表信息根据手机号分组
  149. func GetArticleHistoryRecordAllByMobileList(condition string) (items []*CygxArticleHistoryRecordAll, err error) {
  150. o := orm.NewOrm()
  151. sql := `SELECT * FROM cygx_article_history_record_all WHERE 1 = 1 ` + condition + ` GROUP BY mobile `
  152. _, err = o.Raw(sql).QueryRows(&items)
  153. return
  154. }
  155. // 修改用户阅读的相关信息
  156. func UpdateCygxArticleHistoryRecordAll(wxUser *WxUserItem) (err error) {
  157. o := orm.NewOrm()
  158. var sql string
  159. if wxUser.Mobile != "" {
  160. sql = `UPDATE cygx_article_history_record_all SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  161. _, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
  162. } else if wxUser.Email != "" {
  163. sql = `UPDATE cygx_article_history_record_all SET mobile=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE email=? `
  164. _, err = o.Raw(sql, wxUser.Mobile, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Email).Exec()
  165. }
  166. return
  167. }
  168. type EsUserInteraction struct {
  169. Id int `description:"主键ID"`
  170. ArticleId int `description:"文章id"`
  171. ArticleType int `description:"文章类型 1:查研观向, 2:策略平台"`
  172. Title string `description:"标题"`
  173. PublishDate string `description:"发布时间"`
  174. CreateTime string `description:"创建时间"`
  175. StopTime string `description:"阅读停留时间"`
  176. RealName string `description:"姓名"`
  177. CompanyName string `description:"公司名称"`
  178. CompanyId int `description:"公司ID"`
  179. SellerName string `description:"所属销售"`
  180. SellerId int `description:"所属销售ID"`
  181. Mobile string `description:"手机号"`
  182. Email string `description:"邮箱"`
  183. UserId int `description:"用户ID"`
  184. UserArticleHistoryNum int `description:"用户阅读数量"`
  185. CompanyArticleHistoryNum int `description:"机构阅读数量"`
  186. }
  187. // 机构阅读记录列表
  188. func GetCygxArticleHistoryRecordByCompanyList(condition string, startSize, pageSize int) (items []*EsUserInteraction, err error) {
  189. o := orm.NewOrm()
  190. sql := ` SELECT
  191. r.id,
  192. art.title,
  193. art.article_id,
  194. art.article_id_md5,
  195. art.publish_date,
  196. art.category_name,
  197. r.create_time,
  198. r.mobile,
  199. r.user_id,
  200. r.company_name,
  201. cp.seller_name,
  202. cp.seller_id,
  203. cp.company_id,
  204. r.real_name,
  205. r.stop_time,
  206. ci.article_history_num AS company_article_history_num,
  207. ui.article_history_num AS user_article_history_num
  208. FROM
  209. cygx_article_history_record_all AS r
  210. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  211. INNER JOIN company_product AS cp ON cp.company_id = r.company_id
  212. AND cp.product_id = 2
  213. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  214. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  215. WHERE
  216. 1 = 1
  217. AND r.is_del = 0 ` + condition + ` GROUP BY r.id `
  218. if startSize > 0 || pageSize > 0 {
  219. sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
  220. }
  221. _, err = o.Raw(sql).QueryRows(&items)
  222. return
  223. }
  224. // 获取阅读记录数量
  225. func GetCygxArticleHistoryCountByCompany(condition string) (count int, err error) {
  226. o := orm.NewOrm()
  227. sqlCount := `SELECT
  228. COUNT( 1 ) AS count
  229. FROM
  230. (
  231. SELECT
  232. COUNT( 1 )
  233. FROM
  234. cygx_article_history_record_all AS r
  235. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  236. INNER JOIN company_product AS cp ON cp.company_id = r.company_id
  237. AND cp.product_id = 2
  238. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  239. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  240. WHERE
  241. r.is_del = 0 ` + condition + `
  242. GROUP BY
  243. r.id
  244. ) AS count `
  245. err = o.Raw(sqlCount).QueryRow(&count)
  246. return
  247. }
  248. type CygxArticleHistoryAllTopResp struct {
  249. Pv int `description:"阅读PV"`
  250. ArticleId int `description:"文章id"`
  251. Num int `description:"数量"`
  252. }
  253. // 获取近15天之内的阅读数据最多的15报告
  254. func GetCygxArticleHistoryAllTop(pars []interface{}, condition string) (items []*CygxArticleHistoryAllTopResp, err error) {
  255. o := orm.NewOrm()
  256. sql := ` SELECT
  257. COUNT( 1 ) AS pv,
  258. a.article_id
  259. FROM
  260. cygx_article_history_record_all AS l
  261. INNER JOIN cygx_article AS a ON a.article_id = l.article_id
  262. INNER JOIN cygx_report_mapping AS m ON m.category_id = a.category_id
  263. WHERE
  264. 1 = 1
  265. AND l.platfor = 1` + condition + `
  266. GROUP BY
  267. l.article_id
  268. ORDER BY
  269. pv DESC ,a.publish_date DESC
  270. LIMIT 15 `
  271. _, err = o.Raw(sql, pars).QueryRows(&items)
  272. return
  273. }
  274. // 列表
  275. func GetCygxArticleHistoryRecordNewpvList(condition string, pars []interface{}) (items []*CygxArticleHistoryRecordNewpv, err error) {
  276. o := orm.NewOrm()
  277. sql := `SELECT * FROM cygx_article_history_record_newpv as art WHERE 1= 1 `
  278. if condition != "" {
  279. sql += condition
  280. }
  281. _, err = o.Raw(sql, pars).QueryRows(&items)
  282. return
  283. }