Browse Source

Merge branch 'xi_viewTotal'

ziwen 3 years ago
parent
commit
3010e610d9

+ 37 - 16
logic/report/research_report.go

@@ -7,7 +7,7 @@ import (
 	"hongze/hongze_yb/models/tables/company_report_permission"
 	"hongze/hongze_yb/models/tables/research_report"
 	"hongze/hongze_yb/models/tables/research_report_type"
-	"hongze/hongze_yb/models/tables/user_record"
+	"hongze/hongze_yb/models/tables/user_view_history"
 	"hongze/hongze_yb/models/tables/wx_user"
 	"hongze/hongze_yb/utils"
 	"strconv"
@@ -84,9 +84,20 @@ func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchRepo
 		}
 		return
 	}
-	userViewHistory := &user_record.UserViewHistory{
-		ViewHistoryId:        0,
-		UserId:               userId,
+
+	//查询是否读过这篇报告,如果未读过则阅读人数+1
+	_, err = user_view_history.GetReportByUserId(userId, reportInfo.ResearchReportID)
+	if err != nil {
+		err = reportInfo.UpdateViewers()
+		if err != nil {
+			fmt.Println("UpdateViewers err:", err.Error())
+		}
+	}
+
+	//新增userViewHistory记录
+	userViewHistory := &user_view_history.UserViewHistory{
+		ViewHistoryID:        0,
+		UserID:               userId,
 		Mobile:               wxUserInfo.Mobile,
 		Email:                wxUserInfo.Email,
 		RealName:             wxUserInfo.RealName,
@@ -94,23 +105,23 @@ func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchRepo
 		ViewTitle:            "",
 		ViewPage:             "",
 		ReportChapterModule:  "",
-		CreatedTime:           time.Now(),
+		CreatedTime:          time.Now(),
 		LastUpdatedTime:      time.Now(),
 		Type:                 "weekly_report",
-		ResearchReportId:     0,
-		ResearchReportTypeId: 0,
+		ResearchReportID:     reportInfo.ResearchReportID,
+		ResearchReportTypeID: 0,
 	}
-	historyId, err := user_record.AddUserViewHistory(userViewHistory)
+	err = userViewHistory.AddUserViewHistory()
 	if err != nil {
 		fmt.Println("AddUserViewHistory err", err.Error())
 	}
-	fmt.Println("new historyId:", historyId)
 
 	result = ResearchReportInfo{
 		ResearchReportInfo:     reportInfo,
 		ResearchReportTypeList: researchReportTypeList,
 		HasMenu:                1,
 	}
+
 	if len(researchReportTypeList) <= 0 {
 
 	} else if len(researchReportTypeList) == 1 {
@@ -204,9 +215,20 @@ func GetResearchReportTypeContentInfo(researchReportTypeId, userId uint64) (resu
 		}
 		return
 	}
-	userViewHistory := &user_record.UserViewHistory{
-		ViewHistoryId:        0,
-		UserId:               userId,
+
+	//查询是否读过这篇章节,如果未读过则阅读人数+1
+	_, err = user_view_history.GetReportTypeByUserId(userId, researchReportTypeId)
+	if err != nil {
+		err = reportInfo.UpdateViewers()
+		if err != nil {
+			fmt.Println("UpdateViewers err:", err.Error())
+		}
+	}
+
+	//新增userViewHistory记录
+	userViewHistory := &user_view_history.UserViewHistory{
+		ViewHistoryID:        0,
+		UserID:               userId,
 		Mobile:               wxUserInfo.Mobile,
 		Email:                wxUserInfo.Email,
 		RealName:             wxUserInfo.RealName,
@@ -217,14 +239,13 @@ func GetResearchReportTypeContentInfo(researchReportTypeId, userId uint64) (resu
 		CreatedTime:          time.Now(),
 		LastUpdatedTime:      time.Now(),
 		Type:                 "weekly_report",
-		ResearchReportId:     0,
-		ResearchReportTypeId: 0,
+		ResearchReportID:     reportInfo.ResearchReportID,
+		ResearchReportTypeID: researchReportTypeId,
 	}
-	historyId, err := user_record.AddUserViewHistory(userViewHistory)
+	err = userViewHistory.AddUserViewHistory()
 	if err != nil {
 		fmt.Println("AddUserViewHistory err", err.Error())
 	}
-	fmt.Println("new historyId:", historyId)
 	result = ResearchReportTypeContentInfo{
 		ResearchReportTypeContentList: researchReportTypeContentList,
 		ResearchReportTypeInfo:        researchReportTypeInfo,

+ 12 - 0
models/tables/research_report/update.go

@@ -0,0 +1,12 @@
+package research_report
+
+import (
+	"gorm.io/gorm"
+	"hongze/hongze_yb/global"
+)
+
+// UpdateViewers 更新对应字段数据
+func (researchReport *ResearchReport) UpdateViewers() (err error) {
+	err = global.DEFAULT_MYSQL.Model(researchReport).UpdateColumn("viewers", gorm.Expr("viewers + ?", 1)).Error
+	return
+}

+ 0 - 27
models/tables/user_record/user_record.go

@@ -1,7 +1,6 @@
 package user_record
 
 import (
-	"hongze/hongze_yb/global"
 	"time"
 )
 
@@ -66,29 +65,3 @@ var UserRecordColumns = struct {
 	SessionKey:     "session_key",
 	UserID:         "user_id",
 }
-
-type UserViewHistory struct {
-	ViewHistoryId        uint64    `gorm:"primaryKey,column:view_history_id" description:"自增Id"`
-	UserId               uint64    `description:"用户id"`
-	Mobile               string    `description:"手机号"`
-	Email                string    `description:"邮箱"`
-	RealName             string    `description:"用户实际名称"`
-	CompanyName          string    `description:"公司名称"`
-	ViewTitle            string    `description:"访问标题"`
-	ViewPage             string    `description:"访问页面"`
-	ReportChapterModule  string    `description:"访问核心观点或者图文逻辑"`
-	CreatedTime          time.Time `description:"创建时间"`
-	LastUpdatedTime      time.Time `description:"最后更新时间"`
-	Type                 string    `description:"访问历史类型,weekly_report 周报,pdf"`
-	ResearchReportId     uint64    `description:"研报id"`
-	ResearchReportTypeId uint64    `description:"报告章节id,为0时表示查看目录或者首页"`
-}
-
-func (UserViewHistory) TableName() string {
-	return "user_view_history"
-}
-
-func AddUserViewHistory(item *UserViewHistory) (newId int64, err error) {
-	err = global.DEFAULT_MYSQL.Create(item).Error
-	return
-}

+ 9 - 0
models/tables/user_view_history/create.go

@@ -0,0 +1,9 @@
+package user_view_history
+
+import "hongze/hongze_yb/global"
+
+// AddUserViewHistory 新增记录
+func (userViewHistory *UserViewHistory) AddUserViewHistory() (err error) {
+	err = global.DEFAULT_MYSQL.Create(userViewHistory).Error
+	return
+}

+ 15 - 0
models/tables/user_view_history/query.go

@@ -0,0 +1,15 @@
+package user_view_history
+
+import "hongze/hongze_yb/global"
+
+// GetReportByUserId 根据userId查询文章记录
+func GetReportByUserId(userId,reportId uint64) (item *UserViewHistory, err error) {
+	err = global.DEFAULT_MYSQL.Where("user_id = ? and research_report_id = ? and research_report_type_id = 0", userId, reportId).First(&item).Error
+	return
+}
+
+// GetReportTypeByUserId 根据userId查询章节记录
+func GetReportTypeByUserId(userId,reportTypeId uint64) (item *UserViewHistory, err error) {
+	err = global.DEFAULT_MYSQL.Where("user_id = ? and research_report_type_id = ?", userId, reportTypeId).First(&item).Error
+	return
+}

+ 61 - 0
models/tables/user_view_history/user_view_history.go

@@ -0,0 +1,61 @@
+package user_view_history
+
+import (
+"time"
+)
+
+// UserViewHistory 访问历史
+type UserViewHistory struct {
+	ViewHistoryID        uint64    `gorm:"primaryKey;column:view_history_id" json:"-"`              // id
+	UserID               uint64    `gorm:"column:user_id" json:"userId"`                            // 用户id
+	Mobile               string    `gorm:"column:mobile" json:"mobile"`                             // 手机号
+	Email                string    `gorm:"column:email" json:"email"`                               // 邮箱
+	RealName             string    `gorm:"column:real_name" json:"realName"`                        // 用户实际名称
+	CompanyName          string    `gorm:"column:company_name" json:"companyName"`                  // 公司名称
+	ViewTitle            string    `gorm:"column:view_title" json:"viewTitle"`                      // 访问标题
+	ViewPage             string    `gorm:"column:view_page" json:"viewPage"`                        // 访问页面
+	ReportChapterModule  string    `gorm:"column:report_chapter_module" json:"reportChapterModule"` // 访问核心观点或者图文逻辑
+	CreatedTime          time.Time `gorm:"column:created_time" json:"createdTime"`                  // 创建时间
+	LastUpdatedTime      time.Time `gorm:"column:last_updated_time" json:"lastUpdatedTime"`
+	Type                 string    `gorm:"column:type" json:"type"`                                    // 访问历史类型,weekly_report 周报,pdf
+	ResearchReportID     uint64    `gorm:"column:research_report_id" json:"researchReportId"`          // 研报id
+	ResearchReportTypeID uint64    `gorm:"column:research_report_type_id" json:"researchReportTypeId"` // 报告章节id,为0时表示查看目录或者首页
+}
+
+// TableName get sql table name.获取数据库表名
+func (m *UserViewHistory) TableName() string {
+	return "user_view_history"
+}
+
+// UserViewHistoryColumns get sql column name.获取数据库列名
+var UserViewHistoryColumns = struct {
+	ViewHistoryID        string
+	UserID               string
+	Mobile               string
+	Email                string
+	RealName             string
+	CompanyName          string
+	ViewTitle            string
+	ViewPage             string
+	ReportChapterModule  string
+	CreatedTime          string
+	LastUpdatedTime      string
+	Type                 string
+	ResearchReportID     string
+	ResearchReportTypeID string
+}{
+	ViewHistoryID:        "view_history_id",
+	UserID:               "user_id",
+	Mobile:               "mobile",
+	Email:                "email",
+	RealName:             "real_name",
+	CompanyName:          "company_name",
+	ViewTitle:            "view_title",
+	ViewPage:             "view_page",
+	ReportChapterModule:  "report_chapter_module",
+	CreatedTime:          "created_time",
+	LastUpdatedTime:      "last_updated_time",
+	Type:                 "type",
+	ResearchReportID:     "research_report_id",
+	ResearchReportTypeID: "research_report_type_id",
+}