merchant_product.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. merchantDao "eta/eta_mini_ht_api/models/merchant"
  7. "github.com/shopspring/decimal"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. var (
  12. typeTransfer = map[merchantDao.MerchantProductType]string{
  13. merchantDao.Report: "单篇报告",
  14. merchantDao.Video: "单个视频",
  15. merchantDao.Audio: "单个音频",
  16. merchantDao.Package: "套餐",
  17. }
  18. typeKindTransfer = map[string]merchantDao.MerchantProductType{
  19. "report": merchantDao.Report,
  20. "video": merchantDao.Video,
  21. "audio": merchantDao.Audio,
  22. "package": merchantDao.Package,
  23. }
  24. )
  25. type MerchantProductDTO struct {
  26. Id int
  27. Title string
  28. CoverSrc int
  29. CoverUrl string
  30. Description string
  31. Price decimal.Decimal
  32. ProductTile string
  33. RiskLevel string
  34. Type string
  35. TypeCN string
  36. IsPermanent bool
  37. ValidDays int
  38. SaleStatus string
  39. SourceId int
  40. CreatedTime time.Time
  41. }
  42. func GetMerchantProductById(id int) (productDTO MerchantProductDTO, err error) {
  43. product, err := merchantDao.GetMerchantProductById(id)
  44. if err != nil {
  45. if errors.Is(err, gorm.ErrRecordNotFound) {
  46. logger.Error("商品信息不存在[productId:%d]", id)
  47. } else {
  48. logger.Error("获取商品信息失败[productId:%d],err:", id, err)
  49. }
  50. }
  51. productDTO = convertToDTO(product)
  52. return
  53. }
  54. func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO {
  55. return MerchantProductDTO{
  56. Id: product.ID,
  57. Title: product.Title,
  58. CoverSrc: product.CoverSrc,
  59. CoverUrl: product.CoverUrl,
  60. Description: product.Description,
  61. ProductTile: product.Title,
  62. Price: product.Price,
  63. RiskLevel: "",
  64. Type: string(product.Type),
  65. TypeCN: typeTransfer[product.Type],
  66. IsPermanent: product.IsPermanent,
  67. ValidDays: product.ValidDays,
  68. SourceId: product.SourceID,
  69. CreatedTime: product.CreatedTime,
  70. }
  71. }
  72. func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) {
  73. productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType)
  74. if err != nil {
  75. if errors.Is(err, gorm.ErrRecordNotFound) {
  76. logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType)
  77. } else {
  78. logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err)
  79. }
  80. return
  81. }
  82. product = convertToDTO(productDao)
  83. return
  84. }
  85. func GetProductListBySourceIds(ids []int, productType string) (productDTOS []MerchantProductDTO, err error) {
  86. productList, err := merchantDao.GetProductListBySourceIds(ids, true, typeKindTransfer[productType])
  87. if err != nil {
  88. logger.Error("获取商品列表失败[sourceIds:%v,type:%v],err:%v", ids, productType, err)
  89. return
  90. }
  91. for _, product := range productList {
  92. productDTO := convertToDTO(product)
  93. productDTOS = append(productDTOS, productDTO)
  94. }
  95. return
  96. }
  97. func GetTotalPageCountByProductType(productType string) (total int64, latestId int64) {
  98. return merchantDao.GetTotalPageCountByProductType(typeKindTransfer[productType])
  99. }
  100. func GetProductListByProductType(productType string, info page.PageInfo) (dtoList []MerchantProductDTO, err error) {
  101. offset := page.StartIndex(info.Current, info.PageSize)
  102. productList, err := merchantDao.GetProductListByProductType(typeKindTransfer[productType], info.LatestId, offset, info.PageSize)
  103. if err != nil {
  104. logger.Error("获取商品列表失败[productType:%v,pageInfo:%v],err:%v", productType, info, err)
  105. return
  106. }
  107. for _, product := range productList {
  108. productDTO := convertToDTO(product)
  109. dtoList = append(dtoList, productDTO)
  110. }
  111. return
  112. }