article.go 32 KB

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