rdluck 4 years ago
parent
commit
7716dfdaab
9 changed files with 246 additions and 34 deletions
  1. 0 3
      controllers/wechat.go
  2. 7 0
      models/article.go
  3. 7 0
      models/db.go
  4. 33 0
      models/tactics.go
  5. 0 4
      models/user_record.go
  6. 25 25
      services/article.go
  7. 170 0
      services/tactics.go
  8. 2 0
      services/task.go
  9. 2 2
      utils/config.go

+ 0 - 3
controllers/wechat.go

@@ -79,12 +79,9 @@ func (this *WechatCommonController) WechatLogin() {
 			userRecord.UserId = int(newId)
 			userRecord.OpenId = user.OpenId
 			userRecord.UnionId = user.UnionId
-			userRecord.CompanyId = user.CompanyId
 			userRecord.NickName = user.NickName
 			userRecord.RealName = user.RealName
-			userRecord.Mobile = user.Mobile
 			userRecord.BindAccount = user.Mobile
-			userRecord.Email = user.Email
 			userRecord.Sex = user.Sex
 			userRecord.Province = user.Province
 			userRecord.City = user.City

+ 7 - 0
models/article.go

@@ -164,3 +164,10 @@ type ElasticArticleDetail struct {
 	SellerName           string `description:"销售名称"`
 	ArticleIdMd5         string `description:"纪要id"`
 }
+
+func GetArticleCountById(articleId int) (count int, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT COUNT(1) AS count FROM cygx_article WHERE article_id = ? `
+	err = o.Raw(sql, articleId).QueryRow(&count)
+	return
+}

+ 7 - 0
models/db.go

@@ -25,6 +25,13 @@ func init() {
 	report_db, _ := orm.GetDB("rddp")
 	report_db.SetConnMaxLifetime(10 * time.Minute)
 
+	orm.RegisterDataBase("tactics", "mysql", utils.MYSQL_URL_TACTICS)
+	orm.SetMaxIdleConns("tactics", 50)
+	orm.SetMaxOpenConns("tactics", 100)
+
+	tacticsDb,_:=orm.GetDB("tactics")
+	tacticsDb.SetConnMaxLifetime(10 * time.Minute)
+
 	//注册对象
 	orm.RegisterModel(
 		new(WxUser),

+ 33 - 0
models/tactics.go

@@ -0,0 +1,33 @@
+package models
+
+import "rdluck_tools/orm"
+
+type Tactics struct {
+	ArticleId       int    `description:"文章id"`
+	Title           string `description:"标题"`
+	TitleEn         string `description:"英文标题"`
+	UpdateFrequency string `description:"更新周期"`
+	PublishDate     string `description:"发布时间"`
+	PublishStatus   int    `description:"发布状态"`
+	Department      string `description:"作者"`
+	Body            string `description:"内容"`
+	Abstract        string `description:"摘要"`
+	Annotation      string `description:"注解"`
+	CategoryName    string `description:"分类"`
+	SubCategoryName string `description:"子级分类"`
+	CategoryId      int    `description:"分类id"`
+	CreateDate      string `description:"创建日期"`
+}
+
+func GetTacticsList() (list []*Tactics, err error) {
+	o := orm.NewOrm()
+	o.Using("tactics")
+	sql := `SELECT a.*,b.body,b.abstract,b.annotation,b.article_id,d.category_name,d.sub_category_name,c.category_id
+			FROM article_articleinfo AS a
+			INNER JOIN article_content AS b ON a.id=b.article_id
+			INNER JOIN article_category AS c ON a.id=c.article_id
+			INNER JOIN article_categoryinfo AS d ON c.category_id=d.id 
+			WHERE a.publish_status=1 AND a.id=3439 AND d.id IN (79, 28, 32, 84) `
+	_, err = o.Raw(sql).QueryRows(&list)
+	return
+}

+ 0 - 4
models/user_record.go

@@ -11,17 +11,13 @@ type UserRecord struct {
 	OpenId         string    `description:"open_id"`
 	UnionId        string    `description:"union_id"`
 	Subscribe      int       `description:"subscribe"`
-	CompanyId      int       `description:"客户id"`
 	NickName       string    `description:"用户昵称"`
 	RealName       string    `description:"用户实际名称"`
-	Mobile         string    `description:"手机号码"`
 	BindAccount    string    `description:"绑定时的账号"`
-	Email          string    `description:"邮箱"`
 	Sex            int       `description:"普通用户性别,1为男性,2为女性"`
 	Province       string    `description:"普通用户个人资料填写的省份"`
 	City           string    `description:"普通用户个人资料填写的城市"`
 	Country        string    `description:"国家,如中国为CN"`
-	SubscribeTime  string    `description:"关注时间"`
 	Headimgurl     string    `description:"头像"`
 	CreateTime     time.Time `description:"头像"`
 	CreatePlatform int       `description:"注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序"`

+ 25 - 25
services/article.go

@@ -38,33 +38,33 @@ func GetReportContentTextSub(content string) (contentSub string, err error) {
 	content = html.UnescapeString(content)
 	utils.FileLog.Info("%s", content)
 	doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
-	if err != nil {
-		fmt.Println("create doc err:", err.Error())
-		return
-	}
-	doc.Find("p").Each(func(i int, s *goquery.Selection) {
-		pHtml, _ := s.Html()
-		if strings.Contains(pHtml, "img") || strings.Contains(pHtml, "table") {
-			s.Remove()
-		}
-	})
-	if contentSub == "" || len(contentSub) < 200 {
-		//m := 0
-		doc.Find("span").Each(func(i int, s *goquery.Selection) {
-			spanHtml, _ := s.Html()
-			if strings.Contains(spanHtml, "img") || strings.Contains(spanHtml, "table") {
-				s.Remove()
-			}
-		})
-	}
-	docText:=doc.Text()
+	//if err != nil {
+	//	fmt.Println("create doc err:", err.Error())
+	//	return
+	//}
+	//doc.Find("p").Each(func(i int, s *goquery.Selection) {
+	//	pHtml, _ := s.Html()
+	//	if strings.Contains(pHtml, "img") || strings.Contains(pHtml, "table") {
+	//		s.Remove()
+	//	}
+	//})
+	//if contentSub == "" || len(contentSub) < 200 {
+	//	//m := 0
+	//	doc.Find("span").Each(func(i int, s *goquery.Selection) {
+	//		spanHtml, _ := s.Html()
+	//		if strings.Contains(spanHtml, "img") || strings.Contains(spanHtml, "table") {
+	//			s.Remove()
+	//		}
+	//	})
+	//}
+	docText := doc.Text()
 	bodyRune := []rune(docText)
-	bodyRuneLen:=len(bodyRune)
-	if bodyRuneLen>200 {
-		bodyRuneLen=200
+	bodyRuneLen := len(bodyRune)
+	if bodyRuneLen > 200 {
+		bodyRuneLen = 200
 	}
-	body:= string(bodyRune[:bodyRuneLen])
-	contentSub=body
+	body := string(bodyRune[:bodyRuneLen])
+	contentSub = body
 	return
 }
 

+ 170 - 0
services/tactics.go

@@ -0,0 +1,170 @@
+package services
+
+import (
+	"fmt"
+	"github.com/PuerkitoBio/goquery"
+	"hongze/hongze_cygx/models"
+	"hongze/hongze_cygx/utils"
+	"html"
+	"strconv"
+	"strings"
+)
+
+//同步策略文章
+func SyncTacticsList() {
+	var err error
+	defer func() {
+		if err != nil {
+			fmt.Println("同步失败,Err:", err.Error())
+		}
+	}()
+	list, err := models.GetTacticsList()
+	if err != nil {
+		fmt.Println("GetTacticsList Err:", err.Error())
+		return
+	}
+	for k, v := range list {
+		fmt.Println(v.ArticleId)
+		if v.ArticleId == 3439 {
+			expertNumStr, expertContentStr, interviewDateStr := BodyAnalysis(v.Body)
+			fmt.Println(k,expertNumStr, expertContentStr, interviewDateStr)
+		}else{
+			continue
+		}
+
+		//判断是否已经存在
+		if v.ArticleId < 0 {
+			fmt.Println("参数错误")
+			return
+		}
+		count, err := models.GetArticleCountById(v.ArticleId)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			return
+		}
+		if count > 0 {
+			fmt.Println("modify")
+			expertNumStr, expertContentStr, interviewDateStr := BodyAnalysis(v.Body)
+			bodyText,_:=GetReportContentTextSub(v.Body)
+			updateParams := make(map[string]interface{})
+			updateParams["Title"] = v.Title
+			updateParams["TitleEn"] = v.TitleEn
+			updateParams["UpdateFrequency"] = v.UpdateFrequency
+			updateParams["CreateDate"] = v.CreateDate
+			updateParams["PublishDate"] = v.PublishDate
+			updateParams["Body"] = html.EscapeString(v.Body)
+			updateParams["BodyText"] = bodyText
+			updateParams["Abstract"] = html.EscapeString(v.Abstract)
+			updateParams["CategoryName"] = v.CategoryName
+			updateParams["SubCategoryName"] = v.SubCategoryName
+			updateParams["CategoryId"] = v.CategoryId
+			updateParams["PublishStatus"] = v.PublishStatus
+			updateParams["ExpertBackground"] = expertContentStr
+			updateParams["ExpertNumber"] = expertNumStr
+			updateParams["InterviewDate"] = interviewDateStr
+			updateParams["Department"] = v.Department
+			whereParam := map[string]interface{}{"article_id": v.ArticleId}
+			go models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
+		} else {
+			item := new(models.CygxArticle)
+			articleIdInt := v.ArticleId
+			//判断文章是否已经存在
+			existItem, err := models.GetArticleDetailById(articleIdInt)
+			if err != nil && err.Error() != utils.ErrNoRow() {
+				fmt.Println("GetArticleDetailById Err:" + err.Error())
+				return
+			}
+			if existItem != nil && existItem.ArticleId > 0 {
+				fmt.Println("exist", articleIdInt)
+				continue
+			}
+			expertNumStr, expertContentStr, interviewDateStr := BodyAnalysis(v.Body)
+			item.ArticleId = articleIdInt
+			item.Title = v.Title
+			item.TitleEn = v.TitleEn
+			item.UpdateFrequency = v.UpdateFrequency
+			item.CreateDate = v.CreateDate
+			item.PublishDate = v.PublishDate
+			item.Body = html.EscapeString(v.Body)
+			item.Abstract = html.EscapeString(v.Abstract)
+			item.CategoryName = v.CategoryName
+			item.SubCategoryName = v.SubCategoryName
+			item.CategoryId = v.CategoryId
+			item.PublishStatus = v.PublishStatus
+			item.ExpertBackground = expertContentStr
+			item.ExpertNumber = expertNumStr
+			item.InterviewDate = interviewDateStr
+			item.Department = v.Department
+			item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
+			_, err = models.AddCygxArticle(item)
+			if err != nil {
+				fmt.Println("AddCygxArticle Err:", err.Error())
+				return
+			}
+		}
+	}
+}
+
+//body 解析
+func BodyAnalysis(body string) (expertNumStr, expertContentStr, interviewDateStr string) {
+	doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
+	if err != nil {
+		fmt.Println("create doc err:", err.Error())
+		return
+	}
+
+	var expertNumArr []string
+	var expertContentArr []string
+	var interviewDateArr []string
+
+	doc.Find("p").Each(func(i int, s *goquery.Selection) {
+		fmt.Println("get p")
+		contentTxt := s.Text()
+		if strings.Contains(contentTxt, "#访谈时间:") || strings.Contains(contentTxt, "访谈时间:") {
+			interviewDate := s.Next().Text()
+			interviewDateArr = append(interviewDateArr, interviewDate)
+		}
+		if strings.Contains(contentTxt, "#专家评价") || strings.Contains(contentTxt, "专家评价") {
+			expertContent := s.Next().Text()
+			if expertContent == "" {
+				expertContent = contentTxt
+			}
+			if expertContent != "" {
+				rightIndex := strings.Index(expertContent, ")")
+				if rightIndex == 0 {
+					rightIndex = strings.Index(expertContent, ")")
+				}
+				expertNum := expertContent[:rightIndex]
+				expertNum = strings.Replace(expertNum, "(", "", -1)
+				expertNum = strings.Replace(expertNum, "(", "", -1)
+				expertNum = strings.Replace(expertNum, "专家评价", "", -1)
+				if expertNum != "" {
+					expertNumArr = append(expertNumArr, expertNum)
+					rightIndex = rightIndex
+					expertContentStr := expertContent[rightIndex:]
+					expertContentStr = strings.Replace(expertContentStr, ")", "", -1)
+					expertContentStr = strings.TrimLeft(expertContentStr, ":")
+					expertContentStr = strings.TrimRight(expertContentStr, "(推荐")
+					expertContentArr = append(expertContentArr, expertContentStr)
+				}
+			}
+		}
+	})
+
+
+	if len(expertNumArr) > 0 {
+		expertNumStr = expertNumArr[0]
+		if expertNumStr!="" {
+			expertNumStr = strings.Replace(expertNumStr, "#:", "", -1)
+			expertNumStr = strings.Replace(expertNumStr, "# ", "", -1)
+			expertNumStr = strings.Trim(expertNumStr, "")
+		}
+	}
+	if len(expertContentArr) > 0 {
+		expertContentStr = expertContentArr[0]
+	}
+
+	if len(interviewDateArr) > 0 {
+		interviewDateStr = interviewDateArr[0]
+	}
+	return
+}

+ 2 - 0
services/task.go

@@ -141,6 +141,8 @@ func Task() {
 	////bm.Put(key,1,1*time.Second)
 	//b:=bm.IsExist(key)
 	//fmt.Println(b)
+
+	//SyncTacticsList()
 	fmt.Println("end")
 }
 

+ 2 - 2
utils/config.go

@@ -8,7 +8,7 @@ var (
 	RunMode        string //运行模式
 	MYSQL_URL      string //数据库连接
 	MYSQL_URL_RDDP string //数据库连接
-	MYSQL_URL_EDB  string
+	MYSQL_URL_TACTICS  string
 )
 
 //微信配置信息
@@ -38,7 +38,7 @@ func init() {
 	beego.Info(RunMode + " 模式")
 	MYSQL_URL = config["mysql_url"]
 	MYSQL_URL_RDDP = config["mysql_url_rddp"]
-	MYSQL_URL_EDB = config["mysql_url_edb"]
+	MYSQL_URL_TACTICS = config["mysql_url_tactics"]
 
 	if RunMode == "release" {
 		WxAppId = "wxcc32b61f96720d2f"