es_comprehensive.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/PuerkitoBio/goquery"
  7. "github.com/olivere/elastic/v7"
  8. "strconv"
  9. //"go/doc"
  10. "hongze/hongze_mfyx/models"
  11. "hongze/hongze_mfyx/utils"
  12. "html"
  13. //"strconv"
  14. "errors"
  15. "strings"
  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. // Es研选专栏
  43. func EsAddYanxuanSpecial(sourceId int) {
  44. var err error
  45. defer func() {
  46. if err != nil {
  47. fmt.Println("err:", err)
  48. go utils.SendAlarmMsg(fmt.Sprint("更新研选专栏失败sourceId: ", sourceId), 2)
  49. }
  50. }()
  51. detail, e := models.GetYanxuanSpecialItemById(sourceId)
  52. if e != nil {
  53. err = errors.New("GetArticleInfoOtherByArticleId" + e.Error())
  54. return
  55. }
  56. content := html.UnescapeString(detail.Content)
  57. doc, e := goquery.NewDocumentFromReader(strings.NewReader(content))
  58. if e != nil {
  59. err = errors.New("goquery.NewDocumentFromReader" + e.Error())
  60. return
  61. }
  62. bodyText := doc.Text()
  63. item := new(ElasticComprehensiveDetail)
  64. item.SourceId = detail.Id
  65. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  66. item.Title = detail.Title
  67. item.PublishDate = detail.PublishTime
  68. item.BodyText = bodyText
  69. item.Abstract = bodyText
  70. item.IndustryName = detail.IndustryTags
  71. item.SubjectNames = detail.CompanyTags + detail.Tags
  72. if detail.Status == 3 {
  73. EsAddOrEditComprehensiveData(item) //如果发布了就新增
  74. } else {
  75. EsDeleteComprehensiveData(item) // 没有发布就删除
  76. }
  77. return
  78. }
  79. // 新增和修改数据
  80. func EsAddOrEditComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  81. indexName := utils.IndexNameComprehensive
  82. //return
  83. defer func() {
  84. if err != nil {
  85. fmt.Println(err, item.SourceId)
  86. //go utils.SendAlarmMsg("更新综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  87. }
  88. }()
  89. client := utils.Client
  90. mustMap := make([]interface{}, 0)
  91. mustMap = append(mustMap, map[string]interface{}{
  92. "term": map[string]interface{}{
  93. "SourceId": item.SourceId,
  94. },
  95. })
  96. mustMap = append(mustMap, map[string]interface{}{
  97. "term": map[string]interface{}{
  98. "Source": item.Source,
  99. },
  100. })
  101. queryMap := map[string]interface{}{
  102. "query": map[string]interface{}{
  103. "bool": map[string]interface{}{
  104. "must": mustMap,
  105. },
  106. },
  107. }
  108. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  109. total, e := requestTotalHits.Do(context.Background())
  110. if e != nil {
  111. err = errors.New("requestTotalHits.Do(context.Background()), Err: " + e.Error())
  112. return
  113. }
  114. //return
  115. //根据来源以及ID ,判断内容是否存在,如果存在就新增,如果不存在就修改
  116. if total == 0 {
  117. resp, e := client.Index().Index(indexName).BodyJson(item).Do(context.Background())
  118. if e != nil {
  119. err = errors.New("client.Index().Index(indexName).BodyJson(item).Do(context.Background()), Err: " + e.Error())
  120. return
  121. }
  122. if resp.Status == 0 && resp.Result == "created" {
  123. //fmt.Println("新增成功")
  124. //err = nil
  125. return
  126. } else {
  127. //err = errors.New(fmt.Sprint(resp))
  128. err = errors.New(fmt.Sprint("articleId", item.SourceId))
  129. return
  130. }
  131. } else {
  132. //拼接需要改动的前置条件
  133. bool_query := elastic.NewBoolQuery()
  134. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  135. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  136. //设置需要改动的内容
  137. var script string
  138. script += fmt.Sprint("ctx._source['SubjectNames'] = '", item.SubjectNames, "';")
  139. script += fmt.Sprint("ctx._source['PublishDate'] = '", item.PublishDate, "';")
  140. script += fmt.Sprint("ctx._source['IsSummary'] = ", item.IsSummary, ";")
  141. script += fmt.Sprint("ctx._source['Abstract'] = '", item.Abstract, "';")
  142. script += fmt.Sprint("ctx._source['Title'] = '", item.Title, "';")
  143. script += fmt.Sprint("ctx._source['BodyText'] = '", item.BodyText, "';")
  144. script += fmt.Sprint("ctx._source['Annotation'] = '", item.Annotation, "';")
  145. script += fmt.Sprint("ctx._source['IndustryName'] = '", item.IndustryName, "'")
  146. _, e = client.UpdateByQuery(indexName).
  147. Query(bool_query).
  148. Script(elastic.NewScriptInline(script)).
  149. Refresh("true").
  150. Do(context.Background())
  151. if e != nil && e.Error() != "elastic: Error 400 (Bad Request): compile error [type=script_exception]" {
  152. //文本内容过长的时候,修改会报 400 的错误,暂时先不处理
  153. //fmt.Println("err", e.Error())
  154. ////err = e
  155. //err = errors.New("client.UpdateByQuery(indexName), Err: " + e.Error())
  156. return
  157. }
  158. }
  159. return
  160. }
  161. // 删除数据
  162. func EsDeleteComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  163. defer func() {
  164. if err != nil {
  165. fmt.Println(err)
  166. go utils.SendAlarmMsg("删除数据综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  167. }
  168. }()
  169. indexName := utils.IndexNameComprehensive
  170. client := utils.Client
  171. //拼接需要删除的前置条件
  172. bool_query := elastic.NewBoolQuery()
  173. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  174. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  175. _, e := client.DeleteByQuery(indexName).
  176. Query(bool_query).
  177. Do(context.Background())
  178. if e != nil {
  179. err = errors.New(" client.DeleteByQuery(indexName), Err: " + e.Error())
  180. return
  181. }
  182. return
  183. }
  184. func EsComprehensiveSearch(keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int64, err error) {
  185. indexName := utils.IndexNameComprehensive
  186. client := utils.Client
  187. keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
  188. keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
  189. keyWordLen := len(keyWordArr)
  190. if keyWordLen <= 0 {
  191. keyWordArr = append(keyWordArr, keyWord)
  192. keyWordLen = len(keyWordArr)
  193. }
  194. //Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
  195. //ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
  196. mustMap := make([]interface{}, 0)
  197. shouldMap := make(map[string]interface{}, 0)
  198. shouldMapquery := make([]interface{}, 0)
  199. mustNotMap := make([]interface{}, 0)
  200. shouldNotMap := make(map[string]interface{}, 0)
  201. shouldNotMapquery := make([]interface{}, 0)
  202. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  203. //keyWordWeight := GetWeight(keyWordLen)
  204. var boost int
  205. //lenkeyWordArr := len(keyWordArr)
  206. for k, v := range keyWordArr {
  207. if k > 0 {
  208. continue
  209. }
  210. if k == 0 {
  211. boost = 2 * 1000
  212. } else {
  213. boost = 1
  214. }
  215. if v != "" {
  216. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  217. "function_score": map[string]interface{}{
  218. "query": map[string]interface{}{
  219. "multi_match": map[string]interface{}{
  220. //"boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
  221. "boost": boost, //给查询的值赋予权重
  222. "fields": []interface{}{"Title"},
  223. "query": v,
  224. },
  225. },
  226. },
  227. })
  228. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  229. "function_score": map[string]interface{}{
  230. "query": map[string]interface{}{
  231. "multi_match": map[string]interface{}{
  232. "boost": boost, //给查询的值赋予权重
  233. "fields": []interface{}{"Abstract"},
  234. "query": v,
  235. },
  236. },
  237. },
  238. })
  239. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  240. "function_score": map[string]interface{}{
  241. "query": map[string]interface{}{
  242. "multi_match": map[string]interface{}{
  243. "boost": boost, //给查询的值赋予权重
  244. "fields": []interface{}{"Annotation"},
  245. "query": v,
  246. },
  247. },
  248. },
  249. })
  250. //shouldMapquery = append(shouldMapquery, map[string]interface{}{
  251. // "function_score": map[string]interface{}{
  252. // "query": map[string]interface{}{
  253. // "multi_match": map[string]interface{}{
  254. // //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  255. // "boost": boost, //给查询的值赋予权重
  256. // "fields": []interface{}{"BodyText"},
  257. // "query": v,
  258. // },
  259. // },
  260. // },
  261. //})
  262. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  263. "function_score": map[string]interface{}{
  264. "query": map[string]interface{}{
  265. "multi_match": map[string]interface{}{
  266. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  267. "boost": boost, //给查询的值赋予权重
  268. "fields": []interface{}{"IndustryName"},
  269. "query": v,
  270. },
  271. },
  272. },
  273. })
  274. shouldMapquery = append(shouldMapquery, map[string]interface{}{
  275. "function_score": map[string]interface{}{
  276. "query": map[string]interface{}{
  277. "multi_match": map[string]interface{}{
  278. //"boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
  279. "boost": boost, //给查询的值赋予权重
  280. "fields": []interface{}{"SubjectNames"},
  281. "query": v,
  282. },
  283. },
  284. },
  285. })
  286. }
  287. }
  288. shouldMap = map[string]interface{}{
  289. "should": shouldMapquery,
  290. }
  291. shouldNotMap = map[string]interface{}{
  292. "should": shouldNotMapquery,
  293. }
  294. //排序
  295. sortMap := make([]interface{}, 0)
  296. //时间
  297. sortMap = append(sortMap, map[string]interface{}{
  298. "PublishDate": map[string]interface{}{
  299. "order": "desc",
  300. },
  301. })
  302. //sortMap = append(sortMap, map[string]interface{}{
  303. // "_score": map[string]interface{}{
  304. // "order": "desc",
  305. // },
  306. //})
  307. //高亮
  308. highlightMap := make(map[string]interface{}, 0)
  309. highlightMap = map[string]interface{}{
  310. "fields": map[string]interface{}{
  311. //"BodyText": map[string]interface{}{},
  312. "Title": map[string]interface{}{},
  313. "Abstract": map[string]interface{}{},
  314. "Annotation": map[string]interface{}{},
  315. },
  316. //样式 红色
  317. "post_tags": []interface{}{"</font>"},
  318. "pre_tags": []interface{}{"<font color='red'>"},
  319. "fragment_size": 50,
  320. }
  321. mustMap = append(mustMap, map[string]interface{}{
  322. "bool": shouldMap,
  323. })
  324. mustNotMap = append(mustNotMap, map[string]interface{}{
  325. "bool": shouldNotMap,
  326. })
  327. queryMap := map[string]interface{}{
  328. "query": map[string]interface{}{
  329. "bool": map[string]interface{}{
  330. "must": mustMap,
  331. },
  332. },
  333. }
  334. queryMap["sort"] = sortMap
  335. queryMap["from"] = startSize
  336. queryMap["size"] = pageSize
  337. queryMap["highlight"] = highlightMap
  338. //jsonBytes, _ := json.Marshal(queryMap)
  339. //fmt.Println(string(jsonBytes))
  340. //utils.FileLog.Info(string(jsonBytes))
  341. request := client.Search(indexName).Source(queryMap) // sets the JSON request
  342. searchByMatch, err := request.Do(context.Background())
  343. if searchByMatch != nil {
  344. if searchByMatch.Hits != nil {
  345. for _, v := range searchByMatch.Hits.Hits {
  346. var isAppend bool
  347. articleJson, err := v.Source.MarshalJSON()
  348. if err != nil {
  349. return nil, 0, err
  350. }
  351. article := new(ElasticComprehensiveDetail)
  352. err = json.Unmarshal(articleJson, &article)
  353. if err != nil {
  354. return nil, 0, err
  355. }
  356. //fmt.Println(article.SourceId, article.Title, article.Source)
  357. searchItem := new(SearchComprehensiveItem)
  358. searchItem.SourceId = article.SourceId
  359. if len(v.Highlight["Annotation"]) > 0 {
  360. for _, vText := range v.Highlight["Annotation"] {
  361. searchItem.Body = append(searchItem.Body, vText)
  362. }
  363. }
  364. if len(v.Highlight["Abstract"]) > 0 {
  365. for _, vText := range v.Highlight["Abstract"] {
  366. searchItem.Body = append(searchItem.Body, vText)
  367. }
  368. }
  369. if len(v.Highlight["BodyText"]) > 0 {
  370. for _, vText := range v.Highlight["BodyText"] {
  371. searchItem.Body = append(searchItem.Body, vText)
  372. }
  373. }
  374. //searchItem.IsSummary = article.IsSummary
  375. //if len(searchItem.Body) == 0 {
  376. // bodyRune := []rune(article.BodyText)
  377. // bodyRuneLen := len(bodyRune)
  378. // if bodyRuneLen > 100 {
  379. // bodyRuneLen = 100
  380. // }
  381. // body := string(bodyRune[:bodyRuneLen])
  382. // searchItem.Body = []string{body}
  383. //}
  384. var title string
  385. if len(v.Highlight["Title"]) > 0 {
  386. title = v.Highlight["Title"][0]
  387. } else {
  388. title = article.Title
  389. }
  390. searchItem.Title = title
  391. searchItem.Source = article.Source
  392. searchItem.PublishDate = article.PublishDate
  393. if !isAppend {
  394. result = append(result, searchItem)
  395. }
  396. }
  397. }
  398. total = searchByMatch.Hits.TotalHits.Value
  399. }
  400. return
  401. }
  402. func GetResourceDataEsList(list []*SearchComprehensiveItem, user *models.WxUserItem) (items []*models.CygxResourceDataResp, err error) {
  403. var condition string
  404. var pars []interface{}
  405. uid := user.UserId
  406. titleHighlight := make(map[int]string)
  407. bodyHighlight := make(map[int][]string)
  408. yanXuanbodyHighlight := make(map[int][]string)
  409. //var bodyHighlight []string
  410. mapItems := make(map[string]*models.CygxResourceDataResp)
  411. for _, v := range list {
  412. //预处理文章
  413. item := new(models.CygxResourceDataResp)
  414. item.SourceId = v.SourceId
  415. item.Source = v.Source
  416. //if v.IsSummary == 1 {
  417. item.BodyHighlight = v.Body
  418. titleHighlight[v.SourceId] = v.Title
  419. bodyHighlight[v.SourceId] = v.Body
  420. //fmt.Println(v.Title)
  421. //} else {
  422. // item.BodyHighlight = make([]string, 0)
  423. //}
  424. item.PublishDate = utils.TimeRemoveHms2(v.PublishDate)
  425. mapItems[fmt.Sprint(v.Source, v.SourceId)] = item
  426. }
  427. var articleIds []int
  428. var newchartIds []int
  429. var roadshowIds []string
  430. var activityIds []int
  431. var activityvideoIds []string
  432. var activityvoiceIds []string
  433. var activityspecialIds []int
  434. var researchsummaryIds []int
  435. var minutessummaryIds []int
  436. var meetingreviewchaptIds []int
  437. var productinteriorIds []int
  438. var industrialResourceIdsHz []int // 弘则产业资源包
  439. var industrialResourceIdsYx []int // 研选产业资源包
  440. var yanxuanSpecialIds []int // 研选专栏
  441. var askserieVideoIds []string //问答系列视频
  442. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt "`
  443. for _, v := range list {
  444. if v.Source == "article" {
  445. articleIds = append(articleIds, v.SourceId)
  446. } else if v.Source == "newchart" {
  447. newchartIds = append(newchartIds, v.SourceId)
  448. } else if v.Source == "roadshow" {
  449. roadshowIds = append(roadshowIds, strconv.Itoa(v.SourceId))
  450. } else if v.Source == "activity" {
  451. activityIds = append(activityIds, v.SourceId)
  452. } else if v.Source == "activityvideo" {
  453. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  454. } else if v.Source == "activityvoice" {
  455. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  456. } else if v.Source == "activityspecial" {
  457. activityspecialIds = append(activityspecialIds, v.SourceId)
  458. } else if v.Source == "researchsummary" {
  459. researchsummaryIds = append(researchsummaryIds, v.SourceId)
  460. } else if v.Source == "minutessummary" {
  461. minutessummaryIds = append(minutessummaryIds, v.SourceId)
  462. } else if v.Source == "meetingreviewchapt" {
  463. meetingreviewchaptIds = append(meetingreviewchaptIds, v.SourceId)
  464. } else if v.Source == "productinterior" {
  465. productinteriorIds = append(productinteriorIds, v.SourceId)
  466. } else if v.Source == "industrialsourceHz" {
  467. industrialResourceIdsHz = append(industrialResourceIdsHz, v.SourceId)
  468. } else if v.Source == "industrialsourceYx" {
  469. industrialResourceIdsYx = append(industrialResourceIdsYx, v.SourceId)
  470. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  471. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  472. yanXuanbodyHighlight[v.SourceId] = v.Body
  473. } else if v.Source == utils.CYGX_OBJ_ASKSERIEVIDEO {
  474. askserieVideoIds = append(askserieVideoIds, strconv.Itoa(v.SourceId))
  475. }
  476. }
  477. //处理文章
  478. if len(articleIds) > 0 {
  479. pars = make([]interface{}, 0)
  480. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  481. pars = append(pars, articleIds)
  482. listArticle, e := models.GetHomeList(condition, pars, 0, len(articleIds))
  483. if e != nil {
  484. err = errors.New("GetResourceDataList, Err: " + e.Error())
  485. return
  486. }
  487. listArticle, e = HandleArticleCategoryImg(listArticle)
  488. if e != nil {
  489. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  490. return
  491. }
  492. for _, v := range listArticle {
  493. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  494. }
  495. }
  496. detail, e := models.GetConfigByCode("city_img_url")
  497. if e != nil {
  498. err = errors.New("GetResourceDataList, Err: " + e.Error())
  499. return
  500. }
  501. detailChart, e := models.GetConfigByCode("chart_img_url")
  502. if e != nil {
  503. err = errors.New("GetResourceDataList, Err: " + e.Error())
  504. return
  505. }
  506. addressList := strings.Split(detail.ConfigValue, "{|}")
  507. mapAddress := make(map[string]string)
  508. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  509. mapChart := make(map[string]string)
  510. var cityName string
  511. var chartName string
  512. var imgUrl string
  513. var imgUrlChart string
  514. for _, v := range addressList {
  515. vslice := strings.Split(v, "_")
  516. cityName = vslice[0]
  517. imgUrl = vslice[len(vslice)-1]
  518. mapAddress[cityName] = imgUrl
  519. }
  520. for _, v := range chartList {
  521. vslice := strings.Split(v, "_")
  522. chartName = vslice[0]
  523. imgUrlChart = vslice[len(vslice)-1]
  524. mapChart[chartName] = imgUrlChart
  525. }
  526. var imgUrlResp string
  527. //处理活动
  528. if len(activityIds) > 0 {
  529. for _, vss := range activityIds {
  530. imgUrlResp += strconv.Itoa(vss) + ","
  531. }
  532. pars = make([]interface{}, 0)
  533. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `)`
  534. pars = append(pars, activityIds)
  535. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  536. if e != nil {
  537. err = errors.New("GetResourceDataList, Err: " + e.Error())
  538. return
  539. }
  540. //处理不同的报名方式按钮回显
  541. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  542. if e != nil {
  543. e = errors.New("GetActivitySignupResp, Err: " + e.Error())
  544. return
  545. }
  546. activityTypeIdMap := GetActivityTypeIdMap()
  547. var activityListRersp []*models.ActivityDetail
  548. for _, v := range activityList {
  549. v.SignupType = mapActivitySignup[v.ActivityId]
  550. v.ActivityType = activityTypeIdMap[v.ActivityTypeId] // 线上还是线下
  551. activityListRersp = append(activityListRersp, ActivityButtonShow(v))
  552. }
  553. for _, v := range activityListRersp {
  554. if v == nil {
  555. continue
  556. }
  557. if v.ActivityType == 0 {
  558. if mapAddress[v.City] != "" {
  559. imgUrlResp = mapAddress[v.City]
  560. } else {
  561. imgUrlResp = mapAddress["其它"]
  562. }
  563. } else {
  564. if mapChart[v.ChartPermissionName] != "" {
  565. imgUrlResp = mapChart[v.ChartPermissionName]
  566. }
  567. }
  568. v.SourceType = 1
  569. v.Expert, _ = GetReportContentTextSub(v.Expert)
  570. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  571. }
  572. }
  573. //处理研选专栏
  574. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  575. if lenyanxuanSpecialIds > 0 {
  576. pars = make([]interface{}, 0)
  577. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  578. pars = append(pars, yanxuanSpecialIds)
  579. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  580. if e != nil {
  581. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  582. return
  583. }
  584. yanxuanSpecialPv := GetYanxuanSpecialRecordByYanxuanSpecialId(yanxuanSpecialIds) // 专栏Pv
  585. for _, v := range listyanxuanSpecial {
  586. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  587. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  588. if len(yanXuanbodyHighlight[v.Id]) > 0 {
  589. v.BodyHighlight = yanXuanbodyHighlight[v.Id]
  590. } else {
  591. v.BodyHighlight = append(v.BodyHighlight, v.Annotation)
  592. }
  593. v.Annotation = "" //强制置空,兼容前端优先级
  594. // Source PublishDate 字段兼容前端样式
  595. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  596. v.PublishDate = v.PublishTime
  597. v.Pv = yanxuanSpecialPv[v.Id]
  598. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  599. }
  600. }
  601. for _, vList := range list {
  602. for _, v := range mapItems {
  603. //如果这些类型都为空,那么就不合并
  604. if v.Article == 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 {
  605. continue
  606. }
  607. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  608. items = append(items, v)
  609. }
  610. }
  611. }
  612. for _, v := range items {
  613. if v.IndustrialResource != nil {
  614. v.Source = "industrialsource"
  615. }
  616. }
  617. return
  618. }
  619. func SqlComprehensiveSearch(user *models.WxUserItem, keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int, err error) {
  620. yanxuanActivityIds := GetYanxuanActivityIds(user) // 获取所有的研选活动ID
  621. yanxuanArticleIds := GetYanxuanArticleIds() //获取所有研选文章ID
  622. yanxuanArticleIds = append(yanxuanArticleIds, 0)
  623. yanxuanActivityIds = append(yanxuanActivityIds, 0)
  624. var yanxuanArticleIdsStr []string
  625. var yanxuanActivityIdsStr []string
  626. for _, v := range yanxuanArticleIds {
  627. yanxuanArticleIdsStr = append(yanxuanArticleIdsStr, strconv.Itoa(v))
  628. }
  629. for _, v := range yanxuanActivityIds {
  630. yanxuanActivityIdsStr = append(yanxuanActivityIdsStr, strconv.Itoa(v))
  631. }
  632. //yanxuanspecialIds = append(yanxuanspecialIds, 0)
  633. condition := " AND source IN ('article','activity','yanxuanspecial') " // 只有研选的文章、研选的活动、研选的专栏这三种
  634. condition += ` AND IF ( source = 'article' , source_id IN (` + strings.Join(yanxuanArticleIdsStr, ",") + `) ,1=1 ) `
  635. //pars = append(pars, yanxuanArticleIds)
  636. condition += ` AND IF ( source = 'activity' , source_id IN (` + strings.Join(yanxuanActivityIdsStr, ",") + `) ,1=1 ) `
  637. //pars = append(pars, yanxuanActivityIds)
  638. keyWord = "%" + keyWord + "%"
  639. var conditionTitle string
  640. var parsTitle []interface{}
  641. conditionTitle = " AND search_title LIKE ? " + condition
  642. parsTitle = append(parsTitle, keyWord)
  643. totalTitle, e := models.GetResourceDataCount(conditionTitle, parsTitle)
  644. if e != nil {
  645. err = errors.New("GetResourceDataCount, Err: " + e.Error())
  646. return
  647. }
  648. var conditionContent string
  649. var parsContent []interface{}
  650. conditionContent = " AND search_content LIKE ? AND search_title NOT LIKE ? " + condition
  651. parsContent = append(parsContent, keyWord, keyWord)
  652. totalContent, e := models.GetResourceDataCount(conditionContent, parsContent)
  653. if e != nil {
  654. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  655. return
  656. }
  657. var searchTotal int
  658. searchTotal = (startSize/pageSize + 1) * pageSize
  659. var list []*models.CygxResourceData
  660. fmt.Println("totalTitle", totalTitle)
  661. fmt.Println("totalContent", totalContent)
  662. fmt.Println(searchTotal)
  663. if totalTitle >= searchTotal {
  664. fmt.Println("1")
  665. //全部都是标题搜索
  666. list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
  667. if e != nil && e.Error() != utils.ErrNoRow() {
  668. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  669. return
  670. }
  671. } else if totalTitle <= searchTotal-pageSize {
  672. fmt.Println("2")
  673. //全部都是内容搜索
  674. startSize = startSize - totalTitle
  675. list, e = models.GetResourceDataListCondition(conditionContent, parsContent, startSize, pageSize)
  676. if e != nil && e.Error() != utils.ErrNoRow() {
  677. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  678. return
  679. }
  680. } else {
  681. fmt.Println("3")
  682. //一半标题搜索,一半内容搜索
  683. list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
  684. if e != nil && e.Error() != utils.ErrNoRow() {
  685. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  686. return
  687. }
  688. listContent, e := models.GetResourceDataListCondition(conditionContent, parsContent, 0, pageSize-totalContent%pageSize)
  689. if e != nil && e.Error() != utils.ErrNoRow() {
  690. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  691. return
  692. }
  693. for _, v := range listContent {
  694. list = append(list, v)
  695. }
  696. }
  697. for _, v := range list {
  698. item := new(SearchComprehensiveItem)
  699. item.SourceId = v.SourceId
  700. item.Source = v.Source
  701. result = append(result, item)
  702. }
  703. total = totalTitle + totalContent
  704. return
  705. }