merchant_product.go 2.7 KB

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