article.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/PuerkitoBio/goquery"
  6. "hongze/hongze_clpt/models"
  7. "hongze/hongze_clpt/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. articleIds := make([]int, 0)
  156. for _, v := range list {
  157. if v.IsSpecial == 0 {
  158. articleIds = append(articleIds, v.ArticleId)
  159. }
  160. }
  161. // 报告关联产业信息
  162. industryMap := make(map[int][]*models.IndustrialManagementIdInt, 0)
  163. if len(articleIds) > 0 {
  164. var industryCond string
  165. var industryPars []interface{}
  166. industryCond += ` AND mg.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  167. industryPars = append(industryPars, articleIds)
  168. industryList, e := models.GetIndustrialListByarticleId(industryPars, industryCond)
  169. if e != nil {
  170. err = errors.New("GetIndustrialListByarticleId" + e.Error())
  171. return
  172. }
  173. for i := range industryList {
  174. v := industryList[i]
  175. industryMap[v.ArticleId] = append(industryMap[v.ArticleId], &models.IndustrialManagementIdInt{
  176. ArticleId: v.ArticleId,
  177. IndustrialManagementId: v.IndustrialManagementId,
  178. IndustryName: v.IndustryName,
  179. ChartPermissionId: v.ChartPermissionId,
  180. })
  181. }
  182. }
  183. //// 处理文章收藏字段
  184. //mapCollect, e := GetUserAticleCollectMap(user)
  185. //if e != nil {
  186. // err = errors.New("GetUserAticleCollectMap" + e.Error())
  187. // return
  188. //}
  189. //var articleIds []int
  190. //for _, v := range list {
  191. // articleIds = append(articleIds, v.ArticleId)
  192. //}
  193. articleMapPv := GetArticleHistoryByArticleId(articleIds) //文章Pv
  194. articleCollectMap, _ := GetCygxArticleCollectMap(user.UserId) //用户收藏的文章
  195. articleCollectNumMap, _ := GetCygxArticleCollectNumMapByArtcileIds(articleIds) //文章收藏的数量
  196. articleCollectYanxuanSpecialMap, _ := GetYanxuanSpecialCollectMap(user.UserId) //用户收藏的研选专栏
  197. for k, v := range list {
  198. if len(industryMap[v.ArticleId]) > 0 {
  199. list[k].List = industryMap[v.ArticleId]
  200. } else {
  201. list[k].List = make([]*models.IndustrialManagementIdInt, 0)
  202. }
  203. //if _, ok := mapCollect[v.ArticleId]; ok {
  204. // list[k].IsCollect = true
  205. //}
  206. if v.IsSpecial == 0 {
  207. list[k].Pv = articleMapPv[v.ArticleId]
  208. list[k].IsCollect = articleCollectMap[v.ArticleId]
  209. list[k].CollectNum = articleCollectNumMap[v.ArticleId]
  210. } else {
  211. v.IsCollect = articleCollectYanxuanSpecialMap[v.ArticleId]
  212. }
  213. }
  214. if len(list) == 0 {
  215. list = make([]*models.ArticleListResp, 0)
  216. }
  217. items = list
  218. return
  219. }
  220. // HandleArticleStock 处理报告关联的个股标签
  221. func HandleArticleStock(stock string) (items []*models.ComapnyNameResp) {
  222. sliceSubjects := strings.Split(stock, "/")
  223. if len(sliceSubjects) > 0 {
  224. for _, vSubject := range sliceSubjects {
  225. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  226. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  227. subject := sliceXiahuaxian[0]
  228. items = append(items, &models.ComapnyNameResp{ComapnyName: subject})
  229. }
  230. }
  231. return
  232. }
  233. // 弘则报告发布日期在三个月以内的
  234. func GetArticNewLabelWhithActivity3Month() (labelMap map[int]bool, err error) {
  235. var condition string
  236. var pars []interface{}
  237. condition += ` AND publish_date <= ? AND article_id < ? `
  238. pars = append(pars, time.Now().AddDate(0, -3, 0), utils.SummaryArticleId)
  239. articleList, e := models.GetArticleList(condition, pars)
  240. if e != nil {
  241. err = errors.New("GetArticleList, Err: " + e.Error())
  242. return
  243. }
  244. var articleIds []int
  245. for _, v := range articleList {
  246. articleIds = append(articleIds, v.ArticleId)
  247. }
  248. if len(articleIds) == 0 {
  249. return
  250. }
  251. pars = make([]interface{}, 0)
  252. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  253. pars = append(pars, articleIds)
  254. industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
  255. if e != nil {
  256. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  257. return
  258. }
  259. labelMap = make(map[int]bool, 0)
  260. var industrialIds []int
  261. for _, v := range industrialList {
  262. industrialIds = append(industrialIds, v.IndustrialManagementId)
  263. }
  264. // 获取活动关联的产业
  265. var groupCond string
  266. var groupPars []interface{}
  267. groupCond += ` AND b.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIds)) + `) AND b.source = 1 `
  268. groupPars = append(groupPars, industrialIds)
  269. groups, e := models.GetActivityIndustryRelationList(groupCond, groupPars)
  270. if e != nil {
  271. err = errors.New("获取活动产业关联列表失败, Err: " + e.Error())
  272. return
  273. }
  274. for _, v := range groups {
  275. labelMap[v.ActivityId] = true
  276. }
  277. return
  278. }
  279. // GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
  280. func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
  281. userType, _, e := GetUserType(user.CompanyId)
  282. if e != nil {
  283. err = errors.New("GetSpecialUserType, Err: " + e.Error())
  284. return
  285. }
  286. // 永续客户、大套餐客户可以查看行业升级套餐客户 权限
  287. if userType == 1 || userType == 2 {
  288. havePower = true
  289. return
  290. }
  291. permissionStr, e := GetCompanyPermissionUpgrade(user.CompanyId)
  292. if e != nil {
  293. err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
  294. return
  295. }
  296. reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  297. if e != nil {
  298. err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
  299. return
  300. }
  301. if reportMapDetail == nil {
  302. err = errors.New("GetdetailByCategoryIdP,获取详情失败, Err: ")
  303. return
  304. }
  305. //如果没有对应的升级权限,则返回
  306. if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
  307. return
  308. } else {
  309. havePower = true
  310. }
  311. return
  312. }
  313. //处理核心观点的展示规则
  314. //func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  315. // if item.Annotation != "" {
  316. // annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  317. // }
  318. // return
  319. //}
  320. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  321. content = html.UnescapeString(content)
  322. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  323. if errdoc != nil {
  324. err = errdoc
  325. return
  326. }
  327. docText := doc.Text()
  328. bodyRune := []rune(docText)
  329. bodyRuneLen := len(bodyRune)
  330. body := string(bodyRune[:bodyRuneLen])
  331. contentSub = body
  332. contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
  333. contentSub = strings.Replace(contentSub, " ", "", -1)
  334. 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)
  335. return
  336. }
  337. // 处理核心观点的展示规则
  338. func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  339. if item.ArticleId >= utils.SummaryArticleId {
  340. item.Annotation = YxArticleAnnotation(item)
  341. }
  342. if item.Annotation != "" {
  343. annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  344. } else {
  345. return
  346. }
  347. bodyText, _ := GetReportContentTextSubNew(annotation)
  348. if bodyText == "" {
  349. return
  350. }
  351. if annotation != "" {
  352. annotation = html.UnescapeString(annotation)
  353. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  354. docText := doc.Text()
  355. mapDoc := make(map[int]string)
  356. mapSortRepeat := make(map[string]string)
  357. var mapSort []int
  358. p := doc.Find("p")
  359. p.Each(func(tk int, pd *goquery.Selection) {
  360. pdText := pd.Text()
  361. pdText = strings.Replace(pdText, " ", "", -1)
  362. if pdText != "" {
  363. textLen := strings.Index(docText, pdText)
  364. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  365. mapDoc[(strings.Index(docText, pdText))] = pdText
  366. mapSort = append(mapSort, textLen)
  367. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  368. }
  369. }
  370. })
  371. li := doc.Find("li")
  372. li.Each(func(tk int, li *goquery.Selection) {
  373. liText := li.Text()
  374. liText = strings.Replace(liText, " ", "", -1)
  375. if liText != "" {
  376. textLen := strings.Index(docText, liText)
  377. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  378. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  379. mapSort = append(mapSort, textLen)
  380. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  381. }
  382. }
  383. })
  384. ul := doc.Find("ul")
  385. ul.Each(func(tk int, ul *goquery.Selection) {
  386. ulText := ul.Text()
  387. ulText = strings.Replace(ulText, " ", "", -1)
  388. if ulText != "" {
  389. textLen := strings.Index(docText, ulText)
  390. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  391. mapDoc[(strings.Index(docText, ulText))] = ulText
  392. mapSort = append(mapSort, textLen)
  393. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  394. }
  395. }
  396. })
  397. if len(mapSort) == 0 {
  398. return
  399. } else {
  400. //排序
  401. sort.Ints(mapSort)
  402. var annotationHtml string
  403. for _, vSort := range mapSort {
  404. for k, v := range mapDoc {
  405. if k == vSort && v != "" {
  406. annotationHtml += v + "<br>"
  407. }
  408. }
  409. }
  410. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  411. annotationHtml = "<p>" + annotationHtml + "</p>"
  412. annotation = annotationHtml
  413. }
  414. }
  415. return
  416. }
  417. // 处理核心观点的展示规则
  418. func AnnotationHtml(bodyText string) (annotation string) {
  419. if bodyText == "" {
  420. return
  421. }
  422. annotation = bodyText
  423. annotation = html.UnescapeString(annotation)
  424. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  425. docText := doc.Text()
  426. mapDoc := make(map[int]string)
  427. var mapSort []int
  428. p := doc.Find("p")
  429. p.Each(func(tk int, pd *goquery.Selection) {
  430. pdText := pd.Text()
  431. pdText = strings.Replace(pdText, " ", "", -1)
  432. if pdText != "" {
  433. textLen := strings.Index(docText, pdText)
  434. if textLen >= 0 {
  435. mapDoc[(strings.Index(docText, pdText))] = pdText
  436. mapSort = append(mapSort, textLen)
  437. }
  438. }
  439. })
  440. li := doc.Find("li")
  441. li.Each(func(tk int, li *goquery.Selection) {
  442. liText := li.Text()
  443. liText = strings.Replace(liText, " ", "", -1)
  444. if liText != "" {
  445. textLen := strings.Index(docText, liText)
  446. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  447. mapSort = append(mapSort, textLen)
  448. }
  449. })
  450. ul := doc.Find("ul")
  451. ul.Each(func(tk int, ul *goquery.Selection) {
  452. ulText := ul.Text()
  453. ulText = strings.Replace(ulText, " ", "", -1)
  454. if ulText != "" {
  455. textLen := strings.Index(docText, ulText)
  456. mapDoc[(strings.Index(docText, ulText))] = ulText
  457. mapSort = append(mapSort, textLen)
  458. }
  459. })
  460. if len(mapSort) == 0 {
  461. return
  462. } else {
  463. //排序
  464. sort.Ints(mapSort)
  465. var annotationHtml string
  466. for _, vSort := range mapSort {
  467. for k, v := range mapDoc {
  468. if k == vSort && v != "" {
  469. annotationHtml += v + "<br>"
  470. }
  471. }
  472. }
  473. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  474. annotationHtml = "<p>" + annotationHtml + "</p>"
  475. annotation = annotationHtml
  476. }
  477. return
  478. }
  479. // 处理产品内测展示规则
  480. func ProductInteriorHtml(bodyText string) (annotation string) {
  481. if bodyText == "" {
  482. return
  483. }
  484. sliceBody := strings.Split(bodyText, "</p>")
  485. annotation, _ = GetReportContentTextSub(sliceBody[0])
  486. return
  487. }
  488. // 解析研选内容中的核心观点
  489. func YxArticleAnnotation(article *models.ArticleListResp) (annotation string) {
  490. //如果不规范,就获取内容主体
  491. if strings.Count(article.Body, "<hr") == 0 {
  492. //如果内容不规范而且,还有图片,就把核心观点置空
  493. if article.BodyImg != "" {
  494. return
  495. }
  496. annotation, _ = GetReportContentTextSub(article.Body)
  497. return
  498. }
  499. body := strings.ReplaceAll(article.Body, "<strong>", "")
  500. body = strings.ReplaceAll(body, "</strong>", "")
  501. body = strings.ReplaceAll(body, "</ol>", "</div>")
  502. body = strings.ReplaceAll(body, "<ol>", "<div>")
  503. body = strings.ReplaceAll(body, "</li>", "</p>")
  504. body = strings.ReplaceAll(body, "<li>", "<p>")
  505. re, _ := regexp.Compile("<strong.*?>")
  506. body = re.ReplaceAllString(body, "")
  507. reLi, _ := regexp.Compile("<li.*?>")
  508. body = reLi.ReplaceAllString(body, "")
  509. var plus int
  510. coreIndex := strings.Index(body, "核心观点:")
  511. plus = 15
  512. if coreIndex == -1 {
  513. coreIndex = strings.Index(body, "核心观点:")
  514. plus = 13
  515. }
  516. if coreIndex == -1 {
  517. coreIndex = strings.Index(body, "核心观点")
  518. plus = 12
  519. }
  520. if coreIndex == -1 {
  521. coreIndex = strings.Index(body, "核心结论:")
  522. plus = 15
  523. }
  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. endIndex := strings.Index(body, "<hr")
  533. if coreIndex != -1 && endIndex != -1 {
  534. body = body[coreIndex+plus : endIndex]
  535. }
  536. annotation = body
  537. return
  538. }
  539. // 获取研选类型的文章分类Id
  540. func GetYanXuanArticleTypeIds() (articleTypeIds string, err error) {
  541. var condition string
  542. condition = " AND is_show_yanx = 1 "
  543. listType, e := models.GetCygxArticleTypeListCondition(condition)
  544. if e != nil {
  545. err = errors.New("GetCygxArticleTypeListCondition, Err: " + e.Error())
  546. return
  547. }
  548. for _, v := range listType {
  549. articleTypeIds += strconv.Itoa(v.ArticleTypeId) + ","
  550. }
  551. articleTypeIds = strings.TrimRight(articleTypeIds, ",")
  552. if articleTypeIds == "" {
  553. err = errors.New("研选分类ID不能为空")
  554. return
  555. }
  556. return
  557. }
  558. // GetUserAticleCollectMap 获取用户收藏的文章ID
  559. func GetUserAticleCollectMap(user *models.WxUserItem) (respMap map[int]int, err error) {
  560. list, e := models.GetCygxArticleCollectListByUser(user.UserId)
  561. if e != nil {
  562. err = errors.New("GetCygxArticleCollectListByUser, Err: " + e.Error())
  563. return
  564. }
  565. articleMap := make(map[int]int)
  566. for _, v := range list {
  567. articleMap[v.ArticleId] = v.ArticleId
  568. }
  569. respMap = articleMap
  570. return
  571. }
  572. // 通过接解析带有Md5的文章链接获取文章ID
  573. func GetReportLinkToArticleid(reportLink string) (articleId int, err error) {
  574. defer func() {
  575. if err != nil {
  576. go utils.SendAlarmMsg("通过接解析带有Md5的文章链接获取文章ID失败"+err.Error(), 2)
  577. }
  578. }()
  579. var artMd5 string
  580. //处理Md5的
  581. strnum1 := strings.Index(reportLink, "id=")
  582. if strnum1 > 0 {
  583. sliceId := strings.Split(reportLink, "id=")
  584. if len(sliceId) > 1 {
  585. reportLink = sliceId[1]
  586. sliceMd5Id := strings.Split(reportLink, "&")
  587. artMd5 = sliceMd5Id[0]
  588. }
  589. if artMd5 != "" {
  590. detail, errArt := models.GetArticleDetailByIdMd5(artMd5)
  591. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  592. err = errArt
  593. return
  594. }
  595. if detail != nil {
  596. articleId = detail.ArticleId
  597. }
  598. }
  599. } else {
  600. //处理活动的
  601. linkList := strings.Split(reportLink, "/")
  602. if linkList[len(linkList)-1] != "" {
  603. linkArticleId, _ := strconv.Atoi(linkList[len(linkList)-1])
  604. if linkArticleId > 0 {
  605. articleInfo, errArt := models.GetArticleDetailById(linkArticleId)
  606. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  607. err = errArt
  608. return
  609. }
  610. if articleInfo != nil {
  611. articleId = articleInfo.ArticleId
  612. }
  613. }
  614. }
  615. }
  616. return
  617. }
  618. // GetArticleStockMap 获取个股标签所对应的文章ID
  619. func GetArticleStockMap() (mapResp map[string]int, err error) {
  620. defer func() {
  621. if err != nil {
  622. go utils.SendAlarmMsg("获取个股标签所对应的文章ID失败"+err.Error(), 2)
  623. }
  624. }()
  625. list, err := models.GetArticleStock()
  626. if err != nil && err.Error() != utils.ErrNoRow() {
  627. return
  628. }
  629. mapResp = make(map[string]int, 0)
  630. if len(list) > 0 {
  631. //一对一精准匹配
  632. for _, v := range list {
  633. sliceSubjects := strings.Split(v.Stock, "/")
  634. if len(sliceSubjects) > 0 {
  635. for _, vSubject := range sliceSubjects {
  636. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  637. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  638. subject := sliceXiahuaxian[0]
  639. mapResp[subject] = v.ArticleId
  640. }
  641. }
  642. }
  643. }
  644. return
  645. }
  646. // 用户报告操作行为,模板消息推送
  647. func ArticleUserRemind(user *models.WxUserItem, articleDetail *models.ArticleDetail, source int) (err error) {
  648. defer func() {
  649. if err != nil {
  650. go utils.SendAlarmMsg("同步策略平台阅读数据失败", 2)
  651. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetCeLueArticlePv ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  652. }
  653. }()
  654. countUser, err := models.GetUserRemind(user.UserId)
  655. if err != nil {
  656. return err
  657. }
  658. if countUser == 0 {
  659. return err
  660. }
  661. var sourceMsg string
  662. if source == 1 {
  663. sourceMsg = "阅读报告"
  664. } else {
  665. sourceMsg = "收藏报告"
  666. }
  667. //获取销售手机号
  668. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  669. if err != nil && err.Error() != utils.ErrNoRow() {
  670. return err
  671. }
  672. if sellerItemQy != nil {
  673. openIdList, err := models.GetWxOpenIdByMobileList(sellerItemQy.Mobile)
  674. if err != nil {
  675. return err
  676. }
  677. var keyword1 string
  678. var keyword2 string
  679. keyword1 = articleDetail.Title
  680. keyword2 = fmt.Sprint("互动:", sourceMsg, ",", user.RealName, "--", user.CompanyName)
  681. SendWxMsgWithArticleUserRemind(keyword1, keyword2, openIdList, articleDetail.ArticleId)
  682. }
  683. return
  684. }
  685. // GetAiQianYanArtilceList 获取AI前沿几篇文章
  686. func GetAiQianYanArtilceList(startSize, pageSize int) (items []*models.HomeArticle, total int, err error) {
  687. defer func() {
  688. if err != nil {
  689. go utils.SendAlarmMsg("获取AI前沿几篇文章失败"+err.Error(), 2)
  690. }
  691. }()
  692. var condition string
  693. var pars []interface{}
  694. condition += ` AND title LIKE '%AI前沿%' AND publish_status = 1 ORDER BY publish_date DESC `
  695. articleList, e := models.GetCygxCygxArticleList(condition, pars, startSize, pageSize)
  696. if e != nil {
  697. err = errors.New("GetCygxCygxArticleList, Err: " + e.Error())
  698. return
  699. }
  700. total, e = models.GetCygxArticleCount(condition, pars)
  701. if e != nil {
  702. err = errors.New("GetCygxArticleCount, Err: " + e.Error())
  703. return
  704. }
  705. for _, v := range articleList {
  706. item := new(models.HomeArticle)
  707. item.ArticleId = v.ArticleId
  708. item.Title = v.Title
  709. item.Abstract = v.Abstract
  710. item.Annotation = v.Annotation
  711. item.PublishDate = v.PublishDate
  712. item.CategoryId = strconv.Itoa(v.CategoryId)
  713. item.Body = v.Body
  714. items = append(items, item)
  715. }
  716. return
  717. }
  718. // GetYxArticleIdMap 获取研选文章ID
  719. func GetYxArticleIdMap(articleIds []int) (mapResp map[int]bool) {
  720. var err error
  721. defer func() {
  722. if err != nil {
  723. go utils.SendAlarmMsg("获取研选文章ID失败,GetYxArticleIdMap"+err.Error(), 2)
  724. }
  725. }()
  726. var condition string
  727. var pars []interface{}
  728. condition = ` AND article_type_id > 0 `
  729. if len(articleIds) > 0 {
  730. condition += ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  731. pars = append(pars, articleIds)
  732. }
  733. articleList, e := models.GetArticleList(condition, pars)
  734. if e != nil {
  735. err = errors.New("GetArticleList, Err: " + e.Error())
  736. return
  737. }
  738. mapResp = make(map[int]bool, 0)
  739. for _, v := range articleList {
  740. mapResp[v.ArticleId] = true
  741. }
  742. return
  743. }