product_service.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package product
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. permissionService "eta/eta_mini_ht_api/domian/config"
  7. mediaDomain "eta/eta_mini_ht_api/domian/media"
  8. merchantService "eta/eta_mini_ht_api/domian/merchant"
  9. orderDomain "eta/eta_mini_ht_api/domian/order"
  10. reportDomain "eta/eta_mini_ht_api/domian/report"
  11. "eta/eta_mini_ht_api/domian/user"
  12. "eta/eta_mini_ht_api/models/config"
  13. "eta/eta_mini_ht_api/models/image"
  14. "eta/eta_mini_ht_api/models/media"
  15. merchantDao "eta/eta_mini_ht_api/models/merchant"
  16. configService "eta/eta_mini_ht_api/service/config"
  17. mediaService "eta/eta_mini_ht_api/service/media"
  18. reportService "eta/eta_mini_ht_api/service/report"
  19. userService "eta/eta_mini_ht_api/service/user"
  20. "fmt"
  21. "sort"
  22. "sync"
  23. "sync/atomic"
  24. "time"
  25. )
  26. type ProductDTO struct {
  27. Id int `json:"id"`
  28. Title string `json:"title"`
  29. SourceTitle string `json:"sourceTitle"`
  30. Abstract string `json:"abstract"`
  31. Src string `json:"src"`
  32. PermissionNames []string `json:"permissionNames"`
  33. PermissionIds []int `json:"-"`
  34. Description string `json:"description"`
  35. Price string `json:"price"`
  36. CoverSrc int `json:"coverSrc"`
  37. CoverUrl string `json:"coverUrl"`
  38. RiskLevel string `json:"riskLevel"`
  39. Type string `json:"type"`
  40. BeginDate string `json:"beginDate"`
  41. EndDate string `json:"endDate"`
  42. SourceId int `json:"sourceId"`
  43. IsSubscribe bool `json:"isSubscribe"`
  44. ReportId int `json:"reportId"`
  45. MediaId int `json:"mediaId"`
  46. }
  47. func GetProductRiskLevel(product merchantService.MerchantProductDTO) (riskLevel, sourceTitle, sourceAbsract, coverSrc, sourceSrc string, permissionNames []string, permissionIds []int, err error) {
  48. switch product.Type {
  49. case "package":
  50. permissionRisk, permissionErr := config.PermissionsByPermissionId(product.SourceId)
  51. permissionNames = []string{permissionRisk.Name}
  52. permissionIds = []int{product.SourceId}
  53. if permissionErr != nil {
  54. logger.Error("获取权限信息失败[permissionId:%d]", product.SourceId)
  55. } else {
  56. riskLevel = permissionRisk.RiskLevel
  57. }
  58. coverSrc = product.CoverUrl
  59. case "audio", "video":
  60. mediaInfo, mediaErr := media.GetMediaById(product.Type, product.SourceId)
  61. sourceTitle, coverSrc, sourceSrc = mediaInfo.MediaName, mediaInfo.CoverSrc, mediaInfo.Src
  62. if mediaErr != nil {
  63. logger.Error("获取媒体信息失败[mediaType:%s,mediaId:%d]", product.Type, product.SourceId)
  64. }
  65. permissionIds, mediaErr = media.GetMediaPermissionMappingByMedia(product.Type, product.SourceId)
  66. if mediaErr != nil {
  67. logger.Error("获取媒体权限失败[mediaType:%s,mediaId:%d]", product.Type, product.SourceId)
  68. } else {
  69. permissions, permissionErr := permissionService.GetPermissionListByIds(permissionIds)
  70. if permissionErr != nil {
  71. logger.Error("获取权限信息失败[permissionIds:%v]", permissionIds)
  72. }
  73. permissionNames = mediaService.GetMediaPermissionNames(permissionIds)
  74. riskNum := configService.GetHighestRiskLevel(permissions)
  75. riskLevel = fmt.Sprintf("R%d", riskNum)
  76. }
  77. case "report":
  78. report, reportErr := reportDomain.GetReportById(product.SourceId, 0)
  79. var coverSrcId int
  80. sourceAbsract, sourceTitle, coverSrcId = report.Abstract, report.Title, report.CoverSrc
  81. if coverSrcId > 0 {
  82. var imageSrc string
  83. imageSrc, coverSrcErr := image.GetImageSrc(coverSrcId)
  84. if coverSrcErr != nil {
  85. logger.Error("获取图片资源失败:%v,资源ID:%d", err, coverSrcId)
  86. } else {
  87. coverSrc = imageSrc
  88. }
  89. }
  90. if reportErr != nil {
  91. logger.Error("获取研报信息失败[reportId:%d]", product.SourceId)
  92. } else {
  93. switch report.Source {
  94. case reportDomain.SourceHT:
  95. permission, permissionErr := configService.GetPermissionByName(report.PlateName)
  96. if permissionErr != nil {
  97. logger.Error("获取板块权限失败[plateName:%s]", report.PlateName)
  98. } else {
  99. riskLevel = permission.RiskLevel
  100. permissionIds = []int{permission.PermissionId}
  101. }
  102. case reportDomain.SourceETA:
  103. permissions, permissionErr := configService.GetSecondPermissionsByClassifyId(report.ClassifyId)
  104. if permissionErr != nil {
  105. logger.Error("获取板块权限失败[plateName:%s]", report.PlateName)
  106. } else {
  107. riskNum := configService.GetHighestRiskLevel(permissions)
  108. for _, permission := range permissions {
  109. permissionIds = append(permissionIds, permission.PermissionId)
  110. }
  111. riskLevel = fmt.Sprintf("R%d", riskNum)
  112. }
  113. }
  114. _, permissionNames = reportService.GetReportPermissionNames(report.OrgId, report.Source)
  115. }
  116. default:
  117. logger.Warn("不支持的产品类型[%s]", product.Type)
  118. }
  119. return
  120. }
  121. func GetProductInfoById(productId int) (product ProductDTO, err error) {
  122. merchantProduct, err := merchantService.GetMerchantProductById(productId)
  123. if err != nil {
  124. err = exception.NewWithException(exception.ProductInfoError, err.Error())
  125. return
  126. }
  127. product = convertToProductDTO(merchantProduct)
  128. product.RiskLevel, product.SourceTitle, product.Abstract, product.CoverUrl, product.Src, product.PermissionNames, _, err = GetProductRiskLevel(merchantProduct)
  129. if err != nil {
  130. logger.Error("获取风险等级失败[productId:%d]", productId)
  131. }
  132. return
  133. }
  134. func convertToProductDTO(product merchantService.MerchantProductDTO) (productDTO ProductDTO) {
  135. beginDate := time.Now()
  136. endDate := beginDate.Add(time.Duration(product.ValidDays) * 24 * time.Hour)
  137. productDTO = ProductDTO{
  138. Id: product.Id,
  139. Title: product.Title,
  140. Description: product.Description,
  141. Price: product.Price,
  142. RiskLevel: product.RiskLevel,
  143. CoverSrc: product.CoverSrc,
  144. CoverUrl: product.CoverUrl,
  145. Type: product.Type,
  146. BeginDate: beginDate.Format(time.DateOnly),
  147. EndDate: endDate.Format(time.DateOnly),
  148. SourceId: product.SourceId,
  149. }
  150. if product.CoverUrl == "" && product.CoverSrc > 0 {
  151. imageSrc, err := image.GetImageSrc(product.CoverSrc)
  152. if err != nil {
  153. logger.Error("获取图片地址失败:%v,资源Id:%d", err, product.CoverSrc)
  154. } else {
  155. productDTO.CoverUrl = imageSrc
  156. }
  157. }
  158. return
  159. }
  160. // OpenProduct 开通产品
  161. func OpenProduct(productOrder orderDomain.ProductOrderDTO) (err error) {
  162. product, err := merchantService.GetMerchantProductById(productOrder.ProductID)
  163. if err != nil {
  164. logger.Error("获取产品信息失败:%v", err)
  165. return
  166. }
  167. return merchantService.OpenProduct(productOrder.TemplateUserID, productOrder.OrderID, product)
  168. }
  169. // CloseProduct 退款关闭套餐
  170. func CloseProduct(productOrder orderDomain.ProductOrderDTO) (err error) {
  171. product, err := merchantService.GetMerchantProductById(productOrder.ProductID)
  172. if err != nil {
  173. logger.Error("获取产品信息失败:%v", err)
  174. return
  175. }
  176. return merchantService.CloseProduct(productOrder.TemplateUserID, productOrder.OrderID, product)
  177. }
  178. // 过期产品
  179. func GetRelatePackage(info ProductDTO) (prodList []ProductDTO, err error) {
  180. switch info.Type {
  181. case "audio", "video":
  182. permissionIds, permissionErr := media.GetMediaPermissionMappingByMedia(info.Type, info.SourceId)
  183. if permissionErr != nil {
  184. logger.Error("获取媒体品种信息失败:%v", err)
  185. return
  186. }
  187. merchantProduct, pdErr := merchantService.GetProductListBySourceIds(permissionIds, "package")
  188. if pdErr != nil {
  189. return
  190. }
  191. for _, product := range merchantProduct {
  192. if !product.Deleted && product.SaleStatus == "on_sale" {
  193. prodList = append(prodList, convertToProductDTO(product))
  194. }
  195. }
  196. return
  197. case "report":
  198. reportInfo, reportErr := reportDomain.GetReportById(info.SourceId, 0)
  199. if reportErr != nil {
  200. logger.Error("获取报告信息失败:%v", err)
  201. return
  202. }
  203. permissions := reportDomain.GetReportSecondPermissionsById(reportInfo.OrgId, reportInfo.Source)
  204. var permissionIds []int
  205. if len(permissions) > 0 {
  206. for _, permission := range permissions {
  207. permissionIds = append(permissionIds, permission.PermissionId)
  208. }
  209. permissionIds = append(permissionIds, permissions[0].PermissionId)
  210. }
  211. merchantProduct, pdErr := merchantService.GetProductListBySourceIds(permissionIds, "package")
  212. if pdErr != nil {
  213. return
  214. }
  215. for _, product := range merchantProduct {
  216. prodList = append(prodList, convertToProductDTO(product))
  217. }
  218. return
  219. default:
  220. err = exception.New(exception.ProductTypeError)
  221. return
  222. }
  223. }
  224. func GetProductListByProductType(productType string, permissionIds []int, templateUserId int) (total, latestId int64, productIds []int) {
  225. filterPermissionIds, riskLevel, _, err := userService.GetRiskLevelPermissionList(permissionIds, true, templateUserId)
  226. if err != nil {
  227. return
  228. }
  229. var dtoList []*ProductDTO
  230. productList, err := merchantService.GetProductListByProductType(productType)
  231. var wg sync.WaitGroup
  232. wg.Add(len(productList))
  233. for i := 0; i < len(productList); i++ {
  234. go func(merchantProduct merchantService.MerchantProductDTO) {
  235. defer wg.Done()
  236. product := convertToProductDTO(merchantProduct)
  237. product.RiskLevel, product.SourceTitle, product.Abstract, product.CoverUrl, product.Src, product.PermissionNames, product.PermissionIds, err = GetProductRiskLevel(merchantProduct)
  238. if !compare(product.RiskLevel, riskLevel) {
  239. return
  240. }
  241. match := false
  242. for _, pdPermissionId := range product.PermissionIds {
  243. for _, permissionId := range filterPermissionIds {
  244. if pdPermissionId == permissionId {
  245. match = true
  246. break
  247. }
  248. }
  249. }
  250. if !match {
  251. return
  252. }
  253. dtoList = append(dtoList, &product)
  254. }(productList[i])
  255. }
  256. wg.Wait()
  257. total = int64(len(dtoList))
  258. var maxId int
  259. for i := 0; i < len(dtoList); i++ {
  260. productIds = append(productIds, dtoList[i].Id)
  261. if dtoList[i].Id > maxId {
  262. maxId = dtoList[i].Id
  263. }
  264. }
  265. latestId = int64(maxId)
  266. return
  267. }
  268. func compare(riskLevel, MatchRiskLevel string) bool {
  269. pRiskNum, riskErr := configService.ParseRiskLevel(riskLevel)
  270. if riskErr != nil {
  271. return false
  272. }
  273. riskLevelNum, riskErr := configService.ParseRiskLevel(MatchRiskLevel)
  274. if riskErr != nil {
  275. return false
  276. }
  277. if pRiskNum <= riskLevelNum {
  278. return true
  279. }
  280. return false
  281. }
  282. func ProductList(productIds []int, templateUserId int, info page.PageInfo) (dtoList []ProductDTO, err error) {
  283. var merchantProductList []merchantService.MerchantProductDTO
  284. merchantProductList, err = merchantService.GetProductPageByProductType(productIds, info)
  285. for _, product := range merchantProductList {
  286. productDTO := convertToProductDTO(product)
  287. productDTO.RiskLevel, productDTO.SourceTitle, productDTO.Abstract, productDTO.CoverUrl, productDTO.Src, productDTO.PermissionNames, _, err = GetProductRiskLevel(product)
  288. dtoList = append(dtoList, productDTO)
  289. }
  290. var wg sync.WaitGroup
  291. wg.Add(len(dtoList))
  292. for i := 0; i < len(dtoList); i++ {
  293. go func(productDTO *ProductDTO) {
  294. defer wg.Done()
  295. productDTO.IsSubscribe = isSubscribeSignal(productDTO.Id, templateUserId)
  296. }(&dtoList[i])
  297. }
  298. wg.Wait()
  299. return
  300. }
  301. func RangeProductList() (productIdMap map[string][]int, err error) {
  302. return merchantService.GetProductByProductType()
  303. }
  304. func ProductSearch(key string, templateUserId int, pageInfo page.PageInfo) (list []ProductDTO, err error) {
  305. var merchantProductList []merchantService.MerchantProductDTO
  306. var sourceIds []int
  307. var maxId int
  308. for _, product := range merchantProductList {
  309. if product.SourceId > maxId {
  310. maxId = product.SourceId
  311. }
  312. sourceIds = append(sourceIds, product.SourceId)
  313. }
  314. //reportList, err := reportDomain.SearchReportList(key, sourceIds, 1, len(sourceIds), int64(maxId))
  315. //mediaList, err := mediaDomain.SearchMediaList(key)
  316. for _, product := range merchantProductList {
  317. productDTO := convertToProductDTO(product)
  318. list = append(list, productDTO)
  319. }
  320. return
  321. }
  322. func convertReportToSearchDTO(report reportDomain.ReportDTO) (dto reportDomain.ProductSearchDTO) {
  323. return reportDomain.ProductSearchDTO{
  324. HighLight: report.Highlight[0],
  325. SourceId: report.ReportID,
  326. SourceType: "report",
  327. Score: report.Score,
  328. }
  329. }
  330. func convertMediaToSearchDTO(media mediaDomain.MediaDTO) (dto reportDomain.ProductSearchDTO) {
  331. return reportDomain.ProductSearchDTO{
  332. HighLight: media.Highlight[0],
  333. SourceId: media.MediaId,
  334. SourceType: media.MediaType,
  335. Score: media.Score,
  336. }
  337. }
  338. func SearchRelateProduct(key string, productIdMap map[string][]int) (list []reportDomain.ProductSearchDTO, err error) {
  339. var wg sync.WaitGroup
  340. wg.Add(2)
  341. go func() {
  342. defer wg.Done()
  343. docIds := productIdMap["report"]
  344. reports, reportErr := reportService.SearchReportProduct(key, docIds)
  345. if reportErr != nil {
  346. logger.Error("搜索相关报告失败:%v,key:%s", reportErr, key)
  347. return
  348. }
  349. for _, report := range reports {
  350. list = append(list, convertReportToSearchDTO(report))
  351. }
  352. }()
  353. go func() {
  354. defer wg.Done()
  355. docIds := append(productIdMap["audio"], productIdMap["video"]...)
  356. medias, mediaErr := mediaService.SearchMediaProduct(key, docIds)
  357. if mediaErr != nil {
  358. logger.Error("搜索相关媒体失败:%v,key:%s", mediaErr, key)
  359. return
  360. }
  361. for _, mediaInfo := range medias {
  362. list = append(list, convertMediaToSearchDTO(mediaInfo))
  363. }
  364. }()
  365. wg.Wait()
  366. sort.Slice(list, func(i, j int) bool {
  367. return list[i].Score > list[j].Score
  368. })
  369. return
  370. }
  371. func LatestId() (latestId int64) {
  372. return merchantService.LatestId()
  373. }
  374. func CountSearchPackageList(list []reportDomain.ProductSearchDTO) (total, latestId int64, permissionTotalMap map[int]*atomic.Int32) {
  375. productIdMap := make(map[string][]int, len(list))
  376. permissionTotalMap = make(map[int]*atomic.Int32)
  377. for _, product := range list {
  378. ids := productIdMap[product.SourceType]
  379. ids = append(ids, product.SourceId)
  380. productIdMap[product.SourceType] = ids
  381. }
  382. var wg sync.WaitGroup
  383. wg.Add(len(productIdMap))
  384. for key, ids := range productIdMap {
  385. go func(k string, ids []int) {
  386. defer wg.Done()
  387. var permissionMap map[int]int
  388. switch k {
  389. case "report":
  390. permissionMap = reportService.CountPermissionWeight(ids)
  391. case "audio", "video":
  392. permissionMap = mediaService.CountPermissionWeight(ids)
  393. }
  394. for permissionId, weight := range permissionMap {
  395. if permissionTotalMap[permissionId] == nil {
  396. permissionTotalMap[permissionId] = new(atomic.Int32)
  397. }
  398. permissionTotalMap[permissionId].Add(int32(weight))
  399. }
  400. }(key, ids)
  401. }
  402. wg.Wait()
  403. total = int64(len(permissionTotalMap))
  404. latestId = merchantService.LatestId()
  405. return
  406. }
  407. func ProductListBySort(isSignal bool, list []reportDomain.ProductSearchDTO, weightMap map[int]*atomic.Int32, templateUserId int, info page.PageInfo) (resultList []ProductDTO, err error) {
  408. var pdDTOS []merchantService.MerchantProductDTO
  409. pdDTOS, err = merchantService.ProductListBySort(isSignal, list, weightMap, info)
  410. var wg sync.WaitGroup
  411. wg.Add(len(pdDTOS))
  412. for _, pd := range pdDTOS {
  413. go func(pd merchantService.MerchantProductDTO) {
  414. defer wg.Done()
  415. productDTO := convertToProductDTO(pd)
  416. productDTO.RiskLevel, productDTO.SourceTitle, productDTO.Abstract, productDTO.CoverUrl, productDTO.Src, productDTO.PermissionNames, _, err = GetProductRiskLevel(pd)
  417. productDTO.IsSubscribe = isSubscribeSignal(pd.Id, templateUserId)
  418. resultList = append(resultList, productDTO)
  419. }(pd)
  420. }
  421. wg.Wait()
  422. return
  423. }
  424. func isSubscribeSignal(productId int, templateUserId int) (isSubscribe bool) {
  425. subscribeList, subErr := user.GetUserSubscribe([]int{productId}, templateUserId)
  426. if subErr != nil {
  427. logger.Error("获取用户订阅状态失败:%v", subErr)
  428. return false
  429. } else {
  430. if len(subscribeList) == 0 {
  431. logger.Error("用户未订阅单品,productId:%v", productId)
  432. return false
  433. } else {
  434. return subscribeList[0].Status == merchantDao.SubscribeValid
  435. }
  436. }
  437. }