article.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/PuerkitoBio/goquery"
  6. "hongze/hongze_web_mfyx/models"
  7. "hongze/hongze_web_mfyx/utils"
  8. "html"
  9. "regexp"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "unicode/utf8"
  15. )
  16. func FixArticleImgUrl(body string) (contentSub string, err error) {
  17. r := strings.NewReader(string(body))
  18. doc, err := goquery.NewDocumentFromReader(r)
  19. if err != nil {
  20. fmt.Println(err)
  21. }
  22. doc.Find("img").Each(func(i int, s *goquery.Selection) {
  23. src, _ := s.Attr("src")
  24. if i == 0 && src != "" {
  25. contentSub = src
  26. }
  27. })
  28. return
  29. }
  30. // GetReportContentTextSubByarticle 解析文章内容
  31. func GetReportContentTextSubByarticle(content, abstract string, articleId int) (contentSub string, err error) {
  32. var lenabstract int
  33. //如果不是研选就这么展示
  34. if articleId < utils.SummaryArticleId {
  35. abstract = html.UnescapeString(abstract)
  36. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(abstract))
  37. if errdoc != nil {
  38. err = errdoc
  39. return
  40. }
  41. docabstract := doc.Text()
  42. lenabstract = utf8.RuneCountInString(docabstract)
  43. if lenabstract >= 20 {
  44. contentSub = docabstract
  45. return
  46. } else {
  47. contentSub, err = GetReportContentTextSub(content)
  48. }
  49. } else {
  50. contentSub, err = GetReportContentTextSub(content)
  51. }
  52. return
  53. }
  54. func GetReportContentTextSub(content string) (contentSub string, err error) {
  55. content = html.UnescapeString(content)
  56. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  57. if errdoc != nil {
  58. err = errdoc
  59. return
  60. }
  61. docText := doc.Text()
  62. bodyRune := []rune(docText)
  63. bodyRuneLen := len(bodyRune)
  64. body := string(bodyRune[:bodyRuneLen])
  65. contentSub = body
  66. contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
  67. contentSub = strings.Replace(body, "PoweredbyFroalaEditor", "", -1)
  68. contentSub = strings.Replace(body, " ", "", -1)
  69. return
  70. }
  71. func GetReportContentTextArticleBody(content string) (contentSub string) {
  72. contentSub = html.UnescapeString(content)
  73. contentSub = strings.Replace(contentSub, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;\">Powered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  74. contentSub = strings.Replace(contentSub, "pre", "div", -1)
  75. return
  76. }
  77. // HandleArticleCategoryImg 预处理文章的封面图片
  78. func HandleArticleCategoryImg(list []*models.ArticleListResp, user *models.WxUserItem) (items []*models.ArticleListResp, err error) {
  79. //研选的五张图片
  80. detailResearch, e := models.GetConfigByCode("category_research_img_url")
  81. if e != nil {
  82. err = errors.New("获取研选的五张图片失败" + e.Error())
  83. return
  84. }
  85. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  86. //对应分类的所图片
  87. detailCategoryUrl, err := models.GetConfigByCode("category_map_img_url")
  88. if err != nil {
  89. err = errors.New("获取对应分类的所图片失败" + err.Error())
  90. return
  91. }
  92. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  93. mapCategoryUrl := make(map[string]string)
  94. var categoryId string
  95. var imgUrlChart string
  96. for _, v := range categoryUrlList {
  97. vslice := strings.Split(v, "_")
  98. categoryId = vslice[0]
  99. imgUrlChart = vslice[len(vslice)-1]
  100. mapCategoryUrl[categoryId] = imgUrlChart
  101. }
  102. mapChartPerssion := make(map[string]string)
  103. reportMappingList, err := models.GetReportMappingStrategyAll()
  104. if err != nil {
  105. err = errors.New("GetReportMappingStrategyAll err" + err.Error())
  106. return
  107. }
  108. for _, v := range reportMappingList {
  109. mapChartPerssion[strconv.Itoa(v.CategoryId)] = v.ChartPermissionName
  110. }
  111. for k, v := range list {
  112. if list[k].Annotation == "" {
  113. imgurl, _ := FixArticleImgUrl(html.UnescapeString(list[k].Body))
  114. if imgurl != "" {
  115. list[k].BodyImg = imgurl
  116. }
  117. }
  118. item := list[k]
  119. //如果文章一开始的内容是图片,优先展示第一张图片
  120. //newBody, _ := GetReportContentTextSubByarticle(item.Body, item.Annotation, item.ArticleId)
  121. list[k].Resource = item.Resource
  122. list[k].Annotation = ArticleAnnotation(item)
  123. list[k].Body = ""
  124. list[k].Abstract, _ = GetReportContentTextSub(v.Abstract)
  125. list[k].PublishDate = utils.StrTimeToTime(item.PublishDate).Format(utils.FormatDate) //时间字符串格式转时间格式
  126. list[k].ChartPermissionName = mapChartPerssion[v.CategoryId]
  127. //如果是研选系列的任意取五张图片的中的一张
  128. if v.CategoryId == "0" || v.ArticleId >= utils.SummaryArticleId {
  129. knum := v.ArticleId % 5
  130. list[k].ImgUrlPc = researchList[knum]
  131. } else {
  132. list[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  133. }
  134. if list[k].ArticleId < utils.SummaryArticleId {
  135. list[k].HttpUrl = utils.StrategyPlatform + strconv.Itoa(v.ArticleId)
  136. list[k].IsNeedJump = true
  137. }
  138. list[k].Source = 1
  139. //添加行业默认图片
  140. if v.ImgUrlPc == "" {
  141. if v.ChartPermissionName == utils.YI_YAO_NAME {
  142. list[k].ImgUrlPc = utils.YI_YAO_OTHER_IMG
  143. } else if v.ChartPermissionName == utils.XIAO_FEI_NAME {
  144. list[k].ImgUrlPc = utils.XIAO_FEI_OTHER_IMG
  145. } else if v.ChartPermissionName == utils.KE_JI_NAME {
  146. list[k].ImgUrlPc = utils.KE_JI_OTHER_IMG
  147. } else if v.ChartPermissionName == utils.ZHI_ZAO_NAME {
  148. list[k].ImgUrlPc = utils.ZHI_ZAO_OTHER_IMG
  149. }
  150. }
  151. if v.ArticleTypeId > 0 {
  152. list[k].IsResearch = true
  153. }
  154. //是不是研选报告
  155. if v.ArticleTypeId > 0 {
  156. list[k].IsResearch = true
  157. if v.ArticleTypeId == 12 {
  158. list[k].LabelKeywordImgLink = utils.LABEL_ICO_6
  159. } else {
  160. list[k].LabelKeywordImgLink = utils.LABEL_ICO_5
  161. }
  162. } else {
  163. list[k].LabelKeywordImgLink = utils.LABEL_ICO_4
  164. list[k].SpecialColumnId = v.DepartmentId
  165. }
  166. }
  167. articleIds := make([]int, 0)
  168. for _, v := range list {
  169. if v.IsSpecial == 0 {
  170. articleIds = append(articleIds, v.ArticleId)
  171. }
  172. }
  173. // 报告关联产业信息
  174. industryMap := make(map[int][]*models.IndustrialManagementIdInt, 0)
  175. if len(articleIds) > 0 {
  176. var industryCond string
  177. var industryPars []interface{}
  178. industryCond += ` AND mg.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  179. industryPars = append(industryPars, articleIds)
  180. industryList, e := models.GetIndustrialListByarticleId(industryPars, industryCond)
  181. if e != nil {
  182. err = errors.New("GetIndustrialListByarticleId" + e.Error())
  183. return
  184. }
  185. for i := range industryList {
  186. v := industryList[i]
  187. industryMap[v.ArticleId] = append(industryMap[v.ArticleId], &models.IndustrialManagementIdInt{
  188. ArticleId: v.ArticleId,
  189. IndustrialManagementId: v.IndustrialManagementId,
  190. IndustryName: v.IndustryName,
  191. ChartPermissionId: v.ChartPermissionId,
  192. })
  193. }
  194. }
  195. //// 处理文章收藏字段
  196. //mapCollect, e := GetUserAticleCollectMap(user)
  197. //if e != nil {
  198. // err = errors.New("GetUserAticleCollectMap" + e.Error())
  199. // return
  200. //}
  201. //var articleIds []int
  202. //for _, v := range list {
  203. // articleIds = append(articleIds, v.ArticleId)
  204. //}
  205. articleMapPv := GetArticleHistoryByArticleId(articleIds) //文章Pv
  206. articleCollectMap, _ := GetCygxArticleCollectMap(user.UserId) //用户收藏的文章
  207. articleCollectNumMap, _ := GetCygxArticleCollectNumMapByArtcileIds(articleIds) //文章收藏的数量
  208. articleCollectYanxuanSpecialMap, _ := GetYanxuanSpecialCollectMap(user.UserId) //用户收藏的研选专栏
  209. for k, v := range list {
  210. if len(industryMap[v.ArticleId]) > 0 {
  211. list[k].List = industryMap[v.ArticleId]
  212. } else {
  213. list[k].List = make([]*models.IndustrialManagementIdInt, 0)
  214. }
  215. //if _, ok := mapCollect[v.ArticleId]; ok {
  216. // list[k].IsCollect = true
  217. //}
  218. if v.IsSpecial == 0 {
  219. list[k].Pv = articleMapPv[v.ArticleId]
  220. list[k].IsCollect = articleCollectMap[v.ArticleId]
  221. list[k].CollectNum = articleCollectNumMap[v.ArticleId]
  222. } else {
  223. v.IsCollect = articleCollectYanxuanSpecialMap[v.ArticleId]
  224. }
  225. }
  226. if len(list) == 0 {
  227. list = make([]*models.ArticleListResp, 0)
  228. }
  229. items = list
  230. return
  231. }
  232. // HandleArticleStock 处理报告关联的个股标签
  233. func HandleArticleStock(stock string) (items []*models.ComapnyNameResp) {
  234. sliceSubjects := strings.Split(stock, "/")
  235. if len(sliceSubjects) > 0 {
  236. for _, vSubject := range sliceSubjects {
  237. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  238. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  239. subject := sliceXiahuaxian[0]
  240. items = append(items, &models.ComapnyNameResp{ComapnyName: subject})
  241. }
  242. }
  243. return
  244. }
  245. // 弘则报告发布日期在三个月以内的
  246. func GetArticNewLabelWhithActivity3Month() (labelMap map[int]bool, err error) {
  247. var condition string
  248. var pars []interface{}
  249. condition += ` AND publish_date <= ? AND article_id < ? `
  250. pars = append(pars, time.Now().AddDate(0, -3, 0), utils.SummaryArticleId)
  251. articleList, e := models.GetArticleList(condition, pars)
  252. if e != nil {
  253. err = errors.New("GetArticleList, Err: " + e.Error())
  254. return
  255. }
  256. var articleIds []int
  257. for _, v := range articleList {
  258. articleIds = append(articleIds, v.ArticleId)
  259. }
  260. if len(articleIds) == 0 {
  261. return
  262. }
  263. pars = make([]interface{}, 0)
  264. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  265. pars = append(pars, articleIds)
  266. industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
  267. if e != nil {
  268. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  269. return
  270. }
  271. labelMap = make(map[int]bool, 0)
  272. var industrialIds []int
  273. for _, v := range industrialList {
  274. industrialIds = append(industrialIds, v.IndustrialManagementId)
  275. }
  276. // 获取活动关联的产业
  277. var groupCond string
  278. var groupPars []interface{}
  279. groupCond += ` AND b.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIds)) + `) AND b.source = 1 `
  280. groupPars = append(groupPars, industrialIds)
  281. groups, e := models.GetActivityIndustryRelationList(groupCond, groupPars)
  282. if e != nil {
  283. err = errors.New("获取活动产业关联列表失败, Err: " + e.Error())
  284. return
  285. }
  286. for _, v := range groups {
  287. labelMap[v.ActivityId] = true
  288. }
  289. return
  290. }
  291. // GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
  292. func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
  293. userType, _, e := GetUserType(user.CompanyId)
  294. if e != nil {
  295. err = errors.New("GetSpecialUserType, Err: " + e.Error())
  296. return
  297. }
  298. // 永续客户、大套餐客户可以查看行业升级套餐客户 权限
  299. if userType == 1 || userType == 2 {
  300. havePower = true
  301. return
  302. }
  303. permissionStr, e := GetCompanyPermissionUpgrade(user.CompanyId)
  304. if e != nil {
  305. err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
  306. return
  307. }
  308. reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  309. if e != nil {
  310. err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
  311. return
  312. }
  313. if reportMapDetail == nil {
  314. err = errors.New("GetdetailByCategoryIdP,获取详情失败, Err: ")
  315. return
  316. }
  317. //如果没有对应的升级权限,则返回
  318. if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
  319. return
  320. } else {
  321. havePower = true
  322. }
  323. return
  324. }
  325. //处理核心观点的展示规则
  326. //func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  327. // if item.Annotation != "" {
  328. // annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  329. // }
  330. // return
  331. //}
  332. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  333. content = html.UnescapeString(content)
  334. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  335. if errdoc != nil {
  336. err = errdoc
  337. return
  338. }
  339. docText := doc.Text()
  340. bodyRune := []rune(docText)
  341. bodyRuneLen := len(bodyRune)
  342. body := string(bodyRune[:bodyRuneLen])
  343. contentSub = body
  344. contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
  345. contentSub = strings.Replace(contentSub, " ", "", -1)
  346. contentSub = strings.Replace(contentSub, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sanered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  347. return
  348. }
  349. // 处理核心观点的展示规则
  350. func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  351. if item.ArticleId >= utils.SummaryArticleId {
  352. item.Annotation = YxArticleAnnotation(item)
  353. }
  354. if item.Annotation != "" {
  355. annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  356. } else {
  357. return
  358. }
  359. bodyText, _ := GetReportContentTextSubNew(annotation)
  360. if bodyText == "" {
  361. return
  362. }
  363. if annotation != "" {
  364. annotation = html.UnescapeString(annotation)
  365. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  366. docText := doc.Text()
  367. mapDoc := make(map[int]string)
  368. mapSortRepeat := make(map[string]string)
  369. var mapSort []int
  370. p := doc.Find("p")
  371. p.Each(func(tk int, pd *goquery.Selection) {
  372. pdText := pd.Text()
  373. pdText = strings.Replace(pdText, " ", "", -1)
  374. if pdText != "" {
  375. textLen := strings.Index(docText, pdText)
  376. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  377. mapDoc[(strings.Index(docText, pdText))] = pdText
  378. mapSort = append(mapSort, textLen)
  379. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  380. }
  381. }
  382. })
  383. li := doc.Find("li")
  384. li.Each(func(tk int, li *goquery.Selection) {
  385. liText := li.Text()
  386. liText = strings.Replace(liText, " ", "", -1)
  387. if liText != "" {
  388. textLen := strings.Index(docText, liText)
  389. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  390. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  391. mapSort = append(mapSort, textLen)
  392. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  393. }
  394. }
  395. })
  396. ul := doc.Find("ul")
  397. ul.Each(func(tk int, ul *goquery.Selection) {
  398. ulText := ul.Text()
  399. ulText = strings.Replace(ulText, " ", "", -1)
  400. if ulText != "" {
  401. textLen := strings.Index(docText, ulText)
  402. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  403. mapDoc[(strings.Index(docText, ulText))] = ulText
  404. mapSort = append(mapSort, textLen)
  405. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  406. }
  407. }
  408. })
  409. if len(mapSort) == 0 {
  410. return
  411. } else {
  412. //排序
  413. sort.Ints(mapSort)
  414. var annotationHtml string
  415. for _, vSort := range mapSort {
  416. for k, v := range mapDoc {
  417. if k == vSort && v != "" {
  418. annotationHtml += v + "<br>"
  419. }
  420. }
  421. }
  422. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  423. annotationHtml = "<p>" + annotationHtml + "</p>"
  424. annotation = annotationHtml
  425. }
  426. }
  427. return
  428. }
  429. // 处理核心观点的展示规则
  430. func AnnotationHtml(bodyText string) (annotation string) {
  431. if bodyText == "" {
  432. return
  433. }
  434. annotation = bodyText
  435. annotation = html.UnescapeString(annotation)
  436. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  437. docText := doc.Text()
  438. mapDoc := make(map[int]string)
  439. var mapSort []int
  440. p := doc.Find("p")
  441. p.Each(func(tk int, pd *goquery.Selection) {
  442. pdText := pd.Text()
  443. pdText = strings.Replace(pdText, " ", "", -1)
  444. if pdText != "" {
  445. textLen := strings.Index(docText, pdText)
  446. if textLen >= 0 {
  447. mapDoc[(strings.Index(docText, pdText))] = pdText
  448. mapSort = append(mapSort, textLen)
  449. }
  450. }
  451. })
  452. li := doc.Find("li")
  453. li.Each(func(tk int, li *goquery.Selection) {
  454. liText := li.Text()
  455. liText = strings.Replace(liText, " ", "", -1)
  456. if liText != "" {
  457. textLen := strings.Index(docText, liText)
  458. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  459. mapSort = append(mapSort, textLen)
  460. }
  461. })
  462. ul := doc.Find("ul")
  463. ul.Each(func(tk int, ul *goquery.Selection) {
  464. ulText := ul.Text()
  465. ulText = strings.Replace(ulText, " ", "", -1)
  466. if ulText != "" {
  467. textLen := strings.Index(docText, ulText)
  468. mapDoc[(strings.Index(docText, ulText))] = ulText
  469. mapSort = append(mapSort, textLen)
  470. }
  471. })
  472. if len(mapSort) == 0 {
  473. return
  474. } else {
  475. //排序
  476. sort.Ints(mapSort)
  477. var annotationHtml string
  478. for _, vSort := range mapSort {
  479. for k, v := range mapDoc {
  480. if k == vSort && v != "" {
  481. annotationHtml += v + "<br>"
  482. }
  483. }
  484. }
  485. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  486. annotationHtml = "<p>" + annotationHtml + "</p>"
  487. annotation = annotationHtml
  488. }
  489. return
  490. }
  491. // 处理产品内测展示规则
  492. func ProductInteriorHtml(bodyText string) (annotation string) {
  493. if bodyText == "" {
  494. return
  495. }
  496. sliceBody := strings.Split(bodyText, "</p>")
  497. annotation, _ = GetReportContentTextSub(sliceBody[0])
  498. return
  499. }
  500. // 解析研选内容中的核心观点
  501. func YxArticleAnnotation(article *models.ArticleListResp) (annotation string) {
  502. //如果不规范,就获取内容主体
  503. if strings.Count(article.Body, "<hr") == 0 {
  504. //如果内容不规范而且,还有图片,就把核心观点置空
  505. if article.BodyImg != "" {
  506. return
  507. }
  508. annotation, _ = GetReportContentTextSub(article.Body)
  509. return
  510. }
  511. body := strings.ReplaceAll(article.Body, "<strong>", "")
  512. body = strings.ReplaceAll(body, "</strong>", "")
  513. body = strings.ReplaceAll(body, "</ol>", "</div>")
  514. body = strings.ReplaceAll(body, "<ol>", "<div>")
  515. body = strings.ReplaceAll(body, "</li>", "</p>")
  516. body = strings.ReplaceAll(body, "<li>", "<p>")
  517. re, _ := regexp.Compile("<strong.*?>")
  518. body = re.ReplaceAllString(body, "")
  519. reLi, _ := regexp.Compile("<li.*?>")
  520. body = reLi.ReplaceAllString(body, "")
  521. var plus int
  522. coreIndex := strings.Index(body, "核心观点:")
  523. plus = 15
  524. if coreIndex == -1 {
  525. coreIndex = strings.Index(body, "核心观点:")
  526. plus = 13
  527. }
  528. if coreIndex == -1 {
  529. coreIndex = strings.Index(body, "核心观点")
  530. plus = 12
  531. }
  532. if coreIndex == -1 {
  533. coreIndex = strings.Index(body, "核心结论:")
  534. plus = 15
  535. }
  536. if coreIndex == -1 {
  537. coreIndex = strings.Index(body, "核心结论:")
  538. plus = 13
  539. }
  540. if coreIndex == -1 {
  541. coreIndex = strings.Index(body, "核心结论")
  542. plus = 12
  543. }
  544. endIndex := strings.Index(body, "<hr")
  545. if coreIndex != -1 && endIndex != -1 {
  546. body = body[coreIndex+plus : endIndex]
  547. }
  548. annotation = body
  549. return
  550. }
  551. // 获取研选类型的文章分类Id
  552. func GetYanXuanArticleTypeIds() (articleTypeIds string, err error) {
  553. var condition string
  554. condition = " AND is_show_yanx = 1 "
  555. listType, e := models.GetCygxArticleTypeListCondition(condition)
  556. if e != nil {
  557. err = errors.New("GetCygxArticleTypeListCondition, Err: " + e.Error())
  558. return
  559. }
  560. for _, v := range listType {
  561. articleTypeIds += strconv.Itoa(v.ArticleTypeId) + ","
  562. }
  563. articleTypeIds = strings.TrimRight(articleTypeIds, ",")
  564. if articleTypeIds == "" {
  565. err = errors.New("研选分类ID不能为空")
  566. return
  567. }
  568. return
  569. }
  570. // GetUserAticleCollectMap 获取用户收藏的文章ID
  571. func GetUserAticleCollectMap(user *models.WxUserItem) (respMap map[int]int, err error) {
  572. list, e := models.GetCygxArticleCollectListByUser(user.UserId)
  573. if e != nil {
  574. err = errors.New("GetCygxArticleCollectListByUser, Err: " + e.Error())
  575. return
  576. }
  577. articleMap := make(map[int]int)
  578. for _, v := range list {
  579. articleMap[v.ArticleId] = v.ArticleId
  580. }
  581. respMap = articleMap
  582. return
  583. }
  584. // 通过接解析带有Md5的文章链接获取文章ID
  585. func GetReportLinkToArticleid(reportLink string) (articleId int, err error) {
  586. defer func() {
  587. if err != nil {
  588. go utils.SendAlarmMsg("通过接解析带有Md5的文章链接获取文章ID失败"+err.Error(), 2)
  589. }
  590. }()
  591. var artMd5 string
  592. //处理Md5的
  593. strnum1 := strings.Index(reportLink, "id=")
  594. if strnum1 > 0 {
  595. sliceId := strings.Split(reportLink, "id=")
  596. if len(sliceId) > 1 {
  597. reportLink = sliceId[1]
  598. sliceMd5Id := strings.Split(reportLink, "&")
  599. artMd5 = sliceMd5Id[0]
  600. }
  601. if artMd5 != "" {
  602. detail, errArt := models.GetArticleDetailByIdMd5(artMd5)
  603. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  604. err = errArt
  605. return
  606. }
  607. if detail != nil {
  608. articleId = detail.ArticleId
  609. }
  610. }
  611. } else {
  612. //处理活动的
  613. linkList := strings.Split(reportLink, "/")
  614. if linkList[len(linkList)-1] != "" {
  615. linkArticleId, _ := strconv.Atoi(linkList[len(linkList)-1])
  616. if linkArticleId > 0 {
  617. articleInfo, errArt := models.GetArticleDetailById(linkArticleId)
  618. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  619. err = errArt
  620. return
  621. }
  622. if articleInfo != nil {
  623. articleId = articleInfo.ArticleId
  624. }
  625. }
  626. }
  627. }
  628. return
  629. }
  630. // GetArticleStockMap 获取个股标签所对应的文章ID
  631. func GetArticleStockMap() (mapResp map[string]int, err error) {
  632. defer func() {
  633. if err != nil {
  634. go utils.SendAlarmMsg("获取个股标签所对应的文章ID失败"+err.Error(), 2)
  635. }
  636. }()
  637. list, err := models.GetArticleStock()
  638. if err != nil && err.Error() != utils.ErrNoRow() {
  639. return
  640. }
  641. mapResp = make(map[string]int, 0)
  642. if len(list) > 0 {
  643. //一对一精准匹配
  644. for _, v := range list {
  645. sliceSubjects := strings.Split(v.Stock, "/")
  646. if len(sliceSubjects) > 0 {
  647. for _, vSubject := range sliceSubjects {
  648. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  649. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  650. subject := sliceXiahuaxian[0]
  651. mapResp[subject] = v.ArticleId
  652. }
  653. }
  654. }
  655. }
  656. return
  657. }
  658. // 用户报告操作行为,模板消息推送
  659. func ArticleUserRemind(user *models.WxUserItem, articleDetail *models.ArticleDetail, source int) (err error) {
  660. defer func() {
  661. if err != nil {
  662. go utils.SendAlarmMsg("同步策略平台阅读数据失败", 2)
  663. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetCeLueArticlePv ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  664. }
  665. }()
  666. countUser, err := models.GetUserRemind(user.UserId)
  667. if err != nil {
  668. return err
  669. }
  670. if countUser == 0 {
  671. return err
  672. }
  673. var sourceMsg string
  674. if source == 1 {
  675. sourceMsg = "阅读报告"
  676. } else {
  677. sourceMsg = "收藏报告"
  678. }
  679. //获取销售手机号
  680. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  681. if err != nil && err.Error() != utils.ErrNoRow() {
  682. return err
  683. }
  684. if sellerItemQy != nil {
  685. openIdList, err := models.GetWxOpenIdByMobileList(sellerItemQy.Mobile)
  686. if err != nil {
  687. return err
  688. }
  689. var keyword1 string
  690. var keyword2 string
  691. keyword1 = articleDetail.Title
  692. keyword2 = fmt.Sprint("互动:", sourceMsg, ",", user.RealName, "--", user.CompanyName)
  693. SendWxMsgWithArticleUserRemind(keyword1, keyword2, openIdList, articleDetail.ArticleId)
  694. }
  695. return
  696. }
  697. // GetAiQianYanArtilceList 获取AI前沿几篇文章
  698. func GetAiQianYanArtilceList(startSize, pageSize int) (items []*models.HomeArticle, total int, err error) {
  699. defer func() {
  700. if err != nil {
  701. go utils.SendAlarmMsg("获取AI前沿几篇文章失败"+err.Error(), 2)
  702. }
  703. }()
  704. var condition string
  705. var pars []interface{}
  706. condition += ` AND title LIKE '%AI前沿%' AND publish_status = 1 ORDER BY publish_date DESC `
  707. articleList, e := models.GetCygxCygxArticleList(condition, pars, startSize, pageSize)
  708. if e != nil {
  709. err = errors.New("GetCygxCygxArticleList, Err: " + e.Error())
  710. return
  711. }
  712. total, e = models.GetCygxArticleCount(condition, pars)
  713. if e != nil {
  714. err = errors.New("GetCygxArticleCount, Err: " + e.Error())
  715. return
  716. }
  717. for _, v := range articleList {
  718. item := new(models.HomeArticle)
  719. item.ArticleId = v.ArticleId
  720. item.Title = v.Title
  721. item.Abstract = v.Abstract
  722. item.Annotation = v.Annotation
  723. item.PublishDate = v.PublishDate
  724. item.CategoryId = strconv.Itoa(v.CategoryId)
  725. item.Body = v.Body
  726. items = append(items, item)
  727. }
  728. return
  729. }
  730. // GetYxArticleIdMap 获取研选文章ID
  731. func GetYxArticleIdMap(articleIds []int) (mapResp map[int]bool) {
  732. var err error
  733. defer func() {
  734. if err != nil {
  735. go utils.SendAlarmMsg("获取研选文章ID失败,GetYxArticleIdMap"+err.Error(), 2)
  736. }
  737. }()
  738. var condition string
  739. var pars []interface{}
  740. condition = ` AND article_type_id > 0 `
  741. if len(articleIds) > 0 {
  742. condition += ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  743. pars = append(pars, articleIds)
  744. }
  745. articleList, e := models.GetArticleList(condition, pars)
  746. if e != nil {
  747. err = errors.New("GetArticleList, Err: " + e.Error())
  748. return
  749. }
  750. mapResp = make(map[int]bool, 0)
  751. for _, v := range articleList {
  752. mapResp[v.ArticleId] = true
  753. }
  754. return
  755. }
  756. // GetYanxuanArticleIds 获取研选文章ID
  757. func GetYanxuanArticleIds() (articleIds []int) {
  758. var err error
  759. defer func() {
  760. if err != nil {
  761. fmt.Println(err)
  762. go utils.SendAlarmMsg("获取研选文章ID GetYanxuanArticleIds,失败:"+err.Error(), 2)
  763. }
  764. }()
  765. var condition string
  766. var pars []interface{}
  767. condition = ` AND article_type_id > 0 `
  768. list, e := models.GetCygxCygxArticleIdList(condition, pars)
  769. if e != nil {
  770. err = errors.New("GetCygxCygxArticleIdList, Err: " + e.Error())
  771. return
  772. }
  773. for _, v := range list {
  774. articleIds = append(articleIds, v.ArticleId)
  775. }
  776. return
  777. }
  778. // 获取报告分享图片
  779. func GetArticleShareImg(articleId int) (shareImg string) {
  780. var err error
  781. defer func() {
  782. if err != nil {
  783. fmt.Println(err)
  784. go utils.SendAlarmMsg(fmt.Sprint("获取活动分享封面图片失败 GetArticleShareImg Err:", err.Error(), "活动ID:", articleId), 2)
  785. }
  786. }()
  787. imgDetail, e := models.CygxCygxArticleDataDetail(articleId)
  788. if e != nil && e.Error() != utils.ErrNoRow() {
  789. err = errors.New("CygxCygxArticleDataDetail, Err: " + e.Error())
  790. return
  791. }
  792. if imgDetail != nil {
  793. shareImg = imgDetail.Cover
  794. } else {
  795. shareImg = utils.YANXUAN_ARTICLE_SHARE_IMG
  796. }
  797. return
  798. }