es_comprehensive.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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 activityIds []int //活动
  281. var yanxuanSpecialIds []int // 研选专栏
  282. var yanxuanspecialauthorIds []int //研选作者ID
  283. var activityvideoIds []string // 活动视频
  284. var activityvoiceIds []string //活动音频
  285. var roadshowIds []string //微路演
  286. var askserieVideoIds []string //问答系列视频
  287. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial 、 本周研究汇总: researchsummary 、 上周纪要汇总 :minutessummary 、晨会精华 :meetingreviewchapt 、 产品内测:productinterior
  288. for _, v := range list {
  289. if v.Source == "article" {
  290. articleIds = append(articleIds, v.SourceId)
  291. } else if v.Source == "activity" {
  292. activityIds = append(activityIds, v.SourceId)
  293. } else if v.Source == "activityvideo" {
  294. activityvideoIds = append(activityvideoIds, strconv.Itoa(v.SourceId))
  295. } else if v.Source == "activityvoice" {
  296. activityvoiceIds = append(activityvoiceIds, strconv.Itoa(v.SourceId))
  297. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL {
  298. yanxuanSpecialIds = append(yanxuanSpecialIds, v.SourceId)
  299. } else if v.Source == utils.CYGX_OBJ_YANXUANSPECIAL_AUTHOR {
  300. yanxuanspecialauthorIds = append(yanxuanspecialauthorIds, v.SourceId)
  301. }
  302. }
  303. detail, e := models.GetConfigByCode("city_img_url")
  304. if e != nil {
  305. err = errors.New("GetResourceDataList, Err: " + e.Error())
  306. return
  307. }
  308. detailChart, e := models.GetConfigByCode("chart_img_url")
  309. if e != nil {
  310. err = errors.New("GetResourceDataList, Err: " + e.Error())
  311. return
  312. }
  313. addressList := strings.Split(detail.ConfigValue, "{|}")
  314. mapAddress := make(map[string]string)
  315. chartList := strings.Split(detailChart.ConfigValue, "{|}")
  316. mapChart := make(map[string]string)
  317. var cityName string
  318. var chartName string
  319. var imgUrl string
  320. var imgUrlChart string
  321. for _, v := range addressList {
  322. vslice := strings.Split(v, "_")
  323. cityName = vslice[0]
  324. imgUrl = vslice[len(vslice)-1]
  325. mapAddress[cityName] = imgUrl
  326. }
  327. for _, v := range chartList {
  328. vslice := strings.Split(v, "_")
  329. chartName = vslice[0]
  330. imgUrlChart = vslice[len(vslice)-1]
  331. mapChart[chartName] = imgUrlChart
  332. }
  333. var imgUrlResp string
  334. //处理文章
  335. if len(articleIds) > 0 {
  336. pars = make([]interface{}, 0)
  337. condition = ` AND a.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  338. pars = append(pars, articleIds)
  339. articleList, e := models.GetHomeListPublic(condition, pars, 0, len(articleIds))
  340. if e != nil {
  341. err = errors.New("GetResourceDataList, Err: " + e.Error())
  342. return
  343. }
  344. articleList, e = HandleArticleCategoryImg(articleList, user)
  345. if e != nil {
  346. err = errors.New("HandleArticleCategoryImg, Err: " + e.Error())
  347. return
  348. }
  349. for _, v := range articleList {
  350. v.Body = ""
  351. if titleHighlight[v.ArticleId] != "" {
  352. v.Title = titleHighlight[v.ArticleId]
  353. }
  354. if len(bodyHighlight[v.ArticleId]) > 0 {
  355. v.Abstract = ""
  356. v.Annotation = ""
  357. v.BodyHighlight = bodyHighlight[v.ArticleId]
  358. } else {
  359. v.BodyHighlight = make([]string, 0)
  360. }
  361. mapItems[fmt.Sprint("article", v.ArticleId)].Article = v
  362. }
  363. }
  364. //处理活动
  365. if len(activityIds) > 0 {
  366. for _, vss := range activityIds {
  367. imgUrlResp += strconv.Itoa(vss) + ","
  368. }
  369. pars = make([]interface{}, 0)
  370. condition = ` AND art.activity_id IN (` + utils.GetOrmInReplace(len(activityIds)) + `) `
  371. pars = append(pars, activityIds)
  372. activityList, e := models.GetActivityListNew(condition, pars, uid, 0, len(activityIds), 0, 0, "")
  373. if e != nil {
  374. err = errors.New("GetResourceDataList, Err: " + e.Error())
  375. return
  376. }
  377. var activityListRersp []*models.ActivityListResp
  378. //for _, v := range activityList {
  379. // activityListRersp = append(activityListRersp, ActivityButtonShow(v, user, make([]string, 0)))
  380. //}
  381. //var pArr []string
  382. activityListRersp = ActivityArrButtonShow(activityList, user, make([]string, 0))
  383. //处理不同的报名方式按钮回显
  384. mapActivitySignup, e := GetActivitySignupResp(activityIds, user)
  385. if e != nil {
  386. err = errors.New("GetActivitySignupResp, Err: " + e.Error())
  387. return
  388. }
  389. for _, v := range activityListRersp {
  390. if v == nil {
  391. continue
  392. }
  393. if v.ActivityType == 0 {
  394. if mapAddress[v.City] != "" {
  395. imgUrlResp = mapAddress[v.City]
  396. } else {
  397. imgUrlResp = mapAddress["其它"]
  398. }
  399. } else {
  400. if mapChart[v.ChartPermissionName] != "" {
  401. imgUrlResp = mapChart[v.ChartPermissionName]
  402. }
  403. }
  404. v.ImgUrl = imgUrlResp
  405. v.SourceType = mapActivitySignup[v.ActivityId]
  406. mapItems[fmt.Sprint("activity", v.ActivityId)].Activity = v
  407. }
  408. }
  409. //处理研选专栏
  410. lenyanxuanSpecialIds := len(yanxuanSpecialIds)
  411. if lenyanxuanSpecialIds > 0 {
  412. pars = make([]interface{}, 0)
  413. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanSpecialIds) + `) `
  414. pars = append(pars, yanxuanSpecialIds)
  415. listyanxuanSpecial, e := models.GetYanxuanSpecialList(user.UserId, condition, pars, 0, 0)
  416. if e != nil {
  417. err = errors.New("GetYanxuanSpecialList, Err: " + e.Error())
  418. return
  419. }
  420. yanxuanSpecialPv := GetYanxuanSpecialRecordByYanxuanSpecialId(yanxuanSpecialIds) // 专栏Pv
  421. for _, v := range listyanxuanSpecial {
  422. v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
  423. v.Annotation, _ = GetReportContentTextSubNew(v.Content)
  424. v.Pv = yanxuanSpecialPv[v.Id]
  425. if len(yanXuanbodyHighlight[v.Id]) > 0 {
  426. v.BodyHighlight = yanXuanbodyHighlight[v.Id]
  427. } else {
  428. v.BodyHighlight = append(v.BodyHighlight, v.Annotation)
  429. }
  430. v.Annotation = "" //强制置空,兼容前端优先级
  431. v.LabelKeywordImgLink = utils.LABEL_ICO_4
  432. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL, v.Id)].YanxuanSpecial = v
  433. }
  434. }
  435. //处理研选专栏作者
  436. lenyanxuanspecialauthorIds := len(yanxuanspecialauthorIds)
  437. if lenyanxuanspecialauthorIds > 0 {
  438. pars = make([]interface{}, 0)
  439. condition = ` AND a.id IN (` + utils.GetOrmInReplace(lenyanxuanspecialauthorIds) + `) `
  440. condition += ` ORDER BY latest_publish_time DESC `
  441. pars = append(pars, yanxuanspecialauthorIds)
  442. listAuthor, e := models.GetYanxuanSpecialAuthorList("", condition, pars, 0, lenyanxuanspecialauthorIds)
  443. if e != nil {
  444. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  445. return
  446. }
  447. for _, v := range listAuthor {
  448. v.LatestPublishDate = v.LatestPublishTime.Format(utils.FormatDate)
  449. v.LabelKeywordImgLink = utils.LABEL_ICO_9
  450. mapItems[fmt.Sprint(utils.CYGX_OBJ_YANXUANSPECIAL_AUTHOR, v.Id)].YanxuanSpecialAuthor = v
  451. }
  452. }
  453. if len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds) > 0 {
  454. audioIdstr := strings.Join(activityvoiceIds, ",")
  455. activityVideoIdsStr := strings.Join(activityvideoIds, ",")
  456. roadshowIdsStr := strings.Join(roadshowIds, ",")
  457. askserieVideoIdsStr := strings.Join(askserieVideoIds, ",")
  458. listv, _, e := GetMicroRoadShowMycollectV12(len(roadshowIds)+len(activityvideoIds)+len(activityvoiceIds)+len(askserieVideoIds), 0, audioIdstr, activityVideoIdsStr, roadshowIdsStr, askserieVideoIdsStr, user)
  459. if e != nil {
  460. err = errors.New("GetMicroRoadShowMycollect, Err: " + e.Error())
  461. return
  462. }
  463. // 用户权限
  464. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  465. if e != nil {
  466. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  467. return
  468. }
  469. // 获取默认图配置
  470. audioMap, videoMap, audioShareMap, videoShareMap, e := GetMicroRoadShowDefaultImgConfig()
  471. if e != nil {
  472. err = errors.New("GetMicroRoadShowDefaultImgConfig, Err: " + e.Error())
  473. return
  474. }
  475. //Source string `description:"资源类型 报告 :article 、图表 :newchart、微路演 :roadshow、活动 :activity、活动视频:activityvideo、活动音频:activityvoice、专项调研活动:activityspecial"`
  476. for i := range listv {
  477. // 权限
  478. au := new(models.UserPermissionAuthInfo)
  479. au.SellerName = authInfo.SellerName
  480. au.SellerMobile = authInfo.SellerMobile
  481. au.HasPermission = authInfo.HasPermission
  482. au.OperationMode = authInfo.OperationMode
  483. if au.HasPermission == 1 {
  484. // 非宏观权限进一步判断是否有权限
  485. if listv[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, listv[i].ChartPermissionName) {
  486. au.HasPermission = 2
  487. }
  488. }
  489. // 无权限的弹框提示
  490. if au.HasPermission != 1 {
  491. if au.OperationMode == UserPermissionOperationModeCall {
  492. if listv[i].Type == 1 {
  493. au.PopupMsg = UserPermissionPopupMsgCallActivity
  494. } else {
  495. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  496. }
  497. } else {
  498. if listv[i].Type == 1 {
  499. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  500. } else {
  501. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  502. }
  503. }
  504. }
  505. listv[i].AuthInfo = au
  506. listv[i].PublishTime = utils.StrTimeToTime(listv[i].PublishTime).Format(utils.FormatDate)
  507. // 默认图
  508. if listv[i].BackgroundImg == "" {
  509. if listv[i].Type == 1 {
  510. listv[i].BackgroundImg = audioMap[listv[i].ChartPermissionId]
  511. } else {
  512. listv[i].BackgroundImg = videoMap[listv[i].ChartPermissionId]
  513. }
  514. }
  515. // 分享图
  516. if listv[i].ShareImg == "" {
  517. if listv[i].Type == 1 {
  518. listv[i].ShareImg = audioShareMap[listv[i].ChartPermissionId]
  519. } else {
  520. listv[i].ShareImg = videoShareMap[listv[i].ChartPermissionId]
  521. }
  522. }
  523. listv[i].LabelKeywordImgLink = utils.LABEL_ICO_10
  524. }
  525. //Type int `description:"类型: 1-音频; 2-活动视频; 3-产业视频"`
  526. for _, item := range listv {
  527. if item.Type == 1 {
  528. mapItems[fmt.Sprint("activityvoice", item.Id)].Activityvoice = item
  529. } else if item.Type == 2 {
  530. mapItems[fmt.Sprint("activityvideo", item.Id)].Activityvideo = item
  531. } else if item.Type == 3 {
  532. mapItems[fmt.Sprint("roadshow", item.Id)].Roadshow = item
  533. } else if item.Type == 4 {
  534. mapItems[fmt.Sprint(utils.CYGX_OBJ_ASKSERIEVIDEO, item.Id)].AskserieVideo = item
  535. }
  536. }
  537. }
  538. for _, vList := range list {
  539. for _, v := range mapItems {
  540. //如果这些类型都为空,那么就不合并
  541. if v.Article == nil && v.Newchart == nil && v.Roadshow == nil && v.Activity == nil && v.Activityvideo == nil && v.Activityvoice == nil && v.YanxuanSpecial == nil && v.AskserieVideo == nil {
  542. continue
  543. }
  544. if v.SourceId == vList.SourceId && v.Source == vList.Source {
  545. items = append(items, v)
  546. }
  547. }
  548. }
  549. return
  550. }
  551. func SqlComprehensiveSearch(user *models.WxUserItem, keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int, err error) {
  552. yanxuanActivityIds := GetYanxuanActivityIds(user, "") // 获取所有的研选活动ID
  553. yanxuanArticleIds := GetYanxuanArticleIds() //获取所有研选文章ID
  554. yanxuanArticleIds = append(yanxuanArticleIds, 0)
  555. yanxuanActivityIds = append(yanxuanActivityIds, 0)
  556. var yanxuanArticleIdsStr []string
  557. var yanxuanActivityIdsStr []string
  558. for _, v := range yanxuanArticleIds {
  559. yanxuanArticleIdsStr = append(yanxuanArticleIdsStr, strconv.Itoa(v))
  560. }
  561. for _, v := range yanxuanActivityIds {
  562. yanxuanActivityIdsStr = append(yanxuanActivityIdsStr, strconv.Itoa(v))
  563. }
  564. //yanxuanspecialIds = append(yanxuanspecialIds, 0)
  565. condition := " AND source IN ('article','activity','yanxuanspecial','activityvoice','activityvideo') AND IF ( source IN('activityvoice','activityvideo') , chart_permission_id = 31 ,1=1 ) " // 只有研选的文章、研选的活动、研选的专栏这三种
  566. condition += ` AND IF ( source = 'article' , source_id IN (` + strings.Join(yanxuanArticleIdsStr, ",") + `) ,1=1 ) `
  567. //pars = append(pars, yanxuanArticleIds)
  568. condition += ` AND IF ( source = 'activity' , source_id IN (` + strings.Join(yanxuanActivityIdsStr, ",") + `) ,1=1 ) `
  569. //pars = append(pars, yanxuanActivityIds)
  570. keyWord = "%" + keyWord + "%"
  571. var conditionTitle string
  572. var parsTitle []interface{}
  573. conditionTitle = " AND search_title LIKE ? " + condition
  574. parsTitle = append(parsTitle, keyWord)
  575. var conditionContentYxAuthor string // 研选专栏作者搜索条件
  576. var parsContentYxAuthor []interface{}
  577. conditionContentYxAuthor = " AND (special_name LIKE ? OR nick_name LIKE ? ) "
  578. parsContentYxAuthor = append(parsContentYxAuthor, keyWord, keyWord)
  579. totalTitle, e := models.GetResourceDataAndYanxuanSpecialAuthorCount(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor)
  580. //totalTitle, e := models.GetResourceDataCount(conditionTitle, parsTitle)
  581. if e != nil {
  582. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorCount, Err: " + e.Error())
  583. return
  584. }
  585. var conditionContent string
  586. var parsContent []interface{}
  587. conditionContent = " AND search_content LIKE ? AND search_title NOT LIKE ? " + condition
  588. parsContent = append(parsContent, keyWord, keyWord)
  589. totalContent, e := models.GetResourceDataCount(conditionContent, parsContent)
  590. if e != nil {
  591. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  592. return
  593. }
  594. var searchTotal int
  595. searchTotal = (startSize/pageSize + 1) * pageSize
  596. var list []*models.CygxResourceData
  597. if totalTitle >= searchTotal {
  598. //全部都是标题搜索
  599. list, e = models.GetResourceDataAndYanxuanSpecialAuthorListCondition(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor, startSize, pageSize)
  600. if e != nil && e.Error() != utils.ErrNoRow() {
  601. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  602. return
  603. }
  604. } else if totalTitle <= searchTotal-pageSize {
  605. //全部都是内容搜索
  606. startSize = startSize - totalTitle
  607. list, e = models.GetResourceDataListCondition(conditionContent, parsContent, startSize, pageSize)
  608. if e != nil && e.Error() != utils.ErrNoRow() {
  609. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  610. return
  611. }
  612. } else {
  613. //一半标题搜索,一半内容搜索
  614. //list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
  615. list, e = models.GetResourceDataAndYanxuanSpecialAuthorListCondition(conditionTitle, parsTitle, conditionContentYxAuthor, parsContentYxAuthor, startSize, pageSize)
  616. if e != nil && e.Error() != utils.ErrNoRow() {
  617. err = errors.New("GetResourceDataAndYanxuanSpecialAuthorListCondition, Err: " + e.Error())
  618. return
  619. }
  620. listContent, e := models.GetResourceDataListCondition(conditionContent, parsContent, 0, pageSize-totalContent%pageSize)
  621. if e != nil && e.Error() != utils.ErrNoRow() {
  622. err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
  623. return
  624. }
  625. for _, v := range listContent {
  626. list = append(list, v)
  627. }
  628. }
  629. for _, v := range list {
  630. item := new(SearchComprehensiveItem)
  631. item.SourceId = v.SourceId
  632. item.Source = v.Source
  633. result = append(result, item)
  634. }
  635. total = totalTitle + totalContent
  636. return
  637. }
  638. // Es研选专栏
  639. func EsAddYanxuanSpecial(sourceId int) {
  640. var err error
  641. defer func() {
  642. if err != nil {
  643. fmt.Println("err:", err)
  644. go utils.SendAlarmMsg(fmt.Sprint("更新研选专栏失败sourceId: ", sourceId), 2)
  645. }
  646. }()
  647. detail, e := models.GetYanxuanSpecialItemById(sourceId)
  648. if e != nil {
  649. err = errors.New("GetArticleInfoOtherByArticleId" + e.Error())
  650. return
  651. }
  652. content := html.UnescapeString(detail.Content)
  653. doc, e := goquery.NewDocumentFromReader(strings.NewReader(content))
  654. if e != nil {
  655. err = errors.New("goquery.NewDocumentFromReader" + e.Error())
  656. return
  657. }
  658. bodyText := doc.Text()
  659. item := new(ElasticComprehensiveDetail)
  660. item.SourceId = detail.Id
  661. item.Source = utils.CYGX_OBJ_YANXUANSPECIAL
  662. item.Title = detail.Title
  663. item.PublishDate = detail.PublishTime
  664. item.BodyText = bodyText
  665. item.Abstract = bodyText
  666. item.IndustryName = detail.IndustryTags
  667. item.SubjectNames = detail.CompanyTags + detail.Tags
  668. if detail.Status == 3 {
  669. EsAddOrEditComprehensiveData(item) //如果发布了就新增
  670. } else {
  671. EsDeleteComprehensiveData(item) // 没有发布就删除
  672. }
  673. return
  674. }
  675. // 新增和修改数据
  676. func EsAddOrEditComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  677. indexName := utils.IndexNameComprehensive
  678. //return
  679. defer func() {
  680. if err != nil {
  681. fmt.Println(err, item.SourceId)
  682. //go utils.SendAlarmMsg("更新综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  683. }
  684. }()
  685. client := utils.Client
  686. mustMap := make([]interface{}, 0)
  687. mustMap = append(mustMap, map[string]interface{}{
  688. "term": map[string]interface{}{
  689. "SourceId": item.SourceId,
  690. },
  691. })
  692. mustMap = append(mustMap, map[string]interface{}{
  693. "term": map[string]interface{}{
  694. "Source": item.Source,
  695. },
  696. })
  697. queryMap := map[string]interface{}{
  698. "query": map[string]interface{}{
  699. "bool": map[string]interface{}{
  700. "must": mustMap,
  701. },
  702. },
  703. }
  704. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  705. total, e := requestTotalHits.Do(context.Background())
  706. if e != nil {
  707. err = errors.New("requestTotalHits.Do(context.Background()), Err: " + e.Error())
  708. return
  709. }
  710. //return
  711. //根据来源以及ID ,判断内容是否存在,如果存在就新增,如果不存在就修改
  712. if total == 0 {
  713. resp, e := client.Index().Index(indexName).BodyJson(item).Do(context.Background())
  714. if e != nil {
  715. err = errors.New("client.Index().Index(indexName).BodyJson(item).Do(context.Background()), Err: " + e.Error())
  716. return
  717. }
  718. if resp.Status == 0 && resp.Result == "created" {
  719. //fmt.Println("新增成功")
  720. //err = nil
  721. return
  722. } else {
  723. //err = errors.New(fmt.Sprint(resp))
  724. err = errors.New(fmt.Sprint("articleId", item.SourceId))
  725. return
  726. }
  727. } else {
  728. //拼接需要改动的前置条件
  729. bool_query := elastic.NewBoolQuery()
  730. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  731. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  732. //设置需要改动的内容
  733. var script string
  734. script += fmt.Sprint("ctx._source['SubjectNames'] = '", item.SubjectNames, "';")
  735. script += fmt.Sprint("ctx._source['PublishDate'] = '", item.PublishDate, "';")
  736. script += fmt.Sprint("ctx._source['IsSummary'] = ", item.IsSummary, ";")
  737. script += fmt.Sprint("ctx._source['Abstract'] = '", item.Abstract, "';")
  738. script += fmt.Sprint("ctx._source['Title'] = '", item.Title, "';")
  739. script += fmt.Sprint("ctx._source['BodyText'] = '", item.BodyText, "';")
  740. script += fmt.Sprint("ctx._source['Annotation'] = '", item.Annotation, "';")
  741. script += fmt.Sprint("ctx._source['IndustryName'] = '", item.IndustryName, "'")
  742. _, e = client.UpdateByQuery(indexName).
  743. Query(bool_query).
  744. Script(elastic.NewScriptInline(script)).
  745. Refresh("true").
  746. Do(context.Background())
  747. if e != nil && e.Error() != "elastic: Error 400 (Bad Request): compile error [type=script_exception]" {
  748. //文本内容过长的时候,修改会报 400 的错误,暂时先不处理
  749. //fmt.Println("err", e.Error())
  750. ////err = e
  751. //err = errors.New("client.UpdateByQuery(indexName), Err: " + e.Error())
  752. return
  753. }
  754. }
  755. return
  756. }
  757. // 删除数据
  758. func EsDeleteComprehensiveData(item *ElasticComprehensiveDetail) (err error) {
  759. defer func() {
  760. if err != nil {
  761. fmt.Println(err)
  762. go utils.SendAlarmMsg("删除数据综合页面数据Es失败"+err.Error()+fmt.Sprint(item), 2)
  763. }
  764. }()
  765. indexName := utils.IndexNameComprehensive
  766. client := utils.Client
  767. //拼接需要删除的前置条件
  768. bool_query := elastic.NewBoolQuery()
  769. bool_query.Must(elastic.NewTermQuery("SourceId", item.SourceId))
  770. bool_query.Must(elastic.NewTermQuery("Source", item.Source))
  771. _, e := client.DeleteByQuery(indexName).
  772. Query(bool_query).
  773. Do(context.Background())
  774. if e != nil {
  775. err = errors.New(" client.DeleteByQuery(indexName), Err: " + e.Error())
  776. return
  777. }
  778. return
  779. }