merchant_product.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 string
  28. Description string
  29. Price decimal.Decimal
  30. ProductTile string
  31. RiskLevel string
  32. Type string
  33. TypeCN string
  34. IsPermanent bool
  35. ValidDays int
  36. SaleStatus string
  37. SourceId int
  38. CreatedTime time.Time
  39. }
  40. func GetMerchantProductById(id int) (productDTO MerchantProductDTO, err error) {
  41. product, err := merchantDao.GetMerchantProductById(id)
  42. if err != nil {
  43. if errors.Is(err, gorm.ErrRecordNotFound) {
  44. logger.Error("商品信息不存在[productId:%d]", id)
  45. } else {
  46. logger.Error("获取商品信息失败[productId:%d],err:", id, err)
  47. }
  48. }
  49. productDTO = convertToDTO(product)
  50. return
  51. }
  52. func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO {
  53. return MerchantProductDTO{
  54. Id: product.ID,
  55. Title: product.Title,
  56. CoverSrc: product.CoverSrc,
  57. Description: product.Description,
  58. ProductTile: product.Title,
  59. Price: product.Price,
  60. RiskLevel: "",
  61. Type: string(product.Type),
  62. TypeCN: typeTransfer[product.Type],
  63. IsPermanent: product.IsPermanent,
  64. ValidDays: product.ValidDays,
  65. SourceId: product.SourceID,
  66. CreatedTime: product.CreatedTime,
  67. }
  68. }
  69. func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) {
  70. productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType)
  71. if err != nil {
  72. if errors.Is(err, gorm.ErrRecordNotFound) {
  73. logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType)
  74. } else {
  75. logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err)
  76. }
  77. return
  78. }
  79. product = convertToDTO(productDao)
  80. return
  81. }
  82. func GetProductListBySourceIds(ids []int, productType string) (productDTOS []MerchantProductDTO, err error) {
  83. productList, err := merchantDao.GetProductListBySourceIds(ids, typeKindTransfer[productType])
  84. if err != nil {
  85. logger.Error("获取商品列表失败[sourceIds:%v,type:%v],err:%v", ids, productType, err)
  86. return
  87. }
  88. for _, product := range productList {
  89. productDTO := convertToDTO(product)
  90. productDTOS = append(productDTOS, productDTO)
  91. }
  92. return
  93. }