Browse Source

联系人列表分产品阅读统计新增全部筛选

hsun 1 year ago
parent
commit
2e0fb32c3a

+ 8 - 0
controllers/company_user.go

@@ -5310,6 +5310,14 @@ func (this *CompanyUserController) GetOtherProduct() {
 	if statisticFlag {
 		resp = append(resp, questionItem, roadVideoItem, videoItem)
 	}
+
+	// 全部
+	resp = append(resp, response.OtherProductTypeListResp{
+		ProductId:   0,
+		ProductType: 99,
+		ProductName: "全部",
+	})
+
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"

+ 150 - 1
controllers/yb/product_census.go

@@ -1,12 +1,14 @@
 package yb
 
 import (
+	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"hongze/hz_crm_api/controllers"
 	"hongze/hz_crm_api/models"
 	"hongze/hz_crm_api/models/yb"
 	"hongze/hz_crm_api/models/yb/response"
 	"hongze/hz_crm_api/utils"
+	"sync"
 )
 
 type ProductCensusController struct {
@@ -83,7 +85,7 @@ func (this *ProductCensusController) UserVisitCount() {
 // @Title 用户阅读统计详情
 // @Description 用户阅读统计详情
 // @Param   UserId			query  int  true	"用户ID"
-// @Param   ProductType		query  int  true	"产品类型:1-语音播报 2-视频社区 3-问答社区"
+// @Param   ProductType		query  int  true	"产品类型:1-语音播报 2-视频社区 3-问答社区 99-全部"
 // @Param   ProductId		query  int  false	"产品ID:仅语音播报传该参数"
 // @Param   ClickSort		query  int  false	"点击量排序:1-升序 2-降序"
 // @Param   PageSize		query  int  false	"页码"
@@ -205,6 +207,7 @@ func (this *ProductCensusController) UserVisitCountDetail() {
 			})
 		}
 	}
+
 	// 问答社区
 	if productType == 3 {
 		t, list, e := yb.GetCommunityQuestionVisitCountByUserId(userId, startSize, pageSize, orderRule)
@@ -240,6 +243,152 @@ func (this *ProductCensusController) UserVisitCountDetail() {
 		}
 	}
 
+	// 全部
+	if productType == 99 {
+		listTotal, list, e := yb.GetUserAllYbProductVisitCountPageList(userId, startSize, pageSize, orderRule)
+		if e != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取用户所有产品点击量失败, Err: " + e.Error()
+			return
+		}
+		total = listTotal
+
+		sectionIds := make([]int, 0)
+		videoIds := make([]int, 0)
+		rsVideoIds := make([]int, 0)
+		questionIds := make([]int, 0)
+
+		for _, v := range list {
+			// 语音播报-取出板块ID
+			if v.ProductType == 1 && v.SectionId > 0 && !utils.InArrayByInt(sectionIds, v.SectionId) {
+				sectionIds = append(sectionIds, v.SectionId)
+			}
+			// 视频社区-取出视频ID
+			if v.ProductType == 2 && v.TitleId > 0 {
+				if v.VideoType == 1 && !utils.InArrayByInt(videoIds, v.TitleId) {
+					videoIds = append(videoIds, v.TitleId)
+				}
+				if v.VideoType == 2 && !utils.InArrayByInt(rsVideoIds, v.TitleId) {
+					rsVideoIds = append(rsVideoIds, v.TitleId)
+				}
+			}
+			// 问答社区-取出问题ID
+			if v.ProductType == 3 && v.TitleId > 0 && !utils.InArrayByInt(questionIds, v.TitleId) {
+				questionIds = append(questionIds, v.TitleId)
+			}
+		}
+
+		var sectionErr, videoErr, questionErr error
+		wg := sync.WaitGroup{}
+		wg.Add(3)
+
+		// 板块名称map
+		sectionNameMap := make(map[int]string, 0)
+		go func() {
+			defer func() {
+				wg.Done()
+			}()
+
+			sectionList, e := yb.GetVoiceSectionByIds(sectionIds)
+			if e != nil {
+				sectionErr = fmt.Errorf("获取语音播报板块列表失败, Err: %s", e.Error())
+				return
+			}
+			for _, v := range sectionList {
+				sectionNameMap[v.SectionId] = v.SectionName
+			}
+		}()
+
+		// 视频社区标题map
+		videoTitleMap := make(map[int]string, 0)
+		rsVideoTitleMap := make(map[int]string, 0)
+		go func() {
+			defer func() {
+				wg.Done()
+			}()
+
+			videoList, e := yb.GetCommunityVideoListByIds(videoIds)
+			if e != nil {
+				videoErr = fmt.Errorf("获取视频社区列表失败, Err: %s", e.Error())
+				return
+			}
+			for _, v := range videoList {
+				videoTitleMap[v.CommunityVideoId] = v.Title
+			}
+
+			rsVideoList, e := yb.GetRoadVideoListByIds(rsVideoIds)
+			if e != nil {
+				videoErr = fmt.Errorf("获取路演视频列表失败, Err: %s", e.Error())
+				return
+			}
+			for _, v := range rsVideoList {
+				rsVideoTitleMap[v.RoadVideoId] = v.Title
+			}
+		}()
+
+		// 问答社区标题map
+		questionTitleMap := make(map[int]string, 0)
+		go func() {
+			defer func() {
+				wg.Done()
+			}()
+
+			questionList, e := yb.GetQuestionListByIds(questionIds)
+			if e != nil {
+				questionErr = fmt.Errorf("获取问答列表失败, Err: %s", e.Error())
+				return
+			}
+			for _, v := range questionList {
+				questionTitleMap[v.CommunityQuestionId] = v.QuestionContent
+			}
+		}()
+
+		wg.Wait()
+		if sectionErr != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = sectionErr.Error()
+			return
+		}
+		if videoErr != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = videoErr.Error()
+			return
+		}
+		if questionErr != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = questionErr.Error()
+			return
+		}
+
+		// 填充产品名/标题
+		for _, v := range list {
+			d := new(response.ProductCensusUserVisitCountDetail)
+			d.VisitCount = v.VisitCount
+			d.Source = v.NewSource
+			d.RecentTime = v.RecentTime
+			if v.ProductType == 1 {
+				d.ProductName = sectionNameMap[v.SectionId]
+				d.Title = v.Title
+			}
+			if v.ProductType == 2 {
+				if v.VideoType == 1 {
+					d.ProductName = "5分钟小视频"
+					d.Title = videoTitleMap[v.TitleId]
+				}
+				if v.VideoType == 2 {
+					d.ProductName = "线上路演"
+					d.Title = rsVideoTitleMap[v.TitleId]
+				}
+			}
+			if v.ProductType == 3 {
+				d.ProductName = "问答社区"
+				d.Title = questionTitleMap[v.TitleId]
+			}
+
+			respList = append(respList, d)
+		}
+	}
+
 	page := paging.GetPaging(currentIndex, pageSize, total)
 	resp := response.ProductCensusUserVisitCountDetailResp{
 		List:   respList,

+ 110 - 0
models/yb/community_audio_listen_log.go

@@ -1,6 +1,7 @@
 package yb
 
 import (
+	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"hongze/hz_crm_api/utils"
 	"time"
@@ -135,6 +136,17 @@ func GetCompanyUserYbProductVisitCountByProductType(userIds []int, productType,
 		pars = append(pars, productId)
 	case 3: // 问答社区
 		sql = `SELECT COUNT(1) AS visit_count, user_id FROM yb_community_audio_listen_log WHERE user_id IN (` + inReplace + `) GROUP BY user_id `
+	case 99: // 全部
+		sql = `SELECT COUNT(1) AS visit_count, t.user_id FROM (
+					SELECT user_id, 1 AS product_id FROM yb_voice_broadcast_statistics WHERE user_id IN (` + inReplace + `)
+					UNION ALL
+					SELECT user_id, 2 FROM yb_community_video_play_log WHERE user_id IN (` + inReplace + `)
+					UNION ALL
+					SELECT user_id, 3 FROM yb_community_audio_listen_log WHERE user_id IN (` + inReplace + `)
+				) AS t
+				GROUP BY t.user_id`
+		_, err = o.Raw(sql, pars, pars, pars).QueryRows(&list)
+		return
 	}
 
 	_, err = o.Raw(sql, pars).QueryRows(&list)
@@ -259,3 +271,101 @@ func GetSellerYbProductUserCountByProductType(productId, productType int, compan
 	_, err = o.Raw(sql, pars).QueryRows(&list)
 	return
 }
+
+// UserYbProductVisitCount 用户研报产品阅读量信息
+type UserYbProductVisitCount struct {
+	ProductType int    `description:"类型名称: 问答社区/线上路演/语音播报板块名称等"`
+	VisitCount  int    `description:"点击量"`
+	TitleId     int    `description:"对应的不同产品的ID"`
+	Title       string `description:"标题"`
+	SectionId   int    `description:"语音播报板块ID"`
+	NewSource   int    `description:"来源:1-小程序 2-PC端"`
+	RecentTime  string `description:"最近点击时间"`
+	VideoType   int    `description:"视频社区类型: 1-视频社区; 2-路演视频"`
+}
+
+// GetUserAllYbProductVisitCountPageList 获取用户所有研报产品阅读量-分页
+func GetUserAllYbProductVisitCountPageList(userId, startSize, pageSize int, orderRule string) (total int, items []*UserYbProductVisitCount, err error) {
+	o := orm.NewOrm()
+	sub := `(
+				SELECT
+					1 AS product_type,
+					COUNT(1) AS visit_count,
+					broadcast_id AS title_id,
+					broadcast_name AS title,
+					section_id,
+					IF (source = 4, 2, source) AS new_source,
+					MAX(create_time) AS recent_time,
+					0 AS video_type
+			FROM
+				yb_voice_broadcast_statistics
+			WHERE
+				user_id = ?
+			GROUP BY
+				broadcast_id,
+				new_source
+			)
+
+			UNION ALL
+			(
+				SELECT
+					2 AS product_type,
+					COUNT(1) AS visit_count,
+					community_video_id,
+					"",
+					0,
+					IF (
+						source_agent = 4,
+						2,
+						source_agent
+					) AS new_source,
+					MAX(create_time) AS recent_time,
+					type AS video_type
+			FROM
+				yb_community_video_play_log
+			WHERE
+				user_id = ?
+			GROUP BY
+				community_video_id,
+				new_source
+			)
+
+			UNION ALL
+			(
+				SELECT
+					3 AS product_type,
+					COUNT(1) AS visit_count,
+					community_question_id,
+					"",
+					0,
+					IF (
+						source_agent = 4,
+						2,
+						source_agent
+					) AS new_source,
+					MAX(create_time) AS recent_time,
+					0 AS video_type
+			FROM
+				yb_community_audio_listen_log
+			WHERE
+				user_id = ?
+			GROUP BY
+				community_question_id,
+				new_source
+			)`
+
+	totalSql := fmt.Sprintf(`SELECT COUNT(1) FROM (%s) AS t`, sub)
+	err = o.Raw(totalSql, userId, userId, userId).QueryRow(&total)
+	if err != nil {
+		return
+	}
+
+	order := `ORDER BY recent_time DESC`
+	if orderRule != "" {
+		order = `ORDER BY ` + orderRule
+	}
+
+	sql := fmt.Sprintf(`SELECT * FROM (%s) AS t %s LIMIT ?, ?`, sub, order)
+	_, err = o.Raw(sql, userId, userId, userId, startSize, pageSize).QueryRows(&items)
+	return
+}

+ 5 - 4
models/yb/response/product_census.go

@@ -19,8 +19,9 @@ type ProductCensusUserVisitCountDetailResp struct {
 
 // ProductCensusUserVisitCountDetail 用户阅读统计详情
 type ProductCensusUserVisitCountDetail struct {
-	VisitCount int    `description:"点击量"`
-	Title      string `description:"标题"`
-	Source     int    `description:"来源:1-小程序 2-PC端"`
-	RecentTime string `description:"最近点击时间"`
+	ProductName string `description:"产品名称: 问答社区/线上路演/语音播报板块名称等"`
+	VisitCount  int    `description:"点击量"`
+	Title       string `description:"标题"`
+	Source      int    `description:"来源:1-小程序 2-PC端"`
+	RecentTime  string `description:"最近点击时间"`
 }

+ 13 - 0
models/yb/voice_section.go

@@ -2,6 +2,7 @@ package yb
 
 import (
 	"github.com/beego/beego/v2/client/orm"
+	"hongze/hz_crm_api/utils"
 )
 
 type VoiceSection struct {
@@ -82,3 +83,15 @@ func GetVoiceSectionById(sectionId int) (item *VoiceSection, err error) {
 	err = o.Raw(sql, sectionId).QueryRow(&item)
 	return
 }
+
+// GetVoiceSectionByIds 根据IDs获取板块
+func GetVoiceSectionByIds(ids []int) (list []*VoiceSection, err error) {
+	arrLen := len(ids)
+	if arrLen == 0 {
+		return
+	}
+	o := orm.NewOrm()
+	sql := `SELECT * FROM yb_voice_section WHERE section_id IN (` + utils.GetOrmInReplace(arrLen) + `)`
+	_, err = o.Raw(sql, ids).QueryRows(&list)
+	return
+}