article.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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.SendAlarmMsg("获取图表信息失败", 2)
  285. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  286. }
  287. }()
  288. url := "https://vmp.hzinsights.com/v2api/articles/mp?take=100&skip=0&publish_status=2,4&order=publish_date&sort=DESC"
  289. method := "GET"
  290. client := &nhttp.Client{}
  291. req, err := nhttp.NewRequest(method, url, nil)
  292. if err != nil {
  293. fmt.Println("GetListApi Err:", err.Error())
  294. return err
  295. }
  296. req.Header.Add("Authorization", "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkiLCJwaG9uZV9udW1iZXIiOiIxMjM0NTY3ODkiLCJuYW1lIjoi5YW25LuWIiwiZW50cmFuY2UiOiJwYXNzd3dvcmQiLCJpYXQiOjE2MzQ4NzA1OTQsImV4cCI6MTYzNDg3NDE5NH0.tho2L9jsbDPn8ltEGUVDve_nHsh0Kzf6ZrSz0RcZ0ag")
  297. res, err := client.Do(req)
  298. if err != nil {
  299. fmt.Println(err)
  300. return err
  301. }
  302. defer res.Body.Close()
  303. body, err := ioutil.ReadAll(res.Body)
  304. if err != nil {
  305. fmt.Println("Getres.Body Err:", err.Error())
  306. return err
  307. }
  308. var pdfResult models.ArticleResultApi
  309. err = json.Unmarshal(body, &pdfResult)
  310. if err != nil {
  311. fmt.Println("Getres.pdfResult Err:", err.Error())
  312. return err
  313. }
  314. exitMap := make(map[int]int)
  315. classMap := make(map[int]int)
  316. reportMap := make(map[int]int)
  317. summaryMap := make(map[int]int)
  318. listMap, err := models.GetArticleApiMap()
  319. if err != nil {
  320. fmt.Println("GetlistMap Err:", err.Error())
  321. return err
  322. }
  323. //新旧分类 反向隐射,是否归类,是否是报告,是否是纪要库
  324. for _, v := range listMap {
  325. exitMap[v.Id] = v.OldId
  326. if v.IsClass == 1 {
  327. classMap[v.OldId] = 1
  328. }
  329. if v.IsReport == 1 {
  330. reportMap[v.OldId] = 1
  331. }
  332. if v.IsSummary == 1 {
  333. summaryMap[v.OldId] = 1
  334. }
  335. }
  336. listData := pdfResult.Data
  337. var list []*models.Tactics2
  338. var listAuthor []*models.CygxArticleAuthor
  339. for _, v := range listData {
  340. //状态等于 2 跟 4 的进行同步
  341. if exitMap[v.SeriesId] > 0 && (v.PublishStatus == 2 || v.PublishStatus == 4) {
  342. 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)
  343. item := new(models.Tactics2)
  344. itemAuthor := new(models.CygxArticleAuthor)
  345. item.ArticleId = v.ArticleId
  346. item.Title = v.Title
  347. item.TitleEn = v.TitleEn
  348. item.File = v.File
  349. if v.Frequency == "日度" {
  350. item.UpdateFrequency = "daily"
  351. } else if v.Frequency == "周度" {
  352. item.UpdateFrequency = "weekly"
  353. } else if v.Frequency == "月度" {
  354. item.UpdateFrequency = "monthly"
  355. } else if v.Frequency == "季度" {
  356. item.UpdateFrequency = "quarterly"
  357. } else if v.Frequency == "年度" {
  358. item.UpdateFrequency = "yearly"
  359. } else {
  360. item.UpdateFrequency = "unknow"
  361. }
  362. item.CreateDate = v.CreateDate
  363. item.PublishDate = v.PublishDate
  364. item.PublishStatus = 1
  365. item.Body = v.Content.Body
  366. item.Abstract = v.Content.Abstract
  367. item.CategoryName = v.Industry.Name
  368. item.CategoryId = exitMap[v.SeriesId]
  369. item.SubCategoryName = v.Series.Name
  370. if len(v.Stock) > 0 {
  371. var stock string
  372. for _, vS := range v.Stock {
  373. stock += vS + "/"
  374. }
  375. stock = strings.TrimRight(stock, "/")
  376. item.Stock = stock
  377. }
  378. item.FieldName = v.Field.Name
  379. list = append(list, item)
  380. itemAuthor.ArticleId = v.ArticleId
  381. itemAuthor.Name = v.Author.Name
  382. itemAuthor.Mobile = v.Author.PhoneNumber
  383. listAuthor = append(listAuthor, itemAuthor)
  384. }
  385. }
  386. //同步作者
  387. for _, v := range listAuthor {
  388. var count int
  389. count, err = models.GetActivityAuthorCount(v.ArticleId, v.Mobile)
  390. if err != nil {
  391. fmt.Println("GetCount Err:", err.Error())
  392. return err
  393. }
  394. if count == 0 {
  395. _, err = models.AddCygxActivityAuthor(v)
  396. if err != nil {
  397. fmt.Println("AddCygxActivityAuthor Err:", err.Error())
  398. return err
  399. }
  400. }
  401. }
  402. fmt.Println("同步文章条数:", len(list))
  403. listCustomArticle, err := models.GetCustomArticleId() //手动归类的文章,不替换文章类型
  404. if err != nil {
  405. fmt.Println("GetTacticsList Err:", err.Error())
  406. return err
  407. }
  408. listGetMatchTypeName, errMatch := models.GetMatchTypeNamenNotNull() //手动归类的文章,不替换文章类型
  409. if errMatch != nil {
  410. fmt.Println("GetTacticsList Err:", errMatch.Error())
  411. return err
  412. }
  413. fmt.Println("list len:", len(list))
  414. 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
  415. listNoSummaryArticleIds := strings.Split(noSummaryArticleIds, ",")
  416. for k, v := range list {
  417. //同步匹配类型
  418. matchTypeName := ""
  419. for _, vMatch := range listGetMatchTypeName {
  420. if v.CategoryId == vMatch.CategoryId {
  421. matchTypeName = vMatch.MatchTypeName
  422. }
  423. }
  424. //是否属于纪要库的数据
  425. if _, has := summaryMap[v.CategoryId]; has {
  426. v.IsSummary = 1
  427. }
  428. //排除不属于纪要库类型的文章
  429. for _, vArt := range listNoSummaryArticleIds {
  430. vArtInt, _ := strconv.Atoi(vArt)
  431. if v.ArticleId == vArtInt {
  432. v.IsSummary = 0
  433. }
  434. }
  435. if _, has := reportMap[v.CategoryId]; has {
  436. v.IsReport = 1
  437. if _, ok := classMap[v.CategoryId]; ok {
  438. v.IsClass = 1
  439. v.ReportType = 1 //是否属于行业报告
  440. } else {
  441. v.ReportType = 2 //是否属于产业报告
  442. }
  443. }
  444. v.Department = "弘则权益研究"
  445. //判断是否已经存在
  446. if v.ArticleId < 0 {
  447. fmt.Println("AddCygxArticle Err:")
  448. return err
  449. }
  450. var count int
  451. count, err = models.GetArticleCountById(v.ArticleId)
  452. if err != nil && err.Error() != utils.ErrNoRow() {
  453. fmt.Println("AddCygxArticle Err:", err.Error())
  454. return err
  455. }
  456. v.Body = strings.Replace(v.Body, "http://vmp.hzinsights.com", "https://vmp.hzinsights.com", -1)
  457. expertNumStr, expertContentStr, interviewDateStr, _, bodyReturn := BodyAnalysis2(v.Body)
  458. if strings.Index(v.Body, "报告全文(") > 0 && strings.Index(v.Body, "PDF格式报告下载.pdf") > 0 {
  459. v.Body = strings.Replace(v.Body, "报告全文(", "", -1)
  460. v.Body = strings.Replace(v.Body, "PDF格式报告下载.pdf", "", -1)
  461. v.Body = strings.Replace(v.Body, "):", "", -1)
  462. }
  463. var titleNew string
  464. titleNew = v.Title
  465. // 7资金流向 、11大类资产 、51每日复盘 、80医药周报、9估值研究
  466. if v.CategoryId == 7 || v.CategoryId == 11 || v.CategoryId == 51 || v.CategoryId == 9 {
  467. if v.UpdateFrequency == "daily" {
  468. var daystr string
  469. daystr = strconv.Itoa(v.PublishDate.Day())
  470. if len(daystr) == 1 {
  471. daystr = "0" + daystr
  472. }
  473. titleNew = v.Title + "(" + strconv.Itoa(v.PublishDate.Year())[2:len(strconv.Itoa(v.PublishDate.Year()))-0] + v.PublishDate.Format("01") + daystr + ")"
  474. } else if v.UpdateFrequency == "weekly" {
  475. titleNew = v.Title + utils.WeekByDate(v.PublishDate)
  476. }
  477. }
  478. if v.CategoryId == 80 {
  479. titleNew = v.Title + utils.WeekByDate(v.PublishDate)
  480. }
  481. if count > 0 {
  482. fmt.Println(k, v.ArticleId, "edit")
  483. var isCustom bool
  484. bodyText, _ := GetReportContentTextSub(v.Body)
  485. updateParams := make(map[string]interface{})
  486. //updateParams["Title"] = v.Title
  487. updateParams["Title"] = titleNew
  488. updateParams["TitleEn"] = v.TitleEn
  489. updateParams["UpdateFrequency"] = v.UpdateFrequency
  490. updateParams["CreateDate"] = v.CreateDate
  491. updateParams["PublishDate"] = v.PublishDate
  492. //updateParams["Body"] = html.EscapeString(v.Body)
  493. updateParams["Body"] = html.EscapeString(bodyReturn)
  494. updateParams["BodyText"] = bodyText
  495. updateParams["Abstract"] = html.EscapeString(v.Abstract)
  496. updateParams["CategoryName"] = v.CategoryName
  497. for _, vCustom := range listCustomArticle {
  498. if v.ArticleId == vCustom.ArticleId {
  499. fmt.Println("手动归类的文章:" + strconv.Itoa(v.ArticleId))
  500. isCustom = true
  501. }
  502. }
  503. if isCustom == false {
  504. updateParams["CategoryId"] = v.CategoryId
  505. updateParams["MatchTypeName"] = matchTypeName
  506. updateParams["IsSummary"] = v.IsSummary
  507. updateParams["IsReport"] = v.IsReport
  508. updateParams["ReportType"] = v.ReportType
  509. updateParams["SubCategoryName"] = v.SubCategoryName
  510. }
  511. //updateParams["CategoryId"] = v.CategoryId
  512. updateParams["PublishStatus"] = 1
  513. updateParams["ExpertBackground"] = expertContentStr
  514. updateParams["ExpertNumber"] = expertNumStr
  515. updateParams["InterviewDate"] = interviewDateStr
  516. //updateParams["IsClass"] = v.IsClass
  517. v.Department = "弘则权益研究"
  518. updateParams["Department"] = v.Department
  519. updateParams["FileLink"] = v.File
  520. updateParams["Stock"] = v.Stock
  521. updateParams["FieldName"] = v.FieldName
  522. whereParam := map[string]interface{}{"article_id": v.ArticleId}
  523. err = models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
  524. if err != nil {
  525. fmt.Println("UpdateByExpr Err:" + err.Error())
  526. return err
  527. }
  528. } else {
  529. fmt.Println(k, v.ArticleId, "add")
  530. item := new(models.CygxArticle)
  531. articleIdInt := v.ArticleId
  532. item.ArticleId = articleIdInt
  533. //item.Title = v.Title
  534. item.Title = titleNew
  535. item.TitleEn = v.TitleEn
  536. item.UpdateFrequency = v.UpdateFrequency
  537. item.CreateDate = v.CreateDate
  538. item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
  539. //item.Body = html.EscapeString(v.Body)
  540. item.Body = html.EscapeString(bodyReturn)
  541. item.Abstract = html.EscapeString(v.Abstract)
  542. item.CategoryName = v.CategoryName
  543. item.SubCategoryName = v.SubCategoryName
  544. item.CategoryId = v.CategoryId
  545. item.CategoryIdTwo = v.CategoryId
  546. item.PublishStatus = 1
  547. item.ExpertBackground = expertContentStr
  548. item.ExpertNumber = expertNumStr
  549. item.InterviewDate = interviewDateStr
  550. item.Department = v.Department
  551. item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
  552. item.IsClass = v.IsClass
  553. item.IsSummary = v.IsSummary
  554. item.IsReport = v.IsReport
  555. item.ReportType = v.ReportType
  556. item.FileLink = v.File
  557. item.MatchTypeName = matchTypeName
  558. item.Stock = v.Stock
  559. item.FieldName = v.FieldName
  560. _, err = models.AddCygxArticles(item)
  561. if err != nil {
  562. fmt.Println("AddCygxArticle Err:", err.Error())
  563. return err
  564. }
  565. }
  566. }
  567. return
  568. }
  569. func SynchronizationArtclehistory() {
  570. fmt.Println("同步开始")
  571. list, err := models.GetArticleHistoryList()
  572. if err != nil {
  573. fmt.Println("获取列表失败", err)
  574. }
  575. fmt.Println(len(list))
  576. for _, v := range list {
  577. //endDate := v.ModifyTime.Add(+time.Minute * 10).Format(utils.FormatDateTime)
  578. //detail, err := models.GetNewArticleHistoryRecordNewpv(v.UserId, v.ArticleId, endDate)
  579. //if err != nil && err.Error() != utils.ErrNoRow() {
  580. // fmt.Println("获取信息失败", err)
  581. //}
  582. v.OutType = 1
  583. //fmt.Println(v.Id)
  584. //if detail == nil {
  585. // _, err = models.AddCygxArticleViewRecordNewpv(v)
  586. // if err != nil {
  587. // fmt.Println("新增失败", err)
  588. // }
  589. //} else {
  590. // err = models.UpdateCygxArticleViewRecordNewpvList(v, v.StopTime)
  591. // if err != nil {
  592. // fmt.Println("修改失败", err)
  593. // }
  594. //}
  595. newId, err := models.AddCygxArticleViewRecordNewpv(v)
  596. fmt.Println("新增", newId)
  597. if err != nil {
  598. fmt.Println("新增失败", err)
  599. }
  600. }
  601. fmt.Println("同步结束")
  602. }
  603. //统计报表
  604. func StatisticalReport() {
  605. var isSummaryNumAll, isClassNum, pvNumAll, uvNumAll int
  606. list, err := models.GetChartPermissionActivity()
  607. if err != nil {
  608. fmt.Println("获取列表失败", err)
  609. }
  610. for _, v := range list {
  611. var listPv []*models.ReportMappingStatistical
  612. if v.PermissionName == "研选" {
  613. listPv, err = models.GetStatisticalReportArtilceExpert()
  614. if err != nil {
  615. fmt.Println("获取Pv列表失败", err)
  616. }
  617. } else {
  618. listPv, err = models.GetStatisticalReportArtilce(v.ChartPermissionId)
  619. if err != nil {
  620. fmt.Println("获取Pv列表失败", err)
  621. }
  622. }
  623. var pvNum, uvNum, isSummaryNum int
  624. for _, v2 := range listPv {
  625. pvNum += v2.Pv
  626. uvNum += v2.Uv
  627. if v2.IsSummary == "1" {
  628. isSummaryNum += 1
  629. }
  630. if v2.IsClass == "1" && v.ChartPermissionId <= 22 {
  631. isClassNum += 1
  632. }
  633. if v2.IsSummary == "1" && v.ChartPermissionId <= 22 {
  634. isSummaryNumAll += 1
  635. }
  636. }
  637. if v.ChartPermissionId <= 22 {
  638. pvNumAll += pvNum
  639. uvNumAll += uvNum
  640. }
  641. fmt.Println(v.PermissionName+"行业", len(listPv), "篇,其中主观类报告", isSummaryNum, "篇,客观类报告", len(listPv)-isSummaryNum, "篇。共产生阅读量pv-,", pvNum, ",uv-", uvNum)
  642. }
  643. fmt.Println("目前同步四大行业的总报告(已归类)数量", isClassNum, "篇,其中主观类报告", isSummaryNumAll, "篇,客观类报告", isClassNum-isSummaryNumAll, "篇。共产生阅读量pv-", pvNumAll, ",uv-", uvNumAll)
  644. var totalOnline int //线上
  645. var totalOffline int //线下
  646. var totalPeople int //共累计预约外呼人数
  647. var totalSignUpOff int //线下报名人数
  648. var totalSignUpOffTime int //线下报名人数
  649. var totalPeopleMeet int //线下参会人数
  650. o := orm.NewOrm()
  651. 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();`
  652. err = o.Raw(sql).QueryRow(&totalOnline)
  653. if err != nil {
  654. fmt.Println("获取线上", err)
  655. }
  656. 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();`
  657. err = o.Raw(sql).QueryRow(&totalOffline)
  658. if err != nil {
  659. fmt.Println("获取线下", err)
  660. }
  661. sql = `SELECT COUNT( 1 ) FROM
  662. cygx_activity_signup as s
  663. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  664. WHERE
  665. s.do_fail_type = 0
  666. AND a.is_submit_meeting = 1
  667. AND a.activity_time <= NOW()
  668. AND a.publish_status = 1`
  669. err = o.Raw(sql).QueryRow(&totalPeople)
  670. if err != nil {
  671. fmt.Println("共累计预约外呼人数", err)
  672. }
  673. sql = `SELECT COUNT( 1 ) FROM
  674. cygx_activity_signup as s
  675. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  676. WHERE
  677. s.do_fail_type = 0
  678. AND a.is_submit_meeting = 1
  679. AND a.activity_time <= NOW()
  680. AND a.activity_type_id IN (4,5,6)
  681. AND a.publish_status = 1`
  682. err = o.Raw(sql).QueryRow(&totalSignUpOff)
  683. if err != nil {
  684. fmt.Println("共累计预约外呼人数", err)
  685. }
  686. sql = `SELECT COUNT( 1 ) FROM
  687. cygx_activity_signup as s
  688. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  689. WHERE
  690. s.do_fail_type = 0
  691. AND a.publish_status = 1
  692. AND a.is_submit_meeting = 1
  693. AND a.activity_time <= NOW()
  694. AND a.is_submit_meeting = 1
  695. AND a.activity_type_id IN (4,5,6);`
  696. err = o.Raw(sql).QueryRow(&totalSignUpOffTime)
  697. if err != nil {
  698. fmt.Println("线下报名参会人数", err)
  699. }
  700. sql = `SELECT COUNT( 1 ) FROM
  701. cygx_activity_signup as s
  702. INNER JOIN cygx_activity as a ON a.activity_id = s.activity_id
  703. WHERE
  704. s.do_fail_type = 0
  705. AND a.is_submit_meeting = 1
  706. AND a.activity_time <= NOW()
  707. AND a.publish_status = 1
  708. AND s.is_meeting = 1
  709. AND a.activity_type_id IN (4,5,6);`
  710. err = o.Raw(sql).QueryRow(&totalPeopleMeet)
  711. if err != nil {
  712. fmt.Println("线下参会人数", err)
  713. }
  714. fmt.Println("共上线活动", totalOnline+totalOffline, "个,其中线上", totalOnline, "个,线下", totalOffline, "个")
  715. fmt.Println("共累计预约外呼人数", totalPeople, "人")
  716. fmt.Println("报名线下参会", totalSignUpOff, "人,实际到会人数", totalPeopleMeet, "人,线下到会率约", totalPeopleMeet*100/totalSignUpOff, "%")
  717. num := totalPeopleMeet / totalSignUpOffTime
  718. fmt.Println(num)
  719. fmt.Println(totalOnline)
  720. fmt.Println(totalOffline)
  721. fmt.Println(totalPeople)
  722. fmt.Println(totalSignUpOff)
  723. fmt.Println(totalPeopleMeet)
  724. fmt.Println(totalSignUpOffTime)
  725. fmt.Println(totalPeopleMeet / totalSignUpOffTime)
  726. return
  727. }
  728. // UserViewRedisData 阅读数据
  729. type UserViewRedisData struct {
  730. Mobile string `json:"mobile"`
  731. Email string `json:"email"`
  732. RealName string `json:"real_name"`
  733. CompanyName string `json:"company_name"`
  734. ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
  735. ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
  736. CompanyId int `json:"company_id" description:"客户id"`
  737. }
  738. type ReportViewRecord struct {
  739. Id int `orm:"column(id);pk"`
  740. UserId int `description:"用户id"`
  741. ReportId int `description:"报告id"`
  742. Mobile string `description:"手机号"`
  743. Email string `description:"邮箱"`
  744. RealName string `description:"用户实际姓名"`
  745. CompanyName string `description:"公司名称"`
  746. CreateTime time.Time `description:"创建时间"`
  747. }
  748. // PushViewRecordNewRedisData 阅读数据加入到redis
  749. func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId int) bool {
  750. data := &UserViewRedisData{
  751. Mobile: reportViewRecord.Mobile,
  752. Email: reportViewRecord.Email,
  753. RealName: reportViewRecord.RealName,
  754. CompanyName: reportViewRecord.CompanyName,
  755. ViewTime: reportViewRecord.CreateTime.Format(utils.FormatDateTime),
  756. ProductId: 2,
  757. CompanyId: companyId,
  758. }
  759. if utils.Re == nil {
  760. err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)
  761. if err != nil {
  762. fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
  763. }
  764. return true
  765. }
  766. return false
  767. }