es_comprehensive.go 29 KB

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