company_user.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. package tables
  2. import (
  3. "fmt"
  4. "hongze/hongze_open_api/utils"
  5. "rdluck_tools/orm"
  6. "time"
  7. )
  8. type ViewReportList struct {
  9. CompanyName string `description:"公司名称" json:"company_name"`
  10. RealName string `description:"用户名称" json:"user_name"`
  11. Mobile string `description:"手机号" json:"mobile"`
  12. Email string `description:"邮箱" json:"email"`
  13. ResearchReportName string `description:"报告标题" json:"report_name"`
  14. CreatedTime string `description:"报告阅读时间" json:"created_time"`
  15. ReportType string `description:"报告类型 'day 晨报'、'week 周报'、'twoweek 双周报'、'month 月报'、'rddp 日度点评'、'cygx 查研观向'、'advisory 每日商品聚焦'" json:"-"`
  16. TxtType string `description:"类型 ficc:ficc 、 rights:权益" json:"-"`
  17. MatchTypeName string `description:"匹配类型" json:"-"`
  18. StopTime string `description:"停留时间" json:"-"`
  19. }
  20. type ViewReportListResp struct {
  21. Total int `description:"数量"`
  22. List []*ViewReportList
  23. }
  24. func GetViewReportListByMobile(mobile string, txtType int) (items []*ViewReportList, err error) {
  25. dataName := ""
  26. sql := ``
  27. if utils.RunMode == "debug" {
  28. dataName = "test_v2_hongze_rddp"
  29. } else {
  30. dataName = "hongze_rddp"
  31. }
  32. ficcSql := `SELECT
  33. rr.research_report_name,
  34. rr.type AS report_type,
  35. 'ficc' AS txt_type,
  36. '--' AS match_type_name,
  37. '--' AS stop_time,
  38. uvh.created_time AS created_time
  39. FROM
  40. user_view_history uvh
  41. LEFT JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  42. WHERE
  43. uvh.mobile = ?
  44. UNION ALL
  45. SELECT
  46. r.title AS research_report_name,
  47. 'rddp' AS report_type,
  48. 'ficc' AS txt_type,
  49. r.classify_name_first AS match_type_name,
  50. '--' AS stop_time,
  51. rvr.create_time AS created_time
  52. FROM %s.report_view_record rvr
  53. LEFT JOIN %s.report r ON r.id = rvr.report_id
  54. WHERE
  55. rvr.mobile=?
  56. UNION ALL
  57. SELECT
  58. cha.permission_name AS research_report_name,
  59. 'advisory' AS report_type,
  60. 'ficc' AS txt_type,
  61. cha.classify_name AS match_type_name,
  62. '--' AS stop_time,
  63. auc.create_time AS created_time
  64. FROM
  65. advisory_user_chart_article_record auc
  66. LEFT JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  67. WHERE
  68. auc.mobile = ?`
  69. rightsSql := `SELECT
  70. art.title AS research_report_name,
  71. 'cygx' AS report_type,
  72. 'rights' AS txt_type,
  73. art.match_type_name,
  74. h.stop_time,
  75. h.create_time AS created_time
  76. FROM
  77. cygx_article_history_record h
  78. LEFT JOIN cygx_article art ON art.article_id = h.article_id
  79. WHERE
  80. h.mobile = ? `
  81. if txtType == 1 {
  82. sql = ` SELECT * FROM ( ` + rightsSql + `
  83. )AS t ORDER BY t.created_time DESC`
  84. } else if txtType == 2 {
  85. sql = ` SELECT * FROM ( ` + ficcSql + `
  86. )AS t ORDER BY t.created_time DESC`
  87. } else {
  88. sql = ` SELECT * FROM ( ` + ficcSql + " UNION ALL " + rightsSql + `
  89. )AS t ORDER BY t.created_time DESC`
  90. }
  91. //报告统计删除晨报部分统计加入每日资讯 2021-4-9
  92. //sql := ` SELECT * FROM (
  93. // SELECT
  94. // r.title AS research_report_name,
  95. // 'rddp' AS report_type,
  96. // rvr.create_time AS created_time
  97. // FROM %s.report_view_record rvr
  98. // INNER JOIN %s.report r ON r.id = rvr.report_id
  99. // WHERE
  100. // rvr.mobile=?
  101. // UNION ALL
  102. // SELECT
  103. // cha.permission_name AS research_report_name,
  104. // 'advisory' AS report_type,
  105. // auc.create_time AS created_time
  106. // FROM
  107. // advisory_user_chart_article_record auc
  108. // INNER JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  109. // WHERE
  110. // auc.mobile = ?
  111. // )AS t ORDER BY t.created_time DESC
  112. // `
  113. o := orm.NewOrm()
  114. if txtType == 1 {
  115. _, err = o.Raw(sql, mobile).QueryRows(&items)
  116. } else if txtType == 2 {
  117. sql = fmt.Sprintf(sql, dataName, dataName)
  118. _, err = o.Raw(sql, mobile, mobile, mobile).QueryRows(&items)
  119. } else {
  120. sql = fmt.Sprintf(sql, dataName, dataName)
  121. _, err = o.Raw(sql, mobile, mobile, mobile, mobile).QueryRows(&items)
  122. }
  123. return
  124. }
  125. func GetViewReportListByEmail2(email string, txtType int) (items []*ViewReportList, err error) {
  126. dataName := ""
  127. sql := ``
  128. if utils.RunMode == "debug" {
  129. dataName = "test_hongze_rddp"
  130. } else {
  131. dataName = "hongze_rddp"
  132. }
  133. ficcSql := `SELECT
  134. rr.research_report_name,
  135. rr.type AS report_type,
  136. 'ficc' AS txt_type,
  137. '--' AS match_type_name,
  138. '--' AS stop_time,
  139. uvh.created_time AS created_time
  140. FROM
  141. user_view_history uvh
  142. INNER JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  143. WHERE
  144. uvh.email = ?
  145. UNION ALL
  146. SELECT
  147. r.title AS research_report_name,
  148. 'rddp' AS report_type,
  149. 'ficc' AS txt_type,
  150. r.classify_name_first AS match_type_name,
  151. '--' AS stop_time,
  152. rvr.create_time AS created_time
  153. FROM %s.report_view_record rvr
  154. INNER JOIN %s.report r ON r.id = rvr.report_id
  155. WHERE
  156. rvr.email=?
  157. UNION ALL
  158. SELECT
  159. cha.permission_name AS research_report_name,
  160. 'advisory' AS report_type,
  161. 'ficc' AS txt_type,
  162. cha.classify_name AS match_type_name,
  163. '--' AS stop_time,
  164. auc.create_time AS created_time
  165. FROM
  166. advisory_user_chart_article_record auc
  167. INNER JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  168. WHERE
  169. auc.email = ?`
  170. rightsSql := `SELECT
  171. art.title AS research_report_name,
  172. 'cygx' AS report_type,
  173. 'rights' AS txt_type,
  174. art.match_type_name,
  175. h.stop_time,
  176. h.create_time AS created_time
  177. FROM
  178. cygx_article_history_record h
  179. INNER JOIN cygx_article art ON art.article_id = h.article_id
  180. WHERE
  181. h.email = ? `
  182. if txtType == 1 {
  183. sql = ` SELECT * FROM ( ` + rightsSql + `
  184. )AS t ORDER BY t.created_time DESC`
  185. } else if txtType == 2 {
  186. sql = ` SELECT * FROM ( ` + ficcSql + `
  187. )AS t ORDER BY t.created_time DESC`
  188. } else {
  189. sql = ` SELECT * FROM ( ` + ficcSql + " UNION ALL " + rightsSql + `
  190. )AS t ORDER BY t.created_time DESC`
  191. }
  192. //报告统计删除晨报部分统计加入每日资讯 2021-4-9
  193. //sql := ` SELECT * FROM (
  194. // SELECT
  195. // r.title AS research_report_name,
  196. // 'rddp' AS report_type,
  197. // rvr.create_time AS created_time
  198. // FROM %s.report_view_record rvr
  199. // INNER JOIN %s.report r ON r.id = rvr.report_id
  200. // WHERE
  201. // rvr.mobile=?
  202. // UNION ALL
  203. // SELECT
  204. // cha.permission_name AS research_report_name,
  205. // 'advisory' AS report_type,
  206. // auc.create_time AS created_time
  207. // FROM
  208. // advisory_user_chart_article_record auc
  209. // INNER JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  210. // WHERE
  211. // auc.mobile = ?
  212. // )AS t ORDER BY t.created_time DESC
  213. // `
  214. o := orm.NewOrm()
  215. if txtType == 1 {
  216. _, err = o.Raw(sql, email).QueryRows(&items)
  217. } else if txtType == 2 {
  218. sql = fmt.Sprintf(sql, dataName, dataName)
  219. _, err = o.Raw(sql, email, email, email).QueryRows(&items)
  220. } else {
  221. sql = fmt.Sprintf(sql, dataName, dataName)
  222. _, err = o.Raw(sql, email, email, email, email).QueryRows(&items)
  223. }
  224. return
  225. }
  226. // GetViewReportCount 获取报告列表
  227. func GetViewReportList(mobile, email, startDate, endDate string, startSize, pageSize int) (total int, items []*ViewReportList, err error) {
  228. dataName := ""
  229. sql := ``
  230. var sql1, sql2, sql3 string
  231. var pars []interface{}
  232. var pars1, pars2, pars3 []interface{}
  233. if mobile != "" && email != "" {
  234. sql1 += ` and ( uvh.mobile = ? or uvh.email = ? ) `
  235. sql2 += ` and ( rvr.mobile = ? or rvr.email = ? `
  236. sql3 += ` and ( auc.mobile = ? or auc.email = ? `
  237. pars1 = append(pars1, mobile, email)
  238. pars2 = append(pars2, mobile, email)
  239. pars3 = append(pars3, mobile, email)
  240. } else if mobile != "" {
  241. sql1 += ` and ( uvh.mobile = ? ) `
  242. sql2 += ` and ( rvr.mobile = ? ) `
  243. sql3 += ` and ( auc.mobile = ? ) `
  244. pars1 = append(pars1, mobile)
  245. pars2 = append(pars2, mobile)
  246. pars3 = append(pars3, mobile)
  247. } else if email != "" {
  248. sql1 += ` and ( uvh.email = ? ) `
  249. sql2 += ` and ( rvr.email = ? ) `
  250. sql3 += ` and ( auc.email = ? ) `
  251. pars1 = append(pars1, email)
  252. pars2 = append(pars2, email)
  253. pars3 = append(pars3, email)
  254. }
  255. //时间筛选
  256. if startDate != "" && endDate != "" {
  257. sql1 += ` and uvh.created_time >= ? and uvh.created_time <= ? `
  258. pars1 = append(pars1, startDate, endDate)
  259. sql2 += ` and rvr.create_time >= ? and rvr.create_time <= ? `
  260. pars2 = append(pars2, startDate, endDate)
  261. sql3 += ` and auc.create_time >= ? and auc.create_time <= ? `
  262. pars3 = append(pars3, startDate, endDate)
  263. }
  264. pars = append(pars, pars1, pars2, pars3)
  265. if utils.RunMode == "debug" {
  266. dataName = "test_v2_hongze_rddp"
  267. } else {
  268. dataName = "hongze_rddp"
  269. }
  270. ficcSql := `SELECT
  271. rr.research_report_name,
  272. rr.type AS report_type,
  273. 'ficc' AS txt_type,
  274. '--' AS match_type_name,
  275. '--' AS stop_time,
  276. uvh.created_time AS created_time,uvh.mobile,uvh.email,uvh.real_name,uvh.company_name
  277. FROM
  278. user_view_history uvh
  279. LEFT JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  280. WHERE 1=1
  281. ` + sql1 + `
  282. UNION ALL
  283. SELECT
  284. r.title AS research_report_name,
  285. 'rddp' AS report_type,
  286. 'ficc' AS txt_type,
  287. r.classify_name_first AS match_type_name,
  288. '--' AS stop_time,
  289. rvr.create_time AS created_time,rvr.mobile,rvr.email,rvr.real_name,rvr.company_name
  290. FROM %s.report_view_record rvr
  291. LEFT JOIN %s.report r ON r.id = rvr.report_id
  292. WHERE 1=1 ` + sql2 + `
  293. UNION ALL
  294. SELECT
  295. cha.permission_name AS research_report_name,
  296. 'advisory' AS report_type,
  297. 'ficc' AS txt_type,
  298. cha.classify_name AS match_type_name,
  299. '--' AS stop_time,
  300. auc.create_time AS created_time,auc.mobile,auc.email,auc.real_name,auc.company_name
  301. FROM
  302. advisory_user_chart_article_record auc
  303. LEFT JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  304. WHERE 1=1 ` + sql3
  305. sql = ` SELECT * FROM ( ` + ficcSql + ` )AS t ORDER BY t.created_time DESC`
  306. o := orm.NewOrm()
  307. sql = fmt.Sprintf(sql, dataName, dataName)
  308. totalSql := `SELECT count(*) total FROM ( ` + sql + ` )AS z `
  309. err = o.Raw(totalSql, pars).QueryRow(&total)
  310. if err != nil {
  311. return
  312. }
  313. sql += ` limit ?,?`
  314. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  315. return
  316. }
  317. // GetViewReportCount 获取报告列表总数
  318. func GetViewReportCount(mobile, email string) (total int, err error) {
  319. dataName := ""
  320. sql := ``
  321. var sql1, sql2, sql3 string
  322. var pars []interface{}
  323. if mobile != "" && email != "" {
  324. sql1 = `uvh.mobile = ? or uvh.email = ? `
  325. sql2 = `rvr.mobile = ? or rvr.email = ? `
  326. sql3 = `auc.mobile = ? or auc.email = ? `
  327. pars = append(pars, mobile, email, mobile, email, mobile, email)
  328. } else if mobile != "" {
  329. sql1 = `uvh.mobile = ? `
  330. sql2 = `rvr.mobile = ? `
  331. sql3 = `auc.mobile = ? `
  332. pars = append(pars, mobile, mobile, mobile)
  333. } else if email != "" {
  334. sql1 = `uvh.email = ? `
  335. sql2 = `rvr.email = ? `
  336. sql3 = `auc.email = ? `
  337. pars = append(pars, email, email, email)
  338. }
  339. if utils.RunMode == "debug" {
  340. dataName = "test_v2_hongze_rddp"
  341. } else {
  342. dataName = "hongze_rddp"
  343. }
  344. ficcSql := `SELECT
  345. rr.research_report_name,
  346. rr.type AS report_type,
  347. 'ficc' AS txt_type,
  348. '--' AS match_type_name,
  349. '--' AS stop_time,
  350. uvh.created_time AS created_time
  351. FROM
  352. user_view_history uvh
  353. LEFT JOIN research_report rr ON rr.research_report_id = uvh.research_report_id
  354. WHERE
  355. ` + sql1 + `
  356. UNION ALL
  357. SELECT
  358. r.title AS research_report_name,
  359. 'rddp' AS report_type,
  360. 'ficc' AS txt_type,
  361. r.classify_name_first AS match_type_name,
  362. '--' AS stop_time,
  363. rvr.create_time AS created_time
  364. FROM %s.report_view_record rvr
  365. LEFT JOIN %s.report r ON r.id = rvr.report_id
  366. WHERE ` + sql2 + `
  367. UNION ALL
  368. SELECT
  369. cha.permission_name AS research_report_name,
  370. 'advisory' AS report_type,
  371. 'ficc' AS txt_type,
  372. cha.classify_name AS match_type_name,
  373. '--' AS stop_time,
  374. auc.create_time AS created_time
  375. FROM
  376. advisory_user_chart_article_record auc
  377. LEFT JOIN chart_permission cha ON cha.chart_permission_id = auc.chart_permission_id
  378. WHERE ` + sql3
  379. sql = ` SELECT count(*) total FROM ( ` + ficcSql + `
  380. )AS t ORDER BY t.created_time DESC`
  381. o := orm.NewOrm()
  382. sql = fmt.Sprintf(sql, dataName, dataName)
  383. err = o.Raw(sql, pars).QueryRow(&total)
  384. return
  385. }
  386. // UserViewMobileTotalSlice 根据用户手机号字符串获取用户的浏览数
  387. type UserViewMobileTotalSlice struct {
  388. Mobile string `description:"用户手机号"`
  389. Total int `description:"总阅读数"`
  390. CreatedTime time.Time `description:"用户浏览时间"`
  391. }
  392. // UserViewEmailTotalSlice 根据用户邮箱字符串获取用户的浏览数
  393. type UserViewEmailTotalSlice struct {
  394. Email string `description:"用户邮箱"`
  395. Total int `description:"总阅读数"`
  396. CreatedTime time.Time `description:"用户浏览时间"`
  397. }
  398. func GetCountUserViewHistoryByMobile(dayStr string) (items []*UserViewMobileTotalSlice, err error) {
  399. o := orm.NewOrm()
  400. sql := `SELECT count(1) total,mobile,max(created_time) as created_time FROM user_view_history WHERE created_time >= ? and mobile !="" group by mobile `
  401. _, err = o.Raw(sql, dayStr+" 00:00:00").QueryRows(&items)
  402. return
  403. }
  404. func GetCountUserViewHistoryByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
  405. o := orm.NewOrm()
  406. sql := `SELECT count(1) total,email,max(created_time) as created_time FROM user_view_history WHERE email in (` + emails + `) group by email`
  407. _, err = o.Raw(sql).QueryRows(&items)
  408. return
  409. //return items2,err
  410. }
  411. func GetReportViewMaxTimeByMobile(dayStr string) (items []*UserViewMobileTotalSlice, err error) {
  412. o := orm.NewOrm()
  413. o.Using("rddp")
  414. rddpSql := `SELECT mobile,MAX(create_time) AS created_time,COUNT(1) AS total FROM report_view_record WHERE create_time >= ? and mobile !="" group by mobile`
  415. _, err = o.Raw(rddpSql, dayStr+" 00:00:00").QueryRows(&items)
  416. return
  417. }
  418. func GetReportViewMaxTimeByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
  419. o := orm.NewOrm()
  420. o.Using("rddp")
  421. rddpSql := `SELECT mobile,MAX(create_time) AS created_time,COUNT(1) AS total FROM report_view_record WHERE email in (` + emails + `) group by email`
  422. _, err = o.Raw(rddpSql).QueryRows(&items)
  423. return
  424. }
  425. // GetAdvisoryCountUserViewHistoryByMobile 每日资讯
  426. func GetAdvisoryCountUserViewHistoryByMobile(dayStr string) (items []*UserViewMobileTotalSlice, err error) {
  427. o := orm.NewOrm()
  428. sql := `SELECT count(1) total,mobile,max(create_time) as created_time FROM advisory_user_chart_article_record WHERE create_time >= ? and mobile !="" group by mobile`
  429. _, err = o.Raw(sql, dayStr+" 00:00:00").QueryRows(&items)
  430. return
  431. }
  432. // GetAdvisoryCountUserViewHistoryByEmails 每日资讯
  433. func GetAdvisoryCountUserViewHistoryByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
  434. o := orm.NewOrm()
  435. sql := `SELECT count(1) total,email,max(create_time) as created_time FROM advisory_user_chart_article_record WHERE email in (` + emails + `) group by email`
  436. _, err = o.Raw(sql).QueryRows(&items)
  437. return
  438. //return items2,err
  439. }