article.go 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/PuerkitoBio/goquery"
  8. "hongze/hongze_cygx/models"
  9. "hongze/hongze_cygx/models/company"
  10. "hongze/hongze_cygx/utils"
  11. "html"
  12. "net/url"
  13. "regexp"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. func GetReportContentTextSub(content string) (contentSub string, err error) {
  20. content = html.UnescapeString(content)
  21. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  22. docText := doc.Text()
  23. bodyRune := []rune(docText)
  24. bodyRuneLen := len(bodyRune)
  25. if bodyRuneLen > 200 {
  26. bodyRuneLen = 200
  27. }
  28. body := string(bodyRune[:bodyRuneLen])
  29. contentSub = body
  30. contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
  31. return
  32. }
  33. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  34. content = html.UnescapeString(content)
  35. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  36. if errdoc != nil {
  37. err = errdoc
  38. return
  39. }
  40. docText := doc.Text()
  41. bodyRune := []rune(docText)
  42. bodyRuneLen := len(bodyRune)
  43. body := string(bodyRune[:bodyRuneLen])
  44. contentSub = body
  45. contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
  46. contentSub = strings.Replace(contentSub, " ", "", -1)
  47. 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)
  48. return
  49. }
  50. func FixArticleImgUrl(body string) (contentSub string, err error) {
  51. r := strings.NewReader(string(body))
  52. doc, err := goquery.NewDocumentFromReader(r)
  53. if err != nil {
  54. fmt.Println(err)
  55. }
  56. doc.Find("img").Each(func(i int, s *goquery.Selection) {
  57. src, _ := s.Attr("src")
  58. if i == 0 && src != "" {
  59. contentSub = src
  60. }
  61. })
  62. return
  63. }
  64. func SynchronizationArtclehistory() {
  65. fmt.Println("同步开始")
  66. list, err := models.GetArticleHistoryList()
  67. if err != nil {
  68. fmt.Println("获取列表失败", err)
  69. }
  70. fmt.Println(len(list))
  71. for _, v := range list {
  72. //endDate := v.ModifyTime.Add(+time.Minute * 10).Format(utils.FormatDateTime)
  73. //detail, err := models.GetNewArticleHistoryRecordNewpv(v.UserId, v.ArticleId, endDate)
  74. //if err != nil && err.Error() != utils.ErrNoRow() {
  75. // fmt.Println("获取信息失败", err)
  76. //}
  77. v.OutType = 1
  78. //fmt.Println(v.Id)
  79. //if detail == nil {
  80. // _, err = models.AddCygxArticleViewRecordNewpv(v)
  81. // if err != nil {
  82. // fmt.Println("新增失败", err)
  83. // }
  84. //} else {
  85. // err = models.UpdateCygxArticleViewRecordNewpvList(v, v.StopTime)
  86. // if err != nil {
  87. // fmt.Println("修改失败", err)
  88. // }
  89. //}
  90. newId, err := models.AddCygxArticleViewRecordNewpv(v)
  91. fmt.Println("新增", newId)
  92. if err != nil {
  93. fmt.Println("新增失败", err)
  94. }
  95. }
  96. fmt.Println("同步结束")
  97. }
  98. // UserViewRedisData 阅读数据
  99. type UserViewRedisData struct {
  100. Mobile string `json:"mobile"`
  101. Email string `json:"email"`
  102. RealName string `json:"real_name"`
  103. CompanyName string `json:"company_name"`
  104. ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
  105. ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
  106. CompanyId int `json:"company_id" description:"客户id"`
  107. UserId int `json:"user_id" description:"用户id"`
  108. ReportId int `json:"report_id" description:"报告id"`
  109. StopTime int `json:"stop_time" description:"停留时间"`
  110. ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
  111. OutId int `json:"out_id" description:"记录ID"`
  112. }
  113. type ReportViewRecord struct {
  114. Id int `orm:"column(id);pk"`
  115. //UserId int `json:"user_id" description:"用户ID"`
  116. //ReportId int `description:"报告id"`
  117. //Mobile string `description:"手机号"`
  118. //Email string `description:"邮箱"`
  119. //RealName string `description:"用户实际姓名"`
  120. //CompanyName string `description:"公司名称"`
  121. //CreateTime time.Time `description:"创建时间"`
  122. //StopTime int `json:"stop_time" description:"停留时间"`
  123. //ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
  124. //OutId int `json:"out_id" description:"记录ID"`
  125. Mobile string `json:"mobile"`
  126. Email string `json:"email"`
  127. RealName string `json:"real_name"`
  128. CompanyName string `json:"company_name"`
  129. ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
  130. ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
  131. CompanyId int `json:"company_id" description:"客户id"`
  132. UserId int `json:"user_id" description:"用户id"`
  133. ReportId int `json:"report_id" description:"报告id"`
  134. StopTime int `json:"stop_time" description:"停留时间"`
  135. ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
  136. OutId int `json:"out_id" description:"章节ID"`
  137. }
  138. // PushViewRecordNewRedisData 阅读数据加入到redis
  139. func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId int) bool {
  140. data := &UserViewRedisData{
  141. Mobile: reportViewRecord.Mobile,
  142. UserId: reportViewRecord.UserId,
  143. Email: reportViewRecord.Email,
  144. RealName: reportViewRecord.RealName,
  145. CompanyName: reportViewRecord.CompanyName,
  146. ViewTime: reportViewRecord.ViewTime,
  147. ProductId: 2,
  148. CompanyId: companyId,
  149. ReportId: reportViewRecord.ReportId,
  150. StopTime: reportViewRecord.StopTime,
  151. ReportChapterId: reportViewRecord.ReportChapterId,
  152. OutId: reportViewRecord.OutId,
  153. }
  154. if utils.Re == nil {
  155. err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)
  156. if err != nil {
  157. fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
  158. }
  159. return true
  160. }
  161. return false
  162. }
  163. // 获取策略平台报告阅读数据
  164. func GetCeLueArticlePv(cont context.Context) (err error) {
  165. //func GetCeLueArticlePv() (err error) {
  166. defer func() {
  167. if err != nil {
  168. go utils.SendAlarmMsg("同步策略平台阅读数据失败"+err.Error(), 2)
  169. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetCeLueArticlePv ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  170. }
  171. }()
  172. startTime := time.Now().Add(-time.Minute * 12).Format("2006-01-02 15:04:05")
  173. endTime := time.Now().Format("2006-01-02 15:04:05")
  174. requestUrl := utils.ApiUrl + "backend/statistics_access?take=1000&skip=0&sort=ASC&mode=all&"
  175. encodeData := url.Values{}
  176. encodeData.Add("start_dt", startTime)
  177. encodeData.Add("end_dt", endTime)
  178. encodeStr := encodeData.Encode()
  179. requestUrl += encodeStr
  180. authorization := utils.ApiAuthorization
  181. body, err := PublicGetDate(requestUrl, authorization)
  182. if err != nil {
  183. return
  184. }
  185. var chartResult models.CeLueArticleResultApi
  186. err = json.Unmarshal(body, &chartResult)
  187. if err != nil {
  188. fmt.Println(err)
  189. return err
  190. }
  191. mapMobileArticleId := make(map[string]int)
  192. //获取当天阅读记录
  193. listPv, err := models.GetArticleHistoryRecordAllList()
  194. if err != nil && err.Error() != utils.ErrNoRow() {
  195. fmt.Println("获取当天阅读记录失败", err)
  196. return err
  197. }
  198. if len(listPv) > 0 {
  199. for _, v := range listPv {
  200. mapMobileArticleId[fmt.Sprint(v.Mobile, "_", v.ArticleId)] = v.ArticleId
  201. }
  202. }
  203. var celueHistoryIds []int
  204. var mobiles []string
  205. for _, v := range chartResult.Data {
  206. celueHistoryIds = append(celueHistoryIds, v.CelueHistoryId)
  207. mobiles = append(mobiles, v.Mobile)
  208. }
  209. listcelueHistory, e := models.GetCeLueArticleListByIds(celueHistoryIds)
  210. if e != nil {
  211. err = errors.New("GetCeLueArticleListByIds, Err: " + e.Error())
  212. return
  213. }
  214. mapCelueHistoryIds := make(map[int]int)
  215. for _, v := range listcelueHistory {
  216. mapCelueHistoryIds[v.CelueHistoryId] = v.CelueHistoryId
  217. }
  218. listMobileCompany, e := company.GetCompanyProductListByMobiles(mobiles)
  219. if e != nil {
  220. err = errors.New("GetCompanyProductListByMobiles, Err: " + e.Error())
  221. return
  222. }
  223. mapUserCompany := make(map[string]*company.CompanyProductUser)
  224. for _, v := range listMobileCompany {
  225. mapUserCompany[v.Mobile] = v
  226. }
  227. var celueArticleHistoryItems []*models.CygxCelueArticleHistoryRecord
  228. var allRecordItems []*models.CygxArticleHistoryRecordAll
  229. for _, v := range chartResult.Data {
  230. articleId, _ := strconv.Atoi(v.ArticleId)
  231. if articleId == 0 {
  232. continue
  233. }
  234. //fmt.Println(v.ArticleId)
  235. item := new(models.CygxCelueArticleHistoryRecord)
  236. item.CelueHistoryId = v.CelueHistoryId
  237. item.Mobile = v.Mobile
  238. item.ArticleId = v.ArticleId
  239. if v.CompanyName != nil {
  240. item.CompanyName = v.CompanyName.RealName
  241. }
  242. if v.CrmUser != nil {
  243. item.RealName = v.CrmUser.RealName
  244. }
  245. if mapUserCompany[v.Mobile] != nil {
  246. mapUser := mapUserCompany[v.Mobile]
  247. item.CompanyName = mapUser.CompanyName
  248. item.CompanyStatus = mapUser.Status
  249. item.SellerName = mapUser.SellerName
  250. }
  251. item.CreateDateApi = time.Now()
  252. t1, _ := time.Parse("2006-01-02T15:04:05Z", v.CreateDate)
  253. item.CreateTime = t1.Add(+time.Hour * 8).Format(utils.FormatDateTime)
  254. //email=?,company_id=?,company_name=?,user_id=?,real_name=?
  255. if mapCelueHistoryIds[v.CelueHistoryId] == 0 {
  256. celueArticleHistoryItems = append(celueArticleHistoryItems, item)
  257. //写入记录到总的统计表
  258. record := new(models.CygxArticleHistoryRecordAll)
  259. record.ArticleId = articleId
  260. record.CelueHistoryId = item.CelueHistoryId
  261. record.CreateTime = item.CreateTime
  262. record.ModifyTime = time.Now()
  263. record.CreateDateApi = time.Now()
  264. if mapUserCompany[v.Mobile] != nil {
  265. mapUser := mapUserCompany[v.Mobile]
  266. record.Mobile = mapUser.Mobile
  267. record.CompanyName = mapUser.CompanyName
  268. record.CompanyStatus = mapUser.Status
  269. record.SellerName = mapUser.SellerName
  270. record.CompanyId = mapUser.CompanyId
  271. record.RealName = mapUser.RealName
  272. record.UserId = mapUser.UserId
  273. record.Email = mapUser.Email
  274. }
  275. record.Platfor = 2
  276. record.Source = "CELUE"
  277. if mapMobileArticleId[fmt.Sprint(item.Mobile, "_", item.ArticleId)] == articleId {
  278. record.IsDel = 1
  279. }
  280. allRecordItems = append(allRecordItems, record)
  281. }
  282. //count, err := models.GetCeLueArticleCountById(v.CelueHistoryId)
  283. //if err != nil && err.Error() != utils.ErrNoRow() {
  284. // return err
  285. //}
  286. //if count == 0 {
  287. // _, err := models.AddCeLueArticle(item, mapMobileArticleId)
  288. // if err != nil {
  289. // fmt.Println(err)
  290. // return err
  291. // }
  292. //}
  293. }
  294. if len(celueArticleHistoryItems) > 0 {
  295. e = models.AddCygxArticleHistoryRecordAllMulti(celueArticleHistoryItems, allRecordItems)
  296. if e != nil {
  297. err = errors.New("AddCygxArticleHistoryRecordAllMulti, Err: " + e.Error())
  298. return
  299. }
  300. }
  301. //处理同步过来的阅读记录所属用户
  302. //var condition string
  303. //condition = ` AND create_time > ` + "'" + startTime + "'"
  304. //listArticlePv, err := models.GetArticleHistoryRecordAllByMobileList(condition)
  305. //if err != nil {
  306. // fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
  307. //}
  308. //for _, v := range listArticlePv {
  309. // if v.Mobile != "" {
  310. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  311. // if err != nil && err.Error() != utils.ErrNoRow() {
  312. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  313. // }
  314. // if user != nil {
  315. // err = models.UpdateCygxArticleHistoryRecordAll(user)
  316. // if err != nil {
  317. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  318. // }
  319. // }
  320. // }
  321. //}
  322. return
  323. }
  324. func GetArticleListByApi(cont context.Context) (err error) {
  325. defer func() {
  326. if err != nil {
  327. //fmt.Println("GetArticleListByApi Err:" + err.Error())
  328. go utils.SendAlarmMsg("同步策略平台数据失败", 2)
  329. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  330. }
  331. }()
  332. listUpdateArticle, err := models.GetArticleCeluePushList()
  333. if err != nil && err.Error() != utils.ErrNoRow() {
  334. return err
  335. }
  336. //如果长度为零就不处理
  337. if len(listUpdateArticle) == 0 {
  338. return err
  339. }
  340. for _, v := range listUpdateArticle {
  341. // 这里直接go出去会出现并发,导致文章md5ID唯一索引限制报错
  342. err = HandleArticleListByApi(v.ArticleId)
  343. if err != nil {
  344. utils.FileLog.Info("HandleArticleListByApi", err.Error())
  345. }
  346. }
  347. return
  348. }
  349. // 获取策略平台推送过来的文章并更新
  350. func UpdateArticleByRedis() (err error) {
  351. for {
  352. utils.Rc.Brpop(utils.CYGX_ARTICLE_UPDATE_KEY, func(b []byte) {
  353. var log models.CygxArticleCeluePushRedis
  354. if err := json.Unmarshal(b, &log); err != nil {
  355. fmt.Println("json unmarshal wrong!")
  356. go utils.SendAlarmMsg("获取策略平台推送过来的文章并更新处理Redis队列消息失败:"+err.Error()+string(b), 2)
  357. }
  358. // 这里直接go出去会出现并发,导致文章md5ID唯一索引限制报错
  359. err = HandleArticleListByApi(log.ArticleId)
  360. if err != nil {
  361. utils.FileLog.Info("HandleArticleListByApi", err.Error())
  362. }
  363. })
  364. }
  365. }
  366. // 处理同步过来的文章
  367. func HandleArticleListByApi(artcleId int) (err error) {
  368. defer func() {
  369. if err != nil {
  370. go utils.SendAlarmMsg("处理同步过来的文章失败"+"HandleArticleListByApi ErrMsg:"+err.Error()+"artcleId:"+strconv.Itoa(artcleId), 2)
  371. }
  372. }()
  373. var clueApiUrl string
  374. clueApiUrl = fmt.Sprint(utils.ApiUrl, "articles/", artcleId)
  375. fmt.Println(clueApiUrl)
  376. authorization := utils.ApiAuthorization
  377. body, err := PublicGetDate(clueApiUrl, authorization)
  378. if err != nil {
  379. fmt.Println(err)
  380. return
  381. }
  382. var articleResultDate models.ArticleDetailResultApi
  383. err = json.Unmarshal(body, &articleResultDate)
  384. if err != nil {
  385. fmt.Println("Getres.PublicGetDate Err:", err.Error())
  386. return err
  387. }
  388. item := new(models.CygxShanghaiCompanyLog)
  389. item.CreateTime = time.Now()
  390. item.Url = clueApiUrl
  391. item.Body = ""
  392. item.Result = string(body)
  393. go models.AddCygxShanghaiCompanyLog(item)
  394. go models.UpdateCygxArticleCeluePush(artcleId)
  395. articleResult := articleResultDate.Data
  396. exitMap := make(map[int]int)
  397. categoryIdMap := make(map[int]int)
  398. classMap := make(map[int]int)
  399. reportMap := make(map[int]int)
  400. summaryMap := make(map[int]int)
  401. listMap, err := models.GetArticleApiMap()
  402. if err != nil {
  403. fmt.Println("GetlistMap Err:", err.Error())
  404. return err
  405. }
  406. openIdList, err := models.GetUserRecordListByMobile(4, utils.ArticleTaskClassMobile)
  407. if err != nil {
  408. fmt.Println(err)
  409. return err
  410. }
  411. fmt.Println(openIdList)
  412. //新旧分类 反向隐射,是否归类,是否是报告,是否是纪要库
  413. for _, v := range listMap {
  414. exitMap[v.SeriesId] = v.OldId
  415. categoryIdMap[v.SeriesId] = v.OldIdMap
  416. if v.IsClass == 1 {
  417. classMap[v.OldId] = 1
  418. }
  419. if v.IsReport == 1 {
  420. reportMap[v.OldId] = 1
  421. }
  422. if v.IsSummary == 1 {
  423. summaryMap[v.OldId] = 1
  424. }
  425. }
  426. var list []*models.Tactics2
  427. var listArticleData []*models.CygxArticleData
  428. var listAuthor []*models.CygxArticleAuthor
  429. //如果是英文报告 ,或者香港类型那么则不同步 publish_area :all 全部 ;hk 香港
  430. if articleResult.TypeId == 9 || articleResult.PublishArea != "all" {
  431. // 如果是英文报告,或者是香港报告就进行删除处理
  432. err = models.UpdateArticlePublish(artcleId, 0)
  433. if err != nil {
  434. fmt.Println("UpdateArticlePublish Err:", err.Error())
  435. return err
  436. }
  437. UpdateArticleResourceData(artcleId) //把数据写入 cygx_resource_data 表
  438. return
  439. }
  440. var cover string
  441. if articleResult.Series.Name == "图解市场" {
  442. cover = GetArticleCoverByChartFirst(articleResult.Content.Body)
  443. } else if articleResult.Series.Name == "行业深度" {
  444. cover = articleResult.Cover
  445. }
  446. fmt.Println(cover)
  447. //return
  448. //状态等于 2 跟 4 的进行同步 IsActive 为false 软删除的也不同步
  449. if exitMap[articleResult.SeriesId] > 0 && (articleResult.PublishStatus == 2 || articleResult.PublishStatus == 4) && articleResult.IsActive {
  450. articleResult.PublishDate = time.Date(articleResult.PublishDate.Year(), articleResult.PublishDate.Month(), articleResult.PublishDate.Day(), articleResult.PublishDate.Hour(), articleResult.PublishDate.Minute(), articleResult.PublishDate.Second(), articleResult.PublishDate.Nanosecond(), time.Local)
  451. item := new(models.Tactics2)
  452. itemArticleData := new(models.CygxArticleData)
  453. itemArticleData.ArticleId = articleResult.ArticleId
  454. itemArticleData.Cover = articleResult.Cover
  455. itemArticleData.CreateTime = time.Now()
  456. listArticleData = append(listArticleData, itemArticleData)
  457. itemAuthor := new(models.CygxArticleAuthor)
  458. item.ArticleId = articleResult.ArticleId
  459. item.Title = articleResult.Title
  460. item.TitleEn = articleResult.TitleEn
  461. item.File = articleResult.File
  462. if articleResult.Frequency == "日度" {
  463. item.UpdateFrequency = "daily"
  464. } else if articleResult.Frequency == "周度" {
  465. item.UpdateFrequency = "weekly"
  466. } else if articleResult.Frequency == "月度" {
  467. item.UpdateFrequency = "monthly"
  468. } else if articleResult.Frequency == "季度" {
  469. item.UpdateFrequency = "quarterly"
  470. } else if articleResult.Frequency == "年度" {
  471. item.UpdateFrequency = "yearly"
  472. } else {
  473. item.UpdateFrequency = "unknow"
  474. }
  475. item.CreateDate = articleResult.CreateDate
  476. item.PublishDate = articleResult.PublishDate.Add(time.Hour * 8)
  477. item.PublishStatus = 1
  478. item.Body = articleResult.Content.Body
  479. item.Abstract = articleResult.Content.Abstract
  480. item.Annotation = articleResult.Content.Annotation
  481. item.CategoryName = articleResult.Industry.Name
  482. item.CategoryId = categoryIdMap[articleResult.SeriesId]
  483. item.CategoryIdTwo = exitMap[articleResult.SeriesId]
  484. item.SubCategoryName = articleResult.Series.Name
  485. item.Cover = cover
  486. //{
  487. // //这两种情况特殊处理
  488. // if articleResult.Field.Name == "宏观利率" {
  489. // item.CategoryId = 10070
  490. // item.CategoryIdTwo = item.CategoryId
  491. // }
  492. // if articleResult.Field.Name == "信用" {
  493. // item.CategoryId = 10082
  494. // item.CategoryIdTwo = item.CategoryId
  495. // }
  496. //}
  497. if len(articleResult.Stock) > 0 {
  498. var stock string
  499. for _, vS := range articleResult.Stock {
  500. stock += vS + "/"
  501. }
  502. stock = strings.TrimRight(stock, "/")
  503. item.Stock = stock
  504. }
  505. item.FieldName = articleResult.Field.Name
  506. item.SeriesName = articleResult.Series.Name
  507. item.TypeName = articleResult.Type.Name
  508. item.UpdateDate = articleResult.UpdateDate
  509. item.CeLueFieldId = articleResult.Field.Id
  510. list = append(list, item)
  511. itemAuthor.ArticleId = articleResult.ArticleId
  512. itemAuthor.Name = articleResult.Author.Name
  513. itemAuthor.Mobile = articleResult.Author.PhoneNumber
  514. fmt.Println(articleResult.Cover)
  515. listAuthor = append(listAuthor, itemAuthor)
  516. } else {
  517. // 如果这篇文章没有发布,那么就不作处理。
  518. err = models.UpdateArticlePublish(artcleId, 0)
  519. if err != nil {
  520. fmt.Println("UpdateArticlePublish Err:", err.Error())
  521. return err
  522. }
  523. UpdateArticleResourceData(artcleId) //把数据写入 cygx_resource_data 表
  524. //go UpdateResourceData(artcleId, "article", "delete", time.Now().Format(utils.FormatDateTime))
  525. return
  526. }
  527. //同步作者
  528. for _, v := range listAuthor {
  529. var count int
  530. count, err = models.GetActivityAuthorCount(v.ArticleId, v.Mobile)
  531. if err != nil {
  532. fmt.Println("GetActivityAuthorCount Err:", err.Error())
  533. return err
  534. }
  535. if count == 0 {
  536. _, err = models.AddCygxActivityAuthor(v)
  537. if err != nil {
  538. fmt.Println("AddCygxActivityAuthor Err:", err.Error())
  539. return err
  540. }
  541. }
  542. }
  543. fmt.Println("同步文章条数:", len(list))
  544. listCustomArticle, err := models.GetCustomArticleId() //手动归类的文章,不替换文章类型
  545. if err != nil {
  546. fmt.Println("GetTacticsList Err:", err.Error())
  547. return err
  548. }
  549. listGetMatchTypeName, errMatch := models.GetMatchTypeNamenNotNull() //手动归类的文章,不替换文章类型
  550. if errMatch != nil {
  551. fmt.Println("GetTacticsList Err:", errMatch.Error())
  552. return err
  553. }
  554. //mapCategoryIdSet := make(map[int]int)
  555. //for _, vMatch := range listGetMatchTypeName {
  556. // mapCategoryIdSet[vMatch.CategoryId] = vMatch.CategoryIdSet
  557. //}
  558. fmt.Println("list len:", len(list))
  559. noSummaryArticleIds := "3454,3456,3457,3459,2449,2450,2453,2454,2459,2530,2583,2663,2670,2699,2715,2732,2748,2759,2399,2356,2870,3173,2978,2826,3470" //非纪要库类型的文章ID
  560. listNoSummaryArticleIds := strings.Split(noSummaryArticleIds, ",")
  561. for k, v := range list {
  562. //同步匹配类型
  563. matchTypeName := ""
  564. for _, vMatch := range listGetMatchTypeName {
  565. if v.CategoryId == vMatch.CategoryId {
  566. matchTypeName = vMatch.MatchTypeName
  567. }
  568. }
  569. //是否属于纪要库的数据
  570. //if _, has := summaryMap[v.CategoryId]; has {
  571. // v.IsSummary = 1
  572. //}
  573. if v.TypeName == "纪要" {
  574. v.IsSummary = 1
  575. }
  576. //排除不属于纪要库类型的文章
  577. for _, vArt := range listNoSummaryArticleIds {
  578. vArtInt, _ := strconv.Atoi(vArt)
  579. if v.ArticleId == vArtInt {
  580. v.IsSummary = 0
  581. }
  582. }
  583. if _, has := reportMap[v.CategoryIdTwo]; has {
  584. v.IsReport = 1
  585. if _, ok := classMap[v.CategoryIdTwo]; ok {
  586. v.IsClass = 1
  587. v.ReportType = 1 //是否属于行业报告
  588. } else {
  589. v.ReportType = 2 //是否属于产业报告
  590. }
  591. }
  592. v.Department = "弘则权益研究"
  593. //判断是否已经存在
  594. if v.ArticleId < 0 {
  595. fmt.Println("AddCygxArticle Err:")
  596. return err
  597. }
  598. var count int
  599. count, err = models.GetArticleCountById(v.ArticleId)
  600. if err != nil && err.Error() != utils.ErrNoRow() {
  601. fmt.Println("AddCygxArticle Err:", err.Error())
  602. return err
  603. }
  604. v.Body = strings.Replace(v.Body, "http://vmp.hzinsights.com", "https://vmp.hzinsights.com", -1)
  605. expertNumStr, expertContentStr, interviewDateStr, _, bodyReturn := BodyAnalysis2(v.Body)
  606. if strings.Index(v.Body, "报告全文(") > 0 && strings.Index(v.Body, "PDF格式报告下载.pdf") > 0 {
  607. v.Body = strings.Replace(v.Body, "报告全文(", "", -1)
  608. v.Body = strings.Replace(v.Body, "PDF格式报告下载.pdf", "", -1)
  609. v.Body = strings.Replace(v.Body, "):", "", -1)
  610. }
  611. var titleNew string
  612. titleNew = v.Title
  613. // 7资金流向 、11大类资产 、51每日复盘 、80医药周报、9估值研究
  614. if v.CategoryId == 7 || v.CategoryId == 11 || v.CategoryId == 51 || v.CategoryId == 9 {
  615. if v.UpdateFrequency == "daily" {
  616. var daystr string
  617. daystr = strconv.Itoa(v.PublishDate.Day())
  618. if len(daystr) == 1 {
  619. daystr = "0" + daystr
  620. }
  621. titleNew = v.Title + "(" + strconv.Itoa(v.PublishDate.Year())[2:len(strconv.Itoa(v.PublishDate.Year()))-0] + v.PublishDate.Format("01") + daystr + ")"
  622. } else if v.UpdateFrequency == "weekly" {
  623. titleNew = v.Title + utils.WeekByDate(v.PublishDate)
  624. }
  625. }
  626. //策略周度思考,医药周报,消费周报,消费周度思考,科技前言(周报)、智造精粹、智造本周荟 标题处理
  627. if v.CategoryId == 80 || v.CategoryId == 52 || v.CategoryId == 10074 || v.CategoryId == 1008 || v.CategoryId == 10071 || v.CategoryId == 10073 || v.CategoryId == 50 {
  628. titleNew = v.Title + utils.WeekByDate(v.PublishDate)
  629. }
  630. //类型是综述报告的,不区分是什么系列,统一归类到对应产业的路演精华
  631. if articleResult.Type.Name == "综述报告" {
  632. detailCategory, err := models.GetdetailByCategoryIdLyjh(v.CategoryIdTwo)
  633. if err != nil && err.Error() != utils.ErrNoRow() {
  634. return err
  635. }
  636. v.CategoryId = detailCategory.CategoryId
  637. matchTypeName = "路演精华"
  638. if v.IsClass == 0 {
  639. v.IsSummary = 0
  640. }
  641. }
  642. var chartPermissionId int
  643. var chartPermissionName string
  644. categoryDetail, _ := models.GetdetailByCategoryIdPush(v.CategoryId)
  645. if categoryDetail != nil {
  646. chartPermissionId = categoryDetail.ChartPermissionId
  647. chartPermissionName = categoryDetail.ChartPermissionName
  648. }
  649. if count > 0 {
  650. fmt.Println(k, v.ArticleId, "edit")
  651. articleIfoOld, e := models.GetArticleDetailTestById(v.ArticleId)
  652. if e != nil {
  653. err = e
  654. return
  655. }
  656. bodyTextOld, _ := GetReportContentTextSubNew(articleIfoOld.Body)
  657. bodyText, _ := GetReportContentTextSubNew(v.Body)
  658. if len(bodyText)-len(bodyTextOld) > 100 {
  659. //fmt.Println("触发推送规则")
  660. //更新字数大于一百字触发推送规则
  661. go DoArticleOnenIdWxTemplateMsg(v.ArticleId)
  662. UpdateArticleResourceData(v.ArticleId) //把数据写入 cygx_resource_data 表
  663. //go UpdateResourceData(v.ArticleId, "article", "update", time.Now().Format(utils.FormatDateTime))
  664. }
  665. var isCustom bool
  666. updateParams := make(map[string]interface{})
  667. //updateParams["Title"] = v.Title
  668. updateParams["Title"] = titleNew
  669. updateParams["TitleEn"] = v.TitleEn
  670. updateParams["UpdateFrequency"] = v.UpdateFrequency
  671. updateParams["CreateDate"] = v.CreateDate
  672. updateParams["PublishDate"] = v.PublishDate
  673. //updateParams["Body"] = html.EscapeString(v.Body)
  674. updateParams["Body"] = html.EscapeString(bodyReturn)
  675. updateParams["BodyText"] = bodyText
  676. updateParams["Abstract"] = html.EscapeString(v.Abstract)
  677. updateParams["CategoryName"] = v.CategoryName
  678. for _, vCustom := range listCustomArticle {
  679. if v.ArticleId == vCustom.ArticleId {
  680. fmt.Println("手动归类的文章:" + strconv.Itoa(v.ArticleId))
  681. isCustom = true
  682. }
  683. }
  684. if isCustom == false {
  685. updateParams["CategoryId"] = v.CategoryId
  686. updateParams["MatchTypeName"] = matchTypeName
  687. updateParams["IsSummary"] = v.IsSummary
  688. updateParams["IsReport"] = v.IsReport
  689. updateParams["ReportType"] = v.ReportType
  690. updateParams["SubCategoryName"] = v.SubCategoryName
  691. }
  692. //updateParams["CategoryId"] = v.CategoryId
  693. updateParams["PublishStatus"] = 1
  694. updateParams["ExpertBackground"] = expertContentStr
  695. updateParams["ExpertNumber"] = expertNumStr
  696. updateParams["InterviewDate"] = interviewDateStr
  697. //updateParams["IsClass"] = v.IsClass
  698. v.Department = "弘则权益研究"
  699. updateParams["Department"] = v.Department
  700. updateParams["FileLink"] = v.File
  701. updateParams["Stock"] = v.Stock
  702. updateParams["FieldName"] = v.FieldName
  703. updateParams["SeriesName"] = v.SeriesName
  704. updateParams["Annotation"] = v.Annotation
  705. updateParams["TypeName"] = v.TypeName
  706. updateParams["ModifyTimeByCl"] = v.UpdateDate
  707. updateParams["CeLueFieldId"] = v.CeLueFieldId
  708. updateParams["Cover"] = v.Cover
  709. updateParams["ChartPermissionId"] = chartPermissionId
  710. updateParams["ChartPermissionName"] = chartPermissionName
  711. whereParam := map[string]interface{}{"article_id": v.ArticleId}
  712. err = models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
  713. if err != nil {
  714. fmt.Println("UpdateByExpr Err:" + err.Error())
  715. return err
  716. }
  717. } else {
  718. fmt.Println(k, v.ArticleId, "add")
  719. item := new(models.CygxArticle)
  720. articleIdInt := v.ArticleId
  721. item.ArticleId = articleIdInt
  722. //item.Title = v.Title
  723. item.Title = titleNew
  724. item.TitleEn = v.TitleEn
  725. item.UpdateFrequency = v.UpdateFrequency
  726. item.CreateDate = v.CreateDate
  727. item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
  728. //item.Body = html.EscapeString(v.Body)
  729. item.Body = html.EscapeString(bodyReturn)
  730. item.Abstract = html.EscapeString(v.Abstract)
  731. item.CategoryName = v.CategoryName
  732. item.SubCategoryName = v.SubCategoryName
  733. item.CategoryId = v.CategoryId
  734. item.CategoryIdTwo = v.CategoryIdTwo
  735. item.PublishStatus = 1
  736. item.ExpertBackground = expertContentStr
  737. item.ExpertNumber = expertNumStr
  738. item.InterviewDate = interviewDateStr
  739. item.Department = v.Department
  740. item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
  741. item.IsClass = v.IsClass
  742. item.IsSummary = v.IsSummary
  743. item.IsReport = v.IsReport
  744. item.ReportType = v.ReportType
  745. item.FileLink = v.File
  746. item.MatchTypeName = matchTypeName
  747. item.Stock = v.Stock
  748. item.FieldName = v.FieldName
  749. item.SeriesName = v.SeriesName
  750. item.Annotation = v.Annotation
  751. item.TypeName = v.TypeName
  752. item.ModifyTimeByCl = v.UpdateDate
  753. item.CeLueFieldId = v.CeLueFieldId
  754. item.Cover = v.Cover
  755. item.ChartPermissionId = chartPermissionId
  756. item.ChartPermissionName = chartPermissionName
  757. item.Cover = v.Cover
  758. item.CreateTime = time.Now()
  759. newId, err := models.AddCygxArticles(item)
  760. if err != nil {
  761. fmt.Println("AddCygxArticle Err:", err.Error())
  762. return err
  763. }
  764. //go UpdateResourceData(v.ArticleId, "article", "add", time.Now().Format(utils.FormatDateTime))
  765. UpdateArticleResourceData(v.ArticleId) //把数据写入 cygx_resource_data 表
  766. //fmt.Println(newId)
  767. //报告自动归类,以及推送相关模板消息
  768. if v.ReportType == 2 {
  769. var subjectStr string
  770. var industrialManagementIdStr string
  771. var industrialSubjectIdStr string
  772. var keyword1 string
  773. var keyword2 string
  774. var keyword3 string
  775. var keyword4 string
  776. sliceSubjects := strings.Split(v.Stock, "/")
  777. mapManagementForSubject := make(map[string]string)
  778. mapIndustrialId := make(map[int]int)
  779. if len(sliceSubjects) > 0 {
  780. for _, vSubject := range sliceSubjects {
  781. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  782. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  783. subject := sliceXiahuaxian[0]
  784. subjectStr += "'" + subject + "',"
  785. }
  786. //获取该产业下所对应的行业图片
  787. detailCategory, errCategory := models.GetdetailByCategoryIdOne(v.CategoryId)
  788. if errCategory != nil {
  789. fmt.Println("GetdetailByCategoryIdOne Err:", err.Error())
  790. return err
  791. }
  792. subjectStr = strings.TrimRight(subjectStr, ",")
  793. if subjectStr != "" {
  794. listIndustrial, err := models.GetIndustrialManagementForSubjecName(subjectStr, detailCategory.ChartPermissionId)
  795. if err != nil {
  796. fmt.Println("GetIndustrialManagementForSubjecName Err:", err.Error())
  797. return err
  798. }
  799. subjectStr = strings.Replace(subjectStr, "','", "】【", -1)
  800. subjectStr = strings.Replace(subjectStr, "'", "", -1)
  801. subjectStr = "【" + subjectStr + "】"
  802. if len(listIndustrial) > 0 {
  803. for _, vIndustrial := range listIndustrial {
  804. if _, ok := mapIndustrialId[vIndustrial.IndustrialManagementId]; !ok {
  805. industrialManagementIdStr += strconv.Itoa(vIndustrial.IndustrialManagementId) + ","
  806. }
  807. mapIndustrialId[vIndustrial.IndustrialManagementId] = vIndustrial.IndustrialManagementId
  808. industrialSubjectIdStr += strconv.Itoa(vIndustrial.IndustrialSubjectId) + ","
  809. mapManagementForSubject[vIndustrial.IndustryName] += vIndustrial.SubjectName + "/"
  810. if vIndustrial.LayoutTime == "0000-00-00 00:00:00" {
  811. go models.UpdateIndustrialManagementLayoutTime(v.PublishDate.AddDate(0, 0, -10).Format(utils.FormatDateTime), vIndustrial.IndustrialManagementId)
  812. }
  813. }
  814. industrialManagementIdStr = strings.TrimRight(industrialManagementIdStr, ",")
  815. industrialSubjectIdStr = strings.TrimRight(industrialSubjectIdStr, ",")
  816. if industrialManagementIdStr != "" {
  817. err = models.ReportArticleClassificationEditNew(int(newId), industrialManagementIdStr, v.ArticleId, industrialSubjectIdStr)
  818. if err != nil {
  819. fmt.Println("ReportArticleClassificationEditNew Err:", err.Error())
  820. return err
  821. }
  822. }
  823. var peoductName string
  824. for mk, mv := range mapManagementForSubject {
  825. peoductName += "【" + mk + "--" + strings.TrimRight(mv, "/") + "】"
  826. }
  827. keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
  828. keyword2 = "已自动关联至以下产业和标的:" + peoductName
  829. keyword3 = v.Title
  830. keyword4 = v.PublishDate.Format(utils.FormatDateTime)
  831. SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
  832. } else {
  833. keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
  834. keyword2 = "未归类"
  835. keyword3 = v.Title
  836. keyword4 = v.PublishDate.Format(utils.FormatDateTime)
  837. SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
  838. go models.UpdateIsClassFail(artcleId)
  839. }
  840. }
  841. }
  842. }
  843. go DoArticleOnenIdWxTemplateMsg(v.ArticleId)
  844. }
  845. if v.CategoryName == utils.ZHOU_QI_NAME {
  846. UpdateCygxZhouqiArticleMapTime(v.FieldName) //更新周期对应分类下所管理文章的更新时间
  847. }
  848. //【公司调研】系列纪要发布/更新后 end
  849. //查研观向6.9模板消息推送规则
  850. //但是如果报告的发布时间是在10天以前的,公众号就不推送了
  851. //if time.Now().AddDate(0, 0, -10).Before(articleResult.PublishDate.Add(time.Hour * 8)) {
  852. // go DoArticleOnenIdWxTemplateMsg(v.ArticleId)
  853. //}
  854. }
  855. go models.UpdateCygxArticleCeluePush(artcleId)
  856. AddComprehensiveArticle(artcleId) // 同步到综合页面的搜索引擎
  857. AddComprehensiveIndustrialSource("Hz", artcleId) // 同步到综合页面的搜索引擎(添加产业资源包)
  858. GetSummarytoEs(artcleId) // 同步到ES搜索引擎
  859. GetIndustrialManagementArticleNewPublishData()
  860. if len(listArticleData) > 0 {
  861. for _, v := range listArticleData {
  862. count, err := models.GetCygxArticleDataCount(v.ArticleId)
  863. if err != nil {
  864. fmt.Println("GetCygxArticleDataCount Err:", err.Error())
  865. return err
  866. }
  867. if count == 0 {
  868. _, err := models.AddCygxArticleData(v)
  869. if err != nil {
  870. fmt.Println("AddCygxArticleData Err:", err.Error())
  871. return err
  872. }
  873. }
  874. //} else {
  875. // updateParams := make(map[string]interface{})
  876. // updateParams["Cover"] = v.Cover
  877. // whereParam := map[string]interface{}{"article_id": v.ArticleId}
  878. // err = models.UpdateByExpr(models.CygxArticleData{}, whereParam, updateParams)
  879. // if err != nil {
  880. // fmt.Println("UpdateByExpr CygxArticleData Err:" + err.Error())
  881. // return err
  882. // }
  883. //}
  884. }
  885. }
  886. AddCygxReportMappingCategoryGroupByArticleId(artcleId)
  887. return err
  888. }
  889. //func init() {
  890. // DoArticleOnenIdWxTemplateMsg(8112)
  891. //}
  892. // 过滤策略平台报告,研选报告重复推送,以及权限勾选的推送
  893. func DoArticleOnenIdWxTemplateMsg(articleId int) (err error) {
  894. //策略平台同步过来的文章ID,做微信模板消息推送,同一篇报告12小时只推送一次
  895. cacheKey := fmt.Sprint(utils.CACHE_KEY_ARTICLE_ID_TEMPLATE, ":ArticleId", articleId)
  896. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  897. if ttlTime > 0 {
  898. fmt.Println(ttlTime)
  899. return err
  900. }
  901. var msg string
  902. defer func() {
  903. if err != nil {
  904. go utils.SendAlarmMsg("过滤策略平台报告,研选报告重复推送,以及权限勾选的推送失败 DoArticleOnenIdWxTemplateMsg ErrMsg:"+err.Error(), 2)
  905. fmt.Println(err)
  906. }
  907. if msg != "" {
  908. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  909. }
  910. }()
  911. //获取文章详情
  912. articleInfo, err := models.GetArticleDetailById(articleId)
  913. if err != nil {
  914. return
  915. }
  916. articleInfo.Abstract, _ = GetReportContentTextSub(articleInfo.Abstract)
  917. var chartPermissionId int
  918. //固收的权限,暂时当策略的逻辑一样处理
  919. var chartPermissionName string
  920. reportMapDetail, err := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  921. if err != nil {
  922. return err
  923. }
  924. if reportMapDetail.ChartPermissionId == utils.CE_LUE_ID {
  925. chartPermissionId = utils.CE_LUE_ID
  926. } else if reportMapDetail.ChartPermissionId == utils.GU_SHOU_ID_CHART {
  927. chartPermissionId = utils.GU_SHOU_ID_CHART
  928. } else {
  929. if reportMapDetail.PermissionType == 1 {
  930. chartPermissionName = reportMapDetail.ChartPermissionName + "(主观)"
  931. } else {
  932. chartPermissionName = reportMapDetail.ChartPermissionName + "(客观)"
  933. }
  934. chartPermissionDetail, err := models.GetChartPermissionByRemark(chartPermissionName)
  935. if err != nil {
  936. return err
  937. }
  938. chartPermissionId = chartPermissionDetail.ChartPermissionId
  939. }
  940. //文章关联的产业
  941. var industryName string
  942. var industrialManagementId string
  943. if chartPermissionId == utils.CE_LUE_ID || chartPermissionId == utils.GU_SHOU_ID_CHART {
  944. industryName = reportMapDetail.SubCategoryName
  945. } else {
  946. if articleInfo.ReportType == 2 {
  947. detailIndustryNameList, err := models.GetIndustrialManagemenDetailByAaticleID(articleId)
  948. if err != nil && err.Error() != utils.ErrNoRow() {
  949. return err
  950. }
  951. //如果不存在关联的产业则不推送
  952. if len(detailIndustryNameList) == 0 {
  953. return err
  954. }
  955. for _, v := range detailIndustryNameList {
  956. industryName += v.IndustryName + ","
  957. industrialManagementId += strconv.Itoa(v.IndustrialManagementId) + ","
  958. }
  959. industryName = strings.TrimRight(industryName, ",")
  960. industrialManagementId = strings.TrimRight(industrialManagementId, ",")
  961. //处理勾选了全部行业赛道的用户
  962. var allIn string
  963. allIn = DoXzsChooseSend(reportMapDetail.ChartPermissionName)
  964. if allIn != "" {
  965. allInList, err := models.GetCygxXzsChooseSendByAllIn(allIn)
  966. if err != nil && err.Error() != utils.ErrNoRow() {
  967. return err
  968. }
  969. if len(allInList) > 0 {
  970. var allInUserId string
  971. for _, v := range allInList {
  972. allInUserId += strconv.Itoa(v.UserId) + ","
  973. }
  974. allInUserId = strings.TrimRight(allInUserId, ",")
  975. userFllowIndustrialList, err := models.GetUserFllowIndustrialListByUserIdAndIndustrial(allInUserId, industrialManagementId)
  976. if err != nil && err.Error() != utils.ErrNoRow() {
  977. return err
  978. }
  979. mapUserFllowed := make(map[string]int)
  980. for _, v := range userFllowIndustrialList {
  981. //用户ID与其关注的产业组成唯一索引,判断是否存在,如果不存在就进行自动关注
  982. mapUserFllowed[fmt.Sprint("uid:", v.UserId, "_Industrial:", v.IndustrialManagementId)] = v.UserId
  983. }
  984. //获取这些allIN用户的的信息进行遍历生成需要插入的数据
  985. userList, err := models.GetWxUserListByUserIds(allInUserId)
  986. if err != nil && err.Error() != utils.ErrNoRow() {
  987. return err
  988. }
  989. var industryFllowItems []*models.CygxIndustryFllow
  990. for _, v := range detailIndustryNameList {
  991. for _, vUser := range userList {
  992. //如果用户这个用户没有关注这个产业就进行自动关注
  993. if mapUserFllowed[fmt.Sprint("uid:", vUser.UserId, "_Industrial:", v.IndustrialManagementId)] == 0 {
  994. item := new(models.CygxIndustryFllow)
  995. item.IndustrialManagementId = v.IndustrialManagementId
  996. item.UserId = vUser.UserId
  997. item.Email = vUser.Email
  998. item.Mobile = vUser.Mobile
  999. item.RealName = vUser.RealName
  1000. item.CompanyId = vUser.CompanyId
  1001. item.CompanyName = vUser.CompanyName
  1002. item.Source = 3
  1003. item.Type = 1
  1004. item.CreateTime = time.Now()
  1005. item.ModifyTime = time.Now()
  1006. industryFllowItems = append(industryFllowItems, item)
  1007. }
  1008. }
  1009. }
  1010. err = models.AddCygxIndustryFllowMulti(industryFllowItems)
  1011. if err != nil {
  1012. go utils.SendAlarmMsg("用户关注全部赛道,出现新赛道批量添加失败 ,AddCygxIndustryFllowMulti ErrMsg:"+err.Error(), 2)
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. // 获取所有有权的用户的 openid
  1019. mapOpenidPower := make(map[int]string)
  1020. permissionIdList := make([]string, 0)
  1021. var condition string
  1022. var pars []interface{}
  1023. slicepermissionId := strings.Split(strconv.Itoa(chartPermissionId), ",")
  1024. if len(slicepermissionId) > 0 {
  1025. for _, v := range slicepermissionId {
  1026. if v == strconv.Itoa(utils.GU_SHOU_ID_CHART) {
  1027. v = strconv.Itoa(utils.CE_LUE_ID)
  1028. }
  1029. permissionIdList = append(permissionIdList, v)
  1030. }
  1031. condition += ` AND p.chart_permission_id IN (` + utils.GetOrmInReplace(len(slicepermissionId)) + ` ) `
  1032. pars = append(pars, permissionIdList)
  1033. }
  1034. openidPowerListMobile, err := models.GetCygxUserRecordPower(condition, pars)
  1035. if err != nil {
  1036. return err
  1037. }
  1038. var mobileArr []string
  1039. for _, v := range openidPowerListMobile {
  1040. mobileArr = append(mobileArr, v.Mobile)
  1041. }
  1042. openidPowerList, err := models.GetWxOpenIdByMobileSliceList(mobileArr)
  1043. if err != nil {
  1044. return err
  1045. }
  1046. //四大行业的行业综述报告,对所有开通了对应行业权限的客户都推送。
  1047. if articleInfo.ReportType == 1 && chartPermissionId != utils.CE_LUE_ID && chartPermissionId != utils.GU_SHOU_ID_CHART {
  1048. for _, item := range openidPowerList {
  1049. first := "您关注的赛道,有新的报告发布/更新,欢迎查看"
  1050. keyword1 := "【" + articleInfo.SubCategoryName + "】有报告发布/更新"
  1051. openIditem := new(models.OpenIdList)
  1052. openIditem.OpenId = item.OpenId
  1053. openIditem.UserId = item.UserId
  1054. SendWxMsgWithArticleUpdate(first, keyword1, articleInfo.Title, articleInfo.PublishDate, articleInfo.Abstract, openIditem, articleId)
  1055. }
  1056. return err
  1057. }
  1058. for _, v := range openidPowerList {
  1059. mapOpenidPower[v.UserId] = v.OpenId
  1060. }
  1061. //获取关注这个文章对应产业的用户的 openid
  1062. mapOpenidFllow := make(map[int]string)
  1063. if chartPermissionId != utils.CE_LUE_ID && chartPermissionId != utils.GU_SHOU_ID_CHART {
  1064. openidFllowListMobile, err := models.GetCygxUserFllowOpenid(articleId)
  1065. if err != nil {
  1066. return err
  1067. }
  1068. var mobileArrFllow []string
  1069. for _, v := range openidFllowListMobile {
  1070. mobileArrFllow = append(mobileArrFllow, v.Mobile)
  1071. }
  1072. openidFllowList, err := models.GetWxOpenIdByMobileSliceList(mobileArrFllow)
  1073. if err != nil {
  1074. return err
  1075. }
  1076. for _, v := range openidFllowList {
  1077. mapOpenidFllow[v.UserId] = v.OpenId
  1078. }
  1079. }
  1080. //获取拒绝接收推送的的用户的 openid
  1081. mapOpenidRefuset := make(map[int]string)
  1082. openidRefusetList, err := models.GetCygxUserRefusetOpenid()
  1083. if err != nil {
  1084. return err
  1085. }
  1086. for _, v := range openidRefusetList {
  1087. mapOpenidRefuset[v.UserId] = v.OpenId
  1088. }
  1089. //获取提交过推送规则的用户的 openid
  1090. mapUserIdChooseSend := make(map[int]int)
  1091. mapUserIdChooseSendTypeNoPermission := make(map[int]int)
  1092. chooseSendtList, err := models.GetCygxXzsChooseSend()
  1093. for _, v := range chooseSendtList {
  1094. mapUserIdChooseSend[v.UserId] = v.UserId
  1095. if v.IsRefuse == 1 {
  1096. continue
  1097. }
  1098. //获取选择主客观赛道的用户
  1099. if reportMapDetail.PermissionType == 1 {
  1100. if v.IsSubjective == 1 {
  1101. mapUserIdChooseSendTypeNoPermission[v.UserId] = v.UserId
  1102. }
  1103. } else {
  1104. if v.IsObjective == 1 {
  1105. mapUserIdChooseSendTypeNoPermission[v.UserId] = v.UserId
  1106. }
  1107. }
  1108. }
  1109. //已经推送了的openid
  1110. mapOpenidPushed := make(map[int]string)
  1111. keyword2 := articleInfo.Title
  1112. keyword3 := time.Now().Format(utils.FormatDateTime)
  1113. keyword4 := articleInfo.Abstract
  1114. mapAlreadySend := make(map[int]int)
  1115. //如果是策略就按照策略的推送方式来写
  1116. if reportMapDetail.ChartPermissionName == utils.CE_LUE_NAME || reportMapDetail.ChartPermissionName == utils.GU_SHOU_NAME {
  1117. //获取提交过推送规则的用户的 openid
  1118. mapUserFllowCeLue := make(map[int]string)
  1119. userFllowCeLueList, err := models.GetCygxUserFllowCeLueOpenid(articleInfo.CategoryId)
  1120. if err != nil {
  1121. return err
  1122. }
  1123. for _, v := range userFllowCeLueList {
  1124. mapUserFllowCeLue[v.UserId] = v.OpenId
  1125. }
  1126. for k, v := range mapOpenidPower {
  1127. if mapOpenidPushed[k] != "" {
  1128. continue
  1129. }
  1130. openIditem := new(models.OpenIdList)
  1131. openIditem.UserId = k
  1132. openIditem.OpenId = v
  1133. first := "您关注的赛道,有新的报告发布/更新,欢迎查看"
  1134. keyword1 := "【" + articleInfo.SubCategoryName + "】有报告发布/更新"
  1135. mapActivityUserId := make(map[int]string)
  1136. if articleInfo.ReportType == 2 {
  1137. sliceSubjectId, _ := models.GetSubjectIds(articleId)
  1138. if sliceSubjectId != "" {
  1139. appointmentList, err := models.GetCygxAppointmentSummaryBySubjectId(sliceSubjectId)
  1140. if err != nil {
  1141. fmt.Println("GetCygxAppointmentSummaryListBySubjectId Err:", err.Error())
  1142. return err
  1143. }
  1144. var userIds []string
  1145. if len(appointmentList) > 0 {
  1146. for _, v := range appointmentList {
  1147. //fmt.Println("mapActivityUserId:", v.UserId)
  1148. userIds = append(userIds, strconv.Itoa(v.UserId))
  1149. mapActivityUserId[v.UserId] += v.ActivityName + ","
  1150. }
  1151. userIdstr := strings.Join(userIds, ",")
  1152. openIdLists, err := models.GetOpenIdByUserIds(userIdstr)
  1153. if err != nil {
  1154. fmt.Println("GetOpenIdByUserIds Err:", err.Error())
  1155. return err
  1156. }
  1157. for _, item := range openIdLists {
  1158. first := "您预约的调研,有关联的纪要发布/更新了"
  1159. keyword1 := industryName
  1160. openIditem := new(models.OpenIdList)
  1161. openIditem.OpenId = item.OpenId
  1162. openIditem.UserId = item.UserId
  1163. if _, ok := mapAlreadySend[openIditem.UserId]; !ok {
  1164. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1165. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1166. fmt.Println("AlreadySend:", openIditem.UserId)
  1167. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1168. }
  1169. }
  1170. }
  1171. }
  1172. //fmt.Println("openIditem.UserId:", openIditem.UserId)
  1173. //if _,ok := mapActivityUserId[openIditem.UserId]; ok{
  1174. // first = "您预约的调研,有关联的纪要发布/更新了"
  1175. // keyword1 = industryName
  1176. // SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1177. // delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1178. // mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1179. //}
  1180. }
  1181. _, ok := mapAlreadySend[openIditem.UserId]
  1182. //如果有权限而且小助手没有提交过信息的 就做正常推送
  1183. if mapUserIdChooseSend[openIditem.UserId] == 0 && !ok {
  1184. fmt.Println("1:", articleId)
  1185. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1186. }
  1187. //如果小助手勾选了对应的权限信息 就做正常推送
  1188. if mapUserIdChooseSend[openIditem.UserId] != 0 && mapUserFllowCeLue[openIditem.UserId] != "" && !ok {
  1189. fmt.Println("2:", articleId)
  1190. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1191. }
  1192. }
  1193. } else {
  1194. for k, v := range mapOpenidPower {
  1195. if mapOpenidPushed[k] != "" {
  1196. continue
  1197. }
  1198. mapUserIdChooseSendType := make(map[int]int)
  1199. for _, vS := range chooseSendtList {
  1200. if vS.IsRefuse == 1 {
  1201. continue
  1202. }
  1203. //获取选择主客观赛道的用户
  1204. if reportMapDetail.PermissionType == 1 {
  1205. if vS.IsSubjective == 1 {
  1206. mapUserIdChooseSendType[vS.UserId] = vS.UserId
  1207. }
  1208. } else if reportMapDetail.PermissionType == 2 {
  1209. if vS.IsObjective == 1 {
  1210. mapUserIdChooseSendType[vS.UserId] = vS.UserId
  1211. }
  1212. } else {
  1213. if vS.IsSubjective == 1 || vS.IsObjective == 1 {
  1214. mapUserIdChooseSendType[vS.UserId] = vS.UserId
  1215. }
  1216. }
  1217. }
  1218. openIditem := new(models.OpenIdList)
  1219. openIditem.UserId = k
  1220. openIditem.OpenId = v
  1221. first := "您关注的赛道,有新的报告发布/更新,欢迎查看"
  1222. keyword1 := "【" + articleInfo.SubCategoryName + "】有报告发布/更新"
  1223. mapActivityUserId := make(map[int]string)
  1224. if articleInfo.ReportType == 2 {
  1225. keyword1 = fmt.Sprint("【", industryName, "】有报告发布/更新")
  1226. sliceSubjectId, _ := models.GetSubjectIds(articleId)
  1227. if sliceSubjectId != "" {
  1228. appointmentList, err := models.GetCygxAppointmentSummaryBySubjectId(sliceSubjectId)
  1229. if err != nil {
  1230. fmt.Println("GetCygxAppointmentSummaryListBySubjectId Err:", err.Error())
  1231. return err
  1232. }
  1233. var userIds []string
  1234. if len(appointmentList) > 0 {
  1235. for _, v := range appointmentList {
  1236. //fmt.Println("mapActivityUserId:", v.UserId)
  1237. userIds = append(userIds, strconv.Itoa(v.UserId))
  1238. mapActivityUserId[v.UserId] += v.ActivityName + ","
  1239. }
  1240. userIdstr := strings.Join(userIds, ",")
  1241. openIdLists, err := models.GetOpenIdByUserIds(userIdstr)
  1242. if err != nil {
  1243. fmt.Println("GetOpenIdByUserIds Err:", err.Error())
  1244. return err
  1245. }
  1246. for _, item := range openIdLists {
  1247. //first := "您预约的调研,有关联的纪要发布/更新了"
  1248. //keyword1 := industryName
  1249. openIditem := new(models.OpenIdList)
  1250. openIditem.OpenId = item.OpenId
  1251. openIditem.UserId = item.UserId
  1252. if _, ok := mapAlreadySend[openIditem.UserId]; !ok {
  1253. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1254. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1255. fmt.Println("AlreadySend:", openIditem.UserId)
  1256. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. //如果有权限而且小助手没有提交过信息的 就做正常推送
  1263. _, ok := mapAlreadySend[openIditem.UserId]
  1264. if mapUserIdChooseSend[openIditem.UserId] == 0 && !ok {
  1265. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1266. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1267. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1268. }
  1269. //如果小助手勾选了对应的权限信息 就做正常推送
  1270. if mapUserIdChooseSend[openIditem.UserId] != 0 && mapOpenidFllow[openIditem.UserId] != "" && mapUserIdChooseSendType[openIditem.UserId] > 0 && !ok {
  1271. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1272. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1273. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1274. }
  1275. }
  1276. }
  1277. utils.Rc.Put(cacheKey, articleId, time.Hour*12)
  1278. return
  1279. }
  1280. // GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
  1281. func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
  1282. userType, _, e := GetUserType(user.CompanyId)
  1283. if e != nil {
  1284. err = errors.New("GetSpecialUserType, Err: " + e.Error())
  1285. return
  1286. }
  1287. // 永续客户、大套餐客户可以查看行业升级套餐客户 权限
  1288. if userType == 1 || userType == 2 {
  1289. havePower = true
  1290. return
  1291. }
  1292. permissionStr, e := GetCompanyPermissionUpgrade(user.CompanyId)
  1293. if e != nil {
  1294. err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
  1295. return
  1296. }
  1297. reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  1298. if e != nil {
  1299. err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
  1300. return
  1301. }
  1302. if reportMapDetail == nil {
  1303. err = errors.New("GetdetailByCategoryIdP,获取详情失败, ")
  1304. return
  1305. }
  1306. fmt.Println(permissionStr)
  1307. //如果没有对应的升级权限,则返回
  1308. if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
  1309. return
  1310. } else {
  1311. havePower = true
  1312. }
  1313. return
  1314. }
  1315. // HandleArticleCategoryImg 预处理文章的封面图片
  1316. func HandleArticleCategoryImg(list []*models.HomeArticle) (items []*models.HomeArticle, err error) {
  1317. //研选的五张图片
  1318. detailResearch, e := models.GetConfigByCode("category_research_img_url")
  1319. if e != nil {
  1320. err = errors.New("获取研选的五张图片失败" + e.Error())
  1321. return
  1322. }
  1323. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  1324. //对应分类的所图片
  1325. detailCategoryUrl, err := models.GetConfigByCode("category_map_img_url")
  1326. if err != nil {
  1327. err = errors.New("获取对应分类的所图片失败" + err.Error())
  1328. return
  1329. }
  1330. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  1331. mapCategoryUrl := make(map[string]string)
  1332. var categoryId string
  1333. var imgUrlChart string
  1334. for _, v := range categoryUrlList {
  1335. vslice := strings.Split(v, "_")
  1336. categoryId = vslice[0]
  1337. imgUrlChart = vslice[len(vslice)-1]
  1338. mapCategoryUrl[categoryId] = imgUrlChart
  1339. }
  1340. mapChartPerssion := make(map[string]string)
  1341. reportMappingList, err := models.GetReportMappingStrategyAll()
  1342. if err != nil {
  1343. err = errors.New("GetReportMappingStrategyAll err" + err.Error())
  1344. return
  1345. }
  1346. for _, v := range reportMappingList {
  1347. mapChartPerssion[strconv.Itoa(v.CategoryId)] = v.ChartPermissionName
  1348. }
  1349. for k, v := range list {
  1350. list[k].Abstract, _ = GetReportContentTextSub(v.Abstract)
  1351. item := list[k]
  1352. //如果文章一开始的内容是图片,优先展示第一张图片
  1353. if list[k].Annotation == "" {
  1354. imgurl, _ := FixArticleImgUrl(html.UnescapeString(list[k].Body))
  1355. if imgurl != "" {
  1356. list[k].BodyHtml = imgurl
  1357. }
  1358. }
  1359. //newBody, _ := GetReportContentTextSubByarticle(item.Body, item.Annotation, item.ArticleId)
  1360. list[k].Annotation = ArticleAnnotation(item)
  1361. list[k].Body = ""
  1362. list[k].PublishDate = utils.StrTimeToTime(item.PublishDate).Format(utils.FormatDate) //时间字符串格式转时间格式
  1363. list[k].ChartPermissionName = mapChartPerssion[v.CategoryId]
  1364. //如果是研选系列的任意取五张图片的中的一张
  1365. if v.CategoryId == "0" || v.ArticleId > utils.SummaryArticleId {
  1366. knum := v.ArticleId % 5
  1367. list[k].ImgUrlPc = researchList[knum]
  1368. } else {
  1369. list[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  1370. }
  1371. list[k].ArticleResponse = 4 // 默认展示核心观点
  1372. //ArticleResponse int `description:"报告类型 0:啥也不是,1研选报告,2:研选纪要,3:研选沙龙,4;核心观点"`
  1373. if list[k].ArticleId >= utils.SummaryArticleId {
  1374. list[k].HttpUrl = utils.StrategyPlatform + strconv.Itoa(v.ArticleId)
  1375. list[k].IsNeedJump = true
  1376. list[k].ArticleResponse = 1
  1377. //if v.IsReport == 1 {
  1378. // list[k].ArticleResponse = 1
  1379. //} else if v.ArticleTypeName == "纪要" {
  1380. // list[k].ArticleResponse = 2
  1381. //} else if v.ArticleTypeName == "观点" {
  1382. // list[k].ArticleResponse = 3
  1383. //} else if v.ArticleTypeName == "沙龙" {
  1384. // list[k].ArticleResponse = 3
  1385. //} else if v.ArticleTypeName == "路演精华" {
  1386. // list[k].ArticleResponse = 1
  1387. //}
  1388. }
  1389. list[k].Source = 1
  1390. //添加行业默认图片
  1391. if v.ImgUrlPc == "" {
  1392. if v.ChartPermissionName == utils.YI_YAO_NAME {
  1393. list[k].ImgUrlPc = utils.YI_YAO_OTHER_IMG
  1394. } else if v.ChartPermissionName == utils.XIAO_FEI_NAME {
  1395. list[k].ImgUrlPc = utils.XIAO_FEI_OTHER_IMG
  1396. } else if v.ChartPermissionName == utils.KE_JI_NAME {
  1397. list[k].ImgUrlPc = utils.KE_JI_OTHER_IMG
  1398. } else if v.ChartPermissionName == utils.ZHI_ZAO_NAME {
  1399. list[k].ImgUrlPc = utils.ZHI_ZAO_OTHER_IMG
  1400. }
  1401. }
  1402. //是不是研选报告
  1403. if v.ArticleTypeId > 0 {
  1404. list[k].IsResearch = true
  1405. }
  1406. }
  1407. articleIds := make([]int, 0)
  1408. for i := range list {
  1409. articleIds = append(articleIds, list[i].ArticleId)
  1410. }
  1411. articleMapPv := GetArticleHistoryByArticleId(articleIds) //文章Pv
  1412. // 报告关联产业信息
  1413. industryMap := make(map[int][]*models.IndustrialManagementIdInt, 0)
  1414. if len(articleIds) > 0 {
  1415. var industryCond string
  1416. var industryPars []interface{}
  1417. industryCond += ` AND mg.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  1418. industryPars = append(industryPars, articleIds)
  1419. industryList, e := models.GetIndustrialListByarticleId(industryPars, industryCond)
  1420. if e != nil {
  1421. err = errors.New("GetIndustrialListByarticleId" + e.Error())
  1422. return
  1423. }
  1424. for i := range industryList {
  1425. v := industryList[i]
  1426. industryMap[v.ArticleId] = append(industryMap[v.ArticleId], &models.IndustrialManagementIdInt{
  1427. ArticleId: v.ArticleId,
  1428. IndustrialManagementId: v.IndustrialManagementId,
  1429. IndustryName: v.IndustryName,
  1430. ChartPermissionId: v.ChartPermissionId,
  1431. })
  1432. }
  1433. }
  1434. for k, v := range list {
  1435. if len(industryMap[v.ArticleId]) > 0 {
  1436. list[k].List = industryMap[v.ArticleId]
  1437. } else {
  1438. list[k].List = make([]*models.IndustrialManagementIdInt, 0)
  1439. }
  1440. v.Pv = articleMapPv[v.ArticleId]
  1441. if v.Pv > 999 {
  1442. list[k].Pv = 999
  1443. }
  1444. }
  1445. if len(list) == 0 {
  1446. list = make([]*models.HomeArticle, 0)
  1447. }
  1448. items = list
  1449. return
  1450. }
  1451. // 处理核心观点的展示规则
  1452. func ArticleAnnotation(item *models.HomeArticle) (annotation string) {
  1453. if item.ArticleId >= utils.SummaryArticleId {
  1454. item.Annotation = YxArticleAnnotation(item)
  1455. }
  1456. if item.Annotation != "" {
  1457. annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  1458. annotation = strings.Replace(item.Annotation, "&nbsp;", "", -1)
  1459. } else {
  1460. return
  1461. }
  1462. bodyText, _ := GetReportContentTextSubNew(annotation)
  1463. if bodyText == "" {
  1464. return
  1465. }
  1466. if annotation != "" {
  1467. annotation = html.UnescapeString(annotation)
  1468. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  1469. docText := doc.Text()
  1470. mapDoc := make(map[int]string)
  1471. var mapSort []int
  1472. p := doc.Find("p")
  1473. p.Each(func(tk int, pd *goquery.Selection) {
  1474. pdText := pd.Text()
  1475. pdText = strings.Replace(pdText, " ", "", -1)
  1476. if pdText != "" {
  1477. textLen := strings.Index(docText, pdText)
  1478. mapDoc[(strings.Index(docText, pdText))] = pdText
  1479. mapSort = append(mapSort, textLen)
  1480. }
  1481. })
  1482. li := doc.Find("li")
  1483. li.Each(func(tk int, li *goquery.Selection) {
  1484. liText := li.Text()
  1485. liText = strings.Replace(liText, " ", "", -1)
  1486. if liText != "" {
  1487. textLen := strings.Index(docText, liText)
  1488. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  1489. mapSort = append(mapSort, textLen)
  1490. }
  1491. })
  1492. ul := doc.Find("ul")
  1493. ul.Each(func(tk int, ul *goquery.Selection) {
  1494. ulText := ul.Text()
  1495. ulText = strings.Replace(ulText, " ", "", -1)
  1496. if ulText != "" {
  1497. textLen := strings.Index(docText, ulText)
  1498. mapDoc[(strings.Index(docText, ulText))] = ulText
  1499. mapSort = append(mapSort, textLen)
  1500. }
  1501. })
  1502. if len(mapSort) == 0 {
  1503. return
  1504. } else {
  1505. //排序
  1506. sort.Ints(mapSort)
  1507. var annotationHtml string
  1508. for _, vSort := range mapSort {
  1509. for k, v := range mapDoc {
  1510. if k == vSort && v != "" {
  1511. annotationHtml += v + "<br>"
  1512. }
  1513. }
  1514. }
  1515. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  1516. annotationHtml = "<p>" + annotationHtml + "</p>"
  1517. annotation = annotationHtml
  1518. }
  1519. }
  1520. return
  1521. }
  1522. // 处理核心观点的展示规则
  1523. func AnnotationHtml(bodyText string) (annotation string) {
  1524. if bodyText == "" {
  1525. return
  1526. }
  1527. annotation = bodyText
  1528. annotation = html.UnescapeString(annotation)
  1529. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  1530. docText := doc.Text()
  1531. mapDoc := make(map[int]string)
  1532. var mapSort []int
  1533. p := doc.Find("p")
  1534. p.Each(func(tk int, pd *goquery.Selection) {
  1535. pdText := pd.Text()
  1536. //pdText = strings.Replace(pdText, " ", "", -1)
  1537. if pdText != "" {
  1538. textLen := strings.Index(docText, pdText)
  1539. mapDoc[(strings.Index(docText, pdText))] = pdText
  1540. mapSort = append(mapSort, textLen)
  1541. }
  1542. })
  1543. li := doc.Find("li")
  1544. li.Each(func(tk int, li *goquery.Selection) {
  1545. liText := li.Text()
  1546. liText = strings.Replace(liText, " ", "", -1)
  1547. if liText != "" {
  1548. textLen := strings.Index(docText, liText)
  1549. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  1550. mapSort = append(mapSort, textLen)
  1551. }
  1552. })
  1553. ul := doc.Find("ul")
  1554. ul.Each(func(tk int, ul *goquery.Selection) {
  1555. ulText := ul.Text()
  1556. ulText = strings.Replace(ulText, " ", "", -1)
  1557. if ulText != "" {
  1558. textLen := strings.Index(docText, ulText)
  1559. mapDoc[(strings.Index(docText, ulText))] = ulText
  1560. mapSort = append(mapSort, textLen)
  1561. }
  1562. })
  1563. section := doc.Find("section")
  1564. section.Each(func(tk int, section *goquery.Selection) {
  1565. sectionText := section.Text()
  1566. sectionText = strings.Replace(sectionText, " ", "", -1)
  1567. if sectionText != "" {
  1568. textLen := strings.Index(docText, sectionText)
  1569. mapDoc[(strings.Index(docText, sectionText))] = sectionText
  1570. mapSort = append(mapSort, textLen)
  1571. }
  1572. })
  1573. if len(mapSort) == 0 {
  1574. return
  1575. } else {
  1576. //排序
  1577. sort.Ints(mapSort)
  1578. var annotationHtml string
  1579. for _, vSort := range mapSort {
  1580. for k, v := range mapDoc {
  1581. if k == vSort && v != "" {
  1582. annotationHtml += v + "<br>"
  1583. }
  1584. }
  1585. }
  1586. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  1587. annotationHtml = "<p>" + annotationHtml + "</p>"
  1588. annotation = annotationHtml
  1589. }
  1590. return
  1591. }
  1592. // 处理产品内测展示规则
  1593. func ProductInteriorHtml(bodyText string) (annotation string) {
  1594. if bodyText == "" {
  1595. return
  1596. }
  1597. sliceBody := strings.Split(bodyText, "</p>")
  1598. annotation, _ = GetReportContentTextSub(sliceBody[0])
  1599. return
  1600. }
  1601. // 解析研选内容中的核心观点
  1602. func YxArticleAnnotation(article *models.HomeArticle) (annotation string) {
  1603. //如果不规范,就获取内容主体
  1604. if strings.Count(article.Body, "<hr") == 0 {
  1605. //如果内容不规范而且,还有图片,就把核心观点置空
  1606. if article.BodyHtml != "" {
  1607. return
  1608. }
  1609. annotation, _ = GetReportContentTextSub(article.Body)
  1610. return
  1611. }
  1612. body := strings.ReplaceAll(article.Body, "<strong>", "")
  1613. body = strings.ReplaceAll(body, "</strong>", "")
  1614. body = strings.ReplaceAll(body, "</ol>", "</div>")
  1615. body = strings.ReplaceAll(body, "<ol>", "<div>")
  1616. body = strings.ReplaceAll(body, "</li>", "</p>")
  1617. body = strings.ReplaceAll(body, "<li>", "<p>")
  1618. re, _ := regexp.Compile("<strong.*?>")
  1619. body = re.ReplaceAllString(body, "")
  1620. reLi, _ := regexp.Compile("<li.*?>")
  1621. body = reLi.ReplaceAllString(body, "")
  1622. var plus int
  1623. coreIndex := strings.Index(body, "核心观点:")
  1624. plus = 15
  1625. if coreIndex == -1 {
  1626. coreIndex = strings.Index(body, "核心观点:")
  1627. plus = 13
  1628. }
  1629. if coreIndex == -1 {
  1630. coreIndex = strings.Index(body, "核心观点")
  1631. plus = 12
  1632. }
  1633. if coreIndex == -1 {
  1634. coreIndex = strings.Index(body, "核心结论:")
  1635. plus = 15
  1636. }
  1637. if coreIndex == -1 {
  1638. coreIndex = strings.Index(body, "核心结论:")
  1639. plus = 13
  1640. }
  1641. if coreIndex == -1 {
  1642. coreIndex = strings.Index(body, "核心结论")
  1643. plus = 12
  1644. }
  1645. endIndex := strings.Index(body, "<hr")
  1646. if coreIndex != -1 && endIndex != -1 {
  1647. body = body[coreIndex+plus : endIndex]
  1648. }
  1649. annotation = body
  1650. return
  1651. }
  1652. // 通过接解析带有Md5的文章链接获取文章ID
  1653. func GetReportLinkToArticleid(reportLink string) (articleId int, err error) {
  1654. defer func() {
  1655. if err != nil {
  1656. go utils.SendAlarmMsg("通过接解析带有Md5的文章链接获取文章ID失败"+err.Error(), 2)
  1657. }
  1658. }()
  1659. var artMd5 string
  1660. //处理Md5的
  1661. strnum1 := strings.Index(reportLink, "id=")
  1662. if strnum1 > 0 {
  1663. sliceId := strings.Split(reportLink, "id=")
  1664. if len(sliceId) > 1 {
  1665. reportLink = sliceId[1]
  1666. sliceMd5Id := strings.Split(reportLink, "&")
  1667. artMd5 = sliceMd5Id[0]
  1668. }
  1669. if artMd5 != "" {
  1670. detail, errArt := models.GetArticleDetailByIdMd5(artMd5)
  1671. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  1672. err = errArt
  1673. return
  1674. }
  1675. if detail != nil {
  1676. articleId = detail.ArticleId
  1677. }
  1678. }
  1679. } else {
  1680. //处理活动的
  1681. linkList := strings.Split(reportLink, "/")
  1682. if linkList[len(linkList)-1] != "" {
  1683. linkArticleId, _ := strconv.Atoi(linkList[len(linkList)-1])
  1684. if linkArticleId > 0 {
  1685. articleInfo, errArt := models.GetArticleDetailById(linkArticleId)
  1686. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  1687. err = errArt
  1688. return
  1689. }
  1690. if articleInfo != nil {
  1691. articleId = articleInfo.ArticleId
  1692. }
  1693. }
  1694. }
  1695. }
  1696. return
  1697. }
  1698. // GetArticleStockMap 获取个股标签所对应的文章ID
  1699. func GetArticleStockMap() (mapResp map[string]int, err error) {
  1700. defer func() {
  1701. if err != nil {
  1702. go utils.SendAlarmMsg("获取个股标签所对应的文章ID失败"+err.Error(), 2)
  1703. }
  1704. }()
  1705. list, err := models.GetArticleStock()
  1706. if err != nil && err.Error() != utils.ErrNoRow() {
  1707. return
  1708. }
  1709. mapResp = make(map[string]int, 0)
  1710. if len(list) > 0 {
  1711. //一对一精准匹配
  1712. for _, v := range list {
  1713. sliceSubjects := strings.Split(v.Stock, "/")
  1714. if len(sliceSubjects) > 0 {
  1715. for _, vSubject := range sliceSubjects {
  1716. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  1717. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  1718. subject := sliceXiahuaxian[0]
  1719. mapResp[subject] = v.ArticleId
  1720. }
  1721. }
  1722. }
  1723. }
  1724. return
  1725. }
  1726. // GetAiQianYanArtilceList 获取AI前沿几篇文章
  1727. func GetAiQianYanArtilceList(startSize, pageSize int) (items []*models.ReportArticle, total int, err error) {
  1728. defer func() {
  1729. if err != nil {
  1730. go utils.SendAlarmMsg("获取AI前沿几篇文章失败"+err.Error(), 2)
  1731. }
  1732. }()
  1733. var condition string
  1734. var pars []interface{}
  1735. condition += ` AND title LIKE '%AI前沿%' AND publish_status = 1 ORDER BY publish_date DESC `
  1736. articleList, e := models.GetCygxCygxArticleList(condition, pars, startSize, pageSize)
  1737. if e != nil {
  1738. err = errors.New("GetCygxCygxArticleList, Err: " + e.Error())
  1739. return
  1740. }
  1741. total, e = models.GetCygxArticleCount(condition, pars)
  1742. if e != nil {
  1743. err = errors.New("GetCygxArticleCount, Err: " + e.Error())
  1744. return
  1745. }
  1746. for _, v := range articleList {
  1747. item := new(models.ReportArticle)
  1748. item.ArticleId = v.ArticleId
  1749. item.Title = v.Title
  1750. item.Abstract = v.Abstract
  1751. item.Annotation = v.Annotation
  1752. item.PublishDate = v.PublishDate
  1753. item.Resource = 1
  1754. item.CategoryId = strconv.Itoa(v.CategoryId)
  1755. item.Body = v.Body
  1756. items = append(items, item)
  1757. }
  1758. return
  1759. }
  1760. // 获取文章内部图表的第一张封面图
  1761. func GetArticleCoverByChartFirst(body string) (cover string) {
  1762. var err error
  1763. defer func() {
  1764. if err != nil {
  1765. fmt.Println(err)
  1766. go utils.SendAlarmMsg("获取文章内部图表的第一张封面图失败,Err:"+err.Error()+body, 3)
  1767. }
  1768. }()
  1769. var chartUrl string
  1770. var hrefRegexp = regexp.MustCompile(utils.RegularUrl)
  1771. match := hrefRegexp.FindAllString(body, -1)
  1772. if match != nil {
  1773. for _, v := range match {
  1774. if strings.Contains(v, "charts/display") {
  1775. chartUrl = v
  1776. break
  1777. }
  1778. }
  1779. }
  1780. if chartUrl != "" {
  1781. urlSlice := strings.Split(chartUrl, "/")
  1782. lenurlSlice := len(urlSlice)
  1783. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  1784. if sourceId == 0 {
  1785. sourceId, _ = strconv.Atoi(urlSlice[lenurlSlice-2])
  1786. }
  1787. if sourceId == 0 {
  1788. err = errors.New("新的图标链接无法解析" + chartUrl)
  1789. return
  1790. }
  1791. chartUrlApi := utils.ApiUrl + "charts/" + strconv.Itoa(sourceId)
  1792. authorization := utils.ApiAuthorization
  1793. body, e := PublicGetDate(chartUrlApi, authorization)
  1794. if e != nil {
  1795. err = errors.New("PublicGetDate" + err.Error())
  1796. return
  1797. }
  1798. var resultDate models.ChartResultDetailApi
  1799. e = json.Unmarshal(body, &resultDate)
  1800. if e != nil {
  1801. err = errors.New(" json.Unmarshal" + err.Error())
  1802. }
  1803. cover = resultDate.Data.Cover
  1804. }
  1805. return
  1806. }
  1807. // GetYxArticleIdMap 获取研选文章ID
  1808. func GetYxArticleIdMap(articleIds []int) (mapResp map[int]bool) {
  1809. var err error
  1810. defer func() {
  1811. if err != nil {
  1812. go utils.SendAlarmMsg("获取研选文章ID失败,GetYxArticleIdMap"+err.Error(), 2)
  1813. }
  1814. }()
  1815. var condition string
  1816. var pars []interface{}
  1817. condition = ` AND article_type_id > 0 `
  1818. if len(articleIds) > 0 {
  1819. condition += ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  1820. pars = append(pars, articleIds)
  1821. }
  1822. articleList, e := models.GetArticleList(condition, pars)
  1823. if e != nil {
  1824. err = errors.New("GetArticleList, Err: " + e.Error())
  1825. return
  1826. }
  1827. mapResp = make(map[int]bool, 0)
  1828. for _, v := range articleList {
  1829. mapResp[v.ArticleId] = true
  1830. }
  1831. return
  1832. }