merchant_product.go 5.7 KB

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