merchant_product.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. Deleted bool
  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: product.RiskLevel,
  68. Type: string(product.Type),
  69. TypeCN: typeTransfer[product.Type],
  70. IsPermanent: product.IsPermanent,
  71. ValidDays: product.ValidDays,
  72. SourceId: product.SourceId,
  73. SaleStatus: string(product.SaleStatus),
  74. CreatedTime: product.CreatedTime,
  75. Deleted: product.Deleted > 0,
  76. }
  77. }
  78. func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) {
  79. productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType)
  80. if err != nil {
  81. if errors.Is(err, gorm.ErrRecordNotFound) {
  82. logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType)
  83. } else {
  84. logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err)
  85. }
  86. return
  87. }
  88. product = convertToDTO(productDao)
  89. return
  90. }
  91. func GetProductListBySourceIds(ids []int, productType string) (productDTOS []MerchantProductDTO, err error) {
  92. productList, err := merchantDao.GetProductListBySourceIds(ids, true, typeKindTransfer[productType])
  93. if err != nil {
  94. logger.Error("获取商品列表失败[sourceIds:%v,type:%v],err:%v", ids, productType, err)
  95. return
  96. }
  97. for _, product := range productList {
  98. productDTO := convertToDTO(product)
  99. productDTOS = append(productDTOS, productDTO)
  100. }
  101. return
  102. }
  103. func GetProductListByProductType(productType string) (list []MerchantProductDTO, err error) {
  104. productList, err := merchantDao.GetProductListByProductType(typeKindTransfer[productType], true)
  105. for _, product := range productList {
  106. productDTO := convertToDTO(product)
  107. list = append(list, productDTO)
  108. }
  109. return
  110. }
  111. func GetProductPageByProductType(productIds []int, info page.PageInfo) (dtoList []MerchantProductDTO, err error) {
  112. offset := page.StartIndex(info.Current, info.PageSize)
  113. productList, err := merchantDao.GetProductPageByProductType(productIds, info.LatestId, offset, info.PageSize)
  114. if err != nil {
  115. logger.Error("获取商品列表失败[productIds:%v,pageInfo:%v],err:%v", productIds, info, err)
  116. return
  117. }
  118. for _, productInfo := range productList {
  119. productDTO := convertToDTO(productInfo)
  120. dtoList = append(dtoList, productDTO)
  121. }
  122. return
  123. }
  124. func GetProductByProductType() (productIdMap map[string][]int, err error) {
  125. productIdStrMap, err := merchantDao.GetProductByProductType()
  126. if err != nil {
  127. logger.Error("根据类型分类获取商品ID失败,err:%v", err)
  128. return
  129. }
  130. productIdMap = make(map[string][]int, len(productIdStrMap))
  131. for productType, productIds := range productIdStrMap {
  132. idStr := strings.Split(productIds, ",")
  133. productIdMap[productType], _ = stringUtils.StringToIntSlice(idStr)
  134. }
  135. return
  136. }
  137. func LatestId() (latestId int64) {
  138. return merchantDao.LatestId()
  139. }
  140. func ProductListBySort(isSignal bool, list []reportDomain.ProductSearchDTO, weightMap map[int]*atomic.Int32, info page.PageInfo) (productDTOS []MerchantProductDTO, err error) {
  141. offset := page.StartIndex(info.Current, info.PageSize)
  142. var productList []merchantDao.MerchantProduct
  143. if isSignal {
  144. var searchList []merchantDao.ProductDTO
  145. for _, product := range list {
  146. searchList = append(searchList, merchantDao.ProductDTO{
  147. SourceId: product.SourceId,
  148. SourceType: product.SourceType,
  149. Score: product.Score,
  150. })
  151. }
  152. productList, err = merchantDao.ProductListBySort(searchList, info.LatestId, offset, info.PageSize)
  153. if err != nil {
  154. return
  155. }
  156. } else {
  157. productList, err = merchantDao.PackageListBySort(weightMap, info.LatestId, offset, info.PageSize)
  158. if err != nil {
  159. return
  160. }
  161. }
  162. for _, productInfo := range productList {
  163. productDTOS = append(productDTOS, convertToDTO(productInfo))
  164. }
  165. return
  166. }
  167. func GetOffSaleProducts(productTypeList []string) (dtoList []MerchantProductDTO, err error) {
  168. var productTypeQuery []merchantDao.MerchantProductType
  169. for _, productType := range productTypeList {
  170. productTypeQuery = append(productTypeQuery, typeKindTransfer[productType])
  171. }
  172. list, err := merchantDao.GetOffSaleProducts(productTypeQuery)
  173. for _, product := range list {
  174. dtoList = append(dtoList, convertToDTO(product))
  175. }
  176. return
  177. }