media_service.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package media
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/config"
  5. "eta/eta_mini_ht_api/common/component/es"
  6. logger "eta/eta_mini_ht_api/common/component/log"
  7. "eta/eta_mini_ht_api/common/contants"
  8. "eta/eta_mini_ht_api/common/utils/page"
  9. configDomain "eta/eta_mini_ht_api/domian/config"
  10. reportService "eta/eta_mini_ht_api/domian/report"
  11. "eta/eta_mini_ht_api/models"
  12. configDao "eta/eta_mini_ht_api/models/config"
  13. "eta/eta_mini_ht_api/models/image"
  14. mediaDao "eta/eta_mini_ht_api/models/media"
  15. merchantDao "eta/eta_mini_ht_api/models/merchant"
  16. "sort"
  17. "strconv"
  18. "time"
  19. )
  20. var (
  21. htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
  22. )
  23. const (
  24. DESC models.Order = "desc"
  25. ASC models.Order = "asc"
  26. ESColumn = "mediaName"
  27. ESRangeColumn = "mediaId"
  28. ConditionColumn = "mediaType"
  29. )
  30. var (
  31. sortField = []string{"_score:desc"}
  32. transProductMap = map[string]merchantDao.MerchantProductType{
  33. "report": merchantDao.Report,
  34. "video": merchantDao.Video,
  35. "audio": merchantDao.Audio,
  36. "package": merchantDao.Package,
  37. }
  38. )
  39. func elastic() *es.ESClient {
  40. return es.GetInstance()
  41. }
  42. type MediaDTO struct {
  43. Type string `json:"type"`
  44. MediaId int `json:"mediaId"`
  45. AuthorId int `json:"authorId"`
  46. AuthorName string `json:"authorName,omitempty"`
  47. MediaType string `json:"mediaType"`
  48. Src string `json:"src"`
  49. MediaName string `json:"mediaName"`
  50. MediaTitle string `json:"mediaTitle,omitempty"`
  51. CoverSrc string `json:"coverSrc"`
  52. CoverUrl string `json:"coverUrl"`
  53. SourceType string `json:"sourceType"`
  54. MediaPlayMilliseconds int `json:"mediaPlayMilliseconds"`
  55. PublishedTime string `json:"publishedTime"`
  56. PermissionIDs string `json:"permissionIds"`
  57. PermissionNames []string `json:"permissionNames,omitempty"`
  58. Highlight []string `json:"highlight,omitempty"`
  59. Login bool `json:"login"`
  60. RiskLevelStatus string `json:"riskLevelStatus"`
  61. IsFree bool `json:"isFree"`
  62. Price string `json:"price"`
  63. IsSubscribe bool `json:"isSubscribe"`
  64. SubscribeStatus string `json:"subscribeStatus"`
  65. IsPackage bool `json:"isPackage"`
  66. RiskLevel string `json:"riskLevel"`
  67. ProductId int `json:"productId"`
  68. Score float64 `json:"score"`
  69. }
  70. func SearchMediaList(_ string, key string, mediaIds []int, from int, size int, max int64) (medias []MediaDTO, err error) {
  71. //同步es
  72. var docIds []string
  73. for _, id := range mediaIds {
  74. docIds = append(docIds, strconv.Itoa(id))
  75. }
  76. sorts := append(sortField, "publishedTime:desc")
  77. request := matchRangeWithDocIds(key, from, size, max, sorts, docIds)
  78. re, err := elastic().Search(request)
  79. if err != nil {
  80. logger.Error("es搜索失败:%v", err)
  81. }
  82. hits := elastic().GetSource(re.Hits)
  83. for _, hit := range hits {
  84. var content map[string][]string
  85. err = json.Unmarshal(hit.Highlight, &content)
  86. media := MediaDTO{}
  87. err = json.Unmarshal(hit.Source, &media)
  88. if err != nil {
  89. logger.Error("解析媒体数据失败:%v", err)
  90. continue
  91. }
  92. media.Highlight = content[ESColumn]
  93. media.PublishedTime = media.PublishedTime[:10]
  94. media.MediaTitle = media.Highlight[0]
  95. medias = append(medias, media)
  96. }
  97. return
  98. }
  99. func SearchMaxMediaId(mediaType string, key string) (total int64, latestId int64) {
  100. mediaSort := []string{"mediaId:desc"}
  101. request := matchAllByCondition(mediaSort, key, ConditionColumn, mediaType)
  102. //同步es
  103. //re, err := elastic().Search(request)
  104. re, err := elastic().Count(request)
  105. if err != nil {
  106. logger.Error("es搜索失败:%v", err)
  107. }
  108. count := re.Count
  109. total = int64(count)
  110. if total > 0 {
  111. request = matchByCondition(mediaSort, key, ConditionColumn, mediaType, 0, count)
  112. re, err = elastic().Search(request)
  113. if err != nil {
  114. logger.Error("es搜索异常:%v", err)
  115. }
  116. hits := elastic().GetSource(re.Hits)
  117. data := hits[0].Source
  118. media := MediaDTO{}
  119. err = json.Unmarshal(data, &media)
  120. if err != nil {
  121. logger.Error("获取当前最大媒体id失败:%v", err)
  122. return
  123. }
  124. total = int64(len(hits))
  125. latestId = int64(media.MediaId)
  126. return
  127. }
  128. return
  129. }
  130. func GetMediaPermissionMappingByPermissionIds(mediaType string, permissionIds []int) (total int64, latestId int64, ids []int) {
  131. mediaIds, err := mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, permissionIds)
  132. if err != nil {
  133. logger.Error("获取配置品种的媒体列表信息失败:%v", err)
  134. return 0, 0, ids
  135. }
  136. //获取一下下架的报告产品
  137. var offSaleProducts []merchantDao.MerchantProduct
  138. offSaleProducts, err = merchantDao.GetOffSaleProducts([]merchantDao.MerchantProductType{transProductMap[mediaType], merchantDao.Package})
  139. var disCardMediaIds []int
  140. var ProductPermissionIds []int
  141. var filterIds []int
  142. if err != nil {
  143. logger.Error("获取下架的报告产品失败:%v", err)
  144. } else {
  145. for _, product := range offSaleProducts {
  146. if product.Type == "package" {
  147. ProductPermissionIds = append(ProductPermissionIds, product.SourceId)
  148. }
  149. if product.Type == transProductMap[mediaType] {
  150. disCardMediaIds = append(disCardMediaIds, product.SourceId)
  151. }
  152. }
  153. }
  154. if len(ProductPermissionIds) > 0 {
  155. var disCardIds []int
  156. disCardIds, err = mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, ProductPermissionIds)
  157. if len(disCardIds) > 0 {
  158. disCardMediaIds = append(disCardMediaIds, disCardIds...)
  159. }
  160. }
  161. //对数据去重
  162. disCardMediaIds = uniqueArray(disCardMediaIds)
  163. //获取媒体中还包含上架套餐的id
  164. if len(disCardMediaIds) > 0 {
  165. mediaIdsSalePackage, _ := merchantDao.GetMediaOnSalePackageIds(disCardMediaIds)
  166. mediaIdsSaleProduct, _ := merchantDao.GetOnSaleMediaIds(disCardMediaIds, transProductMap[mediaType])
  167. showReportMap := make(map[int]bool)
  168. disCardMap := make(map[int]bool)
  169. for _, reportId := range mediaIdsSalePackage {
  170. showReportMap[reportId] = true
  171. }
  172. for _, reportId := range mediaIdsSaleProduct {
  173. showReportMap[reportId] = true
  174. }
  175. var filterDisCardReportIds []int
  176. for _, id := range disCardMediaIds {
  177. if _, ok := showReportMap[id]; !ok {
  178. filterDisCardReportIds = append(filterDisCardReportIds, id)
  179. disCardMap[id] = true
  180. }
  181. }
  182. disCardMediaIds = filterDisCardReportIds
  183. for _, id := range mediaIds {
  184. if _, ok := disCardMap[id]; !ok {
  185. filterIds = append(filterIds, id)
  186. }
  187. }
  188. } else {
  189. filterIds = mediaIds
  190. }
  191. if len(filterIds) > 0 {
  192. sort.Slice(filterIds, func(i, j int) bool {
  193. return filterIds[i] > filterIds[j]
  194. })
  195. return int64(len(filterIds)), int64(filterIds[0]), filterIds
  196. }
  197. return 0, 0, []int{}
  198. }
  199. func uniqueArray(arr []int) []int {
  200. uniqueMap := make(map[int]bool)
  201. var result []int
  202. for _, value := range arr {
  203. if _, exists := uniqueMap[value]; !exists {
  204. uniqueMap[value] = true
  205. result = append(result, value)
  206. }
  207. }
  208. return result
  209. }
  210. func GetAnalystMediaPermissionMappingByPermissionIds(mediaType string, permissionIds []int, analystId int) (total int64, latestId int64, ids []int) {
  211. meidaIds, err := mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, permissionIds)
  212. if err != nil {
  213. logger.Error("获取当前最大媒体id失败:%v", err)
  214. return 0, 0, ids
  215. }
  216. ids, err = mediaDao.GetAnalystMediaRangeMediaIds(mediaType, ids, analystId)
  217. if err != nil {
  218. logger.Error("根据研究员过滤媒体列表id失败:%v", err)
  219. return 0, 0, ids
  220. }
  221. if len(ids) == 0 {
  222. logger.Info("根据研究员过滤媒体列表id为空")
  223. return 0, 0, ids
  224. }
  225. var offSaleProducts []merchantDao.MerchantProduct
  226. offSaleProducts, err = merchantDao.GetOffSaleProducts([]merchantDao.MerchantProductType{transProductMap[mediaType], merchantDao.Package})
  227. var disCardMediaIds []int
  228. var filterPermissionIds []int
  229. var ProductPermissionIds []int
  230. var filterIds []int
  231. if err != nil {
  232. logger.Error("获取下架的报告产品失败:%v", err)
  233. } else {
  234. for _, product := range offSaleProducts {
  235. if product.Type == "package" {
  236. ProductPermissionIds = append(ProductPermissionIds, product.SourceId)
  237. }
  238. if product.Type == transProductMap[mediaType] {
  239. disCardMediaIds = append(disCardMediaIds, product.SourceId)
  240. }
  241. }
  242. }
  243. if len(ProductPermissionIds) > 0 {
  244. var disCardIds []int
  245. disCardIds, err = mediaDao.GetMediaPermissionMappingByPermissionId(mediaType, filterPermissionIds)
  246. if len(disCardMediaIds) > 0 {
  247. disCardMediaIds = append(disCardMediaIds, disCardIds...)
  248. }
  249. }
  250. //对数据去重
  251. disCardMediaIds = uniqueArray(disCardMediaIds)
  252. //获取媒体中还包含上架套餐的id
  253. if len(disCardMediaIds) > 0 {
  254. reportIdsSalePackage, _ := merchantDao.GetMediaOnSalePackageIds(disCardMediaIds)
  255. reportIdsSaleProduct, _ := merchantDao.GetOnSaleMediaIds(disCardMediaIds, transProductMap[mediaType])
  256. showReportMap := make(map[int]bool)
  257. disCardMap := make(map[int]bool)
  258. for _, reportId := range reportIdsSalePackage {
  259. showReportMap[reportId] = true
  260. }
  261. for _, reportId := range reportIdsSaleProduct {
  262. showReportMap[reportId] = true
  263. }
  264. var filterDisCardReportIds []int
  265. for _, id := range disCardMediaIds {
  266. if _, ok := showReportMap[id]; !ok {
  267. filterDisCardReportIds = append(filterDisCardReportIds, id)
  268. disCardMap[id] = true
  269. }
  270. }
  271. disCardMediaIds = filterDisCardReportIds
  272. for _, id := range meidaIds {
  273. if _, ok := disCardMap[id]; !ok {
  274. filterIds = append(filterIds, id)
  275. }
  276. }
  277. } else {
  278. filterIds = meidaIds
  279. }
  280. if len(filterIds) > 0 {
  281. sort.Slice(filterIds, func(i, j int) bool {
  282. return filterIds[i] > filterIds[j]
  283. })
  284. return int64(len(filterIds)), int64(filterIds[0]), filterIds
  285. }
  286. return 0, 0, []int{}
  287. }
  288. func GetTotalPageCount(mediaType string) (count int64, latestId int64) {
  289. return mediaDao.GetCountByMediaType(mediaType)
  290. }
  291. func GetTotalPageCountByAnalystId(mediaType string, analystId int) (count int64, latestId int64) {
  292. return mediaDao.GetCountByMediaTypeByAnalystId(mediaType, analystId)
  293. }
  294. func GetMediaPageByIds(mediaType string, pageInfo page.PageInfo, mediaIds []int) (list []MediaDTO, err error) {
  295. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  296. medias, err := mediaDao.GetMediaPageByIds(pageInfo.LatestId, pageInfo.PageSize, offset, mediaType, mediaIds)
  297. if err == nil && medias != nil {
  298. for _, media := range medias {
  299. dto := convertMediaDTO(media, false)
  300. list = append(list, dto)
  301. }
  302. }
  303. return
  304. }
  305. func GetMediaPageByAnalystId(mediaType string, pageInfo page.PageInfo, analystId int, mediaIds []int) (list []MediaDTO, err error) {
  306. offset := page.StartIndex(pageInfo.Current, pageInfo.PageSize)
  307. medias, err := mediaDao.GetMediaPageByAnalystId(pageInfo.LatestId, pageInfo.PageSize, offset, mediaType, analystId, mediaIds)
  308. if err == nil && medias != nil {
  309. for _, media := range medias {
  310. dto := convertMediaDTO(media, false)
  311. list = append(list, dto)
  312. }
  313. }
  314. return
  315. }
  316. func convertMediaDTO(media mediaDao.Media, fullTime bool) MediaDTO {
  317. if fullTime {
  318. return MediaDTO{
  319. MediaId: media.Id,
  320. AuthorId: media.AuthorID,
  321. AuthorName: media.AuthorName,
  322. MediaType: string(media.MediaType),
  323. Src: media.Src,
  324. MediaName: media.MediaName,
  325. SourceType: media.SourceType,
  326. CoverSrc: media.CoverSrc,
  327. CoverUrl: media.CoverSrc,
  328. MediaPlayMilliseconds: media.MediaPlayMilliseconds,
  329. PermissionIDs: media.PermissionIDs,
  330. PublishedTime: media.PublishedTime.Format(time.DateTime),
  331. }
  332. } else {
  333. return MediaDTO{
  334. MediaId: media.Id,
  335. AuthorId: media.AuthorID,
  336. AuthorName: media.AuthorName,
  337. MediaType: string(media.MediaType),
  338. Src: media.Src,
  339. MediaName: media.MediaName,
  340. SourceType: media.SourceType,
  341. CoverSrc: media.CoverSrc,
  342. CoverUrl: media.CoverSrc,
  343. MediaPlayMilliseconds: media.MediaPlayMilliseconds,
  344. PermissionIDs: media.PermissionIDs,
  345. PublishedTime: media.PublishedTime.Format(time.DateOnly),
  346. }
  347. }
  348. }
  349. func GetMediaById(mediaType string, mediaId int) (mediaDTO MediaDTO, err error) {
  350. media, err := mediaDao.GetMediaById(mediaType, mediaId)
  351. if err != nil {
  352. logger.Error("查询媒体信息失败:%v", err)
  353. return
  354. }
  355. mediaDTO = convertMediaDTO(media, true)
  356. return
  357. }
  358. func GetPermissionsByIds(ids []int) (permissionDTOs []configDomain.PermissionDTO, err error) {
  359. return reportService.GetFirstPermissionsByIds(ids)
  360. }
  361. func GetSecondPermissionsByIds(ids []int) (permissionDTOs []configDomain.PermissionDTO, err error) {
  362. return reportService.GetPermissionsByPermissionIds(ids)
  363. }
  364. func matchAllByCondition(sorts []string, key string, column string, value string) (request *es.ESQueryRequest) {
  365. req := new(es.ESQueryRequest)
  366. //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, 0, 1, sorts, es.MatchAllByCondition).ByCondition(column, value)
  367. return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, 0, 1, sorts, es.MatchAll)
  368. }
  369. func matchByCondition(sorts []string, key string, column string, value string, from int, to int) (request *es.ESQueryRequest) {
  370. req := new(es.ESQueryRequest)
  371. //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.MatchAllByCondition).ByCondition(column, value)
  372. return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.MatchAll)
  373. }
  374. func matchRangeByCondition(key string, from int, to int, max int64, sorts []string, column string, value string) (request *es.ESQueryRequest) {
  375. req := new(es.ESQueryRequest)
  376. //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.RangeByCondition).Range(0, max, ESRangeColumn).ByCondition(column, value)
  377. return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.Range).Range(0, max, ESRangeColumn)
  378. }
  379. func matchRangeWithDocIds(key string, from int, to int, max int64, sorts []string, docIds []string) (request *es.ESQueryRequest) {
  380. req := new(es.ESQueryRequest)
  381. //return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.RangeByCondition).Range(0, max, ESRangeColumn).ByCondition(column, value)
  382. return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, from, to, sorts, es.Range).Range(0, max, ESRangeColumn).WithDocs(docIds)
  383. }
  384. func matchLimitByScore(key string, limit int, score float64, docIds []string) (request *es.ESQueryRequest) {
  385. req := new(es.ESQueryRequest)
  386. return req.CreateESQueryRequest(htConfig.GetMediaIndex(), ESColumn, key, 0, limit, sortField, es.LimitByScore).WithScore(score).WithDocs(docIds)
  387. }
  388. func GetImageSrc(id int) (src string, err error) {
  389. return image.GetImageSrc(id)
  390. }
  391. func SearchMediaProduct(key string, limit int, score float64, docIds []int) (medias []MediaDTO, err error) {
  392. var docStrIds []string
  393. for _, docId := range docIds {
  394. docStrIds = append(docStrIds, strconv.Itoa(docId))
  395. }
  396. request := matchLimitByScore(key, limit, score, docStrIds)
  397. re, err := elastic().Search(request)
  398. if err != nil {
  399. logger.Error("es搜索异常:%v", err)
  400. }
  401. hits := elastic().GetSource(re.Hits)
  402. if len(hits) == 0 {
  403. medias = []MediaDTO{}
  404. return
  405. }
  406. for _, hit := range hits {
  407. var content map[string][]string
  408. err = json.Unmarshal(hit.Highlight, &content)
  409. media := MediaDTO{}
  410. err = json.Unmarshal(hit.Source, &media)
  411. if err != nil {
  412. logger.Error("解析研报数据失败:%v", err)
  413. continue
  414. }
  415. media.Score = hit.Score
  416. media.Highlight = content[ESColumn]
  417. media.PublishedTime = media.PublishedTime[:10]
  418. media.MediaTitle = media.Highlight[0]
  419. medias = append(medias, media)
  420. }
  421. return
  422. }
  423. func CountPermissionWeight(ids []int) (list []configDao.PermissionWeight, err error) {
  424. return mediaDao.CountPermissionWeight(ids)
  425. }