article_history_record_newpv.go 12 KB

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