瀏覽代碼

下载浏览统计

xuzhiheng 4 年之前
父節點
當前提交
fc32e85fb9
共有 2 個文件被更改,包括 28 次插入9 次删除
  1. 2 2
      rpc/stdlib/internal/logic/docstatisticslogic.go
  2. 26 7
      services/stdlib/docStatistics.go

+ 2 - 2
rpc/stdlib/internal/logic/docstatisticslogic.go

@@ -25,6 +25,6 @@ func NewDocStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Doc
 
 func (l *DocStatisticsLogic) DocStatistics(in *stdlib.DocStatisticsReq) (*stdlib.DocChangeResp, error) {
 	// todo: add your logic here and delete this line
-	stdlibService.DocStatistics(in.DocId, int(in.DocStatisticsType))
-	return &stdlib.DocChangeResp{State: true}, nil
+	state := stdlibService.DocStatistics(in.DocId, int(in.DocStatisticsType))
+	return &stdlib.DocChangeResp{State: state}, nil
 }

+ 26 - 7
services/stdlib/docStatistics.go

@@ -1,17 +1,36 @@
 package stdlib
 
 import (
+	"log"
+	"time"
+
+	elastic "app.yhyue.com/moapp/jybase/esv7"
 	jyDocsRpcUtil "app.yhyue.com/moapp/jydocs/services/util"
 )
 
-func DocStatistics(docId string, dtype int) {
-	jyDocsRpcUtil.GetJyDocsDB()
+func DocStatistics(docId string, dtype int) bool {
+	sqlStr := ""
+	state := false
 	if dtype == 1 { //下载次数增加1
-		//mysql
-
-		//es
-
+		sqlStr = "update doc_statistics set downTimes = downTimes + 1,updateDate = ? where docId = ?"
 	} else if dtype == 2 { //浏览次数增加1
-
+		sqlStr = "update doc_statistics set viewTimes = viewTimes + 1,updateDate = ? where docId = ?"
+	}
+	//mysql
+	log.Println("sql ", sqlStr)
+	updateDate := time.Now().Format("2006-01-02 15:04:05")
+	err := jyDocsRpcUtil.GetJyDocsDB().Exec(sqlStr, updateDate, docId).Error
+	if err != nil {
+		//es
+		data := map[string]interface{}{}
+		rs := jyDocsRpcUtil.GetJyDocsDB().Table("doc_statistics").Where("docId = ?", id).Find(&data)
+		if len(data) > 0 {
+			//是否缓存
+			//TODO
+			if ok := elastic.UpdateNewDoc("jydocs", "jydocs", data); ok {
+				state = true
+			}
+		}
 	}
+	return state
 }