article.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/PuerkitoBio/goquery"
  7. "github.com/beego/beego/v2/client/orm"
  8. "hongze/hongze_cygx/models"
  9. "hongze/hongze_cygx/utils"
  10. "html"
  11. "io/ioutil"
  12. nhttp "net/http"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. func GetReportContentSub(content string) (contentSub string, err error) {
  18. content = html.UnescapeString(content)
  19. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  20. if err != nil {
  21. fmt.Println("create doc err:", err.Error())
  22. return
  23. }
  24. n := 0
  25. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  26. if n > 3 {
  27. return
  28. }
  29. n++
  30. phtml, err := s.Html()
  31. if err != nil {
  32. fmt.Println("get html err", err.Error())
  33. return
  34. }
  35. if s.Text() != "" || strings.Contains(phtml, "src") {
  36. contentSub = contentSub + "<p>" + phtml + "</p>"
  37. }
  38. })
  39. return
  40. }
  41. func GetReportContentTextSub(content string) (contentSub string, err error) {
  42. content = html.UnescapeString(content)
  43. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  44. docText := doc.Text()
  45. bodyRune := []rune(docText)
  46. bodyRuneLen := len(bodyRune)
  47. if bodyRuneLen > 200 {
  48. bodyRuneLen = 200
  49. }
  50. body := string(bodyRune[:bodyRuneLen])
  51. contentSub = body
  52. contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
  53. return
  54. }
  55. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  56. content = html.UnescapeString(content)
  57. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  58. docText := doc.Text()
  59. bodyRune := []rune(docText)
  60. bodyRuneLen := len(bodyRune)
  61. body := string(bodyRune[:bodyRuneLen])
  62. contentSub = body
  63. contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
  64. return
  65. }
  66. //解析文章内容
  67. func GetArticleAll() {
  68. var err error
  69. defer func() {
  70. if err != nil {
  71. fmt.Println("err:", err.Error())
  72. return
  73. }
  74. }()
  75. list, err := models.GetArticleAll()
  76. if err != nil {
  77. return
  78. }
  79. for _, v := range list {
  80. fmt.Println(v.ArticleId, v.Title)
  81. FixArticleContent(v.ArticleId)
  82. }
  83. }
  84. //解析报告
  85. func FixArticleContent(articleId int) {
  86. item, err := models.GetArticleDetailById(articleId)
  87. if err != nil {
  88. fmt.Println("GetArticleDetailById Err:" + err.Error())
  89. return
  90. }
  91. content := item.Body
  92. bodyText, _ := GetReportContentTextSub(content)
  93. content = html.UnescapeString(content)
  94. content = strings.Replace(content, "http", "https", -1)
  95. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  96. if err != nil {
  97. fmt.Println("create doc err:", err.Error())
  98. return
  99. }
  100. var expertNumArr []string
  101. var expertContentArr []string
  102. var interviewDateArr []string
  103. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  104. contentTxt := s.Text()
  105. if strings.Contains(contentTxt, "#访谈时间:") || strings.Contains(contentTxt, "访谈时间:") {
  106. interviewDate := s.Next().Text()
  107. interviewDateArr = append(interviewDateArr, interviewDate)
  108. }
  109. if strings.Contains(contentTxt, "#专家评价") || strings.Contains(contentTxt, "专家评价") {
  110. expertContent := s.Next().Text()
  111. if expertContent == "" {
  112. expertContent = contentTxt
  113. }
  114. if expertContent != "" {
  115. rightIndex := strings.Index(expertContent, ")")
  116. if rightIndex == 0 {
  117. rightIndex = strings.Index(expertContent, ")")
  118. }
  119. if rightIndex > 0 {
  120. expertNum := expertContent[:rightIndex]
  121. expertNum = strings.Replace(expertNum, "(", "", -1)
  122. expertNum = strings.Replace(expertNum, "(", "", -1)
  123. expertNum = strings.Replace(expertNum, "专家评价", "", -1)
  124. if expertNum != "" {
  125. expertNumArr = append(expertNumArr, expertNum)
  126. rightIndex = rightIndex
  127. expertContentStr := expertContent[rightIndex:]
  128. expertContentStr = strings.Replace(expertContentStr, ")", "", -1)
  129. expertContentStr = strings.TrimLeft(expertContentStr, ":")
  130. expertContentStr = strings.TrimRight(expertContentStr, "(推荐")
  131. expertContentArr = append(expertContentArr, expertContentStr)
  132. }
  133. }
  134. }
  135. }
  136. })
  137. if len(expertContentArr) <= 0 {
  138. doc.Find("pre").Each(func(i int, pre *goquery.Selection) {
  139. pre.Find("span").Each(func(n int, span *goquery.Selection) {
  140. contentTxt := span.Text()
  141. if strings.Contains(contentTxt, "#专家评价") || strings.Contains(contentTxt, "专家评价") {
  142. span.Find("span").Each(func(m int, subspan *goquery.Selection) {
  143. subspanText := subspan.Text()
  144. if strings.Contains(subspanText, "专家评价") {
  145. expertContent := subspan.Next().Text()
  146. if expertContent != "" {
  147. rightIndex := strings.Index(expertContent, ")")
  148. if rightIndex == 0 {
  149. rightIndex = strings.Index(expertContent, ")")
  150. }
  151. if rightIndex > 0 {
  152. expertNum := expertContent[:rightIndex]
  153. expertNum = strings.Replace(expertNum, "(", "", -1)
  154. expertNum = strings.Replace(expertNum, "(", "", -1)
  155. expertNum = strings.Replace(expertNum, "专家评价", "", -1)
  156. if expertNum != "" {
  157. expertNumArr = append(expertNumArr, expertNum)
  158. rightIndex = rightIndex
  159. expertContentStr := expertContent[rightIndex:]
  160. expertContentStr = strings.Replace(expertContentStr, ")", "", -1)
  161. expertContentStr = strings.TrimLeft(expertContentStr, ":")
  162. expertContentStr = strings.TrimRight(expertContentStr, "(推荐")
  163. expertContentArr = append(expertContentArr, expertContentStr)
  164. }
  165. }
  166. }
  167. }
  168. })
  169. }
  170. span.Find("span").Each(func(k int, sspan *goquery.Selection) {
  171. sspanText := sspan.Text()
  172. if strings.Contains(sspanText, "访谈时间") {
  173. sspanText = strings.Replace(sspanText, "#访谈时间:", "", -1)
  174. sspanText = strings.Replace(sspanText, "访谈时间:", "", -1)
  175. sspanText = strings.Replace(sspanText, "\n", "", -1)
  176. sspanText = strings.Replace(sspanText, " ", "", -1)
  177. sspanText = strings.Trim(sspanText, " ")
  178. sspanText = sspanText[:10]
  179. interviewDate := sspanText
  180. if interviewDate != "" {
  181. interviewDateArr = append(interviewDateArr, interviewDate)
  182. }
  183. }
  184. })
  185. })
  186. })
  187. }
  188. if len(expertContentArr) <= 0 {
  189. doc.Find("span").Each(func(i int, span *goquery.Selection) {
  190. span.Find("strong").Each(func(n int, strong *goquery.Selection) {
  191. spanText := span.Text()
  192. strongText := strong.Text()
  193. if strings.Contains(strongText, "#专家评价") || strings.Contains(strongText, "专家评价") {
  194. expertContent := strong.Parents().Text()
  195. if expertContent != "" {
  196. rightIndex := strings.Index(expertContent, ")")
  197. if rightIndex == 0 {
  198. rightIndex = strings.Index(expertContent, ")")
  199. }
  200. if rightIndex > 0 {
  201. expertNum := expertContent[:rightIndex]
  202. expertNum = strings.Replace(expertNum, "(", "", -1)
  203. expertNum = strings.Replace(expertNum, "(", "", -1)
  204. expertNum = strings.Replace(expertNum, "专家评价", "", -1)
  205. expertNum = strings.Replace(expertNum, "#", "", -1)
  206. expertNum = strings.Replace(expertNum, ":", "", -1)
  207. expertNum = strings.Replace(expertNum, "\n", "", -1)
  208. if expertNum != "" {
  209. expertNumArr = append(expertNumArr, expertNum)
  210. rightIndex = rightIndex
  211. expertContentStr := expertContent[rightIndex:]
  212. expertContentStr = strings.Replace(expertContentStr, ")", "", -1)
  213. expertContentStr = strings.TrimLeft(expertContentStr, ":")
  214. expertContentStr = strings.TrimRight(expertContentStr, "(推荐")
  215. expertContentArr = append(expertContentArr, expertContentStr)
  216. return
  217. }
  218. }
  219. }
  220. }
  221. if strings.Contains(spanText, "访谈时间") {
  222. spanText = strings.Replace(spanText, "#访谈时间:", "", -1)
  223. spanText = strings.Replace(spanText, "访谈时间:", "", -1)
  224. spanText = strings.Replace(spanText, "\n", "", -1)
  225. spanText = strings.Replace(spanText, " ", "", -1)
  226. spanText = strings.Trim(spanText, " ")
  227. spanText = spanText[:10]
  228. interviewDate := spanText
  229. if interviewDate != "" {
  230. interviewDateArr = append(interviewDateArr, interviewDate)
  231. }
  232. }
  233. })
  234. })
  235. }
  236. var expertNumStr, expertContentStr, interviewDateStr string
  237. if len(expertNumArr) > 0 {
  238. expertNumStr = expertNumArr[0]
  239. }
  240. if len(expertContentArr) > 0 {
  241. expertContentStr = expertContentArr[0]
  242. }
  243. if len(interviewDateArr) > 0 {
  244. interviewDateStr = interviewDateArr[0]
  245. }
  246. expertNumStr = strings.Replace(expertNumStr, "#:", "", -1)
  247. err = models.ModifyArticleExpert(articleId, expertNumStr, expertContentStr, interviewDateStr, bodyText)
  248. if err != nil {
  249. fmt.Println("ModifyArticleExpert Err:" + err.Error())
  250. return
  251. }
  252. }
  253. func FixArticleImgUrl(body string) (contentSub string, err error) {
  254. r := strings.NewReader(string(body))
  255. doc, err := goquery.NewDocumentFromReader(r)
  256. if err != nil {
  257. fmt.Println(err)
  258. }
  259. doc.Find("img").Each(func(i int, s *goquery.Selection) {
  260. src, _ := s.Attr("src")
  261. if i == 0 && src != "" {
  262. contentSub = src
  263. }
  264. })
  265. return
  266. }
  267. //获取标签里的第一个内容
  268. func FixArticleFirstCount(body string) (contentSub string, err error) {
  269. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  270. if err != nil {
  271. fmt.Println("create doc err:", err.Error())
  272. return
  273. }
  274. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  275. contentTxt := s.Text()
  276. fmt.Println(contentTxt)
  277. })
  278. return
  279. }
  280. func GetArticleListByApi(cont context.Context) (err error) {
  281. defer func() {
  282. if err != nil {
  283. fmt.Println("GetArticleListByApi Err:" + err.Error())
  284. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  285. }
  286. }()
  287. url := "https://vmp.hzinsights.com/v2api/articles/mp?take=100&skip=0&publish_status=2"
  288. method := "GET"
  289. client := &nhttp.Client{}
  290. req, err := nhttp.NewRequest(method, url, nil)
  291. if err != nil {
  292. fmt.Println("GetListApi Err:", err.Error())
  293. return err
  294. }
  295. req.Header.Add("Authorization", "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkiLCJwaG9uZV9udW1iZXIiOiIxMjM0NTY3ODkiLCJuYW1lIjoi5YW25LuWIiwiZW50cmFuY2UiOiJwYXNzd3dvcmQiLCJpYXQiOjE2MzQ4NzA1OTQsImV4cCI6MTYzNDg3NDE5NH0.tho2L9jsbDPn8ltEGUVDve_nHsh0Kzf6ZrSz0RcZ0ag")
  296. res, err := client.Do(req)
  297. if err != nil {
  298. fmt.Println(err)
  299. return err
  300. }
  301. defer res.Body.Close()
  302. body, err := ioutil.ReadAll(res.Body)
  303. if err != nil {
  304. fmt.Println("Getres.Body Err:", err.Error())
  305. return err
  306. }
  307. var pdfResult models.ArticleResultApi
  308. err = json.Unmarshal(body, &pdfResult)
  309. if err != nil {
  310. fmt.Println("Getres.pdfResult Err:", err.Error())
  311. return err
  312. }
  313. exitMap := make(map[int]int)
  314. classMap := make(map[int]int)
  315. reportMap := make(map[int]int)
  316. summaryMap := make(map[int]int)
  317. listMap, err := models.GetArticleApiMap()
  318. if err != nil {
  319. fmt.Println("GetlistMap Err:", err.Error())
  320. return err
  321. }
  322. //新旧分类 反向隐射,是否归类,是否是报告,是否是纪要库
  323. for _, v := range listMap {
  324. exitMap[v.Id] = v.OldId
  325. if v.IsClass == 1 {
  326. classMap[v.OldId] = 1
  327. }
  328. if v.IsReport == 1 {
  329. reportMap[v.OldId] = 1
  330. }
  331. if v.IsSummary == 1 {
  332. summaryMap[v.OldId] = 1
  333. }
  334. }
  335. listData := pdfResult.Data
  336. var list []*models.Tactics2
  337. var listAuthor []*models.CygxArticleAuthor
  338. for _, v := range listData {
  339. if exitMap[v.SeriesId] > 0 {
  340. 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)
  341. item := new(models.Tactics2)
  342. itemAuthor := new(models.CygxArticleAuthor)
  343. item.ArticleId = v.ArticleId
  344. item.Title = v.Title
  345. item.TitleEn = v.TitleEn
  346. item.File = v.File
  347. if v.Frequency == "日度" {
  348. item.UpdateFrequency = "daily"
  349. } else if v.Frequency == "周度" {
  350. item.UpdateFrequency = "weekly"
  351. } else if v.Frequency == "月度" {
  352. item.UpdateFrequency = "monthly"
  353. } else if v.Frequency == "季度" {
  354. item.UpdateFrequency = "quarterly"
  355. } else if v.Frequency == "年度" {
  356. item.UpdateFrequency = "yearly"
  357. } else {
  358. item.UpdateFrequency = "unknow"
  359. }
  360. item.CreateDate = v.CreateDate
  361. item.PublishDate = v.PublishDate
  362. item.PublishStatus = 1
  363. item.Body = v.Content.Body
  364. item.Abstract = v.Content.Abstract
  365. item.CategoryName = v.Industry.Name
  366. item.CategoryId = exitMap[v.SeriesId]
  367. item.SubCategoryName = v.Series.Name
  368. if len(v.Stock) > 0 {
  369. var stock string
  370. for _, vS := range v.Stock {
  371. stock += vS + "/"
  372. }
  373. stock = strings.TrimRight(stock, "/")
  374. item.Stock = stock
  375. }
  376. item.FieldName = v.Field.Name
  377. list = append(list, item)
  378. itemAuthor.ArticleId = v.ArticleId
  379. itemAuthor.Name = v.Author.Name
  380. itemAuthor.Mobile = v.Author.PhoneNumber
  381. listAuthor = append(listAuthor, itemAuthor)
  382. }
  383. }
  384. //同步作者
  385. for _, v := range listAuthor {
  386. var count int
  387. count, err = models.GetActivityAuthorCount(v.ArticleId, v.Mobile)
  388. if err != nil {
  389. fmt.Println("GetCount Err:", err.Error())
  390. return err
  391. }
  392. if count == 0 {
  393. _, err = models.AddCygxActivityAuthor(v)
  394. if err != nil {
  395. fmt.Println("AddCygxActivityAuthor Err:", err.Error())
  396. return err
  397. }
  398. }
  399. }
  400. fmt.Println("同步文章条数:", len(list))
  401. listCustomArticle, err := models.GetCustomArticleId() //手动归类的文章,不替换文章类型
  402. if err != nil {
  403. fmt.Println("GetTacticsList Err:", err.Error())
  404. return err
  405. }
  406. listGetMatchTypeName, errMatch := models.GetMatchTypeNamenNotNull() //手动归类的文章,不替换文章类型
  407. if errMatch != nil {
  408. fmt.Println("GetTacticsList Err:", errMatch.Error())
  409. return err
  410. }
  411. fmt.Println("list len:", len(list))
  412. 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
  413. listNoSummaryArticleIds := strings.Split(noSummaryArticleIds, ",")
  414. for k, v := range list {
  415. //同步匹配类型
  416. matchTypeName := ""
  417. for _, vMatch := range listGetMatchTypeName {
  418. if v.CategoryId == vMatch.CategoryId {
  419. matchTypeName = vMatch.MatchTypeName
  420. }
  421. }
  422. //是否属于纪要库的数据
  423. if _, has := summaryMap[v.CategoryId]; has {
  424. v.IsSummary = 1
  425. }
  426. //排除不属于纪要库类型的文章
  427. for _, vArt := range listNoSummaryArticleIds {
  428. vArtInt, _ := strconv.Atoi(vArt)
  429. if v.ArticleId == vArtInt {
  430. v.IsSummary = 0
  431. }
  432. }
  433. if _, has := reportMap[v.CategoryId]; has {
  434. v.IsReport = 1
  435. if _, ok := classMap[v.CategoryId]; ok {
  436. v.IsClass = 1
  437. v.ReportType = 1 //是否属于行业报告
  438. } else {
  439. v.ReportType = 2 //是否属于产业报告
  440. }
  441. }
  442. v.Department = "弘则权益研究"
  443. //判断是否已经存在
  444. if v.ArticleId < 0 {
  445. fmt.Println("AddCygxArticle Err:")
  446. return err
  447. }
  448. var count int
  449. count, err = models.GetArticleCountById(v.ArticleId)
  450. if err != nil && err.Error() != utils.ErrNoRow() {
  451. fmt.Println("AddCygxArticle Err:", err.Error())
  452. return err
  453. }
  454. v.Body = strings.Replace(v.Body, "http://vmp.hzinsights.com", "https://vmp.hzinsights.com", -1)
  455. expertNumStr, expertContentStr, interviewDateStr, _, bodyReturn := BodyAnalysis2(v.Body)
  456. if strings.Index(v.Body, "报告全文(") > 0 && strings.Index(v.Body, "PDF格式报告下载.pdf") > 0 {
  457. v.Body = strings.Replace(v.Body, "报告全文(", "", -1)
  458. v.Body = strings.Replace(v.Body, "PDF格式报告下载.pdf", "", -1)
  459. v.Body = strings.Replace(v.Body, "):", "", -1)
  460. }
  461. var titleNew string
  462. titleNew = v.Title
  463. // 7资金流向 、11大类资产 、51每日复盘 、80医药周报、9估值研究
  464. if v.CategoryId == 7 || v.CategoryId == 11 || v.CategoryId == 51 || v.CategoryId == 9 {
  465. if v.UpdateFrequency == "daily" {
  466. var daystr string
  467. daystr = strconv.Itoa(v.PublishDate.Day())
  468. if len(daystr) == 1 {
  469. daystr = "0" + daystr
  470. }
  471. titleNew = v.Title + "(" + strconv.Itoa(v.PublishDate.Year())[2:len(strconv.Itoa(v.PublishDate.Year()))-0] + v.PublishDate.Format("01") + daystr + ")"
  472. } else if v.UpdateFrequency == "weekly" {
  473. titleNew = v.Title + utils.WeekByDate(v.PublishDate)
  474. }
  475. }
  476. if v.CategoryId == 80 {
  477. titleNew = v.Title + utils.WeekByDate(v.PublishDate)
  478. }
  479. if count > 0 {
  480. fmt.Println(k, v.ArticleId, "edit")
  481. var isCustom bool
  482. bodyText, _ := GetReportContentTextSub(v.Body)
  483. updateParams := make(map[string]interface{})
  484. //updateParams["Title"] = v.Title
  485. updateParams["Title"] = titleNew
  486. updateParams["TitleEn"] = v.TitleEn
  487. updateParams["UpdateFrequency"] = v.UpdateFrequency
  488. updateParams["CreateDate"] = v.CreateDate
  489. updateParams["PublishDate"] = v.PublishDate
  490. //updateParams["Body"] = html.EscapeString(v.Body)
  491. updateParams["Body"] = html.EscapeString(bodyReturn)
  492. updateParams["BodyText"] = bodyText
  493. updateParams["Abstract"] = html.EscapeString(v.Abstract)
  494. updateParams["CategoryName"] = v.CategoryName
  495. for _, vCustom := range listCustomArticle {
  496. if v.ArticleId == vCustom.ArticleId {
  497. fmt.Println("手动归类的文章:" + strconv.Itoa(v.ArticleId))
  498. isCustom = true
  499. }
  500. }
  501. if isCustom == false {
  502. updateParams["CategoryId"] = v.CategoryId
  503. updateParams["MatchTypeName"] = matchTypeName
  504. updateParams["IsSummary"] = v.IsSummary
  505. updateParams["IsReport"] = v.IsReport
  506. updateParams["ReportType"] = v.ReportType
  507. updateParams["SubCategoryName"] = v.SubCategoryName
  508. }
  509. //updateParams["CategoryId"] = v.CategoryId
  510. updateParams["PublishStatus"] = 1
  511. updateParams["ExpertBackground"] = expertContentStr
  512. updateParams["ExpertNumber"] = expertNumStr
  513. updateParams["InterviewDate"] = interviewDateStr
  514. //updateParams["IsClass"] = v.IsClass
  515. v.Department = "弘则权益研究"
  516. updateParams["Department"] = v.Department
  517. updateParams["FileLink"] = v.File
  518. updateParams["Stock"] = v.Stock
  519. updateParams["FieldName"] = v.FieldName
  520. whereParam := map[string]interface{}{"article_id": v.ArticleId}
  521. err = models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
  522. if err != nil {
  523. fmt.Println("UpdateByExpr Err:" + err.Error())
  524. return err
  525. }
  526. } else {
  527. fmt.Println(k, v.ArticleId, "add")
  528. item := new(models.CygxArticle)
  529. articleIdInt := v.ArticleId
  530. item.ArticleId = articleIdInt
  531. //item.Title = v.Title
  532. item.Title = titleNew
  533. item.TitleEn = v.TitleEn
  534. item.UpdateFrequency = v.UpdateFrequency
  535. item.CreateDate = v.CreateDate
  536. item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
  537. //item.Body = html.EscapeString(v.Body)
  538. item.Body = html.EscapeString(bodyReturn)
  539. item.Abstract = html.EscapeString(v.Abstract)
  540. item.CategoryName = v.CategoryName
  541. item.SubCategoryName = v.SubCategoryName
  542. item.CategoryId = v.CategoryId
  543. item.CategoryIdTwo = v.CategoryId
  544. item.PublishStatus = 1
  545. item.ExpertBackground = expertContentStr
  546. item.ExpertNumber = expertNumStr
  547. item.InterviewDate = interviewDateStr
  548. item.Department = v.Department
  549. item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
  550. item.IsClass = v.IsClass
  551. item.IsSummary = v.IsSummary
  552. item.IsReport = v.IsReport
  553. item.ReportType = v.ReportType
  554. item.FileLink = v.File
  555. item.MatchTypeName = matchTypeName
  556. item.Stock = v.Stock
  557. item.FieldName = v.FieldName
  558. _, err = models.AddCygxArticles(item)
  559. if err != nil {
  560. fmt.Println("AddCygxArticle Err:", err.Error())
  561. return err
  562. }
  563. }
  564. }
  565. return
  566. }
  567. func SynchronizationArtclehistory() {
  568. fmt.Println("同步开始")
  569. list, err := models.GetArticleHistoryList()
  570. if err != nil {
  571. fmt.Println("获取列表失败", err)
  572. }
  573. fmt.Println(len(list))
  574. for _, v := range list {
  575. //endDate := v.ModifyTime.Add(+time.Minute * 10).Format(utils.FormatDateTime)
  576. //detail, err := models.GetNewArticleHistoryRecordNewpv(v.UserId, v.ArticleId, endDate)
  577. //if err != nil && err.Error() != utils.ErrNoRow() {
  578. // fmt.Println("获取信息失败", err)
  579. //}
  580. v.OutType = 1
  581. //fmt.Println(v.Id)
  582. //if detail == nil {
  583. // _, err = models.AddCygxArticleViewRecordNewpv(v)
  584. // if err != nil {
  585. // fmt.Println("新增失败", err)
  586. // }
  587. //} else {
  588. // err = models.UpdateCygxArticleViewRecordNewpvList(v, v.StopTime)
  589. // if err != nil {
  590. // fmt.Println("修改失败", err)
  591. // }
  592. //}
  593. newId, err := models.AddCygxArticleViewRecordNewpv(v)
  594. fmt.Println("新增", newId)
  595. if err != nil {
  596. fmt.Println("新增失败", err)
  597. }
  598. }
  599. fmt.Println("同步结束")
  600. }
  601. //统计报表
  602. func StatisticalReport() {
  603. var isSummaryNumAll, isClassNum, pvNumAll, uvNumAll int
  604. list, err := models.GetChartPermissionActivity()
  605. if err != nil {
  606. fmt.Println("获取列表失败", err)
  607. }
  608. for _, v := range list {
  609. var listPv []*models.ReportMappingStatistical
  610. if v.PermissionName == "研选" {
  611. listPv, err = models.GetStatisticalReportArtilceExpert()
  612. if err != nil {
  613. fmt.Println("获取Pv列表失败", err)
  614. }
  615. } else {
  616. listPv, err = models.GetStatisticalReportArtilce(v.ChartPermissionId)
  617. if err != nil {
  618. fmt.Println("获取Pv列表失败", err)
  619. }
  620. }
  621. var pvNum, uvNum, isSummaryNum int
  622. for _, v2 := range listPv {
  623. pvNum += v2.Pv
  624. uvNum += v2.Uv
  625. if v2.IsSummary == "1" {
  626. isSummaryNum += 1
  627. }
  628. if v2.IsClass == "1" && v.ChartPermissionId <= 22 {
  629. isClassNum += 1
  630. }
  631. if v2.IsSummary == "1" && v.ChartPermissionId <= 22 {
  632. isSummaryNumAll += 1
  633. }
  634. }
  635. if v.ChartPermissionId <= 22 {
  636. pvNumAll += pvNum
  637. uvNumAll += uvNum
  638. }
  639. fmt.Println(v.PermissionName+"行业", len(listPv), "篇,其中主观类报告", isSummaryNum, "篇,客观类报告", len(listPv)-isSummaryNum, "篇。共产生阅读量pv-,", pvNum, ",uv-", uvNum)
  640. }
  641. fmt.Println("目前同步四大行业的总报告(已归类)数量", isClassNum, "篇,其中主观类报告", isSummaryNumAll, "篇,客观类报告", isClassNum-isSummaryNumAll, "篇。共产生阅读量pv-", pvNumAll, ",uv-", uvNumAll)
  642. var totalOnline int //线上
  643. var totalOffline int //线下
  644. var totalPeople int //共累计预约外呼人数
  645. var totalSignUpOff int //线下报名人数
  646. var totalSignUpOffTime int //线下报名人数
  647. var totalPeopleMeet int //线下参会人数
  648. o := orm.NewOrm()
  649. 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();`
  650. err = o.Raw(sql).QueryRow(&totalOnline)
  651. if err != nil {
  652. fmt.Println("获取线上", err)
  653. }
  654. 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();`
  655. err = o.Raw(sql).QueryRow(&totalOffline)
  656. if err != nil {
  657. fmt.Println("获取线下", err)
  658. }
  659. sql = `SELECT COUNT( 1 ) FROM
  660. cygx_activity_signup as s
  661. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  662. WHERE
  663. s.do_fail_type = 0
  664. AND a.is_submit_meeting = 1
  665. AND a.activity_time <= NOW()
  666. AND a.publish_status = 1`
  667. err = o.Raw(sql).QueryRow(&totalPeople)
  668. if err != nil {
  669. fmt.Println("共累计预约外呼人数", err)
  670. }
  671. sql = `SELECT COUNT( 1 ) FROM
  672. cygx_activity_signup as s
  673. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  674. WHERE
  675. s.do_fail_type = 0
  676. AND a.is_submit_meeting = 1
  677. AND a.activity_time <= NOW()
  678. AND a.activity_type_id IN (4,5,6)
  679. AND a.publish_status = 1`
  680. err = o.Raw(sql).QueryRow(&totalSignUpOff)
  681. if err != nil {
  682. fmt.Println("共累计预约外呼人数", err)
  683. }
  684. sql = `SELECT COUNT( 1 ) FROM
  685. cygx_activity_signup as s
  686. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  687. WHERE
  688. s.do_fail_type = 0
  689. AND a.publish_status = 1
  690. AND a.is_submit_meeting = 1
  691. AND a.activity_time <= NOW()
  692. AND a.is_submit_meeting = 1
  693. AND a.activity_type_id IN (4,5,6);`
  694. err = o.Raw(sql).QueryRow(&totalSignUpOffTime)
  695. if err != nil {
  696. fmt.Println("线下报名参会人数", err)
  697. }
  698. sql = `SELECT COUNT( 1 ) FROM
  699. cygx_activity_signup as s
  700. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  701. WHERE
  702. s.do_fail_type = 0
  703. AND a.is_submit_meeting = 1
  704. AND a.activity_time <= NOW()
  705. AND a.publish_status = 1
  706. AND s.is_meeting = 1
  707. AND a.activity_type_id IN (4,5,6);`
  708. err = o.Raw(sql).QueryRow(&totalPeopleMeet)
  709. if err != nil {
  710. fmt.Println("线下参会人数", err)
  711. }
  712. fmt.Println("共上线活动", totalOnline+totalOffline, "个,其中线上", totalOnline, "个,线下", totalOffline, "个")
  713. fmt.Println("共累计预约外呼人数", totalPeople, "人")
  714. fmt.Println("报名线下参会", totalSignUpOff, "人,实际到会人数", totalPeopleMeet, "人,线下到会率约", totalPeopleMeet*100/totalSignUpOff, "%")
  715. num := totalPeopleMeet / totalSignUpOffTime
  716. fmt.Println(num)
  717. fmt.Println(totalOnline)
  718. fmt.Println(totalOffline)
  719. fmt.Println(totalPeople)
  720. fmt.Println(totalSignUpOff)
  721. fmt.Println(totalPeopleMeet)
  722. fmt.Println(totalSignUpOffTime)
  723. fmt.Println(totalPeopleMeet / totalSignUpOffTime)
  724. return
  725. }
  726. // UserViewRedisData 阅读数据
  727. type UserViewRedisData struct {
  728. Mobile string `json:"mobile"`
  729. Email string `json:"email"`
  730. RealName string `json:"real_name"`
  731. CompanyName string `json:"company_name"`
  732. ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
  733. ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
  734. CompanyId int `json:"company_id" description:"客户id"`
  735. }
  736. type ReportViewRecord struct {
  737. Id int `orm:"column(id);pk"`
  738. UserId int `description:"用户id"`
  739. ReportId int `description:"报告id"`
  740. Mobile string `description:"手机号"`
  741. Email string `description:"邮箱"`
  742. RealName string `description:"用户实际姓名"`
  743. CompanyName string `description:"公司名称"`
  744. CreateTime time.Time `description:"创建时间"`
  745. }
  746. // PushViewRecordNewRedisData 阅读数据加入到redis
  747. func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId int) bool {
  748. data := &UserViewRedisData{
  749. Mobile: reportViewRecord.Mobile,
  750. Email: reportViewRecord.Email,
  751. RealName: reportViewRecord.RealName,
  752. CompanyName: reportViewRecord.CompanyName,
  753. ViewTime: reportViewRecord.CreateTime.Format(utils.FormatDateTime),
  754. ProductId: 2,
  755. CompanyId: companyId,
  756. }
  757. if utils.Re == nil {
  758. err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)
  759. if err != nil {
  760. fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
  761. }
  762. return true
  763. }
  764. return false
  765. }