es_comprehensive.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/PuerkitoBio/goquery"
  7. "github.com/olivere/elastic/v7"
  8. "hongze/hongze_clpt/models"
  9. "hongze/hongze_clpt/utils"
  10. "strconv"
  11. //"strconv"
  12. "errors"
  13. "html"
  14. "strings"
  15. "time"
  16. )
  17. type SearchComprehensiveItem struct {
  18. SourceId int `description:"资源ID"`
  19. IsSummary int `description:"是否是纪要"`
  20. Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt 、 产品内测:productinterior"`
  21. Title string `description:"标题"`
  22. BodyText string `description:"内容"`
  23. PublishDate string `description:"发布时间"`
  24. Abstract string `description:"摘要"`
  25. Annotation string `description:"核心观点"`
  26. IndustryName string `description:"产业名称"`
  27. SubjectNames string `description:"标的名称"`
  28. Body []string
  29. }
  30. type ElasticComprehensiveDetail struct {
  31. SourceId int `description:"资源ID"`
  32. IsSummary int `description:"是否是纪要"`
  33. Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt 、 产品内测:productinterior 、 产业资源包:industrialsource"`
  34. Title string `description:"标题"`
  35. BodyText string `description:"内容"`
  36. PublishDate string `description:"发布时间"`
  37. Abstract string `description:"摘要"`
  38. Annotation string `description:"核心观点"`
  39. IndustryName string `description:"产业名称"`
  40. SubjectNames string `description:"标的名称"`
  41. }
  42. func EsComprehensiveSearch(keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int64, err error) {
  43. indexName := utils.IndexNameComprehensive
  44. client := utils.Client
  45. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  46. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  47. keyWordLen := len(keyWordArr)
  48. if keyWordLen <= 0 {
  49. keyWordArr = append(keyWordArr, keyWord)
  50. keyWordLen = len(keyWordArr)
  51. }
  52. //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
  53. //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
  54. mustMap := make([]interface{}, 0)
  55. shouldMap := make(map[string]interface{}, 0)
  56. shouldMapquery := make([]interface{}, 0)
  57. mustNotMap := make([]interface{}, 0)
  58. shouldNotMap := make(map[string]interface{}, 0)
  59. shouldNotMapquery := make([]interface{}, 0)
  60. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  61. //keyWordWeight := GetWeight(keyWordLen)
  62. var boost int
  63. //lenkeyWordArr := len(keyWordArr)
  64. for k, v := range keyWordArr {
  65. if k > 0 {
  66. continue
  67. }
  68. if k == 0 {
  69. boost = 2 * 1000
  70. } else {
  71. boost = 1
  72. }
  73. if v != "" {
  74. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  75. "function_score": map[string]interface{}{
  76. "query": map[string]interface{}{
  77. "multi_match": map[string]interface{}{
  78. //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
  79. "boost": boost, //给查询的值赋予权重
  80. "fields": []interface{}{"Title"},
  81. "query": v,
  82. },
  83. },
  84. },
  85. })
  86. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  87. "function_score": map[string]interface{}{
  88. "query": map[string]interface{}{
  89. "multi_match": map[string]interface{}{
  90. "boost": boost, //给查询的值赋予权重
  91. "fields": []interface{}{"Abstract"},
  92. "query": v,
  93. },
  94. },
  95. },
  96. })
  97. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  98. "function_score": map[string]interface{}{
  99. "query": map[string]interface{}{
  100. "multi_match": map[string]interface{}{
  101. "boost": boost, //给查询的值赋予权重
  102. "fields": []interface{}{"Annotation"},
  103. "query": v,
  104. },
  105. },
  106. },
  107. })
  108. //shouldMapquery = append(shouldMapquery, map[string]interface{}{
  109. // "function_score": map[string]interface{}{
  110. // "query": map[string]interface{}{
  111. // "multi_match": map[string]interface{}{
  112. // //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  113. // "boost": boost, //给查询的值赋予权重
  114. // "fields": []interface{}{"BodyText"},
  115. // "query": v,
  116. // },
  117. // },
  118. // },
  119. //})
  120. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  121. "function_score": map[string]interface{}{
  122. "query": map[string]interface{}{
  123. "multi_match": map[string]interface{}{
  124. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  125. "boost": boost, //给查询的值赋予权重
  126. "fields": []interface{}{"IndustryName"},
  127. "query": v,
  128. },
  129. },
  130. },
  131. })
  132. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  133. "function_score": map[string]interface{}{
  134. "query": map[string]interface{}{
  135. "multi_match": map[string]interface{}{
  136. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  137. "boost": boost, //给查询的值赋予权重
  138. "fields": []interface{}{"SubjectNames"},
  139. "query": v,
  140. },
  141. },
  142. },
  143. })
  144. }
  145. }
  146. shouldMap = map[string]interface{}{
  147. "should": shouldMapquery,
  148. }
  149. shouldNotMap = map[string]interface{}{
  150. "should": shouldNotMapquery,
  151. }
  152. //排序
  153. sortMap := make([]interface{}, 0)
  154. //时间
  155. sortMap = append(sortMap, map[string]interface{}{
  156. "PublishDate": map[string]interface{}{
  157. "order": "desc",
  158. },
  159. })
  160. //sortMap = append(sortMap, map[string]interface{}{
  161. // "_score": map[string]interface{}{
  162. // "order": "desc",
  163. // },
  164. //})
  165. //高亮
  166. highlightMap := make(map[string]interface{}, 0)
  167. highlightMap = map[string]interface{}{
  168. "fields": map[string]interface{}{
  169. //"BodyText": map[string]interface{}{},
  170. "Title": map[string]interface{}{},
  171. "Abstract": map[string]interface{}{},
  172. "Annotation": map[string]interface{}{},
  173. },
  174. //样式 红色
  175. "post_tags": []interface{}{"</font>"},
  176. "pre_tags": []interface{}{"<font color='red'>"},
  177. "fragment_size": 50,
  178. }
  179. mustMap = append(mustMap, map[string]interface{}{
  180. "bool": shouldMap,
  181. })
  182. mustNotMap = append(mustNotMap, map[string]interface{}{
  183. "bool": shouldNotMap,
  184. })
  185. queryMap := map[string]interface{}{
  186. "query": map[string]interface{}{
  187. "bool": map[string]interface{}{
  188. "must": mustMap,
  189. },
  190. },
  191. }
  192. queryMap["sort"] = sortMap
  193. queryMap["from"] = startSize
  194. queryMap["size"] = pageSize
  195. queryMap["highlight"] = highlightMap
  196. //jsonBytes, _ := json.Marshal(queryMap)
  197. //fmt.Println(string(jsonBytes))
  198. //utils.FileLog.Info(string(jsonBytes))
  199. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  200. searchByMatch, err := request.Do(context.Background())
  201. if searchByMatch != nil {
  202. if searchByMatch.Hits != nil {
  203. for _, v := range searchByMatch.Hits.Hits {
  204. var isAppend bool
  205. articleJson, err := v.Source.MarshalJSON()
  206. if err != nil {
  207. return nil, 0, err
  208. }
  209. article := new(ElasticComprehensiveDetail)
  210. err = json.Unmarshal(articleJson, &article)
  211. if err != nil {
  212. return nil, 0, err
  213. }
  214. //fmt.Println(article.SourceId, article.Title, article.Source)
  215. searchItem := new(SearchComprehensiveItem)
  216. searchItem.SourceId = article.SourceId
  217. if len(v.Highlight["Annotation"]) > 0 {
  218. for _, vText := range v.Highlight["Annotation"] {
  219. searchItem.Body = append(searchItem.Body, vText)
  220. }
  221. }
  222. if len(v.Highlight["Abstract"]) > 0 {
  223. for _, vText := range v.Highlight["Abstract"] {
  224. searchItem.Body = append(searchItem.Body, vText)
  225. }
  226. }
  227. if len(v.Highlight["BodyText"]) > 0 {
  228. for _, vText := range v.Highlight["BodyText"] {
  229. searchItem.Body = append(searchItem.Body, vText)
  230. }
  231. }
  232. //searchItem.IsSummary = article.IsSummary
  233. //if len(searchItem.Body) == 0 {
  234. // bodyRune := []rune(article.BodyText)
  235. // bodyRuneLen := len(bodyRune)
  236. // if bodyRuneLen > 100 {
  237. // bodyRuneLen = 100
  238. // }
  239. // body := string(bodyRune[:bodyRuneLen])
  240. // searchItem.Body = []string{body}
  241. //}
  242. var title string
  243. if len(v.Highlight["Title"]) > 0 {
  244. title = v.Highlight["Title"][0]
  245. } else {
  246. title = article.Title
  247. }
  248. searchItem.Title = title
  249. searchItem.Source = article.Source
  250. searchItem.PublishDate = article.PublishDate
  251. if !isAppend {
  252. result = append(result, searchItem)
  253. }
  254. }
  255. }
  256. total = searchByMatch.Hits.TotalHits.Value
  257. }
  258. return
  259. }
  260. func GetResourceDataEsList(list []*SearchComprehensiveItem, user *models.WxUserItem) (items []*models.CygxResourceDataNewResp, err error) {
  261. var condition string
  262. var pars []interface{}
  263. uid := user.UserId
  264. titleHighlight := make(map[int]string)
  265. bodyHighlight := make(map[int][]string)
  266. mapItems := make(map[string]*models.CygxResourceDataNewResp)
  267. for _, v := range list {
  268. //预处理文章
  269. item := new(models.CygxResourceDataNewResp)
  270. item.SourceId = v.SourceId
  271. item.Source = v.Source
  272. item.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  273. item.BodyHighlight = v.Body
  274. item.TitleHighlight = v.Title
  275. titleHighlight[v.SourceId] = v.Title
  276. bodyHighlight[v.SourceId] = v.Body
  277. mapItems[fmt.Sprint(v.Source, v.SourceId)] = item
  278. }
  279. var articleIds []int //报告
  280. var newchartIds []int //图表
  281. var roadshowIds []string //微路演
  282. var activityIds []int //活动
  283. var activityvideoIds []string // 活动视频
  284. var activityvoiceIds []string //活动音频
  285. var activityspecialIds []int //专项调研活动
  286. var researchsummaryIds []int //本周研究汇总
  287. var minutessummaryIds []int //上周纪要汇总
  288. var meetingreviewchaptIds []int //晨会精华
  289. var productinteriorIds []int //产品内测
  290. var industrialResourceIdsHz []int // 弘则产业资源包
  291. var industrialResourceIdsYx []int // 研选产业资源包
  292. var yanxuanSpecialIds []int // 研选专栏
  293. var askserieVideoIds []string //问答系列视频
  294. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt 、 产品内测:productinterior
  295. for _, v := range list {
  296. if v.Source == "article" {
  297. articleIds = append(articleIds, v.SourceId)
  298. } else if v.Source == "newchart" {
  299. newchartIds = append(newchartIds, v.SourceId)
  300. } else if v.Source == "roadshow" {
  301. roadshowIds = append(roadshowIds, strconv.Itoa(v.SourceId))
  302. } else if v.Source == "activity" {
  303. activityIds = append(activityIds, v.SourceId)
  304. } else if v.Source == "activityvideo" {
  305. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  306. } else if v.Source == "activityvoice" {
  307. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  308. } else if v.Source == "activityspecial" {
  309. activityspecialIds = append(activityspecialIds, v.SourceId)
  310. } else if v.Source == "researchsummary" {
  311. researchsummaryIds = append(researchsummaryIds, v.SourceId)
  312. } else if v.Source == "minutessummary" {
  313. minutessummaryIds = append(minutessummaryIds, v.SourceId)
  314. } else if v.Source == "meetingreviewchapt" {
  315. meetingreviewchaptIds = append(meetingreviewchaptIds, v.SourceId)
  316. } else if v.Source == "productinterior" {
  317. productinteriorIds = append(productinteriorIds, v.SourceId)
  318. } else if v.Source == "industrialsourceHz" {
  319. industrialResourceIdsHz = append(industrialResourceIdsHz, v.SourceId)
  320. } else if v.Source == "industrialsourceYx" {
  321. industrialResourceIdsYx = append(industrialResourceIdsYx, v.SourceId)
  322. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  323. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  324. } else if v.Source == utils.CYGX_OBJ_ASKSERIEVIDEO {
  325. askserieVideoIds = append(askserieVideoIds, strconv.Itoa(v.SourceId))
  326. }
  327. }
  328. detail, e := models.GetConfigByCode("city_img_url")
  329. if e != nil {
  330. err = errors.New("GetResourceDataList, Err: " + e.Error())
  331. return
  332. }
  333. detailChart, e := models.GetConfigByCode("chart_img_url")
  334. if e != nil {
  335. err = errors.New("GetResourceDataList, Err: " + e.Error())
  336. return
  337. }
  338. addressList := strings.Split(detail.ConfigValue, "{|}")
  339. mapAddress := make(map[string]string)
  340. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  341. mapChart := make(map[string]string)
  342. var cityName string
  343. var chartName string
  344. var imgUrl string
  345. var imgUrlChart string
  346. for _, v := range addressList {
  347. vslice := strings.Split(v, "_")
  348. cityName = vslice[0]
  349. imgUrl = vslice[len(vslice)-1]
  350. mapAddress[cityName] = imgUrl
  351. }
  352. for _, v := range chartList {
  353. vslice := strings.Split(v, "_")
  354. chartName = vslice[0]
  355. imgUrlChart = vslice[len(vslice)-1]
  356. mapChart[chartName] = imgUrlChart
  357. }
  358. var imgUrlResp string
  359. //处理文章
  360. if len(articleIds) > 0 {
  361. pars = make([]interface{}, 0)
  362. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  363. pars = append(pars, articleIds)
  364. articleList, e := models.GetHomeListPublic(condition, pars, 0, len(articleIds))
  365. if e != nil {
  366. err = errors.New("GetResourceDataList, Err: " + e.Error())
  367. return
  368. }
  369. articleList, e = HandleArticleCategoryImg(articleList, user)
  370. if e != nil {
  371. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  372. return
  373. }
  374. for _, v := range articleList {
  375. v.Body = ""
  376. if titleHighlight[v.ArticleId] != "" {
  377. v.Title = titleHighlight[v.ArticleId]
  378. }
  379. if len(bodyHighlight[v.ArticleId]) > 0 {
  380. v.Abstract = ""
  381. v.Annotation = ""
  382. v.BodyHighlight = bodyHighlight[v.ArticleId]
  383. } else {
  384. v.BodyHighlight = make([]string, 0)
  385. }
  386. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  387. }
  388. }
  389. //处理晨会精华
  390. if len(meetingreviewchaptIds) > 0 {
  391. pars = make([]interface{}, 0)
  392. condition = ` AND c.id IN (` + utils.GetOrmInReplace(len(meetingreviewchaptIds)) + `)`
  393. pars = append(pars, meetingreviewchaptIds)
  394. listrMeet, e := models.GetCygxMorningMeetingReviewChapterList(condition, pars)
  395. if e != nil {
  396. err = errors.New("GetResourceDataList, Err: " + e.Error())
  397. return
  398. }
  399. for _, v := range listrMeet {
  400. v.Content = AnnotationHtml(v.Content)
  401. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  402. mapItems[fmt.Sprint("meetingreviewchapt", v.Id)].Meetingreviewchapt = v
  403. }
  404. }
  405. //处理上周纪要汇总
  406. if len(minutessummaryIds) > 0 {
  407. pars = make([]interface{}, 0)
  408. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(minutessummaryIds)) + `)`
  409. pars = append(pars, minutessummaryIds)
  410. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_minutes_summary", pars, 0, len(minutessummaryIds))
  411. if e != nil {
  412. err = errors.New("GetResourceDataList, Err: " + e.Error())
  413. return
  414. }
  415. for _, v := range listresearchsummary {
  416. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  417. mapItems[fmt.Sprint("minutessummary", v.ArticleId)].Minutessummary = v
  418. }
  419. }
  420. //处理本周纪要汇总
  421. if len(researchsummaryIds) > 0 {
  422. pars = make([]interface{}, 0)
  423. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(researchsummaryIds)) + `)`
  424. pars = append(pars, researchsummaryIds)
  425. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_research_summary", pars, 0, len(researchsummaryIds))
  426. if e != nil {
  427. err = errors.New("GetReportSelectionListHome, Err: " + e.Error())
  428. return
  429. }
  430. for _, v := range listresearchsummary {
  431. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  432. mapItems[fmt.Sprint("researchsummary", v.ArticleId)].Researchsummary = v
  433. }
  434. }
  435. //处理产品内测
  436. if len(productinteriorIds) > 0 {
  437. pars = make([]interface{}, 0)
  438. condition = ` AND art.status = 1 AND art.product_interior_id IN (` + utils.GetOrmInReplace(len(productinteriorIds)) + `)`
  439. pars = append(pars, productinteriorIds)
  440. listProductInterior, e := models.GetCygxProductInteriorList(condition, pars, 0, len(productinteriorIds))
  441. if e != nil {
  442. err = errors.New("GetCygxProductInteriorList, Err: " + e.Error())
  443. return
  444. }
  445. for _, v := range listProductInterior {
  446. v.Body = ProductInteriorHtml(v.Body)
  447. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  448. mapItems[fmt.Sprint("productinterior", v.ProductInteriorId)].ProductInterior = v
  449. }
  450. }
  451. //处理活动
  452. if len(activityIds) > 0 {
  453. for _, vss := range activityIds {
  454. imgUrlResp += strconv.Itoa(vss) + ","
  455. }
  456. pars = make([]interface{}, 0)
  457. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `) `
  458. pars = append(pars, activityIds)
  459. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  460. if e != nil {
  461. err = errors.New("GetResourceDataList, Err: " + e.Error())
  462. return
  463. }
  464. var activityListRersp []*models.ActivityListResp
  465. //for _, v := range activityList {
  466. // activityListRersp = append(activityListRersp, ActivityButtonShow(v, user, make([]string, 0)))
  467. //}
  468. //var pArr []string
  469. activityListRersp = ActivityArrButtonShow(activityList, user, make([]string, 0))
  470. //处理不同的报名方式按钮回显
  471. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  472. if e != nil {
  473. err = errors.New("GetActivitySignupResp, Err: " + e.Error())
  474. return
  475. }
  476. for _, v := range activityListRersp {
  477. if v == nil {
  478. continue
  479. }
  480. if v.ActivityType == 0 {
  481. if mapAddress[v.City] != "" {
  482. imgUrlResp = mapAddress[v.City]
  483. } else {
  484. imgUrlResp = mapAddress["其它"]
  485. }
  486. } else {
  487. if mapChart[v.ChartPermissionName] != "" {
  488. imgUrlResp = mapChart[v.ChartPermissionName]
  489. }
  490. }
  491. v.ImgUrl = imgUrlResp
  492. v.SourceType = mapActivitySignup[v.ActivityId]
  493. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  494. }
  495. }
  496. //产业资源包
  497. if len(industrialResourceIdsHz) > 0 {
  498. pars = make([]interface{}, 0)
  499. var industrialResourceIdstring []string
  500. for _, v := range industrialResourceIdsHz {
  501. industrialResourceIdstring = append(industrialResourceIdstring, strconv.Itoa(v))
  502. }
  503. industrialId := strings.Join(industrialResourceIdstring, ",")
  504. condition = ` AND m.industrial_management_id IN (` + industrialId + `) AND a.article_type_id = 0 `
  505. listIndustrialResourceIds, e := models.GetSearchResourceList(condition)
  506. if e != nil {
  507. err = errors.New("GetSearchResourceList, Err: " + e.Error())
  508. return
  509. }
  510. conditionSubject := condition + ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialResourceIdsHz)) + `)`
  511. pars = append(pars, industrialResourceIdsHz)
  512. listSubjcet, e := models.GetIndustrialSubjectAllByIndustrialId(pars, conditionSubject)
  513. //合并产业关联的标的
  514. if e != nil {
  515. e = errors.New("GetIndustrialSubjectAllByIndustrialId, Err: " + e.Error())
  516. return
  517. }
  518. mapIndustrial := make(map[string]int)
  519. for _, v := range listSubjcet {
  520. for k2, v2 := range listIndustrialResourceIds {
  521. if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] == 0 {
  522. listIndustrialResourceIds[k2].IndustrialSubjectList = append(listIndustrialResourceIds[k2].IndustrialSubjectList, v)
  523. mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] = 1
  524. }
  525. }
  526. }
  527. //listIndustrialResourceIds, _ = HandleIndustryList(listIndustrialResourceIds, user, "Hz")
  528. for _, v := range listIndustrialResourceIds {
  529. v.Source = 1
  530. mapItems[fmt.Sprint("industrialsourceHz", v.IndustrialManagementId)].IndustrialResource = v
  531. }
  532. }
  533. //产业资源包
  534. if len(industrialResourceIdsYx) > 0 {
  535. pars = make([]interface{}, 0)
  536. var industrialResourceIdstring []string
  537. for _, v := range industrialResourceIdsYx {
  538. industrialResourceIdstring = append(industrialResourceIdstring, strconv.Itoa(v))
  539. }
  540. industrialId := strings.Join(industrialResourceIdstring, ",")
  541. condition = ` AND m.industrial_management_id IN (` + industrialId + `) AND a.article_type_id > 0 `
  542. listIndustrialResourceIds, e := models.GetSearchResourceList(condition)
  543. if e != nil {
  544. err = errors.New("GetSearchResourceList, Err: " + e.Error())
  545. return
  546. }
  547. fmt.Println(listIndustrialResourceIds)
  548. conditionSubject := condition + ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialResourceIdsYx)) + `)`
  549. pars = append(pars, industrialResourceIdsYx)
  550. listSubjcet, e := models.GetIndustrialSubjectAllByIndustrialId(pars, conditionSubject)
  551. //合并产业关联的标的
  552. if e != nil {
  553. e = errors.New("GetIndustrialSubjectAllByIndustrialId, Err: " + e.Error())
  554. return
  555. }
  556. mapIndustrial := make(map[string]int)
  557. for _, v := range listSubjcet {
  558. for k2, v2 := range listIndustrialResourceIds {
  559. if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] == 0 {
  560. listIndustrialResourceIds[k2].IndustrialSubjectList = append(listIndustrialResourceIds[k2].IndustrialSubjectList, v)
  561. mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] = 1
  562. }
  563. }
  564. }
  565. //listIndustrialResourceIds, _ = HandleIndustryList(listIndustrialResourceIds, user, "Yx")
  566. for _, v := range listIndustrialResourceIds {
  567. v.Source = 2
  568. mapItems[fmt.Sprint("industrialsourceYx", v.IndustrialManagementId)].IndustrialResource = v
  569. }
  570. }
  571. //处理图表
  572. if len(newchartIds) > 0 {
  573. pars = make([]interface{}, 0)
  574. condition = ` AND a.chart_id IN (` + utils.GetOrmInReplace(len(newchartIds)) + `)`
  575. pars = append(pars, newchartIds)
  576. chartDateList, e := models.GetChartListCollection(condition, pars, uid, 0, len(newchartIds))
  577. if e != nil {
  578. err = errors.New("GetResourceDataList, Err: " + e.Error())
  579. return
  580. }
  581. for _, v := range chartDateList {
  582. mapItems[fmt.Sprint("newchart", v.ChartId)].Newchart = v
  583. }
  584. }
  585. //处理专项调研
  586. if len(activityspecialIds) > 0 {
  587. pars = make([]interface{}, 0)
  588. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityspecialIds)) + `)`
  589. pars = append(pars, activityspecialIds)
  590. activitySpeciallist, e := models.GetCygxActivitySpecialDetailList(condition, pars, user.UserId, 0, len(activityspecialIds))
  591. if e != nil {
  592. err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
  593. return
  594. }
  595. UserMap, e := GetSpecialTripUserMap(activityspecialIds, user.UserId)
  596. if e != nil {
  597. err = errors.New("GetSpecialTripUserMap, Err: " + e.Error())
  598. return
  599. }
  600. for _, v := range activitySpeciallist {
  601. if mapChart[v.ChartPermissionName] != "" {
  602. imgUrlResp = mapChart[v.ChartPermissionName]
  603. }
  604. if _, ok := UserMap[v.ActivityId]; ok {
  605. v.IsTrip = 1
  606. }
  607. if v.Days == 0 {
  608. v.TripStatus = 1
  609. v.TripImgLink = v.TripImgLink
  610. } else {
  611. v.TripStatus = 2
  612. v.TripImgLink = v.TripImgLinkFix
  613. v.ActivityTimeText = v.ActivityTimeTextByDay
  614. }
  615. resultTimeStart := utils.StrTimeToTime(v.ActivityTime) //时间字符串格式转时间格式
  616. resultTimeEnd := utils.StrTimeToTime(v.ActivityTimeEnd) //时间字符串格式转时间格式
  617. if resultTimeStart.After(time.Now()) {
  618. v.ActiveState = "1"
  619. } else if time.Now().After(resultTimeEnd) {
  620. v.ActiveState = "3"
  621. } else {
  622. v.ActiveState = "2"
  623. }
  624. v.ImgUrl = imgUrlResp
  625. mapItems[fmt.Sprint("activityspecial", v.ActivityId)].Activityspecial = v
  626. }
  627. }
  628. //处理路演 处理路活动视频 处理路活音频
  629. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds) > 0 {
  630. audioIdstr := strings.Join(activityvoiceIds, ",")
  631. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  632. roadshowIdsStr := strings.Join(roadshowIds, ",")
  633. askserieVideoIdsStr := strings.Join(askserieVideoIds, ",")
  634. list, _, e := GetMicroRoadShowMycollectV12(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds), 0, audioIdstr, activityVideoIdsStr, roadshowIdsStr, askserieVideoIdsStr, user)
  635. if e != nil {
  636. err = errors.New("GetMicroRoadShowMycollectV12, Err: " + e.Error())
  637. return
  638. }
  639. // 用户权限
  640. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  641. if e != nil {
  642. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  643. return
  644. }
  645. // 获取默认图配置
  646. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  647. if e != nil {
  648. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  649. return
  650. }
  651. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  652. for i := range list {
  653. // 权限
  654. au := new(models.UserPermissionAuthInfo)
  655. au.SellerName = authInfo.SellerName
  656. au.SellerMobile = authInfo.SellerMobile
  657. au.HasPermission = authInfo.HasPermission
  658. au.OperationMode = authInfo.OperationMode
  659. if au.HasPermission == 1 {
  660. // 非宏观权限进一步判断是否有权限
  661. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  662. au.HasPermission = 2
  663. }
  664. }
  665. // 无权限的弹框提示
  666. if au.HasPermission != 1 {
  667. if au.OperationMode == UserPermissionOperationModeCall {
  668. if list[i].Type == 1 {
  669. au.PopupMsg = UserPermissionPopupMsgCallActivity
  670. } else {
  671. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  672. }
  673. } else {
  674. if list[i].Type == 1 {
  675. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  676. } else {
  677. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  678. }
  679. }
  680. }
  681. list[i].AuthInfo = au
  682. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  683. // 默认图
  684. if list[i].BackgroundImg == "" {
  685. if list[i].Type == 1 {
  686. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  687. } else {
  688. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  689. }
  690. }
  691. // 分享图
  692. if list[i].ShareImg == "" {
  693. if list[i].Type == 1 {
  694. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  695. } else {
  696. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  697. }
  698. }
  699. }
  700. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  701. for _, item := range list {
  702. if item.Type == 1 {
  703. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  704. } else if item.Type == 2 {
  705. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  706. } else if item.Type == 3 {
  707. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  708. } else if item.Type == 4 {
  709. mapItems[fmt.Sprint(utils.CYGX_OBJ_ASKSERIEVIDEO, item.Id)].AskserieVideo = item
  710. }
  711. }
  712. }
  713. //处理研选专栏
  714. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  715. if lenyanxuanSpecialIds > 0 {
  716. pars = make([]interface{}, 0)
  717. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  718. pars = append(pars, yanxuanSpecialIds)
  719. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  720. if e != nil {
  721. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  722. return
  723. }
  724. for _, v := range listyanxuanSpecial {
  725. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  726. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  727. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  728. }
  729. }
  730. for _, vList := range list {
  731. for _, v := range mapItems {
  732. //如果这些类型都为空,那么就不合并
  733. if v.Article == nil && v.Newchart == nil && v.Roadshow == nil && v.Activity == nil && v.Activityvideo == nil && v.Activityvoice == nil && v.Activityspecial == nil && v.Researchsummary == nil && v.Minutessummary == nil && v.Meetingreviewchapt == nil && v.ProductInterior == nil && v.IndustrialResource == nil && v.YanxuanSpecial == nil && v.AskserieVideo == nil {
  734. continue
  735. }
  736. if v.Article != nil && v.SourceId == vList.SourceId {
  737. v.Article.Title = vList.Title
  738. }
  739. if v.Newchart != nil && v.SourceId == vList.SourceId {
  740. v.Newchart.Title = vList.Title
  741. }
  742. if v.Roadshow != nil && v.SourceId == vList.SourceId {
  743. v.Roadshow.Title = vList.Title
  744. }
  745. if v.Activity != nil && v.SourceId == vList.SourceId {
  746. v.Activity.ActivityName = vList.Title
  747. }
  748. if v.Activityvideo != nil && v.SourceId == vList.SourceId {
  749. v.Activityvideo.Title = vList.Title
  750. }
  751. if v.Activityvoice != nil && v.SourceId == vList.SourceId {
  752. v.Activityvoice.Title = vList.Title
  753. }
  754. if v.Activityspecial != nil && v.SourceId == vList.SourceId {
  755. v.Activityspecial.ResearchTheme = vList.Title
  756. }
  757. if v.Researchsummary != nil && v.SourceId == vList.SourceId {
  758. v.Researchsummary.Title = vList.Title
  759. }
  760. if v.Minutessummary != nil && v.SourceId == vList.SourceId {
  761. v.Minutessummary.Title = vList.Title
  762. }
  763. if v.ProductInterior != nil && v.SourceId == vList.SourceId {
  764. v.ProductInterior.Title = vList.Title
  765. }
  766. if v.IndustrialResource != nil && v.SourceId == vList.SourceId && v.Source == vList.Source {
  767. v.IndustrialResource.UpdateTime = strings.Replace(vList.PublishDate, "00:00:00", "", -1)
  768. }
  769. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  770. items = append(items, v)
  771. }
  772. }
  773. }
  774. for _, v := range items {
  775. if v.IndustrialResource != nil {
  776. v.Source = "industrialsource"
  777. }
  778. }
  779. return
  780. }
  781. // Es研选专栏
  782. func EsAddYanxuanSpecial(sourceId int) {
  783. var err error
  784. defer func() {
  785. if err != nil {
  786. fmt.Println("err:", err)
  787. go utils.SendAlarmMsg(fmt.Sprint("更新研选专栏失败sourceId: ", sourceId), 2)
  788. }
  789. }()
  790. detail, e := models.GetYanxuanSpecialItemById(sourceId)
  791. if e != nil {
  792. err = errors.New("GetArticleInfoOtherByArticleId" + e.Error())
  793. return
  794. }
  795. content := html.UnescapeString(detail.Content)
  796. doc, e := goquery.NewDocumentFromReader(strings.NewReader(content))
  797. if e != nil {
  798. err = errors.New("goquery.NewDocumentFromReader" + e.Error())
  799. return
  800. }
  801. bodyText := doc.Text()
  802. item := new(ElasticComprehensiveDetail)
  803. item.SourceId = detail.Id
  804. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  805. item.Title = detail.Title
  806. item.PublishDate = detail.PublishTime
  807. item.BodyText = bodyText
  808. if detail.Status == 3 {
  809. EsAddOrEditComprehensiveData(item) //如果发布了就新增
  810. } else {
  811. EsDeleteComprehensiveData(item) // 没有发布就删除
  812. }
  813. return
  814. }
  815. // 新增和修改数据
  816. func EsAddOrEditComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  817. indexName := utils.IndexNameComprehensive
  818. //return
  819. defer func() {
  820. if err != nil {
  821. fmt.Println(err, item.SourceId)
  822. //go utils.SendAlarmMsg("更新综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  823. }
  824. }()
  825. client := utils.Client
  826. mustMap := make([]interface{}, 0)
  827. mustMap = append(mustMap, map[string]interface{}{
  828. "term": map[string]interface{}{
  829. "SourceId": item.SourceId,
  830. },
  831. })
  832. mustMap = append(mustMap, map[string]interface{}{
  833. "term": map[string]interface{}{
  834. "Source": item.Source,
  835. },
  836. })
  837. queryMap := map[string]interface{}{
  838. "query": map[string]interface{}{
  839. "bool": map[string]interface{}{
  840. "must": mustMap,
  841. },
  842. },
  843. }
  844. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  845. total, e := requestTotalHits.Do(context.Background())
  846. if e != nil {
  847. err = errors.New("requestTotalHits.Do(context.Background()), Err: " + e.Error())
  848. return
  849. }
  850. //return
  851. //根据来源以及ID ,判断内容是否存在,如果存在就新增,如果不存在就修改
  852. if total == 0 {
  853. resp, e := client.Index().Index(indexName).BodyJson(item).Do(context.Background())
  854. if e != nil {
  855. err = errors.New("client.Index().Index(indexName).BodyJson(item).Do(context.Background()), Err: " + e.Error())
  856. return
  857. }
  858. if resp.Status == 0 && resp.Result == "created" {
  859. //fmt.Println("新增成功")
  860. //err = nil
  861. return
  862. } else {
  863. //err = errors.New(fmt.Sprint(resp))
  864. err = errors.New(fmt.Sprint("articleId", item.SourceId))
  865. return
  866. }
  867. } else {
  868. //拼接需要改动的前置条件
  869. bool_query := elastic.NewBoolQuery()
  870. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  871. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  872. //设置需要改动的内容
  873. var script string
  874. script += fmt.Sprint("ctx._source['SubjectNames'] = '", item.SubjectNames, "';")
  875. script += fmt.Sprint("ctx._source['PublishDate'] = '", item.PublishDate, "';")
  876. script += fmt.Sprint("ctx._source['IsSummary'] = ", item.IsSummary, ";")
  877. script += fmt.Sprint("ctx._source['Abstract'] = '", item.Abstract, "';")
  878. script += fmt.Sprint("ctx._source['Title'] = '", item.Title, "';")
  879. script += fmt.Sprint("ctx._source['BodyText'] = '", item.BodyText, "';")
  880. script += fmt.Sprint("ctx._source['Annotation'] = '", item.Annotation, "';")
  881. script += fmt.Sprint("ctx._source['IndustryName'] = '", item.IndustryName, "'")
  882. _, e = client.UpdateByQuery(indexName).
  883. Query(bool_query).
  884. Script(elastic.NewScriptInline(script)).
  885. Refresh("true").
  886. Do(context.Background())
  887. if e != nil && e.Error() != "elastic: Error 400 (Bad Request): compile error [type=script_exception]" {
  888. //文本内容过长的时候,修改会报 400 的错误,暂时先不处理
  889. //fmt.Println("err", e.Error())
  890. ////err = e
  891. //err = errors.New("client.UpdateByQuery(indexName), Err: " + e.Error())
  892. return
  893. }
  894. }
  895. return
  896. }
  897. // 删除数据
  898. func EsDeleteComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  899. defer func() {
  900. if err != nil {
  901. fmt.Println(err)
  902. go utils.SendAlarmMsg("删除数据综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  903. }
  904. }()
  905. indexName := utils.IndexNameComprehensive
  906. client := utils.Client
  907. //拼接需要删除的前置条件
  908. bool_query := elastic.NewBoolQuery()
  909. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  910. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  911. _, e := client.DeleteByQuery(indexName).
  912. Query(bool_query).
  913. Do(context.Background())
  914. if e != nil {
  915. err = errors.New(" client.DeleteByQuery(indexName), Err: " + e.Error())
  916. return
  917. }
  918. return
  919. }