es_comprehensive.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 activityIds []int //活动
  429. var yanxuanSpecialIds []int // 研选专栏
  430. var yanxuanspecialauthorIds []int //研选作者ID
  431. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt "`
  432. for _, v := range list {
  433. if v.Source == "article" {
  434. articleIds = append(articleIds, v.SourceId)
  435. } else if v.Source == "activity" {
  436. activityIds = append(activityIds, v.SourceId)
  437. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  438. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  439. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL_AUTHOR {
  440. yanxuanspecialauthorIds = append(yanxuanspecialauthorIds, v.SourceId)
  441. }
  442. }
  443. fmt.Println("yanxuanspecialauthorIds", yanxuanspecialauthorIds)
  444. //处理文章
  445. if len(articleIds) > 0 {
  446. pars = make([]interface{}, 0)
  447. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  448. pars = append(pars, articleIds)
  449. listArticle, e := models.GetHomeList(condition, pars, 0, len(articleIds))
  450. if e != nil {
  451. err = errors.New("GetResourceDataList, Err: " + e.Error())
  452. return
  453. }
  454. listArticle, e = HandleArticleCategoryImg(listArticle)
  455. if e != nil {
  456. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  457. return
  458. }
  459. for _, v := range listArticle {
  460. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  461. }
  462. }
  463. detail, e := models.GetConfigByCode("city_img_url")
  464. if e != nil {
  465. err = errors.New("GetResourceDataList, Err: " + e.Error())
  466. return
  467. }
  468. detailChart, e := models.GetConfigByCode("chart_img_url")
  469. if e != nil {
  470. err = errors.New("GetResourceDataList, Err: " + e.Error())
  471. return
  472. }
  473. addressList := strings.Split(detail.ConfigValue, "{|}")
  474. mapAddress := make(map[string]string)
  475. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  476. mapChart := make(map[string]string)
  477. var cityName string
  478. var chartName string
  479. var imgUrl string
  480. var imgUrlChart string
  481. for _, v := range addressList {
  482. vslice := strings.Split(v, "_")
  483. cityName = vslice[0]
  484. imgUrl = vslice[len(vslice)-1]
  485. mapAddress[cityName] = imgUrl
  486. }
  487. for _, v := range chartList {
  488. vslice := strings.Split(v, "_")
  489. chartName = vslice[0]
  490. imgUrlChart = vslice[len(vslice)-1]
  491. mapChart[chartName] = imgUrlChart
  492. }
  493. var imgUrlResp string
  494. //处理活动
  495. if len(activityIds) > 0 {
  496. for _, vss := range activityIds {
  497. imgUrlResp += strconv.Itoa(vss) + ","
  498. }
  499. pars = make([]interface{}, 0)
  500. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `)`
  501. pars = append(pars, activityIds)
  502. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  503. if e != nil {
  504. err = errors.New("GetResourceDataList, Err: " + e.Error())
  505. return
  506. }
  507. //处理不同的报名方式按钮回显
  508. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  509. if e != nil {
  510. e = errors.New("GetActivitySignupResp, Err: " + e.Error())
  511. return
  512. }
  513. activityTypeIdMap := GetActivityTypeIdMap()
  514. var activityListRersp []*models.ActivityDetail
  515. for _, v := range activityList {
  516. v.SignupType = mapActivitySignup[v.ActivityId]
  517. v.ActivityType = activityTypeIdMap[v.ActivityTypeId] // 线上还是线下
  518. activityListRersp = append(activityListRersp, ActivityButtonShow(v))
  519. }
  520. for _, v := range activityListRersp {
  521. if v == nil {
  522. continue
  523. }
  524. if v.ActivityType == 0 {
  525. if mapAddress[v.City] != "" {
  526. imgUrlResp = mapAddress[v.City]
  527. } else {
  528. imgUrlResp = mapAddress["其它"]
  529. }
  530. } else {
  531. if mapChart[v.ChartPermissionName] != "" {
  532. imgUrlResp = mapChart[v.ChartPermissionName]
  533. }
  534. }
  535. v.SourceType = 1
  536. v.Expert, _ = GetReportContentTextSub(v.Expert)
  537. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  538. }
  539. }
  540. //处理研选专栏
  541. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  542. if lenyanxuanSpecialIds > 0 {
  543. pars = make([]interface{}, 0)
  544. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  545. pars = append(pars, yanxuanSpecialIds)
  546. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  547. if e != nil {
  548. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  549. return
  550. }
  551. yanxuanSpecialPv := GetYanxuanSpecialRecordByYanxuanSpecialId(yanxuanSpecialIds) // 专栏Pv
  552. for _, v := range listyanxuanSpecial {
  553. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  554. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  555. if len(yanXuanbodyHighlight[v.Id]) > 0 {
  556. v.BodyHighlight = yanXuanbodyHighlight[v.Id]
  557. } else {
  558. v.BodyHighlight = append(v.BodyHighlight, v.Annotation)
  559. }
  560. v.Annotation = "" //强制置空,兼容前端优先级
  561. // Source PublishDate 字段兼容前端样式
  562. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  563. v.PublishDate = v.PublishTime
  564. v.Pv = yanxuanSpecialPv[v.Id]
  565. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  566. }
  567. }
  568. //处理研选专栏作者
  569. lenyanxuanspecialauthorIds := len(yanxuanspecialauthorIds)
  570. if lenyanxuanspecialauthorIds > 0 {
  571. pars = make([]interface{}, 0)
  572. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanspecialauthorIds) + `) `
  573. condition += ` ORDER BY latest_publish_time DESC `
  574. pars = append(pars, yanxuanspecialauthorIds)
  575. listAuthor, e := models.GetYanxuanSpecialAuthorList(condition, pars, 0, lenyanxuanspecialauthorIds)
  576. if e != nil {
  577. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  578. return
  579. }
  580. for _, v := range listAuthor {
  581. v.LatestPublishDate = v.LatestPublishTime.Format(utils.FormatDate)
  582. v.LabelKeywordImgLink = utils.LABEL_ICO_9
  583. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL_AUTHOR, v.Id)].YanxuanSpecialAuthor = v
  584. }
  585. }
  586. for _, vList := range list {
  587. for _, v := range mapItems {
  588. //如果这些类型都为空,那么就不合并
  589. //如果这些类型都为空,那么就不合并
  590. if v.Article == nil && v.Activity == nil && v.YanxuanSpecial == nil && v.YanxuanSpecialAuthor == nil {
  591. continue
  592. }
  593. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  594. items = append(items, v)
  595. }
  596. }
  597. }
  598. for _, v := range items {
  599. if v.IndustrialResource != nil {
  600. v.Source = "industrialsource"
  601. }
  602. }
  603. return
  604. }
  605. func SqlComprehensiveSearch(user *models.WxUserItem, keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int, err error) {
  606. yanxuanActivityIds := GetYanxuanActivityIds(user, "") // 获取所有的研选活动ID
  607. yanxuanArticleIds := GetYanxuanArticleIds() //获取所有研选文章ID
  608. yanxuanArticleIds = append(yanxuanArticleIds, 0)
  609. yanxuanActivityIds = append(yanxuanActivityIds, 0)
  610. var yanxuanArticleIdsStr []string
  611. var yanxuanActivityIdsStr []string
  612. for _, v := range yanxuanArticleIds {
  613. yanxuanArticleIdsStr = append(yanxuanArticleIdsStr, strconv.Itoa(v))
  614. }
  615. for _, v := range yanxuanActivityIds {
  616. yanxuanActivityIdsStr = append(yanxuanActivityIdsStr, strconv.Itoa(v))
  617. }
  618. //yanxuanspecialIds = append(yanxuanspecialIds, 0)
  619. condition := " AND source IN ('article','activity','yanxuanspecial') " // 只有研选的文章、研选的活动、研选的专栏这三种
  620. condition += ` AND IF ( source = 'article' , source_id IN (` + strings.Join(yanxuanArticleIdsStr, ",") + `) ,1=1 ) `
  621. //pars = append(pars, yanxuanArticleIds)
  622. condition += ` AND IF ( source = 'activity' , source_id IN (` + strings.Join(yanxuanActivityIdsStr, ",") + `) ,1=1 ) `
  623. //pars = append(pars, yanxuanActivityIds)
  624. keyWord = "%" + keyWord + "%"
  625. var conditionTitle string
  626. var parsTitle []interface{}
  627. conditionTitle = " AND search_title LIKE ? " + condition
  628. parsTitle = append(parsTitle, keyWord)
  629. var conditionContentYxAuthor string // 研选专栏作者搜索条件
  630. var parsContentYxAuthor []interface{}
  631. conditionContentYxAuthor = " AND (special_name LIKE ? OR nick_name LIKE ? ) "
  632. parsContentYxAuthor = append(parsContentYxAuthor, keyWord, keyWord)
  633. totalTitle, e := models.GetResourceDataAndYanxuanSpecialAuthorCount(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor)
  634. //totalTitle, e := models.GetResourceDataCount(conditionTitle, parsTitle)
  635. if e != nil {
  636. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorCount, Err: " + e.Error())
  637. return
  638. }
  639. var conditionContent string
  640. var parsContent []interface{}
  641. conditionContent = " AND search_content LIKE ? AND search_title NOT LIKE ? " + condition
  642. parsContent = append(parsContent, keyWord, keyWord)
  643. totalContent, e := models.GetResourceDataCount(conditionContent, parsContent)
  644. if e != nil {
  645. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  646. return
  647. }
  648. var searchTotal int
  649. searchTotal = (startSize/pageSize + 1) * pageSize
  650. var list []*models.CygxResourceData
  651. fmt.Println("totalTitle", totalTitle)
  652. fmt.Println("totalContent", totalContent)
  653. fmt.Println(searchTotal)
  654. if totalTitle >= searchTotal {
  655. fmt.Println("1")
  656. //全部都是标题搜索
  657. //list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
  658. list, e = models.GetResourceDataAndYanxuanSpecialAuthorListCondition(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor, startSize, pageSize)
  659. if e != nil && e.Error() != utils.ErrNoRow() {
  660. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorListCondition, Err: " + e.Error())
  661. return
  662. }
  663. } else if totalTitle <= searchTotal-pageSize {
  664. fmt.Println("2")
  665. //全部都是内容搜索
  666. startSize = startSize - totalTitle
  667. list, e = models.GetResourceDataListCondition(conditionContent, parsContent, startSize, pageSize)
  668. if e != nil && e.Error() != utils.ErrNoRow() {
  669. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  670. return
  671. }
  672. } else {
  673. fmt.Println("3")
  674. //一半标题搜索,一半内容搜索
  675. //list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
  676. list, e = models.GetResourceDataAndYanxuanSpecialAuthorListCondition(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor, startSize, pageSize)
  677. if e != nil && e.Error() != utils.ErrNoRow() {
  678. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorListCondition, Err: " + e.Error())
  679. return
  680. }
  681. listContent, e := models.GetResourceDataListCondition(conditionContent, parsContent, 0, pageSize-totalContent%pageSize)
  682. if e != nil && e.Error() != utils.ErrNoRow() {
  683. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  684. return
  685. }
  686. for _, v := range listContent {
  687. list = append(list, v)
  688. }
  689. }
  690. for _, v := range list {
  691. item := new(SearchComprehensiveItem)
  692. item.SourceId = v.SourceId
  693. item.Source = v.Source
  694. result = append(result, item)
  695. }
  696. total = totalTitle + totalContent
  697. return
  698. }