Преглед на файлове

Merge branch 'yb/medie_view_duration' into debug

hsun преди 2 години
родител
ревизия
6cab8b12d0

+ 3 - 2
controller/community/question.go

@@ -339,11 +339,12 @@ func AddAudioLog(c *gin.Context) {
 		return
 	}
 	userinfo := user.GetInfoByClaims(c)
-	if err := community.AddAudioListenLog(userinfo, req.CommunityQuestionAudioID, req.SourceAgent); err != nil {
+	newId, err := community.AddAudioListenLog(userinfo, req.CommunityQuestionAudioID, req.SourceAgent)
+	if err != nil {
 		response.Fail("操作失败: "+err.Error(), c)
 		return
 	}
-	response.Ok("操作成功", c)
+	response.OkData("操作成功", newId, c)
 }
 
 // SetLikeOrTease 问答点赞/吐槽设置

+ 2 - 2
controller/community/video.go

@@ -82,12 +82,12 @@ func VideoPlayLog(c *gin.Context) {
 		return
 	}
 	userinfo := user.GetInfoByClaims(c)
-	errMsg, err := community.SaveVideoPlayLog(userinfo, req.VideoId, req.SourceAgent, 1)
+	newId, errMsg, err := community.SaveVideoPlayLog(userinfo, req.VideoId, req.SourceAgent, 1)
 	if err != nil {
 		response.FailMsg(errMsg, "VideoPlayLog ErrMsg:"+err.Error(), c)
 		return
 	}
-	response.Ok("操作成功", c)
+	response.OkData("操作成功", newId, c)
 }
 
 

+ 30 - 0
controller/public.go

@@ -11,6 +11,7 @@ import (
 	"hongze/hongze_yb/models/tables/yb_suncode_pars"
 	"hongze/hongze_yb/services"
 	"hongze/hongze_yb/services/alarm_msg"
+	"hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
 	"io/ioutil"
 	"os"
@@ -258,3 +259,32 @@ func UploadAudio(c *gin.Context) {
 	}
 	response.OkData("上传成功", resp, c)
 }
+
+// UpdateViewLog
+// @Description 更新各模块访问/点击日志(有时间的话统一改版做一个入口,统一访问日志)
+// @Success 200 {string} string "更新成功"
+// @Router /public/view_log/update [post]
+func UpdateViewLog(c *gin.Context) {
+	var req request.ViewLogUpdateReq
+	if err := c.Bind(&req); err != nil {
+		response.Fail("参数有误:"+err.Error(), c)
+		return
+	}
+	if req.Id <= 0 {
+		response.Fail("参数有误", c)
+		return
+	}
+	if req.Source <= 0 {
+		response.Fail("来源有误", c)
+		return
+	}
+
+	userInfo := user.GetInfoByClaims(c)
+	err := services.UpdateViewLogBySource(int(userInfo.UserID), req.Id, req.StopSeconds, req.Source)
+	if err != nil {
+		fmt.Println(err.Error())
+		response.FailMsg("更新日志失败", err.Error(), c)
+		return
+	}
+	response.Ok("更新成功", c)
+}

+ 3 - 4
controller/road/video.go

@@ -45,7 +45,6 @@ func VideoList(c *gin.Context) {
 	response.OkData("获取成功", list, c)
 }
 
-
 // VideoPlayLog 记录视频播放日志
 // @Tags 视频社区模块
 // @Description 记录视频播放日志
@@ -69,10 +68,10 @@ func VideoPlayLog(c *gin.Context) {
 		return
 	}
 	userinfo := user.GetInfoByClaims(c)
-	errMsg, err := community.SaveVideoPlayLog(userinfo, req.VideoId, req.SourceAgent, 2)
+	newId, errMsg, err := community.SaveVideoPlayLog(userinfo, req.VideoId, req.SourceAgent, 2)
 	if err != nil {
 		response.FailMsg(errMsg, "VideoPlayLog ErrMsg:"+err.Error(), c)
 		return
 	}
-	response.Ok("操作成功", c)
-}
+	response.OkData("操作成功", newId, c)
+}

+ 6 - 2
controller/voice_broadcast/voice_broadcast.go

@@ -362,9 +362,13 @@ func AddStatistics(c *gin.Context) {
 	}
 	userinfo := user.GetInfoByClaims(c)
 
-	go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
+	newId, err := services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
+	if err != nil {
+		response.FailMsg("新增播放记录失败", "AddStatistics ErrMsg:"+err.Error(), c)
+		return
+	}
 
-	response.Ok("新增记录成功", c)
+	response.OkData("新增记录成功", newId, c)
 }
 
 // BroadcastDetail 获取语音播报详情

+ 7 - 0
models/request/public.go

@@ -8,3 +8,10 @@ type RddpReportShareImgReq struct {
 type WechatWarningReq struct {
 	Content string `description:"预警信息" json:"content"`
 }
+
+// ViewLogUpdateReq 更新访问日志请求体
+type ViewLogUpdateReq struct {
+	Id          int `json:"id" description:"日志ID"`
+	StopSeconds int `json:"stop_seconds" description:"访问时长"`
+	Source      int `json:"source" description:"来源:1-问答社区; 2-语音播报; 3-视频社区; 4-路演视频..."`
+}

+ 11 - 1
models/tables/voice_broadcast_statistics/create.go

@@ -6,4 +6,14 @@ import "hongze/hongze_yb/global"
 func (voiceBroadcastStatistics *VoiceBroadcastStatistics) AddBroadcastStatistics() (err error) {
 	err = global.DEFAULT_MYSQL.Create(voiceBroadcastStatistics).Error
 	return
-}
+}
+
+func (l *VoiceBroadcastStatistics) GetById() (item *VoiceBroadcastStatistics, err error) {
+	err = global.DEFAULT_MYSQL.Model(l).Where("id = ?", l.Id).First(&item).Error
+	return
+}
+
+func (l *VoiceBroadcastStatistics) Update(updateCols []string) (err error) {
+	err = global.DEFAULT_MYSQL.Model(l).Select(updateCols).Updates(*l).Error
+	return
+}

+ 1 - 0
models/tables/voice_broadcast_statistics/voice_broadcast_statistics.go

@@ -21,6 +21,7 @@ type VoiceBroadcastStatistics struct {
 	Author        string `description:"语音管理员"`
 	PublishTime   string `description:"发布时间"`
 	CreateTime    string `description:"访问时间"`
+	StopSeconds   int    `description:"停止时的秒数"`
 }
 
 // TableName 表名变更

+ 2 - 1
models/tables/yb_community_audio_listen_log/entity.go

@@ -14,7 +14,8 @@ type YbCommunityAudioListenLog struct {
 	SellerID                 int       `gorm:"column:seller_id;type:int(11)" json:"sellerId"`
 	SourceAgent              int       `gorm:"column:source_agent;type:tinyint(4);default:1" json:"source_agent"`             // 操作来源,1:小程序,2:小程序 pc 3:弘则研究公众号,4:web pc
 	CreateTime               time.Time `gorm:"column:create_time;type:datetime;default:CURRENT_TIMESTAMP" json:"create_time"` // 创建日志时间
-	RealName                 string    `gorm:"column:real_name;type:varchar(100);not null;default:''" json:"real_name"`                //用户姓名
+	RealName                 string    `gorm:"column:real_name;type:varchar(100);not null;default:''" json:"real_name"`       //用户姓名
+	StopSeconds              int       `gorm:"column:stop_seconds" json:"stop_seconds"`                                       //停止时的秒数
 }
 
 // TableName get sql table name.获取数据库表名

+ 10 - 0
models/tables/yb_community_audio_listen_log/model.go

@@ -6,3 +6,13 @@ func (l *YbCommunityAudioListenLog) Create() (err error) {
 	err = global.DEFAULT_MYSQL.Create(l).Error
 	return
 }
+
+func (l *YbCommunityAudioListenLog) GetById() (item *YbCommunityAudioListenLog, err error) {
+	err = global.DEFAULT_MYSQL.Model(l).Where("id = ?", l.Id).First(&item).Error
+	return
+}
+
+func (l *YbCommunityAudioListenLog) Update(updateCols []string) (err error) {
+	err = global.DEFAULT_MYSQL.Model(l).Select(updateCols).Updates(*l).Error
+	return
+}

+ 3 - 0
models/tables/yb_community_video_play_log/entity.go

@@ -17,6 +17,7 @@ type YbCommunityVideoPlayLog struct {
 	SellerID         int       `gorm:"column:seller_id;type:int(11)" json:"sellerId"`
 	Type             int8      `gorm:"column:type;type:tinyint(4); unsigned;not null;default:1" json:"type"`         //统计类型(1 视频社区,2 路演视频)
 	CreateTime       time.Time `gorm:"column:create_time;type:datetime;default:CURRENT_TIMESTAMP" json:"createTime"` // 创建时间
+	StopSeconds      int       `gorm:"column:stop_seconds" json:"stop_seconds"`                                      //停止时的秒数
 }
 
 // TableName get sql table name.获取数据库表名
@@ -37,6 +38,7 @@ var YbCommunityVideoPlayLogColumns = struct {
 	CompanyStatus    string
 	SourceAgent      string
 	CreateTime       string
+	StopSeconds      string
 }{
 	ID:               "id",
 	CommunityVideoID: "community_video_id",
@@ -49,4 +51,5 @@ var YbCommunityVideoPlayLogColumns = struct {
 	CompanyStatus:    "company_status",
 	SourceAgent:      "source_agent",
 	CreateTime:       "create_time",
+	StopSeconds:      "stop_seconds",
 }

+ 11 - 1
models/tables/yb_community_video_play_log/model.go

@@ -5,4 +5,14 @@ import "hongze/hongze_yb/global"
 func (item *YbCommunityVideoPlayLog) Create() (err error) {
 	err = global.DEFAULT_MYSQL.Create(item).Error
 	return
-}
+}
+
+func (l *YbCommunityVideoPlayLog) GetById() (item *YbCommunityVideoPlayLog, err error) {
+	err = global.DEFAULT_MYSQL.Model(l).Where("id = ?", l.ID).First(&item).Error
+	return
+}
+
+func (l *YbCommunityVideoPlayLog) Update(updateCols []string) (err error) {
+	err = global.DEFAULT_MYSQL.Model(l).Select(updateCols).Updates(*l).Error
+	return
+}

+ 5 - 3
models/tables/yb_view_log/model.go

@@ -8,6 +8,7 @@ func (i *YbViewLog) Create() (err error) {
 	err = global.DEFAULT_MYSQL.Create(i).Error
 	return
 }
+
 // Update 更新对应字段数据
 func (i *YbViewLog) Update(updateCols []string) (err error) {
 	err = global.DEFAULT_MYSQL.Model(i).Select(updateCols).Updates(*i).Error
@@ -18,11 +19,12 @@ type ActivityAudioLogAddReq struct {
 	PrimaryId int    `json:"primary_id" ` //访问类型对应主键ID(如:活动音频记录音频ID)
 	ExtendId  int    `json:"extend_id" `  //扩展ID(如:活动音频记录活动ID)
 	Source    uint8  `json:"source" `     //来源:1-小程序;2-小程序PC端;3-公众号;4-Web PC端
-	FromPage  string `json:"from_page"`  //来源页面
+	FromPage  string `json:"from_page"`   //来源页面
 }
 type ActivityAudioLogAddResp struct {
-	Id          uint64 `json:"id"`           //日志ID
+	Id uint64 `json:"id"` //日志ID
 }
+
 type ActivityAudioLogStopReq struct {
 	Id          uint64 `json:"id"`           //日志ID
 	StopSeconds int    `json:"stop_seconds"` //停止时的秒数
@@ -31,4 +33,4 @@ type ActivityAudioLogStopReq struct {
 func (i *YbViewLog) GetById() (item *YbViewLog, err error) {
 	err = global.DEFAULT_MYSQL.Model(i).Where("id = ?", i.Id).First(&item).Error
 	return
-}
+}

+ 1 - 0
routers/public.go

@@ -28,5 +28,6 @@ func initPublic(r *gin.Engine) {
 		rGroup.GET("/get_variety_tag_tree", controller.GetVarietyTagTree)
 		rGroup.POST("/wechat_warning", controller.WechatWarning)
 		rGroup.POST("/upload_audio", controller.UploadAudio)
+		rGroup.POST("/view_log/update", controller.UpdateViewLog)
 	}
 }

+ 2 - 1
services/community/question.go

@@ -461,7 +461,7 @@ func GetResearchGroupTree() (respList []*response.ResearchGroupItem, err error)
 }
 
 // AddAudioListenLog 添加用户点击音频日志
-func AddAudioListenLog(userInfo user.UserInfo, audioId int, sourceAgent int) (err error) {
+func AddAudioListenLog(userInfo user.UserInfo, audioId int, sourceAgent int) (newId int, err error) {
 	//1. 查询音频是否存在
 	audio, err := yb_community_question_audio.GetByAudioId(audioId)
 	if err != nil && err != utils.ErrNoRow {
@@ -505,6 +505,7 @@ func AddAudioListenLog(userInfo user.UserInfo, audioId int, sourceAgent int) (er
 		err = errors.New("新增点击日志失败 Err:" + err.Error())
 		return
 	}
+	newId = item.Id
 	return
 }
 

+ 2 - 1
services/community/video.go

@@ -77,7 +77,7 @@ func GetVideoList(userId, pageIndex, pageSize, videoId, varietyTagId int, keywor
 }
 
 // SaveVideoPlayLog 记录用户播放视频日志
-func SaveVideoPlayLog(userInfo user.UserInfo, videoId, sourceAgent int, videoType int8) (errMsg string, err error) {
+func SaveVideoPlayLog(userInfo user.UserInfo, videoId, sourceAgent int, videoType int8) (newId int, errMsg string, err error) {
 	if videoType == 1 {
 		_, e := yb_community_video.GetItemById(videoId)
 		if e != nil {
@@ -130,6 +130,7 @@ func SaveVideoPlayLog(userInfo user.UserInfo, videoId, sourceAgent int, videoTyp
 		err = errors.New("新增播放视频日志失败, Err:" + e.Error())
 		return
 	}
+	newId = item.ID
 	return
 }
 

+ 3 - 3
services/voice_broadcast.go

@@ -135,8 +135,7 @@ func GetVoiceAdminByUserInfo(userInfo user.UserInfo) (ok bool, adminInfo *admin2
 	return
 }
 
-func AddBroadcastRecord(userinfo user.UserInfo, source, broadcastId int) {
-	var err error
+func AddBroadcastRecord(userinfo user.UserInfo, source, broadcastId int) (newId int, err error) {
 	defer func() {
 		if err != nil {
 			global.LOG.Critical(fmt.Sprintf("AddBroadcastLog: userId=%d, err:%s", userinfo.UserID, err.Error()))
@@ -175,6 +174,7 @@ func AddBroadcastRecord(userinfo user.UserInfo, source, broadcastId int) {
 	if err != nil {
 		return
 	}
+	newId = voiceBroadcastStatistics.Id
 	return
 }
 
@@ -443,7 +443,7 @@ func handleBroadcastItem(userInfo user.UserInfo, item *voice_broadcast.VoiceBroa
 	if err != nil {
 		return
 	}
-	if int(userInfo.UserID) == item.AuthorId && ok{
+	if int(userInfo.UserID) == item.AuthorId && ok {
 		resp.IsAuthor = true
 		if item.MsgState == 0 {
 			resp.CouldSendMsg = true

+ 94 - 15
services/yb_common.go

@@ -2,20 +2,32 @@ package services
 
 import (
 	"errors"
+	"gorm.io/gorm"
 	"hongze/hongze_yb/models/tables/company_product"
 	"hongze/hongze_yb/models/tables/rddp/report"
 	"hongze/hongze_yb/models/tables/rddp/report_chapter"
 	"hongze/hongze_yb/models/tables/research_report_type"
+	"hongze/hongze_yb/models/tables/voice_broadcast_statistics"
+	"hongze/hongze_yb/models/tables/yb_community_audio_listen_log"
+	"hongze/hongze_yb/models/tables/yb_community_video_play_log"
 	"hongze/hongze_yb/models/tables/yb_like"
 	"hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
 	"strings"
 )
 
+// 访问日志来源
+const (
+	ViewLogSourceQuestion       = iota + 1 // 问答社区-1
+	ViewLogSourceVoiceBroadcast            // 语音播报-2
+	ViewLogSourceVideo                     // 视频社区-3
+	ViewLogSourceRoadVideo                 // 路演视频-4
+)
+
 // GetReportIdReportChapterIdByOldReportId 根据老报告的ID查询对应的新报告ID
-func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uint64) (reportId int, reportChapterId int, err error, errMsg string)  {
+func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uint64) (reportId int, reportChapterId int, err error, errMsg string) {
 	var reportNew *report.Report
-	if oldReportChapterId >0 {
+	if oldReportChapterId > 0 {
 		//查询章节详情,根据章节类型ID和老报告ID,根据老报告ID,查询新报告ID,根据新报告ID和type_id 找到新的章节ID
 		var oldReportChapter *research_report_type.ResearchReportTypeInfo
 		var reportChapterNew *report_chapter.ReportChapter
@@ -25,7 +37,7 @@ func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uin
 			err = errors.New("找不到报告章节")
 			return
 		}
-		if  oldReportChapter == nil {
+		if oldReportChapter == nil {
 			err = errors.New("找不到报告章节")
 			return
 		}
@@ -44,7 +56,7 @@ func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uin
 			//err = errors.New("找不到新版报告")
 			return
 		}
-		if reportNew.Id <=0 {
+		if reportNew.Id <= 0 {
 			//err = errors.New("找不到新版报告")
 			return
 		}
@@ -60,7 +72,7 @@ func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uin
 		reportId = reportNew.Id
 		reportChapterId = reportChapterNew.ReportChapterId
 		return
-	}else if oldReportId > 0 {
+	} else if oldReportId > 0 {
 		// 查询新报告ID
 		reportNew, err = report.GetReportByOldReportId(oldReportId)
 		if err != nil {
@@ -69,24 +81,25 @@ func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uin
 			return
 		}
 		reportId = reportNew.Id
-        return
+		return
 	}
 	return
 }
+
 // CheckReportExistByReportIdReportChapterId 评论和点赞时,校验传入的报告ID是否正确
-func CheckReportExistByReportIdReportChapterId(reportId, reportChapterId int)(err error, errMsg string) {
+func CheckReportExistByReportIdReportChapterId(reportId, reportChapterId int) (err error, errMsg string) {
 	reportInfo, err := report.GetByReportId(reportId)
 	if err != nil {
 		errMsg = err.Error()
 		err = errors.New("查询报告出错")
 		return
 	}
-	if reportInfo.Id <=0  {
+	if reportInfo.Id <= 0 {
 		err = errors.New("报告不存在")
 		return
 	}
 
-	if (reportInfo.ClassifyNameFirst == "晨报" || reportInfo.ClassifyNameFirst == "周报") && reportChapterId <=0 {
+	if (reportInfo.ClassifyNameFirst == "晨报" || reportInfo.ClassifyNameFirst == "周报") && reportChapterId <= 0 {
 		err = errors.New("请输入报告章节ID")
 		return
 	}
@@ -113,7 +126,7 @@ func CheckReportExistByReportIdReportChapterId(reportId, reportChapterId int)(er
 }
 
 // CheckSimpleCompanyProduct 校验用户是否FICC产品的已购或者试用状态
-func CheckSimpleCompanyProduct(userinfo user.UserInfo) (err error, errMsg string){
+func CheckSimpleCompanyProduct(userinfo user.UserInfo) (err error, errMsg string) {
 	// 判断用户状态是否是正常和永续
 	var productAuthOk bool
 	companyProduct, err := company_product.GetByCompany2ProductId(userinfo.CompanyID, 1)
@@ -141,15 +154,15 @@ func CheckSimpleCompanyProduct(userinfo user.UserInfo) (err error, errMsg string
 	return
 }
 
-func GetReportLikeByReportIdOldReportId(userId uint64,reportId, reportChapterId int, oldReportId, oldReportChapterId int) (likeNum int64, likeEnabled int8, err error)  {
+func GetReportLikeByReportIdOldReportId(userId uint64, reportId, reportChapterId int, oldReportId, oldReportChapterId int) (likeNum int64, likeEnabled int8, err error) {
 	// 根据老报告找到新报告的ID
-	if reportId == 0 && oldReportId > 0{
+	if reportId == 0 && oldReportId > 0 {
 		reportId, reportChapterId, err, _ = GetReportIdReportChapterIdByOldReportId(uint64(oldReportId), uint64(oldReportChapterId))
 	}
 	//查询总的点赞数
-	if reportId > 0{
+	if reportId > 0 {
 		likeNum, err = yb_like.GetLikeNumByReportId(reportId, reportChapterId)
-	} else if oldReportId > 0{
+	} else if oldReportId > 0 {
 		likeNum, err = yb_like.GetLikeNumByOldReportId(oldReportId, oldReportChapterId)
 	}
 
@@ -171,4 +184,70 @@ func GetReportLikeByReportIdOldReportId(userId uint64,reportId, reportChapterId
 	}
 	likeEnabled = likeItem.Enabled
 	return
-}
+}
+
+// UpdateViewLogBySource 更新各模块访问日志
+// Source: 来源:1-问答社区; 2-语音播报; 3-视频社区; 4-路演视频...
+func UpdateViewLogBySource(userId, viewLogId, stopSeconds, source int) (err error) {
+	if viewLogId <= 0 || source <= 0 {
+		return
+	}
+
+	switch source {
+	case ViewLogSourceQuestion:
+		viewLog := new(yb_community_audio_listen_log.YbCommunityAudioListenLog)
+		viewLog.Id = viewLogId
+		item, e := viewLog.GetById()
+		if e != nil {
+			if e == gorm.ErrRecordNotFound {
+				return
+			}
+			err = errors.New("获取访问日志失败, Err: " + e.Error())
+			return
+		}
+		if item.UserID != userId {
+			err = errors.New("用户不一致, 更新日志失败")
+			return
+		}
+		item.StopSeconds = stopSeconds
+		err = item.Update([]string{"stop_seconds"})
+	case ViewLogSourceVoiceBroadcast:
+		viewLog := new(voice_broadcast_statistics.VoiceBroadcastStatistics)
+		viewLog.Id = viewLogId
+		item, e := viewLog.GetById()
+		if e != nil {
+			if e == gorm.ErrRecordNotFound {
+				return
+			}
+			err = errors.New("获取访问日志失败, Err: " + e.Error())
+			return
+		}
+		if item.UserId != userId {
+			err = errors.New("用户不一致, 更新日志失败")
+			return
+		}
+		item.StopSeconds = stopSeconds
+		err = item.Update([]string{"stop_seconds"})
+	case ViewLogSourceVideo, ViewLogSourceRoadVideo:
+		viewLog := new(yb_community_video_play_log.YbCommunityVideoPlayLog)
+		viewLog.ID = viewLogId
+		item, e := viewLog.GetById()
+		if e != nil {
+			if e == gorm.ErrRecordNotFound {
+				return
+			}
+			err = errors.New("获取访问日志失败, Err: " + e.Error())
+			return
+		}
+		if item.UserID != userId {
+			err = errors.New("用户不一致, 更新日志失败")
+			return
+		}
+		item.StopSeconds = stopSeconds
+		err = item.Update([]string{"stop_seconds"})
+	default:
+		err = errors.New("来源有误")
+		return
+	}
+	return
+}