user_view_history.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_ETA_mobile_api/utils"
  5. "time"
  6. )
  7. type UserViewHistory struct {
  8. ViewHistoryId int `orm:"column(id);pk"`
  9. UserId int `description:"用户id"`
  10. Mobile string `description:"手机号"`
  11. Email string `description:"邮箱"`
  12. RealName string `description:"用户实际姓名"`
  13. CompanyName string `description:"公司名称"`
  14. ViewTitle string `description:"访问标题"`
  15. ViewPage string `description:"访问页面"`
  16. ReportChapterModule string `description:"访问核心观点或者图文逻辑"`
  17. CreatedTime string `description:"创建时间"`
  18. LastUpdatedTime time.Time `description:"访问历史类型,weekly_report 周报,pdf;默认值:weekly_report"`
  19. ResearchReportId int `description:"研报id"`
  20. ResearchReportTypeId int `description:"报告章节id,为0时表示查看目录或者首页"`
  21. }
  22. //根据用户id字符串获取用户的浏览数
  23. type UserViewTotalSlice struct {
  24. UserId int `description:"用户id"`
  25. Total int `description:"总阅读数"`
  26. CreatedTime time.Time `description:"用户浏览时间"`
  27. }
  28. func GetCountUserViewHistoryByUserIds(userIds string) (items []*UserViewTotalSlice, err error) {
  29. o := orm.NewOrm()
  30. sql := `SELECT count(1) total,user_id,max(created_time) as created_time FROM user_view_history WHERE user_id in (` + userIds + `) group by user_id`
  31. _, err = o.Raw(sql).QueryRows(&items)
  32. return
  33. //return items2,err
  34. }
  35. //根据用户手机号字符串获取用户的浏览数
  36. type UserViewMobileTotalSlice struct {
  37. Mobile string `description:"用户手机号"`
  38. Total int `description:"总阅读数"`
  39. CreatedTime time.Time `description:"用户浏览时间"`
  40. }
  41. func GetCountUserViewHistoryByMobiles(mobiles string) (items []*UserViewMobileTotalSlice, err error) {
  42. o := orm.NewOrm()
  43. sql := `SELECT count(1) total,mobile,max(created_time) as created_time FROM user_view_history WHERE mobile in (` + mobiles + `) group by mobile`
  44. _, err = o.Raw(sql).QueryRows(&items)
  45. return
  46. }
  47. //根据用户id字符串获取用户的浏览数
  48. type UserViewEmailTotalSlice struct {
  49. Email string `description:"用户邮箱"`
  50. Total int `description:"总阅读数"`
  51. CreatedTime time.Time `description:"用户浏览时间"`
  52. }
  53. func GetCountUserViewHistoryByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
  54. o := orm.NewOrm()
  55. sql := `SELECT count(1) total,email,max(created_time) as created_time FROM user_view_history WHERE email in (` + emails + `) group by email`
  56. _, err = o.Raw(sql).QueryRows(&items)
  57. return
  58. //return items2,err
  59. }
  60. func GetCountCygxArticleHistoryRecordByMobiles(mobiles string) (items []*UserViewMobileTotalSlice, err error) {
  61. o := orm.NewOrm()
  62. sql := `SELECT count(1) total,h.mobile,max(h.create_time) as created_time FROM cygx_article_history_record_all AS h INNER JOIN cygx_article AS art ON art.article_id = h.article_id WHERE h.mobile in (` + mobiles + `) AND h.is_del = 0 AND h.company_id != 16 group by h.mobile`
  63. _, err = o.Raw(sql).QueryRows(&items)
  64. return
  65. }
  66. func GetCountCygxArticleHistoryRecordByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
  67. o := orm.NewOrm()
  68. sql := `SELECT count(1) total,h.email,max(h.create_time) as created_time FROM cygx_article_history_record_all AS h INNER JOIN cygx_article AS art ON art.article_id = h.article_id WHERE h.email in (` + emails + `) AND h.is_del = 0 AND h.company_id != 16 group by email`
  69. _, err = o.Raw(sql).QueryRows(&items)
  70. return
  71. }
  72. // CompanyLastViewSlice 根据手机号获取客户的最新浏览时间
  73. type CompanyLastViewSlice struct {
  74. CompanyId int `description:"客户id"`
  75. ViewTime time.Time `description:"用户浏览时间"`
  76. }
  77. // GetLastUserViewHistoryByCompanyIdsMobile 根据手机号获取客户的最新浏览时间
  78. func GetLastUserViewHistoryByCompanyIdsMobile(companyIds string) (items []*CompanyLastViewSlice, err error) {
  79. today := time.Now().Format(utils.FormatDate)
  80. o := orm.NewOrm()
  81. sql := `SELECT
  82. a.company_id ,max(b.created_time) view_time
  83. FROM
  84. wx_user a
  85. JOIN user_view_history b ON a.mobile = b.mobile
  86. WHERE
  87. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" and b.created_time>=? GROUP BY company_id`
  88. _, err = o.Raw(sql, today).QueryRows(&items)
  89. return
  90. }
  91. // GetLastUserViewHistoryByCompanyIdsEmail 根据邮箱获取客户的最新浏览时间
  92. func GetLastUserViewHistoryByCompanyIdsEmail(companyIds string) (items []*CompanyLastViewSlice, err error) {
  93. today := time.Now().Format(utils.FormatDate)
  94. o := orm.NewOrm()
  95. // sql := `SELECT
  96. // a.company_id ,max(b.created_time) view_time
  97. //FROM
  98. // wx_user a
  99. // JOIN user_view_history b ON a.email = b.email
  100. //WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" and b.created_time>=? GROUP BY company_id`
  101. sql := `SELECT
  102. a.company_id ,max(b.created_time) view_time FROM wx_user a
  103. JOIN user_view_history b ON a.email = b.email
  104. WHERE b.email !="" and b.mobile="" and b.created_time>=? GROUP BY company_id`
  105. _, err = o.Raw(sql, today).QueryRows(&items)
  106. return
  107. }
  108. // GetLastAdvisoryArticleViewRecordByCompanyIdsMobile 根据手机号获取客户的最新浏览时间
  109. func GetLastAdvisoryArticleViewRecordByCompanyIdsMobile(companyIds string) (items []*CompanyLastViewSlice, err error) {
  110. today := time.Now().Format(utils.FormatDate)
  111. o := orm.NewOrm()
  112. sql := `SELECT
  113. a.company_id ,max(b.create_time) view_time
  114. FROM
  115. wx_user a
  116. JOIN advisory_user_chart_article_record b ON a.mobile = b.mobile
  117. WHERE
  118. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" and b.create_time>=? GROUP BY company_id`
  119. _, err = o.Raw(sql, today).QueryRows(&items)
  120. return
  121. }
  122. // GetLastAdvisoryArticleViewRecordByCompanyIdsEmail 根据邮箱获取客户的最新浏览时间
  123. func GetLastAdvisoryArticleViewRecordByCompanyIdsEmail(companyIds string) (items []*CompanyLastViewSlice, err error) {
  124. today := time.Now().Format(utils.FormatDate)
  125. o := orm.NewOrm()
  126. sql := `SELECT
  127. a.company_id ,max(b.create_time) view_time
  128. FROM
  129. wx_user a
  130. JOIN advisory_user_chart_article_record b ON a.email = b.email
  131. WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" and b.create_time>=? GROUP BY company_id`
  132. _, err = o.Raw(sql, today).QueryRows(&items)
  133. return
  134. }
  135. // GetLastCygxArticleViewRecordByCompanyIdsMobile 根据手机号获取客户的最新浏览时间
  136. func GetLastCygxArticleViewRecordByCompanyIdsMobile(companyIds string) (items []*CompanyLastViewSlice, err error) {
  137. o := orm.NewOrm()
  138. //dataName := ""
  139. //if utils.RunMode == "debug" {
  140. // dataName = "test_v2_hongze_rddp"
  141. //} else {
  142. // dataName = "hongze_rddp"
  143. //}
  144. sql := `SELECT
  145. a.company_id ,max(b.create_time) view_time
  146. FROM
  147. wx_user a
  148. JOIN cygx_article_history_record_newpv b ON a.mobile = b.mobile
  149. WHERE
  150. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" GROUP BY company_id`
  151. _, err = o.Raw(sql).QueryRows(&items)
  152. return
  153. }
  154. // GetLastCygxArticleViewRecordByCompanyIdsEmail 根据邮箱获取客户的最新浏览时间
  155. func GetLastCygxArticleViewRecordByCompanyIdsEmail(companyIds string) (items []*CompanyLastViewSlice, err error) {
  156. o := orm.NewOrm()
  157. sql := `SELECT
  158. a.company_id ,max(b.create_time) view_time
  159. FROM
  160. wx_user a
  161. JOIN cygx_article_history_record_newpv b ON a.email = b.email
  162. WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" GROUP BY company_id`
  163. _, err = o.Raw(sql).QueryRows(&items)
  164. return
  165. }
  166. // GetLastReportViewRecordByCompanyIdsMobile 根据手机号获取客户的最新浏览时间
  167. func GetLastReportViewRecordByCompanyIdsMobile(companyIds string) (items []*CompanyLastViewSlice, err error) {
  168. today := time.Now().Format(utils.FormatDate)
  169. o := orm.NewOrm()
  170. dataName := ""
  171. if utils.RunMode == "debug" {
  172. dataName = "test_v2_hongze_rddp"
  173. } else {
  174. dataName = "hongze_rddp"
  175. }
  176. sql := `SELECT
  177. a.company_id ,max(b.create_time) view_time
  178. FROM
  179. wx_user a
  180. JOIN ` + dataName + `.report_view_record b ON a.mobile = b.mobile
  181. WHERE
  182. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" and b.create_time>=? GROUP BY company_id`
  183. _, err = o.Raw(sql, today).QueryRows(&items)
  184. return
  185. }
  186. // GetLastReportViewRecordByCompanyIdsEmail 根据邮箱获取客户的最新浏览时间
  187. func GetLastReportViewRecordByCompanyIdsEmail(companyIds string) (items []*CompanyLastViewSlice, err error) {
  188. today := time.Now().Format(utils.FormatDate)
  189. o := orm.NewOrm()
  190. dataName := ""
  191. if utils.RunMode == "debug" {
  192. dataName = "test_v2_hongze_rddp"
  193. } else {
  194. dataName = "hongze_rddp"
  195. }
  196. sql := `SELECT a.company_id ,max(b.create_time) view_time FROM wx_user a
  197. JOIN ` + dataName + `.report_view_record b ON a.email = b.email
  198. WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" and b.create_time>=? GROUP BY company_id`
  199. _, err = o.Raw(sql, today).QueryRows(&items)
  200. return
  201. }
  202. // GetLastUserViewStatisticsByCompanyIdsMobile 根据手机号获取客户的最新浏览时间
  203. func GetLastUserViewStatisticsByCompanyIdsMobile(companyIds string) (items []*CompanyLastViewSlice, err error) {
  204. today := time.Now().Format(utils.FormatDate)
  205. o := orm.NewOrm()
  206. sql := `SELECT
  207. a.company_id ,max(b.last_view_time) view_time
  208. FROM
  209. wx_user a
  210. JOIN user_view_statistics b ON a.mobile = b.mobile
  211. WHERE
  212. a.company_id IN ( ` + companyIds + ` ) and a.mobile !="" and b.mobile !="" and b.date<=? GROUP BY a.company_id`
  213. _, err = o.Raw(sql, today).QueryRows(&items)
  214. return
  215. }
  216. // GetLastUserViewStatisticsByCompanyIdsEmail 根据邮箱获取客户的最新浏览时间
  217. func GetLastUserViewStatisticsByCompanyIdsEmail(companyIds string) (items []*CompanyLastViewSlice, err error) {
  218. today := time.Now().Format(utils.FormatDate)
  219. o := orm.NewOrm()
  220. sql := `SELECT
  221. a.company_id ,max(b.last_view_time) view_time
  222. FROM
  223. wx_user a
  224. JOIN user_view_statistics b ON a.email = b.email
  225. WHERE a.company_id IN ( ` + companyIds + ` ) and a.email !="" and b.email !="" and a.mobile="" and b.mobile="" and b.date<=? GROUP BY a.company_id`
  226. _, err = o.Raw(sql, today).QueryRows(&items)
  227. return
  228. }
  229. // CompanyViewTotalSlice 获取客户的浏览次数
  230. type CompanyViewTotalSlice struct {
  231. CompanyId int `description:"客户id"`
  232. ViewTotal int `description:"用户浏览次数"`
  233. }
  234. // GetCountUserViewHistoryByCompanyIdsMobile 根据手机号获取客户的浏览次数
  235. func GetCountUserViewHistoryByCompanyIdsMobile(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  236. today := time.Now().Format(utils.FormatDate) + " 00:00:00"
  237. o := orm.NewOrm()
  238. sql := `SELECT
  239. a.company_id ,count(1) view_total FROM wx_user a
  240. JOIN user_view_history b ON a.mobile = b.mobile
  241. WHERE a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" and b.created_time>=? GROUP BY company_id`
  242. _, err = o.Raw(sql, today).QueryRows(&items)
  243. return
  244. }
  245. // GetCountUserViewHistoryByCompanyIdsEmail 根据邮箱获取客户的浏览次数
  246. func GetCountUserViewHistoryByCompanyIdsEmail(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  247. today := time.Now().Format(utils.FormatDate) + " 00:00:00"
  248. o := orm.NewOrm()
  249. // sql := `SELECT
  250. // a.company_id ,count(1) view_total
  251. //FROM
  252. // wx_user a
  253. // JOIN user_view_history b ON a.email = b.email
  254. //WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" and b.created_time>=? GROUP BY company_id`
  255. sql := `SELECT a.company_id ,count(1) view_total FROM wx_user a
  256. JOIN user_view_history b ON a.email = b.email
  257. WHERE b.email !="" and b.mobile="" and b.created_time>=? GROUP BY company_id`
  258. _, err = o.Raw(sql, today).QueryRows(&items)
  259. return
  260. }
  261. // GetCountAdvisoryArticleViewRecordByCompanyIdsMobile 根据手机号获取客户的浏览次数
  262. func GetCountAdvisoryArticleViewRecordByCompanyIdsMobile(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  263. today := time.Now().Format(utils.FormatDate) + " 00:00:00"
  264. o := orm.NewOrm()
  265. sql := `SELECT
  266. a.company_id ,count(1) view_total
  267. FROM
  268. wx_user a
  269. JOIN advisory_user_chart_article_record b ON a.mobile = b.mobile
  270. WHERE
  271. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" and create_time>=? GROUP BY company_id`
  272. _, err = o.Raw(sql, today).QueryRows(&items)
  273. return
  274. }
  275. // GetCountAdvisoryArticleViewRecordByCompanyIdsEmail 根据邮箱获取客户的浏览次数
  276. func GetCountAdvisoryArticleViewRecordByCompanyIdsEmail(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  277. today := time.Now().Format(utils.FormatDate) + " 00:00:00"
  278. o := orm.NewOrm()
  279. sql := `SELECT
  280. a.company_id ,count(1) view_total
  281. FROM
  282. wx_user a
  283. JOIN advisory_user_chart_article_record b ON a.email = b.email
  284. WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" and create_time>=? GROUP BY company_id`
  285. _, err = o.Raw(sql, today).QueryRows(&items)
  286. return
  287. }
  288. // GetCountCygxArticleViewRecordByCompanyIdsMobile 根据手机号获取客户的浏览次数
  289. func GetCountCygxArticleViewRecordByCompanyIdsMobile(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  290. o := orm.NewOrm()
  291. //dataName := ""
  292. //if utils.RunMode == "debug" {
  293. // dataName = "test_v2_hongze_rddp"
  294. //} else {
  295. // dataName = "hongze_rddp"
  296. //}
  297. sql := `SELECT
  298. a.company_id ,count(1) view_total
  299. FROM
  300. wx_user a
  301. JOIN cygx_article_history_record_newpv b ON a.mobile = b.mobile
  302. WHERE
  303. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" GROUP BY company_id`
  304. _, err = o.Raw(sql).QueryRows(&items)
  305. return
  306. }
  307. // GetCountCygxArticleViewRecordByCompanyIdsEmail 根据邮箱获取客户的浏览次数
  308. func GetCountCygxArticleViewRecordByCompanyIdsEmail(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  309. o := orm.NewOrm()
  310. sql := `SELECT
  311. a.company_id ,count(1) view_total
  312. FROM
  313. wx_user a
  314. JOIN cygx_article_history_record_newpv b ON a.email = b.email
  315. WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" GROUP BY company_id`
  316. _, err = o.Raw(sql).QueryRows(&items)
  317. return
  318. }
  319. // GetCountReportViewRecordByCompanyIdsMobile 根据手机号获取客户的浏览次数
  320. func GetCountReportViewRecordByCompanyIdsMobile(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  321. today := time.Now().Format(utils.FormatDate) + " 00:00:00"
  322. o := orm.NewOrm()
  323. dataName := ""
  324. if utils.RunMode == "debug" {
  325. dataName = "test_v2_hongze_rddp"
  326. } else {
  327. dataName = "hongze_rddp"
  328. }
  329. sql := `SELECT
  330. a.company_id ,count(1) view_total
  331. FROM
  332. wx_user a
  333. JOIN ` + dataName + `.report_view_record b ON a.mobile = b.mobile
  334. WHERE
  335. a.company_id IN ( ` + companyIds + ` ) and b.mobile !="" and create_time>=? GROUP BY company_id`
  336. _, err = o.Raw(sql, today).QueryRows(&items)
  337. return
  338. }
  339. // GetCountReportViewRecordByCompanyIdsEmail 根据邮箱获取客户的浏览次数
  340. func GetCountReportViewRecordByCompanyIdsEmail(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  341. today := time.Now().Format(utils.FormatDate) + " 00:00:00"
  342. o := orm.NewOrm()
  343. dataName := ""
  344. if utils.RunMode == "debug" {
  345. dataName = "test_v2_hongze_rddp"
  346. } else {
  347. dataName = "hongze_rddp"
  348. }
  349. sql := `SELECT a.company_id ,count(1) view_total FROM wx_user a
  350. JOIN ` + dataName + `.report_view_record b ON a.email = b.email
  351. WHERE a.company_id IN ( ` + companyIds + ` ) and b.email !="" and b.mobile="" and create_time>=? GROUP BY company_id`
  352. _, err = o.Raw(sql, today).QueryRows(&items)
  353. return
  354. }
  355. // GetUserViewStatisticsByCompanyIdsMobile 根据手机号获取客户的浏览次数
  356. func GetUserViewStatisticsByCompanyIdsMobile(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  357. today := time.Now().Format(utils.FormatDate)
  358. o := orm.NewOrm()
  359. sql := `SELECT
  360. a.company_id ,sum(b.view_num) view_total
  361. FROM
  362. wx_user a
  363. JOIN user_view_statistics b ON a.mobile = b.mobile
  364. WHERE
  365. a.company_id IN ( ` + companyIds + ` ) and a.mobile !="" and b.mobile !="" and date<=? GROUP BY a.company_id`
  366. _, err = o.Raw(sql, today).QueryRows(&items)
  367. return
  368. }
  369. // GetUserViewStatisticsByCompanyIdsEmail 根据邮箱获取客户的浏览次数
  370. func GetUserViewStatisticsByCompanyIdsEmail(companyIds string) (items []*CompanyViewTotalSlice, err error) {
  371. today := time.Now().Format(utils.FormatDate)
  372. o := orm.NewOrm()
  373. sql := `SELECT
  374. a.company_id ,sum(b.view_num) view_total
  375. FROM
  376. wx_user a
  377. JOIN user_view_statistics b ON a.email = b.email
  378. WHERE a.company_id IN ( ` + companyIds + ` ) and a.email !="" and b.email !="" and a.mobile="" and b.mobile="" and date<=? GROUP BY a.company_id`
  379. _, err = o.Raw(sql, today).QueryRows(&items)
  380. return
  381. }
  382. // UserViewStatisticsInfo 根据用户手机号字符串获取用户的浏览数和最晚阅读次数
  383. type UserViewStatisticsInfo struct {
  384. Mobile string `description:"用户手机号"`
  385. Total int `description:"总阅读数"`
  386. LastViewTime time.Time `description:"用户浏览时间"`
  387. }
  388. // GetUserViewStatisticsByMobile 根据手机号获取联系人的浏览次数
  389. func GetUserViewStatisticsByMobile(mobile string) (item *UserViewStatisticsInfo, err error) {
  390. o := orm.NewOrm()
  391. sql := `SELECT mobile,sum(view_num) total,max(last_view_time) last_view_time FROM user_view_statistics WHERE mobile = ? `
  392. err = o.Raw(sql, mobile).QueryRow(&item)
  393. return
  394. }
  395. type ResearchReportViewPUV struct {
  396. ResearchReportId int
  397. Pv int
  398. Uv int
  399. }
  400. // GetPUVByResearchReportIds 通过报告IDs获取老报告PV、UV
  401. func GetPUVByResearchReportIds(reportIds string) (list []*ResearchReportViewPUV, err error) {
  402. o := orm.NewOrm()
  403. sql := `SELECT
  404. research_report_id,
  405. COUNT(1) AS pv,
  406. COUNT(DISTINCT user_id) AS uv
  407. FROM
  408. user_view_history
  409. WHERE
  410. research_report_id IN (` + reportIds +`)
  411. GROUP BY
  412. research_report_id`
  413. _, err = o.Raw(sql).QueryRows(&list)
  414. return
  415. }