merchant_product.go 2.8 KB

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