bill.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_api/utils"
  6. "time"
  7. )
  8. type BillDetailResp struct {
  9. RealName string `description:"用户实际名称"`
  10. TogetherDay string `description:"相伴天数"`
  11. CreateDate string `description:"创建时间"`
  12. FirstReadReportType string `description:"首次阅读报告类型"`
  13. FirstReadReportTitle string `description:"首次阅读报告标题"`
  14. ListenReportCount int `description:"收听报告次数"`
  15. ListenReportDuration float64 `description:"收听报告时长"`
  16. MaxReadReportDate string `description:"阅读报告最多的一天"`
  17. MaxReadReportCount int `description:"阅读报告最多的一天,报告数量"`
  18. LatestReadReportDate string `description:"阅读报告时间最晚的一天"`
  19. LatestReadReportTime string `description:"阅读报告时间最晚的一天,最晚的时间"`
  20. LatestReadReportDateDuration float64 `description:"阅读报告时间最晚的一天,总共阅读报告的时长"`
  21. MaxOpenReportClassify string `description:"打开次数最多报告的栏目"`
  22. MaxOpenReportCount int `description:"打开次数最多报告的栏目下,用户阅读的报告数"`
  23. TotalReadDuration float64 `description:"总阅读时长"`
  24. TotalReportDayCount int `description:"总阅读晨报数"`
  25. TotalReportWeekCount int `description:"总阅读周报数"`
  26. TotalReportMonthCount int `description:"总阅读月报数"`
  27. TotalReportTwoWeekCount int `description:"总阅读双周报数"`
  28. TotalReportRddpCount int `description:"总阅读点评数"`
  29. TotalReport int `description:"总阅读报告数"`
  30. LearnDay int `description:"连续学习天数"`
  31. }
  32. type ReadReportType struct {
  33. ReportType string
  34. CreateTime time.Time
  35. Title string
  36. Stage int
  37. }
  38. func GetRddpMinReportType(uid int, startDate string) (item *ReadReportType, err error) {
  39. o := orm.NewOrmUsingDB("rddp")
  40. sql := `SELECT 'rddp' AS report_type,MIN(a.create_time) AS create_time,b.title,b.stage FROM report_view_record AS a
  41. INNER JOIN report AS b ON a.report_id=b.id
  42. WHERE a.user_id=? AND a.create_time>=? `
  43. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  44. return
  45. }
  46. func GetWeekMinReportType(uid int, startDate string) (item *ReadReportType, err error) {
  47. o := orm.NewOrm()
  48. sql := `SELECT b.type AS report_type,MIN(a.created_time) AS create_time,b.research_report_name AS title,b.periods AS stage FROM user_view_history AS a
  49. INNER JOIN research_report AS b ON a.research_report_id=b.research_report_id
  50. WHERE a.user_id=? AND a.created_time>=? `
  51. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  52. return
  53. }
  54. func GetFirstReportInfo(uid int, startDate string) (firstReadReportType, firstReadReportTitle string, err error) {
  55. rddpFlag := true
  56. weekFlag := true
  57. rddpMinReportItem, err := GetRddpMinReportType(uid, startDate)
  58. if err != nil {
  59. if err.Error() == utils.ErrNoRow() {
  60. rddpFlag = false
  61. } else {
  62. return
  63. }
  64. }
  65. if rddpMinReportItem == nil {
  66. rddpFlag = false
  67. }
  68. weekMinReportItem, err := GetWeekMinReportType(uid, startDate)
  69. if err != nil {
  70. if err.Error() == utils.ErrNoRow() {
  71. weekFlag = false
  72. } else {
  73. return
  74. }
  75. }
  76. if weekMinReportItem == nil {
  77. weekFlag = false
  78. }
  79. if rddpFlag && weekFlag {
  80. if rddpMinReportItem.CreateTime.After(weekMinReportItem.CreateTime) {
  81. firstReadReportType = rddpMinReportItem.ReportType
  82. firstReadReportTitle = rddpMinReportItem.Title
  83. } else {
  84. firstReadReportType = weekMinReportItem.ReportType
  85. firstReadReportTitle = weekMinReportItem.Title
  86. }
  87. } else {
  88. if rddpFlag == false && weekFlag {
  89. firstReadReportType = weekMinReportItem.ReportType
  90. firstReadReportTitle = weekMinReportItem.Title
  91. }
  92. if rddpFlag && weekFlag == false {
  93. firstReadReportType = rddpMinReportItem.ReportType
  94. firstReadReportTitle = rddpMinReportItem.Title
  95. }
  96. }
  97. return
  98. }
  99. type Listen struct {
  100. Count int
  101. VideoPlaySeconds float64
  102. }
  103. func GetRddpListen(uid int, startDate string) (item *Listen, err error) {
  104. o := orm.NewOrmUsingDB("rddp")
  105. sql := ` SELECT COUNT(1) AS count,SUM(b.video_play_seconds) AS video_play_seconds FROM report_audio_record AS a
  106. INNER JOIN report AS b ON a.report_id=b.id
  107. WHERE user_id=? AND a.create_time>=? LIMIT 1`
  108. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  109. return
  110. }
  111. func GetWeekListen(uid int, startDate string) (item *Listen, err error) {
  112. o := orm.NewOrm()
  113. sql := `SELECT COUNT(1) AS count,SUM(b.video_play_seconds) AS video_play_seconds FROM voice_record AS a
  114. INNER JOIN teleconference AS b ON a.teleconference_id=b.teleconference_id
  115. WHERE a.uid=? AND a.create_time>=? LIMIT 1`
  116. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  117. return
  118. }
  119. func GetListenInfo(uid int, startDate string) (count int, videoPlaySeconds float64, err error) {
  120. rddpFlag := true
  121. weekFlag := true
  122. rddpListenItem, err := GetRddpListen(uid, startDate)
  123. if err != nil {
  124. if err.Error() == utils.ErrNoRow() {
  125. rddpFlag = false
  126. } else {
  127. return
  128. }
  129. }
  130. if rddpListenItem == nil {
  131. rddpFlag = false
  132. }
  133. weekListenItem, err := GetWeekListen(uid, startDate)
  134. if err != nil {
  135. if err.Error() == utils.ErrNoRow() {
  136. weekFlag = false
  137. } else {
  138. return
  139. }
  140. }
  141. if weekListenItem == nil {
  142. weekFlag = false
  143. }
  144. if rddpFlag && weekFlag {
  145. count = rddpListenItem.Count + weekListenItem.Count
  146. videoPlaySeconds = rddpListenItem.VideoPlaySeconds + weekListenItem.VideoPlaySeconds
  147. } else {
  148. if rddpFlag == false && weekFlag {
  149. count = weekListenItem.Count
  150. videoPlaySeconds = weekListenItem.VideoPlaySeconds
  151. }
  152. if rddpFlag && weekFlag == false {
  153. count = rddpListenItem.Count
  154. videoPlaySeconds = rddpListenItem.VideoPlaySeconds
  155. }
  156. }
  157. return
  158. }
  159. type LatestReadReport struct {
  160. LatestTime string
  161. CreateTime time.Time
  162. }
  163. func GetRddpLatestReadReport(uid int, startDate string) (item *LatestReadReport, err error) {
  164. o := orm.NewOrmUsingDB("rddp")
  165. sql := ` SELECT DATE_FORMAT(create_time,'%H:%i:%s') AS latest_time,create_time FROM report_view_record
  166. WHERE user_id=? AND create_time>=? AND (DATE_FORMAT(create_time,'%H:%i:%s')<='06:00:00' OR DATE_FORMAT(create_time,'%H:%i:%s')>'06:00:00')
  167. ORDER BY DATE_FORMAT(create_time,'%H:%i:%s') DESC
  168. LIMIT 1 `
  169. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  170. return
  171. }
  172. func GetWeekLatestReadReportCount(uid int, startDate string) (item *LatestReadReport, err error) {
  173. o := orm.NewOrm()
  174. sql := ` SELECT DATE_FORMAT(created_time,'%H:%i:%s') AS latest_time,created_time FROM user_view_history
  175. WHERE user_id=? AND created_time>=? AND (DATE_FORMAT(created_time,'%H:%i:%s')<='06:00:00' OR DATE_FORMAT(created_time,'%H:%i:%s')>'06:00:00')
  176. ORDER BY DATE_FORMAT(created_time,'%H:%i:%s') DESC
  177. LIMIT 1 `
  178. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  179. return
  180. }
  181. func GetLatestReadReportInfo(uid int, startDate string) (latestTime string, latestCreateTime time.Time, err error) {
  182. rddpFlag := true
  183. weekFlag := true
  184. rddpReadReportItem, err := GetRddpLatestReadReport(uid, startDate)
  185. if err != nil {
  186. if err.Error() == utils.ErrNoRow() {
  187. rddpFlag = false
  188. } else {
  189. return
  190. }
  191. }
  192. if rddpReadReportItem == nil {
  193. rddpFlag = false
  194. }
  195. weekReadReportItem, err := GetWeekLatestReadReportCount(uid, startDate)
  196. if err != nil {
  197. if err.Error() == utils.ErrNoRow() {
  198. weekFlag = false
  199. } else {
  200. return
  201. }
  202. }
  203. if weekReadReportItem == nil {
  204. weekFlag = false
  205. }
  206. if rddpFlag && weekFlag {
  207. if rddpReadReportItem.CreateTime.After(weekReadReportItem.CreateTime) {
  208. latestTime = rddpReadReportItem.LatestTime
  209. latestCreateTime = rddpReadReportItem.CreateTime
  210. } else {
  211. latestTime = weekReadReportItem.LatestTime
  212. latestCreateTime = weekReadReportItem.CreateTime
  213. }
  214. } else {
  215. if rddpFlag == false && weekFlag {
  216. latestTime = weekReadReportItem.LatestTime
  217. latestCreateTime = weekReadReportItem.CreateTime
  218. }
  219. if rddpFlag && weekFlag == false {
  220. latestTime = rddpReadReportItem.LatestTime
  221. latestCreateTime = rddpReadReportItem.CreateTime
  222. }
  223. }
  224. fmt.Println("line 244")
  225. fmt.Println(latestTime, latestCreateTime)
  226. return
  227. }
  228. type MaxReadReportCount struct {
  229. Count int
  230. CreateDate time.Time
  231. }
  232. func GetRddpMaxReadReportCount(uid int, startDate string) (item *MaxReadReportCount, err error) {
  233. o := orm.NewOrmUsingDB("rddp")
  234. sql := ` SELECT DATE(create_time)AS create_date,COUNT(1) AS count FROM report_view_record AS a
  235. WHERE a.user_id=?
  236. AND create_time>=?
  237. GROUP BY DATE(create_time)
  238. ORDER BY count DESC
  239. LIMIT 1 `
  240. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  241. return
  242. }
  243. func GetWeekMaxReadReportCount(uid int, startDate string) (item *MaxReadReportCount, err error) {
  244. o := orm.NewOrm()
  245. sql := ` SELECT DATE(a.created_time)AS create_date,COUNT(1) AS count FROM user_view_history AS a
  246. WHERE a.user_id=? AND created_time>=?
  247. GROUP BY DATE(created_time)
  248. ORDER BY count DESC
  249. LIMIT 1 `
  250. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  251. return
  252. }
  253. func GetMaxReadReportInfo(uid int, startDate string) (count int, createDate time.Time, err error) {
  254. rddpFlag := true
  255. weekFlag := true
  256. rddpReadReportItem, err := GetRddpMaxReadReportCount(uid, startDate)
  257. if err != nil {
  258. if err.Error() == utils.ErrNoRow() {
  259. rddpFlag = false
  260. } else {
  261. return
  262. }
  263. }
  264. if rddpReadReportItem == nil {
  265. rddpFlag = false
  266. }
  267. weekReadReportItem, err := GetWeekMaxReadReportCount(uid, startDate)
  268. if err != nil {
  269. if err.Error() == utils.ErrNoRow() {
  270. weekFlag = false
  271. } else {
  272. return
  273. }
  274. }
  275. if weekReadReportItem == nil {
  276. weekFlag = false
  277. }
  278. if rddpFlag && weekFlag {
  279. if rddpReadReportItem.Count > weekReadReportItem.Count {
  280. count = rddpReadReportItem.Count
  281. createDate = rddpReadReportItem.CreateDate
  282. } else {
  283. count = weekReadReportItem.Count
  284. createDate = weekReadReportItem.CreateDate
  285. }
  286. } else {
  287. if rddpFlag == false && weekFlag {
  288. count = weekReadReportItem.Count
  289. createDate = weekReadReportItem.CreateDate
  290. }
  291. if rddpFlag && weekFlag == false {
  292. count = rddpReadReportItem.Count
  293. createDate = rddpReadReportItem.CreateDate
  294. }
  295. }
  296. return
  297. }
  298. func GetRddpReadReportCountByDate(uid int, readDate string) (count int, video_play_seconds float64, err error) {
  299. o := orm.NewOrmUsingDB("rddp")
  300. sql := ` SELECT COUNT(1) AS count,SUM(b.video_play_seconds) AS video_play_seconds FROM report_view_record AS a
  301. LEFT JOIN report AS b ON a.report_id=b.id
  302. WHERE a.user_id=? AND DATE(a.create_time)=? LIMIT 1`
  303. err = o.Raw(sql, uid, readDate).QueryRow(&count, &video_play_seconds)
  304. return
  305. }
  306. func GetWeekReadReportCountByDate(uid int, readDate string) (count int, err error) {
  307. o := orm.NewOrm()
  308. sql := ` SELECT COUNT(1) AS count FROM user_view_history AS a
  309. WHERE a.user_id=? AND DATE(a.created_time)=?
  310. LIMIT 1 `
  311. err = o.Raw(sql, uid, readDate).QueryRow(&count)
  312. return
  313. }
  314. type MaxOpenReport struct {
  315. OpenReportCount int
  316. ClassifyName string
  317. }
  318. func GetRddpOpenReadReport(uid int, startDate string) (item *MaxOpenReport, err error) {
  319. o := orm.NewOrmUsingDB("rddp")
  320. sql := ` SELECT COUNT(b.classify_id_second) AS open_report_count,b.classify_name_second AS classify_name
  321. FROM report_view_record AS a
  322. INNER JOIN report AS b ON a.report_id=b.id
  323. WHERE a.user_id=? AND a.create_time>=?
  324. GROUP BY b.classify_id_second
  325. ORDER BY COUNT(b.classify_id_second) DESC
  326. LIMIT 1 `
  327. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  328. return
  329. }
  330. func GetWeekOpenReadReportCount(uid int, startDate string) (item *MaxOpenReport, err error) {
  331. o := orm.NewOrm()
  332. sql := ` SELECT COUNT(b.research_report_name) AS open_report_count,b.research_report_name AS classify_name
  333. FROM user_view_history AS a
  334. LEFT JOIN research_report AS b ON a.research_report_id=b.research_report_id
  335. WHERE user_id=? AND a.created_time=?
  336. GROUP BY b.research_report_name
  337. ORDER BY COUNT(b.research_report_name) DESC
  338. LIMIT 1 `
  339. err = o.Raw(sql, uid, startDate).QueryRow(&item)
  340. return
  341. }
  342. func GetOpenReadReportInfo(uid int, startDate string) (maxOpenReportClassify string, maxOpenReportCount int, err error) {
  343. rddpFlag := true
  344. weekFlag := true
  345. rddpReadReportItem, err := GetRddpOpenReadReport(uid, startDate)
  346. if err != nil {
  347. if err.Error() == utils.ErrNoRow() {
  348. rddpFlag = false
  349. } else {
  350. return
  351. }
  352. }
  353. if rddpReadReportItem == nil {
  354. rddpFlag = false
  355. }
  356. weekReadReportItem, err := GetWeekOpenReadReportCount(uid, startDate)
  357. if err != nil {
  358. if err.Error() == utils.ErrNoRow() {
  359. weekFlag = false
  360. } else {
  361. return
  362. }
  363. }
  364. if weekReadReportItem == nil {
  365. weekFlag = false
  366. }
  367. if rddpFlag && weekFlag {
  368. if rddpReadReportItem.OpenReportCount > weekReadReportItem.OpenReportCount {
  369. maxOpenReportCount = rddpReadReportItem.OpenReportCount
  370. maxOpenReportClassify = rddpReadReportItem.ClassifyName
  371. } else {
  372. maxOpenReportCount = weekReadReportItem.OpenReportCount
  373. maxOpenReportClassify = weekReadReportItem.ClassifyName
  374. }
  375. } else {
  376. if rddpFlag == false && weekFlag {
  377. maxOpenReportCount = weekReadReportItem.OpenReportCount
  378. maxOpenReportClassify = weekReadReportItem.ClassifyName
  379. }
  380. if rddpFlag && weekFlag == false {
  381. maxOpenReportCount = rddpReadReportItem.OpenReportCount
  382. maxOpenReportClassify = rddpReadReportItem.ClassifyName
  383. }
  384. }
  385. return
  386. }
  387. func GetRddpTotalReadDuration(uid int, startDate string) (video_play_seconds float64, total int, err error) {
  388. sql := `SELECT SUM(b.video_play_seconds) AS video_play_seconds ,COUNT(1) AS total FROM report_view_record AS a
  389. INNER JOIN report AS b ON a.report_id=b.id
  390. WHERE a.user_id=? AND a.create_time>=? `
  391. o := orm.NewOrmUsingDB("rddp")
  392. err = o.Raw(sql, uid, startDate).QueryRow(&video_play_seconds, &total)
  393. return
  394. }
  395. func GetWeekTotalRead(uid int, startDate string) (report_count int, err error) {
  396. o := orm.NewOrm()
  397. sql := ` SELECT SUM(1) AS report_count
  398. FROM user_view_history AS a
  399. INNER JOIN research_report AS b ON a.research_report_id=b.research_report_id
  400. WHERE a.user_id=? AND a.created_time>=? `
  401. err = o.Raw(sql, uid, startDate).QueryRow(&report_count)
  402. return
  403. }
  404. func GetWeekTotalReadByType(uid int, reportType string, startDate string) (report_count int, err error) {
  405. o := orm.NewOrm()
  406. sql := `SELECT count(DISTINCT a.research_report_id) AS report_count
  407. FROM user_view_history AS a
  408. INNER JOIN research_report AS b ON a.research_report_id=b.research_report_id
  409. WHERE a.user_id=? AND a.created_time>=? AND b.type=?`
  410. err = o.Raw(sql, uid, startDate, reportType).QueryRow(&report_count)
  411. return
  412. }
  413. func GetRddpLearnDay(uid int, startDate, endDate string) (learn_day int, err error) {
  414. //, MIN(login_day) start_date,MAX(login_day) end_date
  415. o := orm.NewOrmUsingDB("rddp")
  416. sql := ` SELECT MAX(days) AS learn_day
  417. FROM (SELECT user_id,
  418. @cont_day :=
  419. (CASE
  420. WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)=1) THEN
  421. (@cont_day + 1)
  422. WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)<1) THEN
  423. (@cont_day + 0)
  424. ELSE
  425. 1
  426. END) AS days,
  427. (@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,
  428. @last_uid := user_id,
  429. @last_dt := login_dt login_day
  430. FROM (SELECT user_id, DATE(create_time) AS login_dt
  431. FROM report_view_record
  432. WHERE
  433. user_id=?
  434. AND create_time>=?
  435. ORDER BY user_id, create_time) AS t,
  436. (SELECT @last_uid := '',
  437. @last_dt := '',
  438. @cont_ix := 0,
  439. @cont_day := 0) AS t1) AS t2
  440. GROUP BY user_id, cont_ix
  441. ORDER BY MAX(days) DESC
  442. LIMIT 1 `
  443. err = o.Raw(sql, uid, startDate).QueryRow(&learn_day)
  444. return
  445. }
  446. func GetWeekpLearnDay(uid int, startDate string) (learn_day int, err error) {
  447. //MIN(login_day) start_date,MAX(login_day) end_date
  448. o := orm.NewOrm()
  449. sql := ` SELECT MAX(days) lianxu_days
  450. FROM (SELECT user_id,
  451. @cont_day :=
  452. (CASE
  453. WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)=1) THEN
  454. (@cont_day + 1)
  455. WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)<1) THEN
  456. (@cont_day + 0)
  457. ELSE
  458. 1
  459. END) AS days,
  460. (@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,
  461. @last_uid := user_id,
  462. @last_dt := login_dt login_day
  463. FROM (SELECT user_id, DATE(created_time) AS login_dt
  464. FROM user_view_history
  465. WHERE
  466. user_id=?
  467. AND created_time>=?
  468. ORDER BY user_id, created_time) AS t,
  469. (SELECT @last_uid := '',
  470. @last_dt := '',
  471. @cont_ix := 0,
  472. @cont_day := 0) AS t1) AS t2
  473. GROUP BY user_id, cont_ix
  474. ORDER BY MAX(days) DESC
  475. LIMIT 1 `
  476. err = o.Raw(sql, uid, startDate).QueryRow(&learn_day)
  477. return
  478. }
  479. func GetRddpTotalReadByType(uid int, startDate string) (report_count int, err error) {
  480. o := orm.NewOrmUsingDB("rddp")
  481. sql := `SELECT count(DISTINCT a.report_id) AS report_count
  482. FROM report_view_record AS a
  483. INNER JOIN report AS b ON a.report_id=b.id
  484. WHERE a.user_id=? AND a.create_time>=? `
  485. err = o.Raw(sql, uid, startDate).QueryRow(&report_count)
  486. return
  487. }
  488. func GetAnnualReportCount(userId int) (count int, err error) {
  489. o := orm.NewOrm()
  490. sql := `SELECT COUNT(1) AS count FROM annual_report WHERE user_id=? `
  491. err = o.Raw(sql, userId).QueryRow(&count)
  492. return
  493. }