123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- package models
- import (
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_api/utils"
- "time"
- )
- type BillDetailResp struct {
- RealName string `description:"用户实际名称"`
- TogetherDay string `description:"相伴天数"`
- CreateDate string `description:"创建时间"`
- FirstReadReportType string `description:"首次阅读报告类型"`
- FirstReadReportTitle string `description:"首次阅读报告标题"`
- ListenReportCount int `description:"收听报告次数"`
- ListenReportDuration float64 `description:"收听报告时长"`
- MaxReadReportDate string `description:"阅读报告最多的一天"`
- MaxReadReportCount int `description:"阅读报告最多的一天,报告数量"`
- LatestReadReportDate string `description:"阅读报告时间最晚的一天"`
- LatestReadReportTime string `description:"阅读报告时间最晚的一天,最晚的时间"`
- LatestReadReportDateDuration float64 `description:"阅读报告时间最晚的一天,总共阅读报告的时长"`
- MaxOpenReportClassify string `description:"打开次数最多报告的栏目"`
- MaxOpenReportCount int `description:"打开次数最多报告的栏目下,用户阅读的报告数"`
- TotalReadDuration float64 `description:"总阅读时长"`
- TotalReportDayCount int `description:"总阅读晨报数"`
- TotalReportWeekCount int `description:"总阅读周报数"`
- TotalReportMonthCount int `description:"总阅读月报数"`
- TotalReportTwoWeekCount int `description:"总阅读双周报数"`
- TotalReportRddpCount int `description:"总阅读点评数"`
- TotalReport int `description:"总阅读报告数"`
- LearnDay int `description:"连续学习天数"`
- }
- type ReadReportType struct {
- ReportType string
- CreateTime time.Time
- Title string
- Stage int
- }
- func GetRddpMinReportType(uid int, startDate string) (item *ReadReportType, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := `SELECT 'rddp' AS report_type,MIN(a.create_time) AS create_time,b.title,b.stage FROM report_view_record AS a
- INNER JOIN report AS b ON a.report_id=b.id
- WHERE a.user_id=? AND a.create_time>=? `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetWeekMinReportType(uid int, startDate string) (item *ReadReportType, err error) {
- o := orm.NewOrm()
- 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
- INNER JOIN research_report AS b ON a.research_report_id=b.research_report_id
- WHERE a.user_id=? AND a.created_time>=? `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetFirstReportInfo(uid int, startDate string) (firstReadReportType, firstReadReportTitle string, err error) {
- rddpFlag := true
- weekFlag := true
- rddpMinReportItem, err := GetRddpMinReportType(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- rddpFlag = false
- } else {
- return
- }
- }
- if rddpMinReportItem == nil {
- rddpFlag = false
- }
- weekMinReportItem, err := GetWeekMinReportType(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- weekFlag = false
- } else {
- return
- }
- }
- if weekMinReportItem == nil {
- weekFlag = false
- }
- if rddpFlag && weekFlag {
- if rddpMinReportItem.CreateTime.After(weekMinReportItem.CreateTime) {
- firstReadReportType = rddpMinReportItem.ReportType
- firstReadReportTitle = rddpMinReportItem.Title
- } else {
- firstReadReportType = weekMinReportItem.ReportType
- firstReadReportTitle = weekMinReportItem.Title
- }
- } else {
- if rddpFlag == false && weekFlag {
- firstReadReportType = weekMinReportItem.ReportType
- firstReadReportTitle = weekMinReportItem.Title
- }
- if rddpFlag && weekFlag == false {
- firstReadReportType = rddpMinReportItem.ReportType
- firstReadReportTitle = rddpMinReportItem.Title
- }
- }
- return
- }
- type Listen struct {
- Count int
- VideoPlaySeconds float64
- }
- func GetRddpListen(uid int, startDate string) (item *Listen, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := ` SELECT COUNT(1) AS count,SUM(b.video_play_seconds) AS video_play_seconds FROM report_audio_record AS a
- INNER JOIN report AS b ON a.report_id=b.id
- WHERE user_id=? AND a.create_time>=? LIMIT 1`
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetWeekListen(uid int, startDate string) (item *Listen, err error) {
- o := orm.NewOrm()
- sql := `SELECT COUNT(1) AS count,SUM(b.video_play_seconds) AS video_play_seconds FROM voice_record AS a
- INNER JOIN teleconference AS b ON a.teleconference_id=b.teleconference_id
- WHERE a.uid=? AND a.create_time>=? LIMIT 1`
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetListenInfo(uid int, startDate string) (count int, videoPlaySeconds float64, err error) {
- rddpFlag := true
- weekFlag := true
- rddpListenItem, err := GetRddpListen(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- rddpFlag = false
- } else {
- return
- }
- }
- if rddpListenItem == nil {
- rddpFlag = false
- }
- weekListenItem, err := GetWeekListen(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- weekFlag = false
- } else {
- return
- }
- }
- if weekListenItem == nil {
- weekFlag = false
- }
- if rddpFlag && weekFlag {
- count = rddpListenItem.Count + weekListenItem.Count
- videoPlaySeconds = rddpListenItem.VideoPlaySeconds + weekListenItem.VideoPlaySeconds
- } else {
- if rddpFlag == false && weekFlag {
- count = weekListenItem.Count
- videoPlaySeconds = weekListenItem.VideoPlaySeconds
- }
- if rddpFlag && weekFlag == false {
- count = rddpListenItem.Count
- videoPlaySeconds = rddpListenItem.VideoPlaySeconds
- }
- }
- return
- }
- type LatestReadReport struct {
- LatestTime string
- CreateTime time.Time
- }
- func GetRddpLatestReadReport(uid int, startDate string) (item *LatestReadReport, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := ` SELECT DATE_FORMAT(create_time,'%H:%i:%s') AS latest_time,create_time FROM report_view_record
- 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')
- ORDER BY DATE_FORMAT(create_time,'%H:%i:%s') DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetWeekLatestReadReportCount(uid int, startDate string) (item *LatestReadReport, err error) {
- o := orm.NewOrm()
- sql := ` SELECT DATE_FORMAT(created_time,'%H:%i:%s') AS latest_time,created_time FROM user_view_history
- 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')
- ORDER BY DATE_FORMAT(created_time,'%H:%i:%s') DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetLatestReadReportInfo(uid int, startDate string) (latestTime string, latestCreateTime time.Time, err error) {
- rddpFlag := true
- weekFlag := true
- rddpReadReportItem, err := GetRddpLatestReadReport(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- rddpFlag = false
- } else {
- return
- }
- }
- if rddpReadReportItem == nil {
- rddpFlag = false
- }
- weekReadReportItem, err := GetWeekLatestReadReportCount(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- weekFlag = false
- } else {
- return
- }
- }
- if weekReadReportItem == nil {
- weekFlag = false
- }
- if rddpFlag && weekFlag {
- if rddpReadReportItem.CreateTime.After(weekReadReportItem.CreateTime) {
- latestTime = rddpReadReportItem.LatestTime
- latestCreateTime = rddpReadReportItem.CreateTime
- } else {
- latestTime = weekReadReportItem.LatestTime
- latestCreateTime = weekReadReportItem.CreateTime
- }
- } else {
- if rddpFlag == false && weekFlag {
- latestTime = weekReadReportItem.LatestTime
- latestCreateTime = weekReadReportItem.CreateTime
- }
- if rddpFlag && weekFlag == false {
- latestTime = rddpReadReportItem.LatestTime
- latestCreateTime = rddpReadReportItem.CreateTime
- }
- }
- fmt.Println("line 244")
- fmt.Println(latestTime, latestCreateTime)
- return
- }
- type MaxReadReportCount struct {
- Count int
- CreateDate time.Time
- }
- func GetRddpMaxReadReportCount(uid int, startDate string) (item *MaxReadReportCount, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := ` SELECT DATE(create_time)AS create_date,COUNT(1) AS count FROM report_view_record AS a
- WHERE a.user_id=?
- AND create_time>=?
- GROUP BY DATE(create_time)
- ORDER BY count DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetWeekMaxReadReportCount(uid int, startDate string) (item *MaxReadReportCount, err error) {
- o := orm.NewOrm()
- sql := ` SELECT DATE(a.created_time)AS create_date,COUNT(1) AS count FROM user_view_history AS a
- WHERE a.user_id=? AND created_time>=?
- GROUP BY DATE(created_time)
- ORDER BY count DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetMaxReadReportInfo(uid int, startDate string) (count int, createDate time.Time, err error) {
- rddpFlag := true
- weekFlag := true
- rddpReadReportItem, err := GetRddpMaxReadReportCount(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- rddpFlag = false
- } else {
- return
- }
- }
- if rddpReadReportItem == nil {
- rddpFlag = false
- }
- weekReadReportItem, err := GetWeekMaxReadReportCount(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- weekFlag = false
- } else {
- return
- }
- }
- if weekReadReportItem == nil {
- weekFlag = false
- }
- if rddpFlag && weekFlag {
- if rddpReadReportItem.Count > weekReadReportItem.Count {
- count = rddpReadReportItem.Count
- createDate = rddpReadReportItem.CreateDate
- } else {
- count = weekReadReportItem.Count
- createDate = weekReadReportItem.CreateDate
- }
- } else {
- if rddpFlag == false && weekFlag {
- count = weekReadReportItem.Count
- createDate = weekReadReportItem.CreateDate
- }
- if rddpFlag && weekFlag == false {
- count = rddpReadReportItem.Count
- createDate = rddpReadReportItem.CreateDate
- }
- }
- return
- }
- func GetRddpReadReportCountByDate(uid int, readDate string) (count int, video_play_seconds float64, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := ` SELECT COUNT(1) AS count,SUM(b.video_play_seconds) AS video_play_seconds FROM report_view_record AS a
- LEFT JOIN report AS b ON a.report_id=b.id
- WHERE a.user_id=? AND DATE(a.create_time)=? LIMIT 1`
- err = o.Raw(sql, uid, readDate).QueryRow(&count, &video_play_seconds)
- return
- }
- func GetWeekReadReportCountByDate(uid int, readDate string) (count int, err error) {
- o := orm.NewOrm()
- sql := ` SELECT COUNT(1) AS count FROM user_view_history AS a
- WHERE a.user_id=? AND DATE(a.created_time)=?
- LIMIT 1 `
- err = o.Raw(sql, uid, readDate).QueryRow(&count)
- return
- }
- type MaxOpenReport struct {
- OpenReportCount int
- ClassifyName string
- }
- func GetRddpOpenReadReport(uid int, startDate string) (item *MaxOpenReport, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := ` SELECT COUNT(b.classify_id_second) AS open_report_count,b.classify_name_second AS classify_name
- FROM report_view_record AS a
- INNER JOIN report AS b ON a.report_id=b.id
- WHERE a.user_id=? AND a.create_time>=?
- GROUP BY b.classify_id_second
- ORDER BY COUNT(b.classify_id_second) DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetWeekOpenReadReportCount(uid int, startDate string) (item *MaxOpenReport, err error) {
- o := orm.NewOrm()
- sql := ` SELECT COUNT(b.research_report_name) AS open_report_count,b.research_report_name AS classify_name
- FROM user_view_history AS a
- LEFT JOIN research_report AS b ON a.research_report_id=b.research_report_id
- WHERE user_id=? AND a.created_time=?
- GROUP BY b.research_report_name
- ORDER BY COUNT(b.research_report_name) DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&item)
- return
- }
- func GetOpenReadReportInfo(uid int, startDate string) (maxOpenReportClassify string, maxOpenReportCount int, err error) {
- rddpFlag := true
- weekFlag := true
- rddpReadReportItem, err := GetRddpOpenReadReport(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- rddpFlag = false
- } else {
- return
- }
- }
- if rddpReadReportItem == nil {
- rddpFlag = false
- }
- weekReadReportItem, err := GetWeekOpenReadReportCount(uid, startDate)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- weekFlag = false
- } else {
- return
- }
- }
- if weekReadReportItem == nil {
- weekFlag = false
- }
- if rddpFlag && weekFlag {
- if rddpReadReportItem.OpenReportCount > weekReadReportItem.OpenReportCount {
- maxOpenReportCount = rddpReadReportItem.OpenReportCount
- maxOpenReportClassify = rddpReadReportItem.ClassifyName
- } else {
- maxOpenReportCount = weekReadReportItem.OpenReportCount
- maxOpenReportClassify = weekReadReportItem.ClassifyName
- }
- } else {
- if rddpFlag == false && weekFlag {
- maxOpenReportCount = weekReadReportItem.OpenReportCount
- maxOpenReportClassify = weekReadReportItem.ClassifyName
- }
- if rddpFlag && weekFlag == false {
- maxOpenReportCount = rddpReadReportItem.OpenReportCount
- maxOpenReportClassify = rddpReadReportItem.ClassifyName
- }
- }
- return
- }
- func GetRddpTotalReadDuration(uid int, startDate string) (video_play_seconds float64, total int, err error) {
- sql := `SELECT SUM(b.video_play_seconds) AS video_play_seconds ,COUNT(1) AS total FROM report_view_record AS a
- INNER JOIN report AS b ON a.report_id=b.id
- WHERE a.user_id=? AND a.create_time>=? `
- o := orm.NewOrmUsingDB("rddp")
- err = o.Raw(sql, uid, startDate).QueryRow(&video_play_seconds, &total)
- return
- }
- func GetWeekTotalRead(uid int, startDate string) (report_count int, err error) {
- o := orm.NewOrm()
- sql := ` SELECT SUM(1) AS report_count
- FROM user_view_history AS a
- INNER JOIN research_report AS b ON a.research_report_id=b.research_report_id
- WHERE a.user_id=? AND a.created_time>=? `
- err = o.Raw(sql, uid, startDate).QueryRow(&report_count)
- return
- }
- func GetWeekTotalReadByType(uid int, reportType string, startDate string) (report_count int, err error) {
- o := orm.NewOrm()
- sql := `SELECT count(DISTINCT a.research_report_id) AS report_count
- FROM user_view_history AS a
- INNER JOIN research_report AS b ON a.research_report_id=b.research_report_id
- WHERE a.user_id=? AND a.created_time>=? AND b.type=?`
- err = o.Raw(sql, uid, startDate, reportType).QueryRow(&report_count)
- return
- }
- func GetRddpLearnDay(uid int, startDate, endDate string) (learn_day int, err error) {
- //, MIN(login_day) start_date,MAX(login_day) end_date
- o := orm.NewOrmUsingDB("rddp")
- sql := ` SELECT MAX(days) AS learn_day
- FROM (SELECT user_id,
- @cont_day :=
- (CASE
- WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)=1) THEN
- (@cont_day + 1)
- WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)<1) THEN
- (@cont_day + 0)
- ELSE
- 1
- END) AS days,
- (@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,
- @last_uid := user_id,
- @last_dt := login_dt login_day
- FROM (SELECT user_id, DATE(create_time) AS login_dt
- FROM report_view_record
- WHERE
- user_id=?
- AND create_time>=?
- ORDER BY user_id, create_time) AS t,
- (SELECT @last_uid := '',
- @last_dt := '',
- @cont_ix := 0,
- @cont_day := 0) AS t1) AS t2
- GROUP BY user_id, cont_ix
- ORDER BY MAX(days) DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&learn_day)
- return
- }
- func GetWeekpLearnDay(uid int, startDate string) (learn_day int, err error) {
- //MIN(login_day) start_date,MAX(login_day) end_date
- o := orm.NewOrm()
- sql := ` SELECT MAX(days) lianxu_days
- FROM (SELECT user_id,
- @cont_day :=
- (CASE
- WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)=1) THEN
- (@cont_day + 1)
- WHEN (@last_uid = user_id AND DATEDIFF(login_dt, @last_dt)<1) THEN
- (@cont_day + 0)
- ELSE
- 1
- END) AS days,
- (@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,
- @last_uid := user_id,
- @last_dt := login_dt login_day
- FROM (SELECT user_id, DATE(created_time) AS login_dt
- FROM user_view_history
- WHERE
- user_id=?
- AND created_time>=?
- ORDER BY user_id, created_time) AS t,
- (SELECT @last_uid := '',
- @last_dt := '',
- @cont_ix := 0,
- @cont_day := 0) AS t1) AS t2
- GROUP BY user_id, cont_ix
- ORDER BY MAX(days) DESC
- LIMIT 1 `
- err = o.Raw(sql, uid, startDate).QueryRow(&learn_day)
- return
- }
- func GetRddpTotalReadByType(uid int, startDate string) (report_count int, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := `SELECT count(DISTINCT a.report_id) AS report_count
- FROM report_view_record AS a
- INNER JOIN report AS b ON a.report_id=b.id
- WHERE a.user_id=? AND a.create_time>=? `
- err = o.Raw(sql, uid, startDate).QueryRow(&report_count)
- return
- }
- func GetAnnualReportCount(userId int) (count int, err error) {
- o := orm.NewOrm()
- sql := `SELECT COUNT(1) AS count FROM annual_report WHERE user_id=? `
- err = o.Raw(sql, userId).QueryRow(&count)
- return
- }
|