article.go 22 KB

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