es_comprehensive.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt 、 产品内测:productinterior
  294. for _, v := range list {
  295. if v.Source == "article" {
  296. articleIds = append(articleIds, v.SourceId)
  297. } else if v.Source == "newchart" {
  298. newchartIds = append(newchartIds, v.SourceId)
  299. } else if v.Source == "roadshow" {
  300. roadshowIds = append(roadshowIds, strconv.Itoa(v.SourceId))
  301. } else if v.Source == "activity" {
  302. activityIds = append(activityIds, v.SourceId)
  303. } else if v.Source == "activityvideo" {
  304. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  305. } else if v.Source == "activityvoice" {
  306. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  307. } else if v.Source == "activityspecial" {
  308. activityspecialIds = append(activityspecialIds, v.SourceId)
  309. } else if v.Source == "researchsummary" {
  310. researchsummaryIds = append(researchsummaryIds, v.SourceId)
  311. } else if v.Source == "minutessummary" {
  312. minutessummaryIds = append(minutessummaryIds, v.SourceId)
  313. } else if v.Source == "meetingreviewchapt" {
  314. meetingreviewchaptIds = append(meetingreviewchaptIds, v.SourceId)
  315. } else if v.Source == "productinterior" {
  316. productinteriorIds = append(productinteriorIds, v.SourceId)
  317. } else if v.Source == "industrialsourceHz" {
  318. industrialResourceIdsHz = append(industrialResourceIdsHz, v.SourceId)
  319. } else if v.Source == "industrialsourceYx" {
  320. industrialResourceIdsYx = append(industrialResourceIdsYx, v.SourceId)
  321. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  322. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  323. }
  324. }
  325. detail, e := models.GetConfigByCode("city_img_url")
  326. if e != nil {
  327. err = errors.New("GetResourceDataList, Err: " + e.Error())
  328. return
  329. }
  330. detailChart, e := models.GetConfigByCode("chart_img_url")
  331. if e != nil {
  332. err = errors.New("GetResourceDataList, Err: " + e.Error())
  333. return
  334. }
  335. addressList := strings.Split(detail.ConfigValue, "{|}")
  336. mapAddress := make(map[string]string)
  337. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  338. mapChart := make(map[string]string)
  339. var cityName string
  340. var chartName string
  341. var imgUrl string
  342. var imgUrlChart string
  343. for _, v := range addressList {
  344. vslice := strings.Split(v, "_")
  345. cityName = vslice[0]
  346. imgUrl = vslice[len(vslice)-1]
  347. mapAddress[cityName] = imgUrl
  348. }
  349. for _, v := range chartList {
  350. vslice := strings.Split(v, "_")
  351. chartName = vslice[0]
  352. imgUrlChart = vslice[len(vslice)-1]
  353. mapChart[chartName] = imgUrlChart
  354. }
  355. var imgUrlResp string
  356. //处理文章
  357. if len(articleIds) > 0 {
  358. pars = make([]interface{}, 0)
  359. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  360. pars = append(pars, articleIds)
  361. articleList, e := models.GetHomeListPublic(condition, pars, 0, len(articleIds))
  362. if e != nil {
  363. err = errors.New("GetResourceDataList, Err: " + e.Error())
  364. return
  365. }
  366. articleList, e = HandleArticleCategoryImg(articleList, user)
  367. if e != nil {
  368. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  369. return
  370. }
  371. for _, v := range articleList {
  372. v.Body = ""
  373. if titleHighlight[v.ArticleId] != "" {
  374. v.Title = titleHighlight[v.ArticleId]
  375. }
  376. if len(bodyHighlight[v.ArticleId]) > 0 {
  377. v.Abstract = ""
  378. v.Annotation = ""
  379. v.BodyHighlight = bodyHighlight[v.ArticleId]
  380. } else {
  381. v.BodyHighlight = make([]string, 0)
  382. }
  383. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  384. }
  385. }
  386. //处理晨会精华
  387. if len(meetingreviewchaptIds) > 0 {
  388. pars = make([]interface{}, 0)
  389. condition = ` AND c.id IN (` + utils.GetOrmInReplace(len(meetingreviewchaptIds)) + `)`
  390. pars = append(pars, meetingreviewchaptIds)
  391. listrMeet, e := models.GetCygxMorningMeetingReviewChapterList(condition, pars)
  392. if e != nil {
  393. err = errors.New("GetResourceDataList, Err: " + e.Error())
  394. return
  395. }
  396. for _, v := range listrMeet {
  397. v.Content = AnnotationHtml(v.Content)
  398. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  399. mapItems[fmt.Sprint("meetingreviewchapt", v.Id)].Meetingreviewchapt = v
  400. }
  401. }
  402. //处理上周纪要汇总
  403. if len(minutessummaryIds) > 0 {
  404. pars = make([]interface{}, 0)
  405. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(minutessummaryIds)) + `)`
  406. pars = append(pars, minutessummaryIds)
  407. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_minutes_summary", pars, 0, len(minutessummaryIds))
  408. if e != nil {
  409. err = errors.New("GetResourceDataList, Err: " + e.Error())
  410. return
  411. }
  412. for _, v := range listresearchsummary {
  413. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  414. mapItems[fmt.Sprint("minutessummary", v.ArticleId)].Minutessummary = v
  415. }
  416. }
  417. //处理本周纪要汇总
  418. if len(researchsummaryIds) > 0 {
  419. pars = make([]interface{}, 0)
  420. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(researchsummaryIds)) + `)`
  421. pars = append(pars, researchsummaryIds)
  422. listresearchsummary, e := models.GetReportSelectionListHome(condition, "cygx_research_summary", pars, 0, len(researchsummaryIds))
  423. if e != nil {
  424. err = errors.New("GetReportSelectionListHome, Err: " + e.Error())
  425. return
  426. }
  427. for _, v := range listresearchsummary {
  428. v.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  429. mapItems[fmt.Sprint("researchsummary", v.ArticleId)].Researchsummary = v
  430. }
  431. }
  432. //处理产品内测
  433. if len(productinteriorIds) > 0 {
  434. pars = make([]interface{}, 0)
  435. condition = ` AND art.status = 1 AND art.product_interior_id IN (` + utils.GetOrmInReplace(len(productinteriorIds)) + `)`
  436. pars = append(pars, productinteriorIds)
  437. listProductInterior, e := models.GetCygxProductInteriorList(condition, pars, 0, len(productinteriorIds))
  438. if e != nil {
  439. err = errors.New("GetCygxProductInteriorList, Err: " + e.Error())
  440. return
  441. }
  442. for _, v := range listProductInterior {
  443. v.Body = ProductInteriorHtml(v.Body)
  444. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  445. mapItems[fmt.Sprint("productinterior", v.ProductInteriorId)].ProductInterior = v
  446. }
  447. }
  448. //处理活动
  449. if len(activityIds) > 0 {
  450. for _, vss := range activityIds {
  451. imgUrlResp += strconv.Itoa(vss) + ","
  452. }
  453. pars = make([]interface{}, 0)
  454. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `) `
  455. pars = append(pars, activityIds)
  456. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  457. if e != nil {
  458. err = errors.New("GetResourceDataList, Err: " + e.Error())
  459. return
  460. }
  461. var activityListRersp []*models.ActivityListResp
  462. //for _, v := range activityList {
  463. // activityListRersp = append(activityListRersp, ActivityButtonShow(v, user, make([]string, 0)))
  464. //}
  465. //var pArr []string
  466. activityListRersp = ActivityArrButtonShow(activityList, user, make([]string, 0))
  467. //处理不同的报名方式按钮回显
  468. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  469. if e != nil {
  470. err = errors.New("GetActivitySignupResp, Err: " + e.Error())
  471. return
  472. }
  473. for _, v := range activityListRersp {
  474. if v == nil {
  475. continue
  476. }
  477. if v.ActivityType == 0 {
  478. if mapAddress[v.City] != "" {
  479. imgUrlResp = mapAddress[v.City]
  480. } else {
  481. imgUrlResp = mapAddress["其它"]
  482. }
  483. } else {
  484. if mapChart[v.ChartPermissionName] != "" {
  485. imgUrlResp = mapChart[v.ChartPermissionName]
  486. }
  487. }
  488. v.ImgUrl = imgUrlResp
  489. v.SourceType = mapActivitySignup[v.ActivityId]
  490. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  491. }
  492. }
  493. //产业资源包
  494. if len(industrialResourceIdsHz) > 0 {
  495. pars = make([]interface{}, 0)
  496. var industrialResourceIdstring []string
  497. for _, v := range industrialResourceIdsHz {
  498. industrialResourceIdstring = append(industrialResourceIdstring, strconv.Itoa(v))
  499. }
  500. industrialId := strings.Join(industrialResourceIdstring, ",")
  501. condition = ` AND m.industrial_management_id IN (` + industrialId + `) AND a.article_type_id = 0 `
  502. listIndustrialResourceIds, e := models.GetSearchResourceList(condition)
  503. if e != nil {
  504. err = errors.New("GetSearchResourceList, Err: " + e.Error())
  505. return
  506. }
  507. conditionSubject := condition + ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialResourceIdsHz)) + `)`
  508. pars = append(pars, industrialResourceIdsHz)
  509. listSubjcet, e := models.GetIndustrialSubjectAllByIndustrialId(pars, conditionSubject)
  510. //合并产业关联的标的
  511. if e != nil {
  512. e = errors.New("GetIndustrialSubjectAllByIndustrialId, Err: " + e.Error())
  513. return
  514. }
  515. mapIndustrial := make(map[string]int)
  516. for _, v := range listSubjcet {
  517. for k2, v2 := range listIndustrialResourceIds {
  518. if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] == 0 {
  519. listIndustrialResourceIds[k2].IndustrialSubjectList = append(listIndustrialResourceIds[k2].IndustrialSubjectList, v)
  520. mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] = 1
  521. }
  522. }
  523. }
  524. //listIndustrialResourceIds, _ = HandleIndustryList(listIndustrialResourceIds, user, "Hz")
  525. for _, v := range listIndustrialResourceIds {
  526. v.Source = 1
  527. mapItems[fmt.Sprint("industrialsourceHz", v.IndustrialManagementId)].IndustrialResource = v
  528. }
  529. }
  530. //产业资源包
  531. if len(industrialResourceIdsYx) > 0 {
  532. pars = make([]interface{}, 0)
  533. var industrialResourceIdstring []string
  534. for _, v := range industrialResourceIdsYx {
  535. industrialResourceIdstring = append(industrialResourceIdstring, strconv.Itoa(v))
  536. }
  537. industrialId := strings.Join(industrialResourceIdstring, ",")
  538. condition = ` AND m.industrial_management_id IN (` + industrialId + `) AND a.article_type_id > 0 `
  539. listIndustrialResourceIds, e := models.GetSearchResourceList(condition)
  540. if e != nil {
  541. err = errors.New("GetSearchResourceList, Err: " + e.Error())
  542. return
  543. }
  544. fmt.Println(listIndustrialResourceIds)
  545. conditionSubject := condition + ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialResourceIdsYx)) + `)`
  546. pars = append(pars, industrialResourceIdsYx)
  547. listSubjcet, e := models.GetIndustrialSubjectAllByIndustrialId(pars, conditionSubject)
  548. //合并产业关联的标的
  549. if e != nil {
  550. e = errors.New("GetIndustrialSubjectAllByIndustrialId, Err: " + e.Error())
  551. return
  552. }
  553. mapIndustrial := make(map[string]int)
  554. for _, v := range listSubjcet {
  555. for k2, v2 := range listIndustrialResourceIds {
  556. if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] == 0 {
  557. listIndustrialResourceIds[k2].IndustrialSubjectList = append(listIndustrialResourceIds[k2].IndustrialSubjectList, v)
  558. mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, v.SubjectName)] = 1
  559. }
  560. }
  561. }
  562. //listIndustrialResourceIds, _ = HandleIndustryList(listIndustrialResourceIds, user, "Yx")
  563. for _, v := range listIndustrialResourceIds {
  564. v.Source = 2
  565. mapItems[fmt.Sprint("industrialsourceYx", v.IndustrialManagementId)].IndustrialResource = v
  566. }
  567. }
  568. //处理图表
  569. if len(newchartIds) > 0 {
  570. pars = make([]interface{}, 0)
  571. condition = ` AND a.chart_id IN (` + utils.GetOrmInReplace(len(newchartIds)) + `)`
  572. pars = append(pars, newchartIds)
  573. chartDateList, e := models.GetChartListCollection(condition, pars, uid, 0, len(newchartIds))
  574. if e != nil {
  575. err = errors.New("GetResourceDataList, Err: " + e.Error())
  576. return
  577. }
  578. for _, v := range chartDateList {
  579. mapItems[fmt.Sprint("newchart", v.ChartId)].Newchart = v
  580. }
  581. }
  582. //处理专项调研
  583. if len(activityspecialIds) > 0 {
  584. pars = make([]interface{}, 0)
  585. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityspecialIds)) + `)`
  586. pars = append(pars, activityspecialIds)
  587. activitySpeciallist, e := models.GetCygxActivitySpecialDetailList(condition, pars, user.UserId, 0, len(activityspecialIds))
  588. if e != nil {
  589. err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
  590. return
  591. }
  592. UserMap, e := GetSpecialTripUserMap(activityspecialIds, user.UserId)
  593. if e != nil {
  594. err = errors.New("GetSpecialTripUserMap, Err: " + e.Error())
  595. return
  596. }
  597. for _, v := range activitySpeciallist {
  598. if mapChart[v.ChartPermissionName] != "" {
  599. imgUrlResp = mapChart[v.ChartPermissionName]
  600. }
  601. if _, ok := UserMap[v.ActivityId]; ok {
  602. v.IsTrip = 1
  603. }
  604. if v.Days == 0 {
  605. v.TripStatus = 1
  606. v.TripImgLink = v.TripImgLink
  607. } else {
  608. v.TripStatus = 2
  609. v.TripImgLink = v.TripImgLinkFix
  610. v.ActivityTimeText = v.ActivityTimeTextByDay
  611. }
  612. resultTimeStart := utils.StrTimeToTime(v.ActivityTime) //时间字符串格式转时间格式
  613. resultTimeEnd := utils.StrTimeToTime(v.ActivityTimeEnd) //时间字符串格式转时间格式
  614. if resultTimeStart.After(time.Now()) {
  615. v.ActiveState = "1"
  616. } else if time.Now().After(resultTimeEnd) {
  617. v.ActiveState = "3"
  618. } else {
  619. v.ActiveState = "2"
  620. }
  621. v.ImgUrl = imgUrlResp
  622. mapItems[fmt.Sprint("activityspecial", v.ActivityId)].Activityspecial = v
  623. }
  624. }
  625. //处理路演 处理路活动视频 处理路活音频
  626. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds) > 0 {
  627. audioIdstr := strings.Join(activityvoiceIds, ",")
  628. ideoIdsStr := strings.Join(roadshowIds, ",")
  629. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  630. list, _, e := GetMicroRoadShowMycollect(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds), 0, audioIdstr, ideoIdsStr, activityVideoIdsStr)
  631. if e != nil {
  632. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  633. return
  634. }
  635. fmt.Println("list", list)
  636. for _, item := range list {
  637. if item.Type == 1 {
  638. //音频
  639. count, e := models.GetVoiceCollectCount(user.UserId, item.Id)
  640. if e != nil {
  641. err = errors.New("GetVoiceCollectCount, Err: " + e.Error())
  642. return
  643. }
  644. if count > 0 {
  645. item.IsCollect = true
  646. }
  647. } else if item.Type == 2 {
  648. //活动视频
  649. count, e := models.GetActivityVideoCollectCount(user.UserId, item.Id)
  650. if e != nil {
  651. err = errors.New("GetActivityVideoCollectCount, Err: " + e.Error())
  652. return
  653. }
  654. if count > 0 {
  655. item.IsCollect = true
  656. }
  657. } else if item.Type == 3 {
  658. //微路演视频
  659. count, e := models.GetVideoCollectCount(user.UserId, item.Id)
  660. if e != nil {
  661. err = errors.New("GetVideoCollectCount, Err: " + e.Error())
  662. return
  663. }
  664. if count > 0 {
  665. item.IsCollect = true
  666. }
  667. }
  668. }
  669. // 用户权限
  670. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  671. if e != nil {
  672. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  673. return
  674. }
  675. // 获取默认图配置
  676. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  677. if e != nil {
  678. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  679. return
  680. }
  681. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  682. for i := range list {
  683. // 权限
  684. au := new(models.UserPermissionAuthInfo)
  685. au.SellerName = authInfo.SellerName
  686. au.SellerMobile = authInfo.SellerMobile
  687. au.HasPermission = authInfo.HasPermission
  688. au.OperationMode = authInfo.OperationMode
  689. if au.HasPermission == 1 {
  690. // 非宏观权限进一步判断是否有权限
  691. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  692. au.HasPermission = 2
  693. }
  694. }
  695. // 无权限的弹框提示
  696. if au.HasPermission != 1 {
  697. if au.OperationMode == UserPermissionOperationModeCall {
  698. if list[i].Type == 1 {
  699. au.PopupMsg = UserPermissionPopupMsgCallActivity
  700. } else {
  701. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  702. }
  703. } else {
  704. if list[i].Type == 1 {
  705. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  706. } else {
  707. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  708. }
  709. }
  710. }
  711. list[i].AuthInfo = au
  712. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  713. // 默认图
  714. if list[i].BackgroundImg == "" {
  715. if list[i].Type == 1 {
  716. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  717. } else {
  718. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  719. }
  720. }
  721. // 分享图
  722. if list[i].ShareImg == "" {
  723. if list[i].Type == 1 {
  724. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  725. } else {
  726. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  727. }
  728. }
  729. }
  730. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  731. for _, item := range list {
  732. if item.Type == 1 {
  733. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  734. } else if item.Type == 2 {
  735. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  736. } else if item.Type == 3 {
  737. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  738. }
  739. }
  740. }
  741. //处理研选专栏
  742. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  743. if lenyanxuanSpecialIds > 0 {
  744. pars = make([]interface{}, 0)
  745. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  746. pars = append(pars, yanxuanSpecialIds)
  747. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  748. if e != nil {
  749. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  750. return
  751. }
  752. for _, v := range listyanxuanSpecial {
  753. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  754. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  755. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  756. }
  757. }
  758. for _, vList := range list {
  759. for _, v := range mapItems {
  760. //如果这些类型都为空,那么就不合并
  761. 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 {
  762. continue
  763. }
  764. if v.Article != nil && v.SourceId == vList.SourceId {
  765. v.Article.Title = vList.Title
  766. }
  767. if v.Newchart != nil && v.SourceId == vList.SourceId {
  768. v.Newchart.Title = vList.Title
  769. }
  770. if v.Roadshow != nil && v.SourceId == vList.SourceId {
  771. v.Roadshow.Title = vList.Title
  772. }
  773. if v.Activity != nil && v.SourceId == vList.SourceId {
  774. v.Activity.ActivityName = vList.Title
  775. }
  776. if v.Activityvideo != nil && v.SourceId == vList.SourceId {
  777. v.Activityvideo.Title = vList.Title
  778. }
  779. if v.Activityvoice != nil && v.SourceId == vList.SourceId {
  780. v.Activityvoice.Title = vList.Title
  781. }
  782. if v.Activityspecial != nil && v.SourceId == vList.SourceId {
  783. v.Activityspecial.ResearchTheme = vList.Title
  784. }
  785. if v.Researchsummary != nil && v.SourceId == vList.SourceId {
  786. v.Researchsummary.Title = vList.Title
  787. }
  788. if v.Minutessummary != nil && v.SourceId == vList.SourceId {
  789. v.Minutessummary.Title = vList.Title
  790. }
  791. if v.ProductInterior != nil && v.SourceId == vList.SourceId {
  792. v.ProductInterior.Title = vList.Title
  793. }
  794. if v.IndustrialResource != nil && v.SourceId == vList.SourceId && v.Source == vList.Source {
  795. v.IndustrialResource.UpdateTime = strings.Replace(vList.PublishDate, "00:00:00", "", -1)
  796. }
  797. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  798. items = append(items, v)
  799. }
  800. }
  801. }
  802. for _, v := range items {
  803. if v.IndustrialResource != nil {
  804. v.Source = "industrialsource"
  805. }
  806. }
  807. return
  808. }
  809. // Es研选专栏
  810. func EsAddYanxuanSpecial(sourceId int) {
  811. var err error
  812. defer func() {
  813. if err != nil {
  814. fmt.Println("err:", err)
  815. go utils.SendAlarmMsg(fmt.Sprint("更新研选专栏失败sourceId: ", sourceId), 2)
  816. }
  817. }()
  818. detail, e := models.GetYanxuanSpecialItemById(sourceId)
  819. if e != nil {
  820. err = errors.New("GetArticleInfoOtherByArticleId" + e.Error())
  821. return
  822. }
  823. content := html.UnescapeString(detail.Content)
  824. doc, e := goquery.NewDocumentFromReader(strings.NewReader(content))
  825. if e != nil {
  826. err = errors.New("goquery.NewDocumentFromReader" + e.Error())
  827. return
  828. }
  829. bodyText := doc.Text()
  830. item := new(ElasticComprehensiveDetail)
  831. item.SourceId = detail.Id
  832. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  833. item.Title = detail.Title
  834. item.PublishDate = detail.PublishTime
  835. item.BodyText = bodyText
  836. if detail.Status == 3 {
  837. EsAddOrEditComprehensiveData(item) //如果发布了就新增
  838. } else {
  839. EsDeleteComprehensiveData(item) // 没有发布就删除
  840. }
  841. return
  842. }
  843. // 新增和修改数据
  844. func EsAddOrEditComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  845. indexName := utils.IndexNameComprehensive
  846. //return
  847. defer func() {
  848. if err != nil {
  849. fmt.Println(err, item.SourceId)
  850. //go utils.SendAlarmMsg("更新综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  851. }
  852. }()
  853. client := utils.Client
  854. mustMap := make([]interface{}, 0)
  855. mustMap = append(mustMap, map[string]interface{}{
  856. "term": map[string]interface{}{
  857. "SourceId": item.SourceId,
  858. },
  859. })
  860. mustMap = append(mustMap, map[string]interface{}{
  861. "term": map[string]interface{}{
  862. "Source": item.Source,
  863. },
  864. })
  865. queryMap := map[string]interface{}{
  866. "query": map[string]interface{}{
  867. "bool": map[string]interface{}{
  868. "must": mustMap,
  869. },
  870. },
  871. }
  872. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  873. total, e := requestTotalHits.Do(context.Background())
  874. if e != nil {
  875. err = errors.New("requestTotalHits.Do(context.Background()), Err: " + e.Error())
  876. return
  877. }
  878. //return
  879. //根据来源以及ID ,判断内容是否存在,如果存在就新增,如果不存在就修改
  880. if total == 0 {
  881. resp, e := client.Index().Index(indexName).BodyJson(item).Do(context.Background())
  882. if e != nil {
  883. err = errors.New("client.Index().Index(indexName).BodyJson(item).Do(context.Background()), Err: " + e.Error())
  884. return
  885. }
  886. if resp.Status == 0 && resp.Result == "created" {
  887. //fmt.Println("新增成功")
  888. //err = nil
  889. return
  890. } else {
  891. //err = errors.New(fmt.Sprint(resp))
  892. err = errors.New(fmt.Sprint("articleId", item.SourceId))
  893. return
  894. }
  895. } else {
  896. //拼接需要改动的前置条件
  897. bool_query := elastic.NewBoolQuery()
  898. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  899. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  900. //设置需要改动的内容
  901. var script string
  902. script += fmt.Sprint("ctx._source['SubjectNames'] = '", item.SubjectNames, "';")
  903. script += fmt.Sprint("ctx._source['PublishDate'] = '", item.PublishDate, "';")
  904. script += fmt.Sprint("ctx._source['IsSummary'] = ", item.IsSummary, ";")
  905. script += fmt.Sprint("ctx._source['Abstract'] = '", item.Abstract, "';")
  906. script += fmt.Sprint("ctx._source['Title'] = '", item.Title, "';")
  907. script += fmt.Sprint("ctx._source['BodyText'] = '", item.BodyText, "';")
  908. script += fmt.Sprint("ctx._source['Annotation'] = '", item.Annotation, "';")
  909. script += fmt.Sprint("ctx._source['IndustryName'] = '", item.IndustryName, "'")
  910. _, e = client.UpdateByQuery(indexName).
  911. Query(bool_query).
  912. Script(elastic.NewScriptInline(script)).
  913. Refresh("true").
  914. Do(context.Background())
  915. if e != nil && e.Error() != "elastic: Error 400 (Bad Request): compile error [type=script_exception]" {
  916. //文本内容过长的时候,修改会报 400 的错误,暂时先不处理
  917. //fmt.Println("err", e.Error())
  918. ////err = e
  919. //err = errors.New("client.UpdateByQuery(indexName), Err: " + e.Error())
  920. return
  921. }
  922. }
  923. return
  924. }
  925. // 删除数据
  926. func EsDeleteComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  927. defer func() {
  928. if err != nil {
  929. fmt.Println(err)
  930. go utils.SendAlarmMsg("删除数据综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  931. }
  932. }()
  933. indexName := utils.IndexNameComprehensive
  934. client := utils.Client
  935. //拼接需要删除的前置条件
  936. bool_query := elastic.NewBoolQuery()
  937. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  938. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  939. _, e := client.DeleteByQuery(indexName).
  940. Query(bool_query).
  941. Do(context.Background())
  942. if e != nil {
  943. err = errors.New(" client.DeleteByQuery(indexName), Err: " + e.Error())
  944. return
  945. }
  946. return
  947. }