es_comprehensive.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. var activityvideoIds []string // 活动视频
  432. var activityvoiceIds []string //活动音频
  433. var roadshowIds []string //微路演
  434. var askserieVideoIds []string //问答系列视频
  435. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt "`
  436. for _, v := range list {
  437. if v.Source == "article" {
  438. articleIds = append(articleIds, v.SourceId)
  439. } else if v.Source == "activity" {
  440. activityIds = append(activityIds, v.SourceId)
  441. } else if v.Source == "activityvideo" {
  442. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  443. } else if v.Source == "activityvoice" {
  444. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  445. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  446. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  447. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL_AUTHOR {
  448. yanxuanspecialauthorIds = append(yanxuanspecialauthorIds, v.SourceId)
  449. }
  450. }
  451. fmt.Println("yanxuanspecialauthorIds", yanxuanspecialauthorIds)
  452. //处理文章
  453. if len(articleIds) > 0 {
  454. pars = make([]interface{}, 0)
  455. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  456. pars = append(pars, articleIds)
  457. listArticle, e := models.GetHomeList(condition, pars, 0, len(articleIds))
  458. if e != nil {
  459. err = errors.New("GetResourceDataList, Err: " + e.Error())
  460. return
  461. }
  462. listArticle, e = HandleArticleCategoryImg(listArticle)
  463. if e != nil {
  464. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  465. return
  466. }
  467. for _, v := range listArticle {
  468. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  469. }
  470. }
  471. detail, e := models.GetConfigByCode("city_img_url")
  472. if e != nil {
  473. err = errors.New("GetResourceDataList, Err: " + e.Error())
  474. return
  475. }
  476. detailChart, e := models.GetConfigByCode("chart_img_url")
  477. if e != nil {
  478. err = errors.New("GetResourceDataList, Err: " + e.Error())
  479. return
  480. }
  481. addressList := strings.Split(detail.ConfigValue, "{|}")
  482. mapAddress := make(map[string]string)
  483. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  484. mapChart := make(map[string]string)
  485. var cityName string
  486. var chartName string
  487. var imgUrl string
  488. var imgUrlChart string
  489. for _, v := range addressList {
  490. vslice := strings.Split(v, "_")
  491. cityName = vslice[0]
  492. imgUrl = vslice[len(vslice)-1]
  493. mapAddress[cityName] = imgUrl
  494. }
  495. for _, v := range chartList {
  496. vslice := strings.Split(v, "_")
  497. chartName = vslice[0]
  498. imgUrlChart = vslice[len(vslice)-1]
  499. mapChart[chartName] = imgUrlChart
  500. }
  501. var imgUrlResp string
  502. //处理活动
  503. if len(activityIds) > 0 {
  504. for _, vss := range activityIds {
  505. imgUrlResp += strconv.Itoa(vss) + ","
  506. }
  507. pars = make([]interface{}, 0)
  508. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `)`
  509. pars = append(pars, activityIds)
  510. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  511. if e != nil {
  512. err = errors.New("GetResourceDataList, Err: " + e.Error())
  513. return
  514. }
  515. //处理不同的报名方式按钮回显
  516. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  517. if e != nil {
  518. e = errors.New("GetActivitySignupResp, Err: " + e.Error())
  519. return
  520. }
  521. activityTypeIdMap := GetActivityTypeIdMap()
  522. var activityListRersp []*models.ActivityDetail
  523. for _, v := range activityList {
  524. v.SignupType = mapActivitySignup[v.ActivityId]
  525. v.ActivityType = activityTypeIdMap[v.ActivityTypeId] // 线上还是线下
  526. activityListRersp = append(activityListRersp, ActivityButtonShow(v))
  527. }
  528. for _, v := range activityListRersp {
  529. if v == nil {
  530. continue
  531. }
  532. if v.ActivityType == 0 {
  533. if mapAddress[v.City] != "" {
  534. imgUrlResp = mapAddress[v.City]
  535. } else {
  536. imgUrlResp = mapAddress["其它"]
  537. }
  538. } else {
  539. if mapChart[v.ChartPermissionName] != "" {
  540. imgUrlResp = mapChart[v.ChartPermissionName]
  541. }
  542. }
  543. v.SourceType = 1
  544. v.Expert, _ = GetReportContentTextSub(v.Expert)
  545. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  546. }
  547. }
  548. //处理研选专栏
  549. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  550. if lenyanxuanSpecialIds > 0 {
  551. pars = make([]interface{}, 0)
  552. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  553. pars = append(pars, yanxuanSpecialIds)
  554. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  555. if e != nil {
  556. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  557. return
  558. }
  559. yanxuanSpecialPv := GetYanxuanSpecialRecordByYanxuanSpecialId(yanxuanSpecialIds) // 专栏Pv
  560. for _, v := range listyanxuanSpecial {
  561. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  562. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  563. if len(yanXuanbodyHighlight[v.Id]) > 0 {
  564. v.BodyHighlight = yanXuanbodyHighlight[v.Id]
  565. } else {
  566. v.BodyHighlight = append(v.BodyHighlight, v.Annotation)
  567. }
  568. v.Annotation = "" //强制置空,兼容前端优先级
  569. // Source PublishDate 字段兼容前端样式
  570. v.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  571. v.PublishDate = v.PublishTime
  572. v.Pv = yanxuanSpecialPv[v.Id]
  573. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  574. }
  575. }
  576. //处理研选专栏作者
  577. lenyanxuanspecialauthorIds := len(yanxuanspecialauthorIds)
  578. if lenyanxuanspecialauthorIds > 0 {
  579. pars = make([]interface{}, 0)
  580. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanspecialauthorIds) + `) `
  581. condition += ` ORDER BY latest_publish_time DESC `
  582. pars = append(pars, yanxuanspecialauthorIds)
  583. listAuthor, e := models.GetYanxuanSpecialAuthorList(condition, pars, 0, lenyanxuanspecialauthorIds)
  584. if e != nil {
  585. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  586. return
  587. }
  588. for _, v := range listAuthor {
  589. v.LatestPublishDate = v.LatestPublishTime.Format(utils.FormatDate)
  590. v.LabelKeywordImgLink = utils.LABEL_ICO_9
  591. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL_AUTHOR, v.Id)].YanxuanSpecialAuthor = v
  592. }
  593. }
  594. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds) > 0 {
  595. audioIdstr := strings.Join(activityvoiceIds, ",")
  596. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  597. roadshowIdsStr := strings.Join(roadshowIds, ",")
  598. askserieVideoIdsStr := strings.Join(askserieVideoIds, ",")
  599. list, _, e := GetMicroRoadShowMycollectV12(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds), 0, audioIdstr, activityVideoIdsStr, roadshowIdsStr, askserieVideoIdsStr, user)
  600. if e != nil {
  601. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  602. return
  603. }
  604. // 用户权限
  605. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  606. if e != nil {
  607. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  608. return
  609. }
  610. // 获取默认图配置
  611. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  612. if e != nil {
  613. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  614. return
  615. }
  616. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  617. for i := range list {
  618. // 权限
  619. au := new(models.UserPermissionAuthInfo)
  620. au.SellerName = authInfo.SellerName
  621. au.SellerMobile = authInfo.SellerMobile
  622. au.HasPermission = authInfo.HasPermission
  623. au.OperationMode = authInfo.OperationMode
  624. if au.HasPermission == 1 {
  625. // 非宏观权限进一步判断是否有权限
  626. if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
  627. au.HasPermission = 2
  628. }
  629. }
  630. // 无权限的弹框提示
  631. if au.HasPermission != 1 {
  632. if au.OperationMode == UserPermissionOperationModeCall {
  633. if list[i].Type == 1 {
  634. au.PopupMsg = UserPermissionPopupMsgCallActivity
  635. } else {
  636. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  637. }
  638. } else {
  639. if list[i].Type == 1 {
  640. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  641. } else {
  642. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  643. }
  644. }
  645. }
  646. list[i].AuthInfo = au
  647. list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
  648. // 默认图
  649. if list[i].BackgroundImg == "" {
  650. if list[i].Type == 1 {
  651. list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
  652. } else {
  653. list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
  654. }
  655. }
  656. // 分享图
  657. if list[i].ShareImg == "" {
  658. if list[i].Type == 1 {
  659. list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
  660. } else {
  661. list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
  662. }
  663. }
  664. list[i].LabelKeywordImgLink = utils.LABEL_ICO_10
  665. }
  666. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  667. for _, item := range list {
  668. if item.Type == 1 {
  669. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  670. } else if item.Type == 2 {
  671. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  672. } else if item.Type == 3 {
  673. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  674. } else if item.Type == 4 {
  675. mapItems[fmt.Sprint(utils.CYGX_OBJ_ASKSERIEVIDEO, item.Id)].AskserieVideo = item
  676. }
  677. }
  678. }
  679. for _, vList := range list {
  680. for _, v := range mapItems {
  681. //如果这些类型都为空,那么就不合并
  682. if v.Article == nil && v.Activity == nil && v.Activityvideo == nil && v.Activityvoice == nil && v.YanxuanSpecial == nil && v.YanxuanSpecialAuthor == nil {
  683. continue
  684. }
  685. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  686. items = append(items, v)
  687. }
  688. }
  689. }
  690. return
  691. }
  692. func SqlComprehensiveSearch(user *models.WxUserItem, keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int, err error) {
  693. yanxuanActivityIds := GetYanxuanActivityIds(user, "") // 获取所有的研选活动ID
  694. yanxuanArticleIds := GetYanxuanArticleIds() //获取所有研选文章ID
  695. yanxuanArticleIds = append(yanxuanArticleIds, 0)
  696. yanxuanActivityIds = append(yanxuanActivityIds, 0)
  697. var yanxuanArticleIdsStr []string
  698. var yanxuanActivityIdsStr []string
  699. for _, v := range yanxuanArticleIds {
  700. yanxuanArticleIdsStr = append(yanxuanArticleIdsStr, strconv.Itoa(v))
  701. }
  702. for _, v := range yanxuanActivityIds {
  703. yanxuanActivityIdsStr = append(yanxuanActivityIdsStr, strconv.Itoa(v))
  704. }
  705. //yanxuanspecialIds = append(yanxuanspecialIds, 0)
  706. condition := " AND source IN ('article','activity','yanxuanspecial','activityvoice','activityvideo') AND IF ( source IN('activityvoice','activityvideo') , chart_permission_id = 31 ,1=1 ) " // 只有研选的文章、研选的活动、研选的专栏这三种
  707. condition += ` AND IF ( source = 'article' , source_id IN (` + strings.Join(yanxuanArticleIdsStr, ",") + `) ,1=1 ) `
  708. //pars = append(pars, yanxuanArticleIds)
  709. condition += ` AND IF ( source = 'activity' , source_id IN (` + strings.Join(yanxuanActivityIdsStr, ",") + `) ,1=1 ) `
  710. //pars = append(pars, yanxuanActivityIds)
  711. keyWord = "%" + keyWord + "%"
  712. var conditionTitle string
  713. var parsTitle []interface{}
  714. conditionTitle = " AND search_title LIKE ? " + condition
  715. parsTitle = append(parsTitle, keyWord)
  716. var conditionContentYxAuthor string // 研选专栏作者搜索条件
  717. var parsContentYxAuthor []interface{}
  718. conditionContentYxAuthor = " AND (special_name LIKE ? OR nick_name LIKE ? ) "
  719. parsContentYxAuthor = append(parsContentYxAuthor, keyWord, keyWord)
  720. totalTitle, e := models.GetResourceDataAndYanxuanSpecialAuthorCount(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor)
  721. //totalTitle, e := models.GetResourceDataCount(conditionTitle, parsTitle)
  722. if e != nil {
  723. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorCount, Err: " + e.Error())
  724. return
  725. }
  726. var conditionContent string
  727. var parsContent []interface{}
  728. conditionContent = " AND search_content LIKE ? AND search_title NOT LIKE ? " + condition
  729. parsContent = append(parsContent, keyWord, keyWord)
  730. totalContent, e := models.GetResourceDataCount(conditionContent, parsContent)
  731. if e != nil {
  732. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  733. return
  734. }
  735. var searchTotal int
  736. searchTotal = (startSize/pageSize + 1) * pageSize
  737. var list []*models.CygxResourceData
  738. fmt.Println("totalTitle", totalTitle)
  739. fmt.Println("totalContent", totalContent)
  740. fmt.Println(searchTotal)
  741. if totalTitle >= searchTotal {
  742. fmt.Println("1")
  743. //全部都是标题搜索
  744. list, e = models.GetResourceDataAndYanxuanSpecialAuthorListCondition(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor, startSize, pageSize)
  745. if e != nil && e.Error() != utils.ErrNoRow() {
  746. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  747. return
  748. }
  749. } else if totalTitle <= searchTotal-pageSize {
  750. fmt.Println("2")
  751. //全部都是内容搜索
  752. startSize = startSize - totalTitle
  753. list, e = models.GetResourceDataListCondition(conditionContent, parsContent, startSize, pageSize)
  754. if e != nil && e.Error() != utils.ErrNoRow() {
  755. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  756. return
  757. }
  758. } else {
  759. fmt.Println("3")
  760. //一半标题搜索,一半内容搜索
  761. //list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
  762. list, e = models.GetResourceDataAndYanxuanSpecialAuthorListCondition(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor, startSize, pageSize)
  763. if e != nil && e.Error() != utils.ErrNoRow() {
  764. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorListCondition, Err: " + e.Error())
  765. return
  766. }
  767. listContent, e := models.GetResourceDataListCondition(conditionContent, parsContent, 0, pageSize-totalContent%pageSize)
  768. if e != nil && e.Error() != utils.ErrNoRow() {
  769. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  770. return
  771. }
  772. for _, v := range listContent {
  773. list = append(list, v)
  774. }
  775. }
  776. for _, v := range list {
  777. item := new(SearchComprehensiveItem)
  778. item.SourceId = v.SourceId
  779. item.Source = v.Source
  780. result = append(result, item)
  781. }
  782. total = totalTitle + totalContent
  783. return
  784. }