merchant_product.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package merchant
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. stringUtils "eta/eta_mini_ht_api/common/utils/string"
  7. reportDomain "eta/eta_mini_ht_api/domian/report"
  8. merchantDao "eta/eta_mini_ht_api/models/merchant"
  9. "github.com/shopspring/decimal"
  10. "gorm.io/gorm"
  11. "strings"
  12. "sync/atomic"
  13. "time"
  14. )
  15. var (
  16. typeTransfer = map[merchantDao.MerchantProductType]string{
  17. merchantDao.Report: "单篇报告",
  18. merchantDao.Video: "单个视频",
  19. merchantDao.Audio: "单个音频",
  20. merchantDao.Package: "套餐",
  21. }
  22. typeKindTransfer = map[string]merchantDao.MerchantProductType{
  23. "report": merchantDao.Report,
  24. "video": merchantDao.Video,
  25. "audio": merchantDao.Audio,
  26. "package": merchantDao.Package,
  27. }
  28. )
  29. type MerchantProductDTO struct {
  30. Id int
  31. Title string
  32. CoverSrc int
  33. CoverUrl string
  34. Description string
  35. Price decimal.Decimal
  36. ProductTile string
  37. RiskLevel string
  38. Type string
  39. TypeCN string
  40. IsPermanent bool
  41. ValidDays int
  42. SaleStatus string
  43. SourceId int
  44. CreatedTime time.Time
  45. }
  46. func GetMerchantProductById(id int) (productDTO MerchantProductDTO, err error) {
  47. product, err := merchantDao.GetMerchantProductById(id)
  48. if err != nil {
  49. if errors.Is(err, gorm.ErrRecordNotFound) {
  50. logger.Error("商品信息不存在[productId:%d]", id)
  51. } else {
  52. logger.Error("获取商品信息失败[productId:%d],err:", id, err)
  53. }
  54. }
  55. productDTO = convertToDTO(product)
  56. return
  57. }
  58. func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO {
  59. return MerchantProductDTO{
  60. Id: product.ID,
  61. Title: product.Title,
  62. CoverSrc: product.CoverSrc,
  63. CoverUrl: product.CoverUrl,
  64. Description: product.Description,
  65. ProductTile: product.Title,
  66. Price: product.Price,
  67. RiskLevel: "",
  68. Type: string(product.Type),
  69. TypeCN: typeTransfer[product.Type],
  70. IsPermanent: product.IsPermanent,
  71. ValidDays: product.ValidDays,
  72. SourceId: product.SourceID,
  73. CreatedTime: product.CreatedTime,
  74. }
  75. }
  76. func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) {
  77. productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType)
  78. if err != nil {
  79. if errors.Is(err, gorm.ErrRecordNotFound) {
  80. logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType)
  81. } else {
  82. logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err)
  83. }
  84. return
  85. }
  86. product = convertToDTO(productDao)
  87. return
  88. }
  89. func GetProductListBySourceIds(ids []int, productType string) (productDTOS []MerchantProductDTO, err error) {
  90. productList, err := merchantDao.GetProductListBySourceIds(ids, true, typeKindTransfer[productType])
  91. if err != nil {
  92. logger.Error("获取商品列表失败[sourceIds:%v,type:%v],err:%v", ids, productType, err)
  93. return
  94. }
  95. for _, product := range productList {
  96. productDTO := convertToDTO(product)
  97. productDTOS = append(productDTOS, productDTO)
  98. }
  99. return
  100. }
  101. func GetProductListByProductType(productType string) (list []MerchantProductDTO, err error) {
  102. productList, err := merchantDao.GetProductListByProductType(typeKindTransfer[productType], true)
  103. for _, product := range productList {
  104. productDTO := convertToDTO(product)
  105. list = append(list, productDTO)
  106. }
  107. return
  108. }
  109. func GetProductPageByProductType(productIds []int, info page.PageInfo) (dtoList []MerchantProductDTO, err error) {
  110. offset := page.StartIndex(info.Current, info.PageSize)
  111. productList, err := merchantDao.GetProductPageByProductType(productIds, info.LatestId, offset, info.PageSize)
  112. if err != nil {
  113. logger.Error("获取商品列表失败[productIds:%v,pageInfo:%v],err:%v", productIds, info, err)
  114. return
  115. }
  116. for _, productInfo := range productList {
  117. productDTO := convertToDTO(productInfo)
  118. dtoList = append(dtoList, productDTO)
  119. }
  120. return
  121. }
  122. func GetProductByProductType() (productIdMap map[string][]int, err error) {
  123. productMap, err := merchantDao.GetProductByProductType()
  124. if err != nil {
  125. logger.Error("根据类型分类获取商品ID失败,err:%v", err)
  126. return
  127. }
  128. productIdMap = make(map[string][]int, len(productMap))
  129. for productType, productIds := range productMap {
  130. idStr := strings.Split(productIds, ",")
  131. productIdMap[productType], _ = stringUtils.StringToIntSlice(idStr)
  132. }
  133. return
  134. }
  135. func LatestId() (latestId int64) {
  136. return merchantDao.LatestId()
  137. }
  138. func ProductListBySort(isSignal bool, list []reportDomain.ProductSearchDTO, weightMap map[int]*atomic.Int32, info page.PageInfo) (productDTOS []MerchantProductDTO, err error) {
  139. offset := page.StartIndex(info.Current, info.PageSize)
  140. var productList []merchantDao.MerchantProduct
  141. if isSignal {
  142. var searchList []merchantDao.ProductDTO
  143. for _, product := range list {
  144. searchList = append(searchList, merchantDao.ProductDTO{
  145. SourceId: product.SourceId,
  146. SourceType: product.SourceType,
  147. Score: product.Score,
  148. })
  149. }
  150. productList, err = merchantDao.ProductListBySort(searchList, info.LatestId, offset, info.PageSize)
  151. if err != nil {
  152. return
  153. }
  154. } else {
  155. productList, err = merchantDao.PackageListBySort(weightMap, info.LatestId, offset, info.PageSize)
  156. if err != nil {
  157. return
  158. }
  159. }
  160. for _, productInfo := range productList {
  161. productDTOS = append(productDTOS, convertToDTO(productInfo))
  162. }
  163. return
  164. }