article.go 25 KB

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