Explorar o código

添加文章阅读记录

xingzai %!s(int64=2) %!d(string=hai) anos
pai
achega
f30d2501d4

+ 3 - 0
controllers/article.go

@@ -281,6 +281,9 @@ Loop:
 	//var haveResearch bool
 	fmt.Println(hasFree)
 	fmt.Println(haveResearch)
+	if hasPermission == 1 {
+		go services.ArticleHistory(articleId, user)
+	}
 	if detail.SubCategoryName == "专项调研" {
 		detail.IsSpecialArticle = true
 	}

+ 9 - 9
models/activity_special.go

@@ -6,7 +6,7 @@ import (
 	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
-//专项调研活动列表
+// 专项调研活动列表
 type CygxActivitySpecialDetail struct {
 	ActivityId            int    `description:"活动ID "`
 	ActivityTypeName      string `description:"活动名称"`
@@ -41,7 +41,7 @@ type CygxActivitySpecialDetail struct {
 	AdminId               int    `description:"管理员ID"`
 }
 
-//获取数量
+// 获取数量
 func GetActivitySpecialCount(condition string, pars []interface{}) (count int, err error) {
 	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_activity_special as art WHERE 1= 1  `
 	if condition != "" {
@@ -52,7 +52,7 @@ func GetActivitySpecialCount(condition string, pars []interface{}) (count int, e
 	return
 }
 
-//主题列表
+// 主题列表
 func GetActivitySpecialListAll(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivitySpecialDetail, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT	*
@@ -65,7 +65,7 @@ func GetActivitySpecialListAll(condition string, pars []interface{}, startSize,
 	return
 }
 
-//修改发布状态
+// 修改发布状态
 func UpdateActivitySpecialPublishStatus(publishStatus, activityId int) (err error) {
 	sql := ` UPDATE cygx_activity_special SET  publish_status= ?  WHERE activity_id = ?`
 	o := orm.NewOrm()
@@ -73,10 +73,10 @@ func UpdateActivitySpecialPublishStatus(publishStatus, activityId int) (err erro
 	return
 }
 
-//主题列表
+// 主题列表
 func GetActivityLabelSpecialListAll(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityLabelList, err error) {
 	o := orm.NewOrm()
-	sql := `SELECT	label,activity_id,is_show_subject_name
+	sql := `SELECT	label,activity_id,is_show_subject_name,days
 		FROM cygx_activity_special as art WHERE 1= 1 `
 	if condition != "" {
 		sql += condition
@@ -86,7 +86,7 @@ func GetActivityLabelSpecialListAll(condition string, pars []interface{}, startS
 	return
 }
 
-//获取专项调研活动列表
+// 获取专项调研活动列表
 func GetCygxActivitySpecialDetailList(condition string, pars []interface{}, uid, startSize, pageSize int) (items []*CygxActivitySpecialDetail, err error) {
 	o := orm.NewOrm()
 	sql := ` SELECT
@@ -130,7 +130,7 @@ type CygxActivitySpecialResp struct {
 	Detail        *CygxActivitySpecialDetail
 }
 
-//通过活动ID获取活动详情
+// 通过活动ID获取活动详情
 func GetCygxActivitySpecialDetailById(uid, ActivityId int) (item *CygxActivitySpecialDetail, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT *,( SELECT COUNT( 1 ) FROM cygx_activity_special_signup AS s WHERE s.activity_id = a.activity_id AND s.user_id = ? ) AS is_signup 
@@ -148,7 +148,7 @@ type ActivityIdRep struct {
 	PlaySeconds int `description:"播放时长"`
 }
 
-//通过活动ID获取活动详情
+// 通过活动ID获取活动详情
 func GetCygxActivitySpecialDetail(ActivityId int) (item *CygxActivitySpecialDetail, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT * FROM cygx_activity_special  WHERE activity_id=? AND publish_status = 1 `

+ 142 - 0
models/article_history_record_newpv.go

@@ -0,0 +1,142 @@
+package models
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"hongze/hongze_clpt/utils"
+	"time"
+)
+
+type CygxArticleHistoryRecordNewpv struct {
+	Id          int `orm:"column(id);pk"`
+	ArticleId   int
+	UserId      int
+	CreateTime  time.Time
+	ModifyTime  time.Time
+	Mobile      string `description:"手机号"`
+	Email       string `description:"邮箱"`
+	CompanyId   int    `description:"公司id"`
+	CompanyName string `description:"公司名称"`
+	StopTime    int    `description:"停留时间"`
+	OutType     int    `description:"退出方式,1正常退出,2强制关闭"`
+	Source      string `description:"来源,MOBILE:手机端,PC:电脑端"`
+}
+
+type CygxArticleHistoryRecord struct {
+	Id          int `orm:"column(id);pk"`
+	ArticleId   int
+	UserId      int
+	CreateTime  time.Time
+	Mobile      string    `description:"手机号"`
+	Email       string    `description:"邮箱"`
+	CompanyId   int       `description:"公司id"`
+	CompanyName string    `description:"公司名称"`
+	ModifyTime  time.Time `description:"修改时间"`
+	StopTime    int       `description:"停留时间"`
+	OutType     int       `description:"退出方式,1正常退出,2强制关闭"`
+}
+
+// 添加阅读记录信息
+func AddCygxArticleViewRecordNewpv(item *CygxArticleHistoryRecordNewpv) (lastId int64, err error) {
+	o, err := orm.NewOrm().Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		fmt.Println(err)
+		if err == nil {
+			o.Commit()
+		} else {
+			o.Rollback()
+		}
+	}()
+	lastId, err = o.Insert(item)
+	if err != nil {
+		return
+	}
+
+	historyRecord := new(CygxArticleHistoryRecord)
+	historyRecord.UserId = item.UserId
+	historyRecord.ArticleId = item.ArticleId
+	historyRecord.CreateTime = time.Now()
+	historyRecord.Mobile = item.Mobile
+	historyRecord.Email = item.Email
+	historyRecord.CompanyId = item.CompanyId
+	historyRecord.CompanyName = item.CompanyName
+	lastId, err = o.Insert(historyRecord)
+	if err != nil {
+		return
+	}
+
+	//写入记录到总的统计表
+	record := new(CygxArticleHistoryRecordAll)
+	record.UserId = item.UserId
+	record.ArticleId = item.ArticleId
+	record.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
+	record.ModifyTime = item.ModifyTime
+	record.Mobile = item.Mobile
+	record.Email = item.Email
+	record.CompanyId = item.CompanyId
+	record.CompanyName = item.CompanyName
+	record.StopTime = item.StopTime
+	record.OutType = item.OutType
+	record.Source = item.Source
+	record.Platfor = 1
+	lastId, err = o.Insert(record)
+	if err != nil {
+		return
+	}
+
+	// 软删除当天策略平台的文章阅读记录
+	if item.Mobile != "" {
+		sql := `UPDATE cygx_article_history_record_all 
+			SET is_del = 1 
+			WHERE
+			article_id = ? 
+			AND mobile = ?
+			AND platfor = 2
+			AND create_time >= date(NOW()) `
+		_, err = o.Raw(sql, record.ArticleId, record.Mobile).Exec()
+	}
+	return
+}
+
+type CygxArticleHistoryRecordAll struct {
+	Id             int `orm:"column(id);pk"`
+	ArticleId      int
+	UserId         int
+	CreateTime     string
+	ModifyTime     time.Time
+	Mobile         string    `description:"手机号"`
+	Email          string    `description:"邮箱"`
+	CompanyId      int       `description:"公司id"`
+	CompanyName    string    `description:"公司名称"`
+	StopTime       int       `description:"停留时间"`
+	OutType        int       `description:"退出方式,1正常退出,2强制关闭"`
+	Source         string    `description:"来源,MOBILE:手机端,PC:电脑端"`
+	RealName       string    `description:"用户实际名称"`
+	CreateDateApi  time.Time `description:"同步创建时间"`
+	CelueHistoryId int       `description:"策略平台记录的ID"`
+	Platfor        int       `description:"PV阅读记录来源,1:查研观向,2:策略平台"`
+	IsDel          int       `description:"是否删除"`
+}
+
+type EsUserInteraction struct {
+	Id                       int    `description:"主键ID"`
+	ArticleId                int    `description:"文章id"`
+	ArticleType              int    `description:"文章类型 1:查研观向, 2:策略平台"`
+	Title                    string `description:"标题"`
+	PublishDate              string `description:"发布时间"`
+	CreateTime               string `description:"创建时间"`
+	StopTime                 string `description:"阅读停留时间"`
+	RealName                 string `description:"姓名"`
+	CompanyName              string `description:"公司名称"`
+	CompanyId                int    `description:"公司ID"`
+	SellerName               string `description:"所属销售"`
+	SellerId                 int    `description:"所属销售ID"`
+	Mobile                   string `description:"手机号"`
+	Email                    string `description:"邮箱"`
+	UserId                   int    `description:"用户ID"`
+	UserArticleHistoryNum    int    `description:"用户阅读数量"`
+	CompanyArticleHistoryNum int    `description:"机构阅读数量"`
+}

+ 3 - 0
models/db.go

@@ -57,6 +57,9 @@ func init() {
 		new(CygxPageHistoryRecord),
 		new(CygxArticleDepartmentFollow),
 		new(CygxArticleComment),
+		new(CygxArticleHistoryRecordNewpv),
+		new(CygxArticleHistoryRecord),
+		new(CygxArticleHistoryRecordAll),
 	)
 	// 记录ORM查询日志
 	orm.Debug = true

+ 84 - 0
services/article_red.go

@@ -2,8 +2,10 @@ package services
 
 import (
 	"errors"
+	"fmt"
 	"hongze/hongze_clpt/models"
 	"hongze/hongze_clpt/utils"
+	"time"
 )
 
 //处理文章小红点
@@ -60,3 +62,85 @@ func GetShowTimeLineIsRed(user *models.WxUserItem, industrialManagementId int) (
 	return
 
 }
+
+// UserViewRedisData 阅读数据
+type UserViewRedisData struct {
+	Mobile      string `json:"mobile"`
+	Email       string `json:"email"`
+	RealName    string `json:"real_name"`
+	CompanyName string `json:"company_name"`
+	ViewTime    string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
+	ProductId   int    `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
+	CompanyId   int    `json:"company_id" description:"客户id"`
+}
+
+type ReportViewRecord struct {
+	Id          int       `orm:"column(id);pk"`
+	UserId      int       `description:"用户id"`
+	ReportId    int       `description:"报告id"`
+	Mobile      string    `description:"手机号"`
+	Email       string    `description:"邮箱"`
+	RealName    string    `description:"用户实际姓名"`
+	CompanyName string    `description:"公司名称"`
+	CreateTime  time.Time `description:"创建时间"`
+}
+
+// PushViewRecordNewRedisData 阅读数据加入到redis
+func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId int) bool {
+	data := &UserViewRedisData{
+		Mobile:      reportViewRecord.Mobile,
+		Email:       reportViewRecord.Email,
+		RealName:    reportViewRecord.RealName,
+		CompanyName: reportViewRecord.CompanyName,
+		ViewTime:    reportViewRecord.CreateTime.Format(utils.FormatDateTime),
+		ProductId:   2,
+		CompanyId:   companyId,
+	}
+	if utils.Re == nil {
+		err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)
+		if err != nil {
+			fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
+		}
+		return true
+	}
+	return false
+}
+
+// 记录用户文章浏览记录
+func ArticleHistory(articleId int, user *models.WxUserItem) (err error) {
+	defer func() {
+		if err != nil {
+			go utils.SendAlarmMsg("记录用户文章浏览记录,失败"+err.Error(), 2)
+		}
+	}()
+	uid := user.UserId
+	if uid == 0 {
+		return
+	}
+	record := new(models.CygxArticleHistoryRecordNewpv)
+	record.UserId = uid
+	record.ArticleId = articleId
+	record.CreateTime = time.Now()
+	record.ModifyTime = time.Now()
+	record.Mobile = user.Mobile
+	record.Email = user.Email
+	record.CompanyId = user.CompanyId
+	record.CompanyName = user.CompanyName
+	record.StopTime = 0
+	record.OutType = 1
+	record.Source = "WEB"
+
+	recordRedis := new(ReportViewRecord)
+	recordRedis.UserId = user.UserId
+	recordRedis.ReportId = articleId
+	recordRedis.Mobile = user.Mobile
+	recordRedis.Email = user.Email
+	recordRedis.RealName = user.RealName
+	recordRedis.CompanyName = user.CompanyName
+	recordRedis.CreateTime = time.Now()
+
+	go PushViewRecordNewRedisData(recordRedis, user.CompanyId)
+
+	_, err = models.AddCygxArticleViewRecordNewpv(record)
+	return
+}

+ 6 - 0
utils/constants.go

@@ -59,6 +59,12 @@ var (
 	AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
 )
 
+// 缓存key
+const (
+	CACHE_KEY_USER_VIEW           = "user_view_record"           //用户阅读数据
+	CACHE_KEY_ARTICLE_ID_TEMPLATE = "ce_lue_article_id_template" //策略平台同步过来的文章ID,做微信模板消息推送,同一篇报告12小时只推送一次
+)
+
 const (
 	CE_LUE_NAME                      string = "策略"
 	CE_LUE_ID                        int    = 23