article.go 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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. var isCustom bool
  659. updateParams := make(map[string]interface{})
  660. //updateParams["Title"] = v.Title
  661. updateParams["Title"] = titleNew
  662. updateParams["TitleEn"] = v.TitleEn
  663. updateParams["UpdateFrequency"] = v.UpdateFrequency
  664. updateParams["CreateDate"] = v.CreateDate
  665. updateParams["PublishDate"] = v.PublishDate
  666. //updateParams["Body"] = html.EscapeString(v.Body)
  667. updateParams["Body"] = html.EscapeString(bodyReturn)
  668. updateParams["BodyText"] = bodyText
  669. updateParams["Abstract"] = html.EscapeString(v.Abstract)
  670. updateParams["CategoryName"] = v.CategoryName
  671. for _, vCustom := range listCustomArticle {
  672. if v.ArticleId == vCustom.ArticleId {
  673. fmt.Println("手动归类的文章:" + strconv.Itoa(v.ArticleId))
  674. isCustom = true
  675. }
  676. }
  677. if isCustom == false {
  678. updateParams["CategoryId"] = v.CategoryId
  679. updateParams["MatchTypeName"] = matchTypeName
  680. updateParams["IsSummary"] = v.IsSummary
  681. updateParams["IsReport"] = v.IsReport
  682. updateParams["ReportType"] = v.ReportType
  683. updateParams["SubCategoryName"] = v.SubCategoryName
  684. }
  685. //updateParams["CategoryId"] = v.CategoryId
  686. updateParams["PublishStatus"] = 1
  687. updateParams["ExpertBackground"] = expertContentStr
  688. updateParams["ExpertNumber"] = expertNumStr
  689. updateParams["InterviewDate"] = interviewDateStr
  690. //updateParams["IsClass"] = v.IsClass
  691. v.Department = "弘则权益研究"
  692. updateParams["Department"] = v.Department
  693. updateParams["FileLink"] = v.File
  694. updateParams["Stock"] = v.Stock
  695. updateParams["FieldName"] = v.FieldName
  696. updateParams["SeriesName"] = v.SeriesName
  697. updateParams["Annotation"] = v.Annotation
  698. updateParams["TypeName"] = v.TypeName
  699. updateParams["ModifyTimeByCl"] = v.UpdateDate
  700. updateParams["CeLueFieldId"] = v.CeLueFieldId
  701. updateParams["Cover"] = v.Cover
  702. updateParams["ChartPermissionId"] = chartPermissionId
  703. updateParams["ChartPermissionName"] = chartPermissionName
  704. whereParam := map[string]interface{}{"article_id": v.ArticleId}
  705. err = models.UpdateByExpr(models.CygxArticle{}, whereParam, updateParams)
  706. if err != nil {
  707. fmt.Println("UpdateByExpr Err:" + err.Error())
  708. return err
  709. }
  710. if len(bodyText)-len(bodyTextOld) > 100 {
  711. //fmt.Println("触发推送规则")
  712. //更新字数大于一百字触发推送规则
  713. go DoArticleOnenIdWxTemplateMsg(v.ArticleId)
  714. UpdateArticleResourceData(v.ArticleId) //把数据写入 cygx_resource_data 表
  715. //go UpdateResourceData(v.ArticleId, "article", "update", time.Now().Format(utils.FormatDateTime))
  716. } else {
  717. models.UpdatecygxResourceDatasearchTitle(v.Title, v.ArticleId)
  718. }
  719. } else {
  720. fmt.Println(k, v.ArticleId, "add")
  721. item := new(models.CygxArticle)
  722. articleIdInt := v.ArticleId
  723. item.ArticleId = articleIdInt
  724. //item.Title = v.Title
  725. item.Title = titleNew
  726. item.TitleEn = v.TitleEn
  727. item.UpdateFrequency = v.UpdateFrequency
  728. item.CreateDate = v.CreateDate
  729. item.PublishDate = v.PublishDate.Format(utils.FormatDateTime)
  730. //item.Body = html.EscapeString(v.Body)
  731. item.Body = html.EscapeString(bodyReturn)
  732. item.Abstract = html.EscapeString(v.Abstract)
  733. item.CategoryName = v.CategoryName
  734. item.SubCategoryName = v.SubCategoryName
  735. item.CategoryId = v.CategoryId
  736. item.CategoryIdTwo = v.CategoryIdTwo
  737. item.PublishStatus = 1
  738. item.ExpertBackground = expertContentStr
  739. item.ExpertNumber = expertNumStr
  740. item.InterviewDate = interviewDateStr
  741. item.Department = v.Department
  742. item.ArticleIdMd5 = utils.MD5(strconv.Itoa(articleIdInt))
  743. item.IsClass = v.IsClass
  744. item.IsSummary = v.IsSummary
  745. item.IsReport = v.IsReport
  746. item.ReportType = v.ReportType
  747. item.FileLink = v.File
  748. item.MatchTypeName = matchTypeName
  749. item.Stock = v.Stock
  750. item.FieldName = v.FieldName
  751. item.SeriesName = v.SeriesName
  752. item.Annotation = v.Annotation
  753. item.TypeName = v.TypeName
  754. item.ModifyTimeByCl = v.UpdateDate
  755. item.CeLueFieldId = v.CeLueFieldId
  756. item.Cover = v.Cover
  757. item.ChartPermissionId = chartPermissionId
  758. item.ChartPermissionName = chartPermissionName
  759. item.Cover = v.Cover
  760. item.CreateTime = time.Now()
  761. newId, err := models.AddCygxArticles(item)
  762. if err != nil {
  763. fmt.Println("AddCygxArticle Err:", err.Error())
  764. return err
  765. }
  766. //go UpdateResourceData(v.ArticleId, "article", "add", time.Now().Format(utils.FormatDateTime))
  767. UpdateArticleResourceData(v.ArticleId) //把数据写入 cygx_resource_data 表
  768. //fmt.Println(newId)
  769. //报告自动归类,以及推送相关模板消息
  770. if v.ReportType == 2 {
  771. var subjectStr string
  772. var industrialManagementIdStr string
  773. var industrialSubjectIdStr string
  774. var keyword1 string
  775. var keyword2 string
  776. var keyword3 string
  777. var keyword4 string
  778. sliceSubjects := strings.Split(v.Stock, "/")
  779. mapManagementForSubject := make(map[string]string)
  780. mapIndustrialId := make(map[int]int)
  781. if len(sliceSubjects) > 0 {
  782. for _, vSubject := range sliceSubjects {
  783. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  784. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  785. subject := sliceXiahuaxian[0]
  786. subjectStr += "'" + subject + "',"
  787. }
  788. //获取该产业下所对应的行业图片
  789. detailCategory, errCategory := models.GetdetailByCategoryIdOne(v.CategoryId)
  790. if errCategory != nil {
  791. fmt.Println("GetdetailByCategoryIdOne Err:", err.Error())
  792. return err
  793. }
  794. subjectStr = strings.TrimRight(subjectStr, ",")
  795. if subjectStr != "" {
  796. listIndustrial, err := models.GetIndustrialManagementForSubjecName(subjectStr, detailCategory.ChartPermissionId)
  797. if err != nil {
  798. fmt.Println("GetIndustrialManagementForSubjecName Err:", err.Error())
  799. return err
  800. }
  801. subjectStr = strings.Replace(subjectStr, "','", "】【", -1)
  802. subjectStr = strings.Replace(subjectStr, "'", "", -1)
  803. subjectStr = "【" + subjectStr + "】"
  804. if len(listIndustrial) > 0 {
  805. for _, vIndustrial := range listIndustrial {
  806. if _, ok := mapIndustrialId[vIndustrial.IndustrialManagementId]; !ok {
  807. industrialManagementIdStr += strconv.Itoa(vIndustrial.IndustrialManagementId) + ","
  808. }
  809. mapIndustrialId[vIndustrial.IndustrialManagementId] = vIndustrial.IndustrialManagementId
  810. industrialSubjectIdStr += strconv.Itoa(vIndustrial.IndustrialSubjectId) + ","
  811. mapManagementForSubject[vIndustrial.IndustryName] += vIndustrial.SubjectName + "/"
  812. if vIndustrial.LayoutTime == "0000-00-00 00:00:00" {
  813. go models.UpdateIndustrialManagementLayoutTime(v.PublishDate.AddDate(0, 0, -10).Format(utils.FormatDateTime), vIndustrial.IndustrialManagementId)
  814. }
  815. }
  816. industrialManagementIdStr = strings.TrimRight(industrialManagementIdStr, ",")
  817. industrialSubjectIdStr = strings.TrimRight(industrialSubjectIdStr, ",")
  818. if industrialManagementIdStr != "" {
  819. err = models.ReportArticleClassificationEditNew(int(newId), industrialManagementIdStr, v.ArticleId, industrialSubjectIdStr)
  820. if err != nil {
  821. fmt.Println("ReportArticleClassificationEditNew Err:", err.Error())
  822. return err
  823. }
  824. }
  825. var peoductName string
  826. for mk, mv := range mapManagementForSubject {
  827. peoductName += "【" + mk + "--" + strings.TrimRight(mv, "/") + "】"
  828. }
  829. keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
  830. keyword2 = "已自动关联至以下产业和标的:" + peoductName
  831. keyword3 = v.Title
  832. keyword4 = v.PublishDate.Format(utils.FormatDateTime)
  833. SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
  834. } else {
  835. keyword1 = "新报告产业标签:【" + v.FieldName + "】,个股标签:" + subjectStr
  836. keyword2 = "未归类"
  837. keyword3 = v.Title
  838. keyword4 = v.PublishDate.Format(utils.FormatDateTime)
  839. SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4, openIdList, articleIdInt)
  840. go models.UpdateIsClassFail(artcleId)
  841. }
  842. }
  843. }
  844. }
  845. go DoArticleOnenIdWxTemplateMsg(v.ArticleId)
  846. }
  847. if v.CategoryName == utils.ZHOU_QI_NAME {
  848. UpdateCygxZhouqiArticleMapTime(v.FieldName) //更新周期对应分类下所管理文章的更新时间
  849. }
  850. //【公司调研】系列纪要发布/更新后 end
  851. //查研观向6.9模板消息推送规则
  852. //但是如果报告的发布时间是在10天以前的,公众号就不推送了
  853. //if time.Now().AddDate(0, 0, -10).Before(articleResult.PublishDate.Add(time.Hour * 8)) {
  854. // go DoArticleOnenIdWxTemplateMsg(v.ArticleId)
  855. //}
  856. }
  857. go models.UpdateCygxArticleCeluePush(artcleId)
  858. AddComprehensiveArticle(artcleId) // 同步到综合页面的搜索引擎
  859. AddComprehensiveIndustrialSource("Hz", artcleId) // 同步到综合页面的搜索引擎(添加产业资源包)
  860. GetSummarytoEs(artcleId) // 同步到ES搜索引擎
  861. GetIndustrialManagementArticleNewPublishData()
  862. if len(listArticleData) > 0 {
  863. for _, v := range listArticleData {
  864. count, err := models.GetCygxArticleDataCount(v.ArticleId)
  865. if err != nil {
  866. fmt.Println("GetCygxArticleDataCount Err:", err.Error())
  867. return err
  868. }
  869. if count == 0 {
  870. _, err := models.AddCygxArticleData(v)
  871. if err != nil {
  872. fmt.Println("AddCygxArticleData Err:", err.Error())
  873. return err
  874. }
  875. }
  876. //} else {
  877. // updateParams := make(map[string]interface{})
  878. // updateParams["Cover"] = v.Cover
  879. // whereParam := map[string]interface{}{"article_id": v.ArticleId}
  880. // err = models.UpdateByExpr(models.CygxArticleData{}, whereParam, updateParams)
  881. // if err != nil {
  882. // fmt.Println("UpdateByExpr CygxArticleData Err:" + err.Error())
  883. // return err
  884. // }
  885. //}
  886. }
  887. }
  888. AddCygxReportMappingCategoryGroupByArticleId(artcleId)
  889. return err
  890. }
  891. //func init() {
  892. // DoArticleOnenIdWxTemplateMsg(8112)
  893. //}
  894. // 过滤策略平台报告,研选报告重复推送,以及权限勾选的推送
  895. func DoArticleOnenIdWxTemplateMsg(articleId int) (err error) {
  896. //策略平台同步过来的文章ID,做微信模板消息推送,同一篇报告12小时只推送一次
  897. cacheKey := fmt.Sprint(utils.CACHE_KEY_ARTICLE_ID_TEMPLATE, ":ArticleId", articleId)
  898. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  899. if ttlTime > 0 {
  900. fmt.Println(ttlTime)
  901. return err
  902. }
  903. var msg string
  904. defer func() {
  905. if err != nil {
  906. go utils.SendAlarmMsg("过滤策略平台报告,研选报告重复推送,以及权限勾选的推送失败 DoArticleOnenIdWxTemplateMsg ErrMsg:"+err.Error(), 2)
  907. fmt.Println(err)
  908. }
  909. if msg != "" {
  910. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  911. }
  912. }()
  913. //获取文章详情
  914. articleInfo, err := models.GetArticleDetailById(articleId)
  915. if err != nil {
  916. return
  917. }
  918. articleInfo.Abstract, _ = GetReportContentTextSub(articleInfo.Abstract)
  919. var chartPermissionId int
  920. //固收的权限,暂时当策略的逻辑一样处理
  921. var chartPermissionName string
  922. reportMapDetail, err := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  923. if err != nil {
  924. return err
  925. }
  926. if reportMapDetail.ChartPermissionId == utils.CE_LUE_ID {
  927. chartPermissionId = utils.CE_LUE_ID
  928. } else if reportMapDetail.ChartPermissionId == utils.GU_SHOU_ID_CHART {
  929. chartPermissionId = utils.GU_SHOU_ID_CHART
  930. } else {
  931. if reportMapDetail.PermissionType == 1 {
  932. chartPermissionName = reportMapDetail.ChartPermissionName + "(主观)"
  933. } else {
  934. chartPermissionName = reportMapDetail.ChartPermissionName + "(客观)"
  935. }
  936. chartPermissionDetail, err := models.GetChartPermissionByRemark(chartPermissionName)
  937. if err != nil {
  938. return err
  939. }
  940. chartPermissionId = chartPermissionDetail.ChartPermissionId
  941. }
  942. //文章关联的产业
  943. var industryName string
  944. var industrialManagementId string
  945. if chartPermissionId == utils.CE_LUE_ID || chartPermissionId == utils.GU_SHOU_ID_CHART {
  946. industryName = reportMapDetail.SubCategoryName
  947. } else {
  948. if articleInfo.ReportType == 2 {
  949. detailIndustryNameList, err := models.GetIndustrialManagemenDetailByAaticleID(articleId)
  950. if err != nil && err.Error() != utils.ErrNoRow() {
  951. return err
  952. }
  953. //如果不存在关联的产业则不推送
  954. if len(detailIndustryNameList) == 0 {
  955. return err
  956. }
  957. for _, v := range detailIndustryNameList {
  958. industryName += v.IndustryName + ","
  959. industrialManagementId += strconv.Itoa(v.IndustrialManagementId) + ","
  960. }
  961. industryName = strings.TrimRight(industryName, ",")
  962. industrialManagementId = strings.TrimRight(industrialManagementId, ",")
  963. //处理勾选了全部行业赛道的用户
  964. var allIn string
  965. allIn = DoXzsChooseSend(reportMapDetail.ChartPermissionName)
  966. if allIn != "" {
  967. allInList, err := models.GetCygxXzsChooseSendByAllIn(allIn)
  968. if err != nil && err.Error() != utils.ErrNoRow() {
  969. return err
  970. }
  971. if len(allInList) > 0 {
  972. var allInUserId string
  973. for _, v := range allInList {
  974. allInUserId += strconv.Itoa(v.UserId) + ","
  975. }
  976. allInUserId = strings.TrimRight(allInUserId, ",")
  977. userFllowIndustrialList, err := models.GetUserFllowIndustrialListByUserIdAndIndustrial(allInUserId, industrialManagementId)
  978. if err != nil && err.Error() != utils.ErrNoRow() {
  979. return err
  980. }
  981. mapUserFllowed := make(map[string]int)
  982. for _, v := range userFllowIndustrialList {
  983. //用户ID与其关注的产业组成唯一索引,判断是否存在,如果不存在就进行自动关注
  984. mapUserFllowed[fmt.Sprint("uid:", v.UserId, "_Industrial:", v.IndustrialManagementId)] = v.UserId
  985. }
  986. //获取这些allIN用户的的信息进行遍历生成需要插入的数据
  987. userList, err := models.GetWxUserListByUserIds(allInUserId)
  988. if err != nil && err.Error() != utils.ErrNoRow() {
  989. return err
  990. }
  991. var industryFllowItems []*models.CygxIndustryFllow
  992. for _, v := range detailIndustryNameList {
  993. for _, vUser := range userList {
  994. //如果用户这个用户没有关注这个产业就进行自动关注
  995. if mapUserFllowed[fmt.Sprint("uid:", vUser.UserId, "_Industrial:", v.IndustrialManagementId)] == 0 {
  996. item := new(models.CygxIndustryFllow)
  997. item.IndustrialManagementId = v.IndustrialManagementId
  998. item.UserId = vUser.UserId
  999. item.Email = vUser.Email
  1000. item.Mobile = vUser.Mobile
  1001. item.RealName = vUser.RealName
  1002. item.CompanyId = vUser.CompanyId
  1003. item.CompanyName = vUser.CompanyName
  1004. item.Source = 3
  1005. item.Type = 1
  1006. item.CreateTime = time.Now()
  1007. item.ModifyTime = time.Now()
  1008. industryFllowItems = append(industryFllowItems, item)
  1009. }
  1010. }
  1011. }
  1012. err = models.AddCygxIndustryFllowMulti(industryFllowItems)
  1013. if err != nil {
  1014. go utils.SendAlarmMsg("用户关注全部赛道,出现新赛道批量添加失败 ,AddCygxIndustryFllowMulti ErrMsg:"+err.Error(), 2)
  1015. }
  1016. }
  1017. }
  1018. }
  1019. }
  1020. // 获取所有有权的用户的 openid
  1021. mapOpenidPower := make(map[int]string)
  1022. permissionIdList := make([]string, 0)
  1023. var condition string
  1024. var pars []interface{}
  1025. slicepermissionId := strings.Split(strconv.Itoa(chartPermissionId), ",")
  1026. if len(slicepermissionId) > 0 {
  1027. for _, v := range slicepermissionId {
  1028. if v == strconv.Itoa(utils.GU_SHOU_ID_CHART) {
  1029. v = strconv.Itoa(utils.CE_LUE_ID)
  1030. }
  1031. permissionIdList = append(permissionIdList, v)
  1032. }
  1033. condition += ` AND p.chart_permission_id IN (` + utils.GetOrmInReplace(len(slicepermissionId)) + ` ) `
  1034. pars = append(pars, permissionIdList)
  1035. }
  1036. openidPowerListMobile, err := models.GetCygxUserRecordPower(condition, pars)
  1037. if err != nil {
  1038. return err
  1039. }
  1040. var mobileArr []string
  1041. for _, v := range openidPowerListMobile {
  1042. mobileArr = append(mobileArr, v.Mobile)
  1043. }
  1044. openidPowerList, err := models.GetWxOpenIdByMobileSliceList(mobileArr)
  1045. if err != nil {
  1046. return err
  1047. }
  1048. //四大行业的行业综述报告,对所有开通了对应行业权限的客户都推送。
  1049. if articleInfo.ReportType == 1 && chartPermissionId != utils.CE_LUE_ID && chartPermissionId != utils.GU_SHOU_ID_CHART {
  1050. for _, item := range openidPowerList {
  1051. first := "您关注的赛道,有新的报告发布/更新,欢迎查看"
  1052. keyword1 := "【" + articleInfo.SubCategoryName + "】有报告发布/更新"
  1053. openIditem := new(models.OpenIdList)
  1054. openIditem.OpenId = item.OpenId
  1055. openIditem.UserId = item.UserId
  1056. SendWxMsgWithArticleUpdate(first, keyword1, articleInfo.Title, articleInfo.PublishDate, articleInfo.Abstract, openIditem, articleId)
  1057. }
  1058. return err
  1059. }
  1060. for _, v := range openidPowerList {
  1061. mapOpenidPower[v.UserId] = v.OpenId
  1062. }
  1063. //获取关注这个文章对应产业的用户的 openid
  1064. mapOpenidFllow := make(map[int]string)
  1065. if chartPermissionId != utils.CE_LUE_ID && chartPermissionId != utils.GU_SHOU_ID_CHART {
  1066. openidFllowListMobile, err := models.GetCygxUserFllowOpenid(articleId)
  1067. if err != nil {
  1068. return err
  1069. }
  1070. var mobileArrFllow []string
  1071. for _, v := range openidFllowListMobile {
  1072. mobileArrFllow = append(mobileArrFllow, v.Mobile)
  1073. }
  1074. openidFllowList, err := models.GetWxOpenIdByMobileSliceList(mobileArrFllow)
  1075. if err != nil {
  1076. return err
  1077. }
  1078. for _, v := range openidFllowList {
  1079. mapOpenidFllow[v.UserId] = v.OpenId
  1080. }
  1081. }
  1082. //获取拒绝接收推送的的用户的 openid
  1083. mapOpenidRefuset := make(map[int]string)
  1084. openidRefusetList, err := models.GetCygxUserRefusetOpenid()
  1085. if err != nil {
  1086. return err
  1087. }
  1088. for _, v := range openidRefusetList {
  1089. mapOpenidRefuset[v.UserId] = v.OpenId
  1090. }
  1091. //获取提交过推送规则的用户的 openid
  1092. mapUserIdChooseSend := make(map[int]int)
  1093. mapUserIdChooseSendTypeNoPermission := make(map[int]int)
  1094. chooseSendtList, err := models.GetCygxXzsChooseSend()
  1095. for _, v := range chooseSendtList {
  1096. mapUserIdChooseSend[v.UserId] = v.UserId
  1097. if v.IsRefuse == 1 {
  1098. continue
  1099. }
  1100. //获取选择主客观赛道的用户
  1101. if reportMapDetail.PermissionType == 1 {
  1102. if v.IsSubjective == 1 {
  1103. mapUserIdChooseSendTypeNoPermission[v.UserId] = v.UserId
  1104. }
  1105. } else {
  1106. if v.IsObjective == 1 {
  1107. mapUserIdChooseSendTypeNoPermission[v.UserId] = v.UserId
  1108. }
  1109. }
  1110. }
  1111. //已经推送了的openid
  1112. mapOpenidPushed := make(map[int]string)
  1113. keyword2 := articleInfo.Title
  1114. keyword3 := time.Now().Format(utils.FormatDateTime)
  1115. keyword4 := articleInfo.Abstract
  1116. mapAlreadySend := make(map[int]int)
  1117. //如果是策略就按照策略的推送方式来写
  1118. if reportMapDetail.ChartPermissionName == utils.CE_LUE_NAME || reportMapDetail.ChartPermissionName == utils.GU_SHOU_NAME {
  1119. //获取提交过推送规则的用户的 openid
  1120. mapUserFllowCeLue := make(map[int]string)
  1121. userFllowCeLueList, err := models.GetCygxUserFllowCeLueOpenid(articleInfo.CategoryId)
  1122. if err != nil {
  1123. return err
  1124. }
  1125. for _, v := range userFllowCeLueList {
  1126. mapUserFllowCeLue[v.UserId] = v.OpenId
  1127. }
  1128. for k, v := range mapOpenidPower {
  1129. if mapOpenidPushed[k] != "" {
  1130. continue
  1131. }
  1132. openIditem := new(models.OpenIdList)
  1133. openIditem.UserId = k
  1134. openIditem.OpenId = v
  1135. first := "您关注的赛道,有新的报告发布/更新,欢迎查看"
  1136. keyword1 := "【" + articleInfo.SubCategoryName + "】有报告发布/更新"
  1137. mapActivityUserId := make(map[int]string)
  1138. if articleInfo.ReportType == 2 {
  1139. sliceSubjectId, _ := models.GetSubjectIds(articleId)
  1140. if sliceSubjectId != "" {
  1141. appointmentList, err := models.GetCygxAppointmentSummaryBySubjectId(sliceSubjectId)
  1142. if err != nil {
  1143. fmt.Println("GetCygxAppointmentSummaryListBySubjectId Err:", err.Error())
  1144. return err
  1145. }
  1146. var userIds []string
  1147. if len(appointmentList) > 0 {
  1148. for _, v := range appointmentList {
  1149. //fmt.Println("mapActivityUserId:", v.UserId)
  1150. userIds = append(userIds, strconv.Itoa(v.UserId))
  1151. mapActivityUserId[v.UserId] += v.ActivityName + ","
  1152. }
  1153. userIdstr := strings.Join(userIds, ",")
  1154. openIdLists, err := models.GetOpenIdByUserIds(userIdstr)
  1155. if err != nil {
  1156. fmt.Println("GetOpenIdByUserIds Err:", err.Error())
  1157. return err
  1158. }
  1159. for _, item := range openIdLists {
  1160. first := "您预约的调研,有关联的纪要发布/更新了"
  1161. keyword1 := industryName
  1162. openIditem := new(models.OpenIdList)
  1163. openIditem.OpenId = item.OpenId
  1164. openIditem.UserId = item.UserId
  1165. if _, ok := mapAlreadySend[openIditem.UserId]; !ok {
  1166. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1167. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1168. fmt.Println("AlreadySend:", openIditem.UserId)
  1169. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1170. }
  1171. }
  1172. }
  1173. }
  1174. //fmt.Println("openIditem.UserId:", openIditem.UserId)
  1175. //if _,ok := mapActivityUserId[openIditem.UserId]; ok{
  1176. // first = "您预约的调研,有关联的纪要发布/更新了"
  1177. // keyword1 = industryName
  1178. // SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1179. // delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1180. // mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1181. //}
  1182. }
  1183. _, ok := mapAlreadySend[openIditem.UserId]
  1184. //如果有权限而且小助手没有提交过信息的 就做正常推送
  1185. if mapUserIdChooseSend[openIditem.UserId] == 0 && !ok {
  1186. fmt.Println("1:", articleId)
  1187. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1188. }
  1189. //如果小助手勾选了对应的权限信息 就做正常推送
  1190. if mapUserIdChooseSend[openIditem.UserId] != 0 && mapUserFllowCeLue[openIditem.UserId] != "" && !ok {
  1191. fmt.Println("2:", articleId)
  1192. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1193. }
  1194. }
  1195. } else {
  1196. for k, v := range mapOpenidPower {
  1197. if mapOpenidPushed[k] != "" {
  1198. continue
  1199. }
  1200. mapUserIdChooseSendType := make(map[int]int)
  1201. for _, vS := range chooseSendtList {
  1202. if vS.IsRefuse == 1 {
  1203. continue
  1204. }
  1205. //获取选择主客观赛道的用户
  1206. if reportMapDetail.PermissionType == 1 {
  1207. if vS.IsSubjective == 1 {
  1208. mapUserIdChooseSendType[vS.UserId] = vS.UserId
  1209. }
  1210. } else if reportMapDetail.PermissionType == 2 {
  1211. if vS.IsObjective == 1 {
  1212. mapUserIdChooseSendType[vS.UserId] = vS.UserId
  1213. }
  1214. } else {
  1215. if vS.IsSubjective == 1 || vS.IsObjective == 1 {
  1216. mapUserIdChooseSendType[vS.UserId] = vS.UserId
  1217. }
  1218. }
  1219. }
  1220. openIditem := new(models.OpenIdList)
  1221. openIditem.UserId = k
  1222. openIditem.OpenId = v
  1223. first := "您关注的赛道,有新的报告发布/更新,欢迎查看"
  1224. keyword1 := "【" + articleInfo.SubCategoryName + "】有报告发布/更新"
  1225. mapActivityUserId := make(map[int]string)
  1226. if articleInfo.ReportType == 2 {
  1227. keyword1 = fmt.Sprint("【", industryName, "】有报告发布/更新")
  1228. sliceSubjectId, _ := models.GetSubjectIds(articleId)
  1229. if sliceSubjectId != "" {
  1230. appointmentList, err := models.GetCygxAppointmentSummaryBySubjectId(sliceSubjectId)
  1231. if err != nil {
  1232. fmt.Println("GetCygxAppointmentSummaryListBySubjectId Err:", err.Error())
  1233. return err
  1234. }
  1235. var userIds []string
  1236. if len(appointmentList) > 0 {
  1237. for _, v := range appointmentList {
  1238. //fmt.Println("mapActivityUserId:", v.UserId)
  1239. userIds = append(userIds, strconv.Itoa(v.UserId))
  1240. mapActivityUserId[v.UserId] += v.ActivityName + ","
  1241. }
  1242. userIdstr := strings.Join(userIds, ",")
  1243. openIdLists, err := models.GetOpenIdByUserIds(userIdstr)
  1244. if err != nil {
  1245. fmt.Println("GetOpenIdByUserIds Err:", err.Error())
  1246. return err
  1247. }
  1248. for _, item := range openIdLists {
  1249. //first := "您预约的调研,有关联的纪要发布/更新了"
  1250. //keyword1 := industryName
  1251. openIditem := new(models.OpenIdList)
  1252. openIditem.OpenId = item.OpenId
  1253. openIditem.UserId = item.UserId
  1254. if _, ok := mapAlreadySend[openIditem.UserId]; !ok {
  1255. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1256. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1257. fmt.Println("AlreadySend:", openIditem.UserId)
  1258. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. //如果有权限而且小助手没有提交过信息的 就做正常推送
  1265. _, ok := mapAlreadySend[openIditem.UserId]
  1266. if mapUserIdChooseSend[openIditem.UserId] == 0 && !ok {
  1267. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1268. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1269. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1270. }
  1271. //如果小助手勾选了对应的权限信息 就做正常推送
  1272. if mapUserIdChooseSend[openIditem.UserId] != 0 && mapOpenidFllow[openIditem.UserId] != "" && mapUserIdChooseSendType[openIditem.UserId] > 0 && !ok {
  1273. SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4, openIditem, articleId)
  1274. delete(mapUserIdChooseSendTypeNoPermission, openIditem.UserId)
  1275. mapAlreadySend[openIditem.UserId] = openIditem.UserId
  1276. }
  1277. }
  1278. }
  1279. utils.Rc.Put(cacheKey, articleId, time.Hour*12)
  1280. return
  1281. }
  1282. // GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
  1283. func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
  1284. userType, _, e := GetUserType(user.CompanyId)
  1285. if e != nil {
  1286. err = errors.New("GetSpecialUserType, Err: " + e.Error())
  1287. return
  1288. }
  1289. // 永续客户、大套餐客户可以查看行业升级套餐客户 权限
  1290. if userType == 1 || userType == 2 {
  1291. havePower = true
  1292. return
  1293. }
  1294. permissionStr, e := GetCompanyPermissionUpgrade(user.CompanyId)
  1295. if e != nil {
  1296. err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
  1297. return
  1298. }
  1299. reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  1300. if e != nil {
  1301. err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
  1302. return
  1303. }
  1304. if reportMapDetail == nil {
  1305. err = errors.New("GetdetailByCategoryIdP,获取详情失败, ")
  1306. return
  1307. }
  1308. fmt.Println(permissionStr)
  1309. //如果没有对应的升级权限,则返回
  1310. if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
  1311. return
  1312. } else {
  1313. havePower = true
  1314. }
  1315. return
  1316. }
  1317. // HandleArticleCategoryImg 预处理文章的封面图片
  1318. func HandleArticleCategoryImg(list []*models.HomeArticle) (items []*models.HomeArticle, err error) {
  1319. //研选的五张图片
  1320. detailResearch, e := models.GetConfigByCode("category_research_img_url")
  1321. if e != nil {
  1322. err = errors.New("获取研选的五张图片失败" + e.Error())
  1323. return
  1324. }
  1325. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  1326. //对应分类的所图片
  1327. detailCategoryUrl, err := models.GetConfigByCode("category_map_img_url")
  1328. if err != nil {
  1329. err = errors.New("获取对应分类的所图片失败" + err.Error())
  1330. return
  1331. }
  1332. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  1333. mapCategoryUrl := make(map[string]string)
  1334. var categoryId string
  1335. var imgUrlChart string
  1336. for _, v := range categoryUrlList {
  1337. vslice := strings.Split(v, "_")
  1338. categoryId = vslice[0]
  1339. imgUrlChart = vslice[len(vslice)-1]
  1340. mapCategoryUrl[categoryId] = imgUrlChart
  1341. }
  1342. mapChartPerssion := make(map[string]string)
  1343. reportMappingList, err := models.GetReportMappingStrategyAll()
  1344. if err != nil {
  1345. err = errors.New("GetReportMappingStrategyAll err" + err.Error())
  1346. return
  1347. }
  1348. for _, v := range reportMappingList {
  1349. mapChartPerssion[strconv.Itoa(v.CategoryId)] = v.ChartPermissionName
  1350. }
  1351. for k, v := range list {
  1352. list[k].Abstract, _ = GetReportContentTextSub(v.Abstract)
  1353. item := list[k]
  1354. //如果文章一开始的内容是图片,优先展示第一张图片
  1355. if list[k].Annotation == "" {
  1356. imgurl, _ := FixArticleImgUrl(html.UnescapeString(list[k].Body))
  1357. if imgurl != "" {
  1358. list[k].BodyHtml = imgurl
  1359. }
  1360. }
  1361. //newBody, _ := GetReportContentTextSubByarticle(item.Body, item.Annotation, item.ArticleId)
  1362. list[k].Annotation = ArticleAnnotation(item)
  1363. list[k].Body = ""
  1364. list[k].PublishDate = utils.StrTimeToTime(item.PublishDate).Format(utils.FormatDate) //时间字符串格式转时间格式
  1365. list[k].ChartPermissionName = mapChartPerssion[v.CategoryId]
  1366. //如果是研选系列的任意取五张图片的中的一张
  1367. if v.CategoryId == "0" || v.ArticleId > utils.SummaryArticleId {
  1368. knum := v.ArticleId % 5
  1369. list[k].ImgUrlPc = researchList[knum]
  1370. } else {
  1371. list[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  1372. }
  1373. list[k].ArticleResponse = 4 // 默认展示核心观点
  1374. //ArticleResponse int `description:"报告类型 0:啥也不是,1研选报告,2:研选纪要,3:研选沙龙,4;核心观点"`
  1375. if list[k].ArticleId >= utils.SummaryArticleId {
  1376. list[k].HttpUrl = utils.StrategyPlatform + strconv.Itoa(v.ArticleId)
  1377. list[k].IsNeedJump = true
  1378. list[k].ArticleResponse = 1
  1379. //if v.IsReport == 1 {
  1380. // list[k].ArticleResponse = 1
  1381. //} else if v.ArticleTypeName == "纪要" {
  1382. // list[k].ArticleResponse = 2
  1383. //} else if v.ArticleTypeName == "观点" {
  1384. // list[k].ArticleResponse = 3
  1385. //} else if v.ArticleTypeName == "沙龙" {
  1386. // list[k].ArticleResponse = 3
  1387. //} else if v.ArticleTypeName == "路演精华" {
  1388. // list[k].ArticleResponse = 1
  1389. //}
  1390. }
  1391. list[k].Source = 1
  1392. //添加行业默认图片
  1393. if v.ImgUrlPc == "" {
  1394. if v.ChartPermissionName == utils.YI_YAO_NAME {
  1395. list[k].ImgUrlPc = utils.YI_YAO_OTHER_IMG
  1396. } else if v.ChartPermissionName == utils.XIAO_FEI_NAME {
  1397. list[k].ImgUrlPc = utils.XIAO_FEI_OTHER_IMG
  1398. } else if v.ChartPermissionName == utils.KE_JI_NAME {
  1399. list[k].ImgUrlPc = utils.KE_JI_OTHER_IMG
  1400. } else if v.ChartPermissionName == utils.ZHI_ZAO_NAME {
  1401. list[k].ImgUrlPc = utils.ZHI_ZAO_OTHER_IMG
  1402. }
  1403. }
  1404. //是不是研选报告
  1405. if v.ArticleTypeId > 0 {
  1406. list[k].IsResearch = true
  1407. }
  1408. }
  1409. articleIds := make([]int, 0)
  1410. for i := range list {
  1411. articleIds = append(articleIds, list[i].ArticleId)
  1412. }
  1413. articleMapPv := GetArticleHistoryByArticleId(articleIds) //文章Pv
  1414. // 报告关联产业信息
  1415. industryMap := make(map[int][]*models.IndustrialManagementIdInt, 0)
  1416. if len(articleIds) > 0 {
  1417. var industryCond string
  1418. var industryPars []interface{}
  1419. industryCond += ` AND mg.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  1420. industryPars = append(industryPars, articleIds)
  1421. industryList, e := models.GetIndustrialListByarticleId(industryPars, industryCond)
  1422. if e != nil {
  1423. err = errors.New("GetIndustrialListByarticleId" + e.Error())
  1424. return
  1425. }
  1426. for i := range industryList {
  1427. v := industryList[i]
  1428. industryMap[v.ArticleId] = append(industryMap[v.ArticleId], &models.IndustrialManagementIdInt{
  1429. ArticleId: v.ArticleId,
  1430. IndustrialManagementId: v.IndustrialManagementId,
  1431. IndustryName: v.IndustryName,
  1432. ChartPermissionId: v.ChartPermissionId,
  1433. })
  1434. }
  1435. }
  1436. for k, v := range list {
  1437. if len(industryMap[v.ArticleId]) > 0 {
  1438. list[k].List = industryMap[v.ArticleId]
  1439. } else {
  1440. list[k].List = make([]*models.IndustrialManagementIdInt, 0)
  1441. }
  1442. v.Pv = articleMapPv[v.ArticleId]
  1443. if v.Pv > 999 {
  1444. list[k].Pv = 999
  1445. }
  1446. }
  1447. if len(list) == 0 {
  1448. list = make([]*models.HomeArticle, 0)
  1449. }
  1450. items = list
  1451. return
  1452. }
  1453. // 处理核心观点的展示规则
  1454. func ArticleAnnotation(item *models.HomeArticle) (annotation string) {
  1455. if item.ArticleId >= utils.SummaryArticleId {
  1456. item.Annotation = YxArticleAnnotation(item)
  1457. }
  1458. if item.Annotation != "" {
  1459. annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  1460. annotation = strings.Replace(item.Annotation, "&nbsp;", "", -1)
  1461. } else {
  1462. return
  1463. }
  1464. bodyText, _ := GetReportContentTextSubNew(annotation)
  1465. if bodyText == "" {
  1466. return
  1467. }
  1468. if annotation != "" {
  1469. annotation = html.UnescapeString(annotation)
  1470. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  1471. docText := doc.Text()
  1472. mapDoc := make(map[int]string)
  1473. var mapSort []int
  1474. p := doc.Find("p")
  1475. p.Each(func(tk int, pd *goquery.Selection) {
  1476. pdText := pd.Text()
  1477. pdText = strings.Replace(pdText, " ", "", -1)
  1478. if pdText != "" {
  1479. textLen := strings.Index(docText, pdText)
  1480. mapDoc[(strings.Index(docText, pdText))] = pdText
  1481. mapSort = append(mapSort, textLen)
  1482. }
  1483. })
  1484. li := doc.Find("li")
  1485. li.Each(func(tk int, li *goquery.Selection) {
  1486. liText := li.Text()
  1487. liText = strings.Replace(liText, " ", "", -1)
  1488. if liText != "" {
  1489. textLen := strings.Index(docText, liText)
  1490. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  1491. mapSort = append(mapSort, textLen)
  1492. }
  1493. })
  1494. ul := doc.Find("ul")
  1495. ul.Each(func(tk int, ul *goquery.Selection) {
  1496. ulText := ul.Text()
  1497. ulText = strings.Replace(ulText, " ", "", -1)
  1498. if ulText != "" {
  1499. textLen := strings.Index(docText, ulText)
  1500. mapDoc[(strings.Index(docText, ulText))] = ulText
  1501. mapSort = append(mapSort, textLen)
  1502. }
  1503. })
  1504. if len(mapSort) == 0 {
  1505. return
  1506. } else {
  1507. //排序
  1508. sort.Ints(mapSort)
  1509. var annotationHtml string
  1510. for _, vSort := range mapSort {
  1511. for k, v := range mapDoc {
  1512. if k == vSort && v != "" {
  1513. annotationHtml += v + "<br>"
  1514. }
  1515. }
  1516. }
  1517. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  1518. annotationHtml = "<p>" + annotationHtml + "</p>"
  1519. annotation = annotationHtml
  1520. }
  1521. }
  1522. return
  1523. }
  1524. // 处理核心观点的展示规则
  1525. func AnnotationHtml(bodyText string) (annotation string) {
  1526. if bodyText == "" {
  1527. return
  1528. }
  1529. annotation = bodyText
  1530. annotation = html.UnescapeString(annotation)
  1531. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  1532. docText := doc.Text()
  1533. mapDoc := make(map[int]string)
  1534. var mapSort []int
  1535. p := doc.Find("p")
  1536. p.Each(func(tk int, pd *goquery.Selection) {
  1537. pdText := pd.Text()
  1538. //pdText = strings.Replace(pdText, " ", "", -1)
  1539. if pdText != "" {
  1540. textLen := strings.Index(docText, pdText)
  1541. mapDoc[(strings.Index(docText, pdText))] = pdText
  1542. mapSort = append(mapSort, textLen)
  1543. }
  1544. })
  1545. li := doc.Find("li")
  1546. li.Each(func(tk int, li *goquery.Selection) {
  1547. liText := li.Text()
  1548. liText = strings.Replace(liText, " ", "", -1)
  1549. if liText != "" {
  1550. textLen := strings.Index(docText, liText)
  1551. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  1552. mapSort = append(mapSort, textLen)
  1553. }
  1554. })
  1555. ul := doc.Find("ul")
  1556. ul.Each(func(tk int, ul *goquery.Selection) {
  1557. ulText := ul.Text()
  1558. ulText = strings.Replace(ulText, " ", "", -1)
  1559. if ulText != "" {
  1560. textLen := strings.Index(docText, ulText)
  1561. mapDoc[(strings.Index(docText, ulText))] = ulText
  1562. mapSort = append(mapSort, textLen)
  1563. }
  1564. })
  1565. section := doc.Find("section")
  1566. section.Each(func(tk int, section *goquery.Selection) {
  1567. sectionText := section.Text()
  1568. sectionText = strings.Replace(sectionText, " ", "", -1)
  1569. if sectionText != "" {
  1570. textLen := strings.Index(docText, sectionText)
  1571. mapDoc[(strings.Index(docText, sectionText))] = sectionText
  1572. mapSort = append(mapSort, textLen)
  1573. }
  1574. })
  1575. if len(mapSort) == 0 {
  1576. return
  1577. } else {
  1578. //排序
  1579. sort.Ints(mapSort)
  1580. var annotationHtml string
  1581. for _, vSort := range mapSort {
  1582. for k, v := range mapDoc {
  1583. if k == vSort && v != "" {
  1584. annotationHtml += v + "<br>"
  1585. }
  1586. }
  1587. }
  1588. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  1589. annotationHtml = "<p>" + annotationHtml + "</p>"
  1590. annotation = annotationHtml
  1591. }
  1592. return
  1593. }
  1594. // 处理产品内测展示规则
  1595. func ProductInteriorHtml(bodyText string) (annotation string) {
  1596. if bodyText == "" {
  1597. return
  1598. }
  1599. sliceBody := strings.Split(bodyText, "</p>")
  1600. annotation, _ = GetReportContentTextSub(sliceBody[0])
  1601. return
  1602. }
  1603. // 解析研选内容中的核心观点
  1604. func YxArticleAnnotation(article *models.HomeArticle) (annotation string) {
  1605. //如果不规范,就获取内容主体
  1606. if strings.Count(article.Body, "<hr") == 0 {
  1607. //如果内容不规范而且,还有图片,就把核心观点置空
  1608. if article.BodyHtml != "" {
  1609. return
  1610. }
  1611. annotation, _ = GetReportContentTextSub(article.Body)
  1612. return
  1613. }
  1614. body := strings.ReplaceAll(article.Body, "<strong>", "")
  1615. body = strings.ReplaceAll(body, "</strong>", "")
  1616. body = strings.ReplaceAll(body, "</ol>", "</div>")
  1617. body = strings.ReplaceAll(body, "<ol>", "<div>")
  1618. body = strings.ReplaceAll(body, "</li>", "</p>")
  1619. body = strings.ReplaceAll(body, "<li>", "<p>")
  1620. re, _ := regexp.Compile("<strong.*?>")
  1621. body = re.ReplaceAllString(body, "")
  1622. reLi, _ := regexp.Compile("<li.*?>")
  1623. body = reLi.ReplaceAllString(body, "")
  1624. var plus int
  1625. coreIndex := strings.Index(body, "核心观点:")
  1626. plus = 15
  1627. if coreIndex == -1 {
  1628. coreIndex = strings.Index(body, "核心观点:")
  1629. plus = 13
  1630. }
  1631. if coreIndex == -1 {
  1632. coreIndex = strings.Index(body, "核心观点")
  1633. plus = 12
  1634. }
  1635. if coreIndex == -1 {
  1636. coreIndex = strings.Index(body, "核心结论:")
  1637. plus = 15
  1638. }
  1639. if coreIndex == -1 {
  1640. coreIndex = strings.Index(body, "核心结论:")
  1641. plus = 13
  1642. }
  1643. if coreIndex == -1 {
  1644. coreIndex = strings.Index(body, "核心结论")
  1645. plus = 12
  1646. }
  1647. endIndex := strings.Index(body, "<hr")
  1648. if coreIndex != -1 && endIndex != -1 {
  1649. body = body[coreIndex+plus : endIndex]
  1650. }
  1651. annotation = body
  1652. return
  1653. }
  1654. // 通过接解析带有Md5的文章链接获取文章ID
  1655. func GetReportLinkToArticleid(reportLink string) (articleId int, err error) {
  1656. defer func() {
  1657. if err != nil {
  1658. go utils.SendAlarmMsg("通过接解析带有Md5的文章链接获取文章ID失败"+err.Error(), 2)
  1659. }
  1660. }()
  1661. var artMd5 string
  1662. //处理Md5的
  1663. strnum1 := strings.Index(reportLink, "id=")
  1664. if strnum1 > 0 {
  1665. sliceId := strings.Split(reportLink, "id=")
  1666. if len(sliceId) > 1 {
  1667. reportLink = sliceId[1]
  1668. sliceMd5Id := strings.Split(reportLink, "&")
  1669. artMd5 = sliceMd5Id[0]
  1670. }
  1671. if artMd5 != "" {
  1672. detail, errArt := models.GetArticleDetailByIdMd5(artMd5)
  1673. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  1674. err = errArt
  1675. return
  1676. }
  1677. if detail != nil {
  1678. articleId = detail.ArticleId
  1679. }
  1680. }
  1681. } else {
  1682. //处理活动的
  1683. linkList := strings.Split(reportLink, "/")
  1684. if linkList[len(linkList)-1] != "" {
  1685. linkArticleId, _ := strconv.Atoi(linkList[len(linkList)-1])
  1686. if linkArticleId > 0 {
  1687. articleInfo, errArt := models.GetArticleDetailById(linkArticleId)
  1688. if errArt != nil && errArt.Error() != utils.ErrNoRow() {
  1689. err = errArt
  1690. return
  1691. }
  1692. if articleInfo != nil {
  1693. articleId = articleInfo.ArticleId
  1694. }
  1695. }
  1696. }
  1697. }
  1698. return
  1699. }
  1700. // GetArticleStockMap 获取个股标签所对应的文章ID
  1701. func GetArticleStockMap() (mapResp map[string]int, err error) {
  1702. defer func() {
  1703. if err != nil {
  1704. go utils.SendAlarmMsg("获取个股标签所对应的文章ID失败"+err.Error(), 2)
  1705. }
  1706. }()
  1707. list, err := models.GetArticleStock()
  1708. if err != nil && err.Error() != utils.ErrNoRow() {
  1709. return
  1710. }
  1711. mapResp = make(map[string]int, 0)
  1712. if len(list) > 0 {
  1713. //一对一精准匹配
  1714. for _, v := range list {
  1715. sliceSubjects := strings.Split(v.Stock, "/")
  1716. if len(sliceSubjects) > 0 {
  1717. for _, vSubject := range sliceSubjects {
  1718. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  1719. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  1720. subject := sliceXiahuaxian[0]
  1721. mapResp[subject] = v.ArticleId
  1722. }
  1723. }
  1724. }
  1725. }
  1726. return
  1727. }
  1728. // GetAiQianYanArtilceList 获取AI前沿几篇文章
  1729. func GetAiQianYanArtilceList(startSize, pageSize int) (items []*models.ReportArticle, total int, err error) {
  1730. defer func() {
  1731. if err != nil {
  1732. go utils.SendAlarmMsg("获取AI前沿几篇文章失败"+err.Error(), 2)
  1733. }
  1734. }()
  1735. var condition string
  1736. var pars []interface{}
  1737. condition += ` AND title LIKE '%AI前沿%' AND publish_status = 1 ORDER BY publish_date DESC `
  1738. articleList, e := models.GetCygxCygxArticleList(condition, pars, startSize, pageSize)
  1739. if e != nil {
  1740. err = errors.New("GetCygxCygxArticleList, Err: " + e.Error())
  1741. return
  1742. }
  1743. total, e = models.GetCygxArticleCount(condition, pars)
  1744. if e != nil {
  1745. err = errors.New("GetCygxArticleCount, Err: " + e.Error())
  1746. return
  1747. }
  1748. for _, v := range articleList {
  1749. item := new(models.ReportArticle)
  1750. item.ArticleId = v.ArticleId
  1751. item.Title = v.Title
  1752. item.Abstract = v.Abstract
  1753. item.Annotation = v.Annotation
  1754. item.PublishDate = v.PublishDate
  1755. item.Resource = 1
  1756. item.CategoryId = strconv.Itoa(v.CategoryId)
  1757. item.Body = v.Body
  1758. items = append(items, item)
  1759. }
  1760. return
  1761. }
  1762. // 获取文章内部图表的第一张封面图
  1763. func GetArticleCoverByChartFirst(body string) (cover string) {
  1764. var err error
  1765. defer func() {
  1766. if err != nil {
  1767. fmt.Println(err)
  1768. go utils.SendAlarmMsg("获取文章内部图表的第一张封面图失败,Err:"+err.Error()+body, 3)
  1769. }
  1770. }()
  1771. var chartUrl string
  1772. var hrefRegexp = regexp.MustCompile(utils.RegularUrl)
  1773. match := hrefRegexp.FindAllString(body, -1)
  1774. if match != nil {
  1775. for _, v := range match {
  1776. if strings.Contains(v, "charts/display") {
  1777. chartUrl = v
  1778. break
  1779. }
  1780. }
  1781. }
  1782. if chartUrl != "" {
  1783. urlSlice := strings.Split(chartUrl, "/")
  1784. lenurlSlice := len(urlSlice)
  1785. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  1786. if sourceId == 0 {
  1787. sourceId, _ = strconv.Atoi(urlSlice[lenurlSlice-2])
  1788. }
  1789. if sourceId == 0 {
  1790. err = errors.New("新的图标链接无法解析" + chartUrl)
  1791. return
  1792. }
  1793. chartUrlApi := utils.ApiUrl + "charts/" + strconv.Itoa(sourceId)
  1794. authorization := utils.ApiAuthorization
  1795. body, e := PublicGetDate(chartUrlApi, authorization)
  1796. if e != nil {
  1797. err = errors.New("PublicGetDate" + err.Error())
  1798. return
  1799. }
  1800. var resultDate models.ChartResultDetailApi
  1801. e = json.Unmarshal(body, &resultDate)
  1802. if e != nil {
  1803. err = errors.New(" json.Unmarshal" + err.Error())
  1804. }
  1805. cover = resultDate.Data.Cover
  1806. }
  1807. return
  1808. }
  1809. // GetYxArticleIdMap 获取研选文章ID
  1810. func GetYxArticleIdMap(articleIds []int) (mapResp map[int]bool) {
  1811. var err error
  1812. defer func() {
  1813. if err != nil {
  1814. go utils.SendAlarmMsg("获取研选文章ID失败,GetYxArticleIdMap"+err.Error(), 2)
  1815. }
  1816. }()
  1817. var condition string
  1818. var pars []interface{}
  1819. condition = ` AND article_type_id > 0 `
  1820. if len(articleIds) > 0 {
  1821. condition += ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  1822. pars = append(pars, articleIds)
  1823. }
  1824. articleList, e := models.GetArticleList(condition, pars)
  1825. if e != nil {
  1826. err = errors.New("GetArticleList, Err: " + e.Error())
  1827. return
  1828. }
  1829. mapResp = make(map[int]bool, 0)
  1830. for _, v := range articleList {
  1831. mapResp[v.ArticleId] = true
  1832. }
  1833. return
  1834. }