article_history_record_newpv.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // 获取当天总表的阅读记录
  120. func GetArticleHistoryRecordAllList() (items []*CygxArticleHistoryRecordNewpv, err error) {
  121. o := orm.NewOrm()
  122. sql := ` SELECT * FROM cygx_article_history_record_all WHERE create_time >= date(NOW())
  123. AND mobile <> ''
  124. AND platfor = 1
  125. GROUP BY mobile,article_id `
  126. _, err = o.Raw(sql).QueryRows(&items)
  127. return
  128. }
  129. // 获取列表信息根据手机号分组
  130. func GetArticleHistoryRecordAllByMobileList(condition string) (items []*CygxArticleHistoryRecordAll, err error) {
  131. o := orm.NewOrm()
  132. sql := `SELECT * FROM cygx_article_history_record_all WHERE 1 = 1 ` + condition + ` GROUP BY mobile `
  133. _, err = o.Raw(sql).QueryRows(&items)
  134. return
  135. }
  136. // 修改用户阅读的相关信息
  137. func UpdateCygxArticleHistoryRecordAll(wxUser *WxUserItem) (err error) {
  138. o := orm.NewOrm()
  139. var sql string
  140. if wxUser.Mobile != "" {
  141. sql = `UPDATE cygx_article_history_record_all SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  142. _, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
  143. } else if wxUser.Email != "" {
  144. sql = `UPDATE cygx_article_history_record_all SET mobile=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE email=? `
  145. _, err = o.Raw(sql, wxUser.Mobile, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Email).Exec()
  146. }
  147. return
  148. }
  149. type EsUserInteraction struct {
  150. Id int `description:"主键ID"`
  151. ArticleId int `description:"文章id"`
  152. ArticleType int `description:"文章类型 1:查研观向, 2:策略平台"`
  153. Title string `description:"标题"`
  154. PublishDate string `description:"发布时间"`
  155. CreateTime string `description:"创建时间"`
  156. StopTime string `description:"阅读停留时间"`
  157. RealName string `description:"姓名"`
  158. CompanyName string `description:"公司名称"`
  159. CompanyId int `description:"公司ID"`
  160. SellerName string `description:"所属销售"`
  161. SellerId int `description:"所属销售ID"`
  162. Mobile string `description:"手机号"`
  163. Email string `description:"邮箱"`
  164. UserId int `description:"用户ID"`
  165. UserArticleHistoryNum int `description:"用户阅读数量"`
  166. CompanyArticleHistoryNum int `description:"机构阅读数量"`
  167. }
  168. // 机构阅读记录列表
  169. func GetCygxArticleHistoryRecordByCompanyList(condition string, startSize, pageSize int) (items []*EsUserInteraction, err error) {
  170. o := orm.NewOrm()
  171. sql := ` SELECT
  172. r.id,
  173. art.title,
  174. art.article_id,
  175. art.article_id_md5,
  176. art.publish_date,
  177. art.category_name,
  178. r.create_time,
  179. r.mobile,
  180. r.user_id,
  181. r.company_name,
  182. cp.seller_name,
  183. cp.seller_id,
  184. cp.company_id,
  185. r.real_name,
  186. r.stop_time,
  187. ci.article_history_num AS company_article_history_num,
  188. ui.article_history_num AS user_article_history_num
  189. FROM
  190. cygx_article_history_record_all AS r
  191. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  192. INNER JOIN company_product AS cp ON cp.company_id = r.company_id
  193. AND cp.product_id = 2
  194. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  195. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  196. WHERE
  197. 1 = 1
  198. AND r.is_del = 0 ` + condition + ` GROUP BY r.id `
  199. if startSize > 0 || pageSize > 0 {
  200. sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
  201. }
  202. _, err = o.Raw(sql).QueryRows(&items)
  203. return
  204. }
  205. // 机构阅读记录列表
  206. func GetCygxArticleHistoryRecordByCompanyListNew(condition string, pars interface{}, startSize, pageSize int) (items []*EsUserInteraction, err error) {
  207. o := orm.NewOrm()
  208. sql := ` SELECT
  209. r.id,
  210. art.title,
  211. art.article_id,
  212. art.article_id_md5,
  213. art.publish_date,
  214. art.category_name,
  215. r.create_time,
  216. r.mobile,
  217. r.user_id,
  218. r.company_name,
  219. r.company_id,
  220. r.real_name,
  221. r.stop_time,
  222. ci.article_history_num AS company_article_history_num,
  223. ui.article_history_num AS user_article_history_num
  224. FROM
  225. cygx_article_history_record_all AS r
  226. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  227. LEFT JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  228. LEFT JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  229. WHERE
  230. 1 = 1
  231. AND r.is_del = 0 ` + condition + ` GROUP BY r.id `
  232. if startSize > 0 || pageSize > 0 {
  233. sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
  234. }
  235. _, err = o.Raw(sql, pars).QueryRows(&items)
  236. return
  237. }
  238. // 获取阅读记录数量
  239. func GetCygxArticleHistoryCountByCompany(condition string) (count int, err error) {
  240. o := orm.NewOrm()
  241. sqlCount := `SELECT
  242. COUNT( 1 ) AS count
  243. FROM
  244. (
  245. SELECT
  246. COUNT( 1 )
  247. FROM
  248. cygx_article_history_record_all AS r
  249. INNER JOIN cygx_article AS art ON art.article_id = r.article_id
  250. INNER JOIN company_product AS cp ON cp.company_id = r.company_id
  251. AND cp.product_id = 2
  252. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  253. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  254. WHERE
  255. r.is_del = 0 ` + condition + `
  256. GROUP BY
  257. r.id
  258. ) AS count `
  259. err = o.Raw(sqlCount).QueryRow(&count)
  260. return
  261. }
  262. type CygxArticleHistoryAllTopResp struct {
  263. Pv int `description:"阅读PV"`
  264. ArticleId int `description:"文章id"`
  265. Num int `description:"数量"`
  266. }
  267. // 获取近15天之内的阅读数据最多的15报告
  268. func GetCygxArticleHistoryAllTop(pars []interface{}, condition string) (items []*CygxArticleHistoryAllTopResp, err error) {
  269. o := orm.NewOrm()
  270. sql := ` SELECT
  271. COUNT( 1 ) AS pv,
  272. a.article_id
  273. FROM
  274. cygx_article_history_record_all AS l
  275. INNER JOIN cygx_article AS a ON a.article_id = l.article_id
  276. INNER JOIN cygx_report_mapping AS m ON m.category_id = a.category_id
  277. WHERE
  278. 1 = 1
  279. AND l.platfor = 1` + condition + `
  280. GROUP BY
  281. l.article_id
  282. ORDER BY
  283. pv DESC ,a.publish_date DESC
  284. LIMIT 15 `
  285. _, err = o.Raw(sql, pars).QueryRows(&items)
  286. return
  287. }
  288. // 列表
  289. func GetCygxArticleHistoryRecordNewpvList(condition string, pars []interface{}) (items []*CygxArticleHistoryRecordNewpv, err error) {
  290. o := orm.NewOrm()
  291. sql := `SELECT * FROM cygx_article_history_record_newpv as art WHERE 1= 1 `
  292. if condition != "" {
  293. sql += condition
  294. }
  295. _, err = o.Raw(sql, pars).QueryRows(&items)
  296. return
  297. }
  298. type ListPvUvResp struct {
  299. ArticleId int `description:"文章ID"`
  300. Pv int `description:"pv"`
  301. Uv int `description:"pv"`
  302. }
  303. // 列表
  304. func GetCygxArticleHistoryRecordNewpvListPv(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  305. o := orm.NewOrm()
  306. sql := `SELECT
  307. COUNT( 1 ) AS pv,
  308. article_id
  309. FROM
  310. cygx_article_history_record_newpv WHERE 1 = 1 `
  311. if condition != "" {
  312. sql += condition
  313. }
  314. sql += ` GROUP BY article_id `
  315. _, err = o.Raw(sql, pars).QueryRows(&items)
  316. return
  317. }
  318. // 查研PV列表
  319. func GetCygxArticleHistoryRecordNewpvListPvCy(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  320. o := orm.NewOrm()
  321. sql := `SELECT
  322. COUNT( 1 ) AS pv,
  323. article_id
  324. FROM
  325. cygx_article_history_record_all WHERE 1 = 1 AND platfor = 1 `
  326. if condition != "" {
  327. sql += condition
  328. }
  329. sql += ` GROUP BY article_id `
  330. _, err = o.Raw(sql, pars).QueryRows(&items)
  331. return
  332. }
  333. // 策略PV列表
  334. func GetCygxArticleHistoryRecordNewpvListPvCl(condition string, pars []interface{}) (items []*ListPvUvResp, err error) {
  335. o := orm.NewOrm()
  336. sql := `SELECT
  337. COUNT( 1 ) AS pv,
  338. article_id
  339. FROM
  340. cygx_article_history_record_all WHERE 1 = 1 AND platfor = 2 AND is_del = 0 `
  341. if condition != "" {
  342. sql += condition
  343. }
  344. sql += ` GROUP BY article_id `
  345. _, err = o.Raw(sql, pars).QueryRows(&items)
  346. return
  347. }
  348. // 获取数量
  349. func GetCygxArticleHistoryRecordAllCount(condition string, pars []interface{}) (count int, err error) {
  350. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_article_history_record_all WHERE 1= 1 `
  351. if condition != "" {
  352. sqlCount += condition
  353. }
  354. o := orm.NewOrm()
  355. err = o.Raw(sqlCount, pars).QueryRow(&count)
  356. return
  357. }