12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- package services
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/PuerkitoBio/goquery"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/utils"
- "html"
- "io/ioutil"
- nhttp "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
- func GetReportContentSub(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- if err != nil {
- fmt.Println("create doc err:", err.Error())
- return
- }
- n := 0
- doc.Find("p").Each(func(i int, s *goquery.Selection) {
- if n > 3 {
- return
- }
- n++
- phtml, err := s.Html()
- if err != nil {
- fmt.Println("get html err", err.Error())
- return
- }
- if s.Text() != "" || strings.Contains(phtml, "src") {
- contentSub = contentSub + "<p>" + phtml + "</p>"
- }
- })
- return
- }
- func GetReportContentTextSub(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- docText := doc.Text()
- bodyRune := []rune(docText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 200 {
- bodyRuneLen = 200
- }
- body := string(bodyRune[:bodyRuneLen])
- contentSub = body
- contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
- return
- }
- func GetReportContentTextSubNew(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- docText := doc.Text()
- bodyRune := []rune(docText)
- bodyRuneLen := len(bodyRune)
- body := string(bodyRune[:bodyRuneLen])
- contentSub = body
- contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
- return
- }
- //解析文章内容
- func GetArticleAll() {
- var err error
- defer func() {
- if err != nil {
- fmt.Println("err:", err.Error())
- return
- }
- }()
- list, err := models.GetArticleAll()
- if err != nil {
- return
- }
- for _, v := range list {
- fmt.Println(v.ArticleId, v.Title)
- FixArticleContent(v.ArticleId)
- }
- }
- //解析报告
- func FixArticleContent(articleId int) {
- item, err := models.GetArticleDetailById(articleId)
- if err != nil {
- fmt.Println("GetArticleDetailById Err:" + err.Error())
- return
- }
- content := item.Body
- bodyText, _ := GetReportContentTextSub(content)
- content = html.UnescapeString(content)
- content = strings.Replace(content, "http", "https", -1)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- 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) {
- 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, ")")
- }
- if rightIndex > 0 {
- 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(expertContentArr) <= 0 {
- doc.Find("pre").Each(func(i int, pre *goquery.Selection) {
- pre.Find("span").Each(func(n int, span *goquery.Selection) {
- contentTxt := span.Text()
- if strings.Contains(contentTxt, "#专家评价") || strings.Contains(contentTxt, "专家评价") {
- span.Find("span").Each(func(m int, subspan *goquery.Selection) {
- subspanText := subspan.Text()
- if strings.Contains(subspanText, "专家评价") {
- expertContent := subspan.Next().Text()
- if expertContent != "" {
- rightIndex := strings.Index(expertContent, ")")
- if rightIndex == 0 {
- rightIndex = strings.Index(expertContent, ")")
- }
- if rightIndex > 0 {
- 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)
- }
- }
- }
- }
- })
- }
- span.Find("span").Each(func(k int, sspan *goquery.Selection) {
- sspanText := sspan.Text()
- if strings.Contains(sspanText, "访谈时间") {
- sspanText = strings.Replace(sspanText, "#访谈时间:", "", -1)
- sspanText = strings.Replace(sspanText, "访谈时间:", "", -1)
- sspanText = strings.Replace(sspanText, "\n", "", -1)
- sspanText = strings.Replace(sspanText, " ", "", -1)
- sspanText = strings.Trim(sspanText, " ")
- sspanText = sspanText[:10]
- interviewDate := sspanText
- if interviewDate != "" {
- interviewDateArr = append(interviewDateArr, interviewDate)
- }
- }
- })
- })
- })
- }
- if len(expertContentArr) <= 0 {
- doc.Find("span").Each(func(i int, span *goquery.Selection) {
- span.Find("strong").Each(func(n int, strong *goquery.Selection) {
- spanText := span.Text()
- strongText := strong.Text()
- if strings.Contains(strongText, "#专家评价") || strings.Contains(strongText, "专家评价") {
- expertContent := strong.Parents().Text()
- if expertContent != "" {
- rightIndex := strings.Index(expertContent, ")")
- if rightIndex == 0 {
- rightIndex = strings.Index(expertContent, ")")
- }
- if rightIndex > 0 {
- expertNum := expertContent[:rightIndex]
- expertNum = strings.Replace(expertNum, "(", "", -1)
- expertNum = strings.Replace(expertNum, "(", "", -1)
- expertNum = strings.Replace(expertNum, "专家评价", "", -1)
- expertNum = strings.Replace(expertNum, "#", "", -1)
- expertNum = strings.Replace(expertNum, ":", "", -1)
- expertNum = strings.Replace(expertNum, "\n", "", -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)
- return
- }
- }
- }
- }
- if strings.Contains(spanText, "访谈时间") {
- spanText = strings.Replace(spanText, "#访谈时间:", "", -1)
- spanText = strings.Replace(spanText, "访谈时间:", "", -1)
- spanText = strings.Replace(spanText, "\n", "", -1)
- spanText = strings.Replace(spanText, " ", "", -1)
- spanText = strings.Trim(spanText, " ")
- spanText = spanText[:10]
- interviewDate := spanText
- if interviewDate != "" {
- interviewDateArr = append(interviewDateArr, interviewDate)
- }
- }
- })
- })
- }
- var expertNumStr, expertContentStr, interviewDateStr string
- if len(expertNumArr) > 0 {
- expertNumStr = expertNumArr[0]
- }
- if len(expertContentArr) > 0 {
- expertContentStr = expertContentArr[0]
- }
- if len(interviewDateArr) > 0 {
- interviewDateStr = interviewDateArr[0]
- }
- expertNumStr = strings.Replace(expertNumStr, "#:", "", -1)
- err = models.ModifyArticleExpert(articleId, expertNumStr, expertContentStr, interviewDateStr, bodyText)
- if err != nil {
- fmt.Println("ModifyArticleExpert Err:" + err.Error())
- return
- }
- }
- func FixArticleImgUrl(body string) (contentSub string, err error) {
- r := strings.NewReader(string(body))
- doc, err := goquery.NewDocumentFromReader(r)
- if err != nil {
- fmt.Println(err)
- }
- doc.Find("img").Each(func(i int, s *goquery.Selection) {
- src, _ := s.Attr("src")
- if i == 0 && src != "" {
- contentSub = src
- }
- })
- return
- }
- //获取标签里的第一个内容
- func FixArticleFirstCount(body string) (contentSub string, err error) {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
- if err != nil {
- fmt.Println("create doc err:", err.Error())
- return
- }
- doc.Find("p").Each(func(i int, s *goquery.Selection) {
- contentTxt := s.Text()
- fmt.Println(contentTxt)
- })
- return
- }
- func GetArticleListByApi(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("GetArticleListByApi Err:" + err.Error())
- go utils.SendAlarmMsg("同步策略平台数据失败", 2)
- go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- requestUrl := "https://vmp.hzinsights.com/v2api/articles/mp?take=100&skip=0&publish_status=2,4&order=publish_date&sort=DESC"
- method := "GET"
- client := &nhttp.Client{}
- req, err := nhttp.NewRequest(method, requestUrl, nil)
- if err != nil {
- fmt.Println("GetListApi Err:", err.Error())
- return err
- }
- req.Header.Add("Authorization", "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkiLCJwaG9uZV9udW1iZXIiOiIxMjM0NTY3ODkiLCJuYW1lIjoi5YW25LuWIiwiZW50cmFuY2UiOiJwYXNzd3dvcmQiLCJpYXQiOjE2MzQ4NzA1OTQsImV4cCI6MTYzNDg3NDE5NH0.tho2L9jsbDPn8ltEGUVDve_nHsh0Kzf6ZrSz0RcZ0ag")
- res, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return err
- }
- defer res.Body.Close()
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- fmt.Println("Getres.Body Err:", err.Error())
- return err
- }
- var pdfResult models.ArticleResultApi
- err = json.Unmarshal(body, &pdfResult)
- if err != nil {
- fmt.Println("Getres.pdfResult Err:", err.Error())
- return err
- }
- exitMap := make(map[int]int)
- classMap := make(map[int]int)
- reportMap := make(map[int]int)
- summaryMap := make(map[int]int)
- listMap, err := models.GetArticleApiMap()
- if err != nil {
- fmt.Println("GetlistMap Err:", err.Error())
- return err
- }
- openIdList, err := models.GetUserRecordListByMobile(4, utils.ArticleTaskClassMobile)
- if err != nil {
- fmt.Println(err)
- return err
- }
- fmt.Println(openIdList)
- //新旧分类 反向隐射,是否归类,是否是报告,是否是纪要库
- for _, v := range listMap {
- exitMap[v.Id] = v.OldId
- if v.IsClass == 1 {
- classMap[v.OldId] = 1
- }
- if v.IsReport == 1 {
- reportMap[v.OldId] = 1
- }
- if v.IsSummary == 1 {
- summaryMap[v.OldId] = 1
- }
- }
- listData := pdfResult.Data
- var list []*models.Tactics2
- var listAuthor []*models.CygxArticleAuthor
- for _, v := range listData {
- //状态等于 2 跟 4 的进行同步
- if exitMap[v.SeriesId] > 0 && (v.PublishStatus == 2 || v.PublishStatus == 4) {
- v.PublishDate = time.Date(v.PublishDate.Year(), v.PublishDate.Month(), v.PublishDate.Day(), v.PublishDate.Hour(), v.PublishDate.Minute(), v.PublishDate.Second(), v.PublishDate.Nanosecond(), time.Local)
- item := new(models.Tactics2)
- itemAuthor := new(models.CygxArticleAuthor)
- item.ArticleId = v.ArticleId
- item.Title = v.Title
- item.TitleEn = v.TitleEn
- item.File = v.File
- if v.Frequency == "日度" {
- item.UpdateFrequency = "daily"
- } else if v.Frequency == "周度" {
- item.UpdateFrequency = "weekly"
- } else if v.Frequency == "月度" {
- item.UpdateFrequency = "monthly"
- } else if v.Frequency == "季度" {
- item.UpdateFrequency = "quarterly"
- } else if v.Frequency == "年度" {
- item.UpdateFrequency = "yearly"
- } else {
- item.UpdateFrequency = "unknow"
- }
- item.CreateDate = v.CreateDate
- item.PublishDate = v.PublishDate.Add(time.Hour * 8)
- item.PublishStatus = 1
- item.Body = v.Content.Body
- item.Abstract = v.Content.Abstract
- item.CategoryName = v.Industry.Name
- item.CategoryId = exitMap[v.SeriesId]
- item.SubCategoryName = v.Series.Name
- if len(v.Stock) > 0 {
- var stock string
- for _, vS := range v.Stock {
- stock += vS + "/"
- }
- stock = strings.TrimRight(stock, "/")
- item.Stock = stock
- }
- item.FieldName = v.Field.Name
- list = append(list, item)
- itemAuthor.ArticleId = v.ArticleId
- itemAuthor.Name = v.Author.Name
- itemAuthor.Mobile = v.Author.PhoneNumber
- listAuthor = append(listAuthor, itemAuthor)
- }
- }
- //同步作者
- for _, v := range listAuthor {
- var count int
- count, err = models.GetActivityAuthorCount(v.ArticleId, v.Mobile)
- if err != nil {
- fmt.Println("GetCount Err:", err.Error())
- return err
- }
- if count == 0 {
- _, err = models.AddCygxActivityAuthor(v)
- if err != nil {
- fmt.Println("AddCygxActivityAuthor Err:", err.Error())
- return err
- }
- }
- }
- fmt.Println("同步文章条数:", len(list))
- listCustomArticle, err := models.GetCustomArticleId() //手动归类的文章,不替换文章类型
- if err != nil {
- fmt.Println("GetTacticsList Err:", err.Error())
- return err
- }
- listGetMatchTypeName, errMatch := models.GetMatchTypeNamenNotNull() //手动归类的文章,不替换文章类型
- if errMatch != nil {
- fmt.Println("GetTacticsList Err:", errMatch.Error())
- return err
- }
- fmt.Println("list len:", len(list))
- noSummaryArticleIds := "3454,3456,3457,3459,2449,2450,2453,2454,2459,2530,2583,2663,2670,2699,2715,2732,2748,2759,2399,2356,2870,3173,2978,2826,3470" //非纪要库类型的文章ID
- listNoSummaryArticleIds := strings.Split(noSummaryArticleIds, ",")
- for k, v := range list {
- //同步匹配类型
- matchTypeName := ""
- for _, vMatch := range listGetMatchTypeName {
- if v.CategoryId == vMatch.CategoryId {
- matchTypeName = vMatch.MatchTypeName
- }
- }
- //是否属于纪要库的数据
- if _, has := summaryMap[v.CategoryId]; has {
- v.IsSummary = 1
- }
- //排除不属于纪要库类型的文章
- for _, vArt := range listNoSummaryArticleIds {
- vArtInt, _ := strconv.Atoi(vArt)
- if v.ArticleId == vArtInt {
- v.IsSummary = 0
- }
- }
- if _, has := reportMap[v.CategoryId]; has {
- v.IsReport = 1
- if _, ok := classMap[v.CategoryId]; ok {
- v.IsClass = 1
- v.ReportType = 1 //是否属于行业报告
- } else {
- v.ReportType = 2 //是否属于产业报告
- }
- }
- v.Department = "弘则权益研究"
- //判断是否已经存在
- if v.ArticleId < 0 {
- fmt.Println("AddCygxArticle Err:")
- return err
- }
- var count int
- count, err = models.GetArticleCountById(v.ArticleId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- fmt.Println("AddCygxArticle Err:", err.Error())
- return err
- }
- v.Body = strings.Replace(v.Body, "http://vmp.hzinsights.com", "https://vmp.hzinsights.com", -1)
- expertNumStr, expertContentStr, interviewDateStr, _, bodyReturn := BodyAnalysis2(v.Body)
- if strings.Index(v.Body, "报告全文(") > 0 && strings.Index(v.Body, "PDF格式报告下载.pdf") > 0 {
- v.Body = strings.Replace(v.Body, "报告全文(", "", -1)
- v.Body = strings.Replace(v.Body, "PDF格式报告下载.pdf", "", -1)
- v.Body = strings.Replace(v.Body, "):", "", -1)
- }
- var titleNew string
- titleNew = v.Title
- // 7资金流向 、11大类资产 、51每日复盘 、80医药周报、9估值研究
- if v.CategoryId == 7 || v.CategoryId == 11 || v.CategoryId == 51 || v.CategoryId == 9 {
- if v.UpdateFrequency == "daily" {
- var daystr string
- daystr = strconv.Itoa(v.PublishDate.Day())
- if len(daystr) == 1 {
- daystr = "0" + daystr
- }
- titleNew = v.Title + "(" + strconv.Itoa(v.PublishDate.Year())[2:len(strconv.Itoa(v.PublishDate.Year()))-0] + v.PublishDate.Format("01") + daystr + ")"
- } else if v.UpdateFrequency == "weekly" {
- titleNew = v.Title + utils.WeekByDate(v.PublishDate)
- }
- }
- if v.CategoryId == 80 {
- titleNew = v.Title + utils.WeekByDate(v.PublishDate)
- }
- if count > 0 {
- fmt.Println(k, v.ArticleId, "edit")
- var isCustom bool
- bodyText, _ := GetReportContentTextSub(v.Body)
- updateParams := make(map[string]interface{})
- //updateParams["Title"] = v.Title
- updateParams["Title"] = titleNew
- updateParams["TitleEn"] = v.TitleEn
- updateParams["UpdateFrequency"] = v.UpdateFrequency
- updateParams["CreateDate"] = v.CreateDate
- updateParams["PublishDate"] = v.PublishDate
- //updateParams["Body"] = html.EscapeString(v.Body)
- updateParams["Body"] = html.EscapeString(bodyReturn)
- updateParams["BodyText"] = bodyText
- updateParams["Abstract"] = html.EscapeString(v.Abstract)
- updateParams["CategoryName"] = v.CategoryName
- for _, vCustom := range listCustomArticle {
- if v.ArticleId == vCustom.ArticleId {
- fmt.Println("手动归类的文章:" + strconv.Itoa(v.ArticleId))
- isCustom = true
- }
- }
- if isCustom == false {
- updateParams["CategoryId"] = v.CategoryId
- updateParams["MatchTypeName"] = matchTypeName
- updateParams["IsSummary"] = v.IsSummary
- updateParams["IsReport"] = v.IsReport
- updateParams["ReportType"] = v.ReportType
- updateParams["SubCategoryName"] = v.SubCategoryName
- }
- //updateParams["CategoryId"] = v.CategoryId
- updateParams["PublishStatus"] = 1
- updateParams["ExpertBackground"] = expertContentStr
- updateParams["ExpertNumber"] = expertNumStr
- updateParams["InterviewDate"] = interviewDateStr
- //updateParams["IsClass"] = v.IsClass
- v.Department = "弘则权益研究"
- updateParams["Department"] = v.Department
- updateParams["FileLink"] = v.File
- updateParams["Stock"] = v.Stock
- updateParams["FieldName"] = v.FieldName
- whereParam := map[string]interface{}{"article_id": v.ArticleId}
- err = models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
- if err != nil {
- fmt.Println("UpdateByExpr Err:" + err.Error())
- return err
- }
- } else {
- fmt.Println(k, v.ArticleId, "add")
- item := new(models.CygxArticle)
- articleIdInt := v.ArticleId
- item.ArticleId = articleIdInt
- //item.Title = v.Title
- item.Title = titleNew
- item.TitleEn = v.TitleEn
- item.UpdateFrequency = v.UpdateFrequency
- item.CreateDate = v.CreateDate
- item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
- //item.Body = html.EscapeString(v.Body)
- item.Body = html.EscapeString(bodyReturn)
- item.Abstract = html.EscapeString(v.Abstract)
- item.CategoryName = v.CategoryName
- item.SubCategoryName = v.SubCategoryName
- item.CategoryId = v.CategoryId
- item.CategoryIdTwo = v.CategoryId
- item.PublishStatus = 1
- item.ExpertBackground = expertContentStr
- item.ExpertNumber = expertNumStr
- item.InterviewDate = interviewDateStr
- item.Department = v.Department
- item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
- item.IsClass = v.IsClass
- item.IsSummary = v.IsSummary
- item.IsReport = v.IsReport
- item.ReportType = v.ReportType
- item.FileLink = v.File
- item.MatchTypeName = matchTypeName
- item.Stock = v.Stock
- item.FieldName = v.FieldName
- newId, err := models.AddCygxArticles(item)
- if err != nil {
- fmt.Println("AddCygxArticle Err:", err.Error())
- return err
- }
- //fmt.Println(newId)
- //报告自动归类,以及推送相关模板消息
- if v.ReportType == 2 {
- var subjectStr string
- var industrialManagementIdStr string
- var industrialSubjectIdStr string
- var keyword1 string
- var keyword2 string
- var keyword3 string
- var keyword4 string
- sliceSubjects := strings.Split(v.Stock, "/")
- mapManagementForSubject := make(map[string]string)
- if len(sliceSubjects) > 0 {
- for _, vSubject := range sliceSubjects {
- sliceKuohao := strings.Split(vSubject, "(") //过滤括号
- sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
- subject := sliceXiahuaxian[0]
- subjectStr += "'" + subject + "',"
- }
- //获取该产业下所对应的行业图片
- detailCategory, errCategory := models.GetdetailByCategoryIdOne(v.CategoryId)
- if errCategory != nil {
- fmt.Println("GetdetailByCategoryIdOne Err:", err.Error())
- return err
- }
- subjectStr = strings.TrimRight(subjectStr, ",")
- if subjectStr != "" {
- listIndustrial, err := models.GetIndustrialManagementForSubjecName(subjectStr, detailCategory.ChartPermissionId)
- if err != nil {
- fmt.Println("GetIndustrialManagementForSubjecName Err:", err.Error())
- return err
- }
- subjectStr = strings.Replace(subjectStr, "','", "】【", -1)
- subjectStr = strings.Replace(subjectStr, "'", "", -1)
- subjectStr = "【" + subjectStr + "】"
- if len(listIndustrial) > 0 {
- for _, vIndustrial := range listIndustrial {
- industrialManagementIdStr += strconv.Itoa(vIndustrial.IndustrialManagementId) + ","
- industrialSubjectIdStr += strconv.Itoa(vIndustrial.IndustrialSubjectId) + ","
- mapManagementForSubject[vIndustrial.IndustryName] += vIndustrial.SubjectName + "/"
- }
- industrialManagementIdStr = strings.TrimRight(industrialManagementIdStr, ",")
- industrialSubjectIdStr = strings.TrimRight(industrialSubjectIdStr, ",")
- if industrialManagementIdStr != "" {
- err = models.ReportArticleClassificationEditNew(int(newId), industrialManagementIdStr, v.ArticleId, industrialSubjectIdStr)
- if err != nil {
- fmt.Println("ReportArticleClassificationEditNew Err:", err.Error())
- //keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
- //keyword2 = "归类失败"
- //keyword3 = v.Title
- //keyword4 = v.PublishDate.Format(utils.FormatDateTime)
- //SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
- return err
- }
- }
- var peoductName string
- for mk, mv := range mapManagementForSubject {
- peoductName += "【" + mk + "--" + strings.TrimRight(mv, "/") + "】"
- }
- keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
- keyword2 = "已自动关联至以下产业和标的:" + peoductName
- keyword3 = v.Title
- keyword4 = v.PublishDate.Format(utils.FormatDateTime)
- SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
- } else {
- keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
- keyword2 = "未归类"
- keyword3 = v.Title
- keyword4 = v.PublishDate.Format(utils.FormatDateTime)
- SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
- }
- }
- }
- }
- //类型ID 医药(医享会:28 、药调研:301)、消费【渠道新声:32】、科技【科技前言:79】、智造【匠心智造:84】或者是纪要做消息模板推送
- fmt.Println(v.CategoryId)
- if v.IsSummary == 1 || (v.CategoryId == 28 || v.CategoryId == 301 || v.CategoryId == 32 || v.CategoryId == 79 || v.CategoryId == 84) {
- sliceSubjects := strings.Split(v.Stock, "/")
- fmt.Println(sliceSubjects)
- if len(sliceSubjects) > 0 {
- var subjectStr string
- for _, vSubject := range sliceSubjects {
- sliceKuohao := strings.Split(vSubject, "(") //过滤括号
- sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
- subject := sliceXiahuaxian[0]
- subjectStr += "'" + subject + "',"
- }
- if subjectStr != "" {
- subjectStr = strings.TrimRight(subjectStr, ",")
- activityIdList, err := models.GetActivityIdListBySubjecName(subjectStr)
- if err != nil {
- fmt.Println("GetActivityIdListBySubjecName Err:", err.Error())
- return err
- }
- if len(activityIdList) > 0 {
- var activityIdStr string
- for _, vAct := range activityIdList {
- activityIdStr += strconv.Itoa(vAct.ActivityId) + ","
- }
- activityIdStr = strings.TrimRight(activityIdStr, ",")
- if activityIdStr != "" {
- appointmentList, err := models.GetAppointmentListByActivityId(activityIdStr, "1,2,5")
- if err != nil {
- fmt.Println("GetAppointmentListByActivityId Err:", err.Error())
- return err
- }
- if len(appointmentList) > 0 {
- for _, vApp := range appointmentList {
- appointmentByMobileList, err := models.GetAppointmentListByActivityIdAndMobile(activityIdStr, vApp.Mobile)
- if err != nil {
- fmt.Println("GetAppointmentListByActivityId Err:", err.Error())
- return err
- }
- var appointmentActivityName string
- if len(appointmentByMobileList) > 0 {
- for _, vAppM := range appointmentByMobileList {
- appointmentActivityName += vAppM.ActivityName + ","
- }
- }
- appointmentActivityName = strings.TrimRight(appointmentActivityName, ",")
- if vApp.ActivityTypeId == 5 && v.CategoryId != 301 {
- continue
- }
- if vApp.Mobile != "" {
- openIdListByAppointment, err := models.GetUserRecordListByMobile(4, vApp.Mobile)
- if err != nil {
- fmt.Println(err)
- return err
- }
- keyword1 := "您预约的调研,有关联的纪要发布/更新了"
- keyword2 := appointmentActivityName
- keyword3 := v.Title
- keyword4 := v.PublishDate.Format(utils.FormatDateTime)
- SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdListByAppointment, articleIdInt)
- }
- }
- }
- }
- }
- }
- }
- }
- //【公司调研】系列纪要发布/更新后
- if v.CategoryId == 45 || v.CategoryId == 74 || v.CategoryId == 86 || v.CategoryId == 88 {
- sliceSubjects := strings.Split(v.Stock, "/")
- if len(sliceSubjects) > 0 {
- var subjectStr string
- for _, vSubject := range sliceSubjects {
- sliceKuohao := strings.Split(vSubject, "(") //过滤括号
- sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
- subject := sliceXiahuaxian[0]
- subjectStr += "'" + subject + "',"
- }
- if subjectStr != "" {
- subjectStr = strings.TrimRight(subjectStr, ",")
- activityIdList, err := models.GetActivityIdListBySubjecName(subjectStr)
- if err != nil {
- fmt.Println("GetActivityIdListBySubjecName Err:", err.Error())
- return err
- }
- if len(activityIdList) > 0 {
- var activityIdStr string
- for _, vAct := range activityIdList {
- activityIdStr += strconv.Itoa(vAct.ActivityId) + ","
- }
- activityIdStr = strings.TrimRight(activityIdStr, ",")
- if activityIdStr != "" {
- appointmentList, err := models.GetAppointmentListByActivityId(activityIdStr, "3,4")
- if err != nil {
- fmt.Println("GetAppointmentListByActivityId Err:", err.Error())
- return err
- }
- if len(appointmentList) > 0 {
- for _, vApp := range appointmentList {
- appointmentByMobileList, err := models.GetAppointmentListByActivityIdAndMobile(activityIdStr, vApp.Mobile)
- if err != nil {
- fmt.Println("GetAppointmentListByActivityId Err:", err.Error())
- return err
- }
- var appointmentActivityName string
- if len(appointmentByMobileList) > 0 {
- for _, vAppM := range appointmentByMobileList {
- appointmentActivityName += vAppM.ActivityName + ","
- }
- }
- appointmentActivityName = strings.TrimRight(appointmentActivityName, ",")
- if vApp.Mobile != "" {
- openIdListByAppointment, err := models.GetUserRecordListByMobile(4, vApp.Mobile)
- if err != nil {
- fmt.Println(err)
- return err
- }
- keyword1 := "您预约的调研,有关联的纪要发布/更新了"
- keyword2 := appointmentActivityName
- keyword3 := v.Title
- keyword4 := v.PublishDate.Format(utils.FormatDateTime)
- SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdListByAppointment, articleIdInt)
- }
- }
- }
- }
- }
- }
- }
- }
- //【公司调研】系列纪要发布/更新后 end
- }
- }
- return
- }
- func SynchronizationArtclehistory() {
- fmt.Println("同步开始")
- list, err := models.GetArticleHistoryList()
- if err != nil {
- fmt.Println("获取列表失败", err)
- }
- fmt.Println(len(list))
- for _, v := range list {
- //endDate := v.ModifyTime.Add(+time.Minute * 10).Format(utils.FormatDateTime)
- //detail, err := models.GetNewArticleHistoryRecordNewpv(v.UserId, v.ArticleId, endDate)
- //if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("获取信息失败", err)
- //}
- v.OutType = 1
- //fmt.Println(v.Id)
- //if detail == nil {
- // _, err = models.AddCygxArticleViewRecordNewpv(v)
- // if err != nil {
- // fmt.Println("新增失败", err)
- // }
- //} else {
- // err = models.UpdateCygxArticleViewRecordNewpvList(v, v.StopTime)
- // if err != nil {
- // fmt.Println("修改失败", err)
- // }
- //}
- newId, err := models.AddCygxArticleViewRecordNewpv(v)
- fmt.Println("新增", newId)
- if err != nil {
- fmt.Println("新增失败", err)
- }
- }
- fmt.Println("同步结束")
- }
- //统计报表
- func StatisticalReport() {
- var isSummaryNumAll, isClassNum, pvNumAll, uvNumAll int
- list, err := models.GetChartPermissionActivity()
- if err != nil {
- fmt.Println("获取列表失败", err)
- }
- for _, v := range list {
- var listPv []*models.ReportMappingStatistical
- if v.PermissionName == "研选" {
- listPv, err = models.GetStatisticalReportArtilceExpert()
- if err != nil {
- fmt.Println("获取Pv列表失败", err)
- }
- } else {
- listPv, err = models.GetStatisticalReportArtilce(v.ChartPermissionId)
- if err != nil {
- fmt.Println("获取Pv列表失败", err)
- }
- }
- var pvNum, uvNum, isSummaryNum int
- for _, v2 := range listPv {
- pvNum += v2.Pv
- uvNum += v2.Uv
- if v2.IsSummary == "1" {
- isSummaryNum += 1
- }
- if v2.IsClass == "1" && v.ChartPermissionId <= 22 {
- isClassNum += 1
- }
- if v2.IsSummary == "1" && v.ChartPermissionId <= 22 {
- isSummaryNumAll += 1
- }
- }
- if v.ChartPermissionId <= 22 {
- pvNumAll += pvNum
- uvNumAll += uvNum
- }
- fmt.Println(v.PermissionName+"行业", len(listPv), "篇,其中主观类报告", isSummaryNum, "篇,客观类报告", len(listPv)-isSummaryNum, "篇。共产生阅读量pv-,", pvNum, ",uv-", uvNum)
- }
- fmt.Println("目前同步四大行业的总报告(已归类)数量", isClassNum, "篇,其中主观类报告", isSummaryNumAll, "篇,客观类报告", isClassNum-isSummaryNumAll, "篇。共产生阅读量pv-", pvNumAll, ",uv-", uvNumAll)
- var totalOnline int //线上
- var totalOffline int //线下
- var totalPeople int //共累计预约外呼人数
- var totalSignUpOff int //线下报名人数
- var totalSignUpOffTime int //线下报名人数
- var totalPeopleMeet int //线下参会人数
- o := orm.NewOrm()
- sql := `SELECT COUNT(1) FROM cygx_activity WHERE activity_type_id IN (1,2,3) AND publish_status = 1 AND is_submit_meeting = 1 AND activity_time <= NOW();`
- err = o.Raw(sql).QueryRow(&totalOnline)
- if err != nil {
- fmt.Println("获取线上", err)
- }
- sql = `SELECT COUNT(1) FROM cygx_activity WHERE activity_type_id IN (4,5,6) AND publish_status = 1 AND is_submit_meeting = 1 AND activity_time <= NOW();`
- err = o.Raw(sql).QueryRow(&totalOffline)
- if err != nil {
- fmt.Println("获取线下", err)
- }
- sql = `SELECT COUNT( 1 ) FROM
- cygx_activity_signup as s
- INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
- WHERE
- s.do_fail_type = 0
- AND a.is_submit_meeting = 1
- AND a.activity_time <= NOW()
- AND a.publish_status = 1`
- err = o.Raw(sql).QueryRow(&totalPeople)
- if err != nil {
- fmt.Println("共累计预约外呼人数", err)
- }
- sql = `SELECT COUNT( 1 ) FROM
- cygx_activity_signup as s
- INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
- WHERE
- s.do_fail_type = 0
- AND a.is_submit_meeting = 1
- AND a.activity_time <= NOW()
- AND a.activity_type_id IN (4,5,6)
- AND a.publish_status = 1`
- err = o.Raw(sql).QueryRow(&totalSignUpOff)
- if err != nil {
- fmt.Println("共累计预约外呼人数", err)
- }
- sql = `SELECT COUNT( 1 ) FROM
- cygx_activity_signup as s
- INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
- WHERE
- s.do_fail_type = 0
- AND a.publish_status = 1
- AND a.is_submit_meeting = 1
- AND a.activity_time <= NOW()
- AND a.is_submit_meeting = 1
- AND a.activity_type_id IN (4,5,6);`
- err = o.Raw(sql).QueryRow(&totalSignUpOffTime)
- if err != nil {
- fmt.Println("线下报名参会人数", err)
- }
- sql = `SELECT COUNT( 1 ) FROM
- cygx_activity_signup as s
- INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
- WHERE
- s.do_fail_type = 0
- AND a.is_submit_meeting = 1
- AND a.activity_time <= NOW()
- AND a.publish_status = 1
- AND s.is_meeting = 1
- AND a.activity_type_id IN (4,5,6);`
- err = o.Raw(sql).QueryRow(&totalPeopleMeet)
- if err != nil {
- fmt.Println("线下参会人数", err)
- }
- fmt.Println("共上线活动", totalOnline+totalOffline, "个,其中线上", totalOnline, "个,线下", totalOffline, "个")
- fmt.Println("共累计预约外呼人数", totalPeople, "人")
- fmt.Println("报名线下参会", totalSignUpOff, "人,实际到会人数", totalPeopleMeet, "人,线下到会率约", totalPeopleMeet*100/totalSignUpOff, "%")
- num := totalPeopleMeet / totalSignUpOffTime
- fmt.Println(num)
- fmt.Println(totalOnline)
- fmt.Println(totalOffline)
- fmt.Println(totalPeople)
- fmt.Println(totalSignUpOff)
- fmt.Println(totalPeopleMeet)
- fmt.Println(totalSignUpOffTime)
- fmt.Println(totalPeopleMeet / totalSignUpOffTime)
- 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 GetCeLueArticlePv() {
- // sum := 0
- // for i := 0; i <= 450; i++ {
- // if i >= 102 {
- // //GetCeLueArticlePvs(strconv.Itoa(i * 1000))
- // }
- // }
- // fmt.Println(sum)
- //}
- //获取策略平台报告阅读数据
- func GetCeLueArticlePv(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("同步策略平台阅读数据失败", 2)
- go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetCeLueArticlePv ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- startTime := time.Now().Add(-time.Minute * 12).Format("2006-01-02 15:04:05")
- endTime := time.Now().Format("2006-01-02 15:04:05")
- requestUrl := utils.ApiUrl + "backend/statistics_access?take=1000&skip=0&sort=ASC&mode=all&"
- encodeData := url.Values{}
- encodeData.Add("start_dt", startTime)
- encodeData.Add("end_dt", endTime)
- encodeStr := encodeData.Encode()
- requestUrl += encodeStr
- authorization := utils.ApiAuthorization
- body, err := PublicGetDate(requestUrl, authorization)
- if err != nil {
- return
- }
- var chartResult models.CeLueArticleResultApi
- err = json.Unmarshal(body, &chartResult)
- if err != nil {
- fmt.Println(err)
- return err
- }
- mapMobileArticleId := make(map[string]int)
- //获取当天阅读记录
- listPv, err := models.GetArticleHistoryRecordAllList()
- if err != nil && err.Error() != utils.ErrNoRow() {
- fmt.Println("获取当天阅读记录失败", err)
- return err
- }
- if len(listPv) > 0 {
- for _, v := range listPv {
- mapMobileArticleId[fmt.Sprint(v.Mobile, "_", v.ArticleId)] = v.ArticleId
- }
- }
- for _, v := range chartResult.Data {
- //fmt.Println(v.ArticleId)
- item := new(models.CygxCelueArticleHistoryRecord)
- item.CelueHistoryId = v.CelueHistoryId
- item.Mobile = v.Mobile
- item.ArticleId = v.ArticleId
- if v.CrmUser != nil {
- item.RealName = v.CrmUser.RealName
- }
- if v.CompanyName != nil {
- item.CompanyName = v.CompanyName.RealName
- }
- item.CreateDateApi = time.Now()
- t1, _ := time.Parse("2006-01-02T15:04:05Z", v.CreateDate)
- item.CreateTime = t1.Add(+time.Hour * 8).Format(utils.FormatDateTime)
- count, err := models.GetCeLueArticleCountById(v.CelueHistoryId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return err
- }
- if count == 0 {
- _, err := models.AddCeLueArticle(item, mapMobileArticleId)
- if err != nil {
- fmt.Println(err)
- return err
- }
- }
- }
- return
- }
|