merchant_product.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. )
  17. type MerchantProductDTO struct {
  18. Id int
  19. Title string
  20. CoverSrc string
  21. Description string
  22. Price decimal.Decimal
  23. ProductTile string
  24. RiskLevel string
  25. Type string
  26. TypeCN string
  27. IsPermanent bool
  28. ValidDays int
  29. SaleStatus string
  30. SourceId int
  31. }
  32. func GetMerchantProductById(id int) (productDTO MerchantProductDTO, err error) {
  33. product, err := merchantDao.GetMerchantProductById(id)
  34. if err != nil {
  35. if errors.Is(err, gorm.ErrRecordNotFound) {
  36. logger.Error("商品信息不存在[productId:%d]", id)
  37. } else {
  38. logger.Error("获取商品信息失败[productId:%d],err:", id, err)
  39. }
  40. }
  41. productDTO = convertToDTO(product)
  42. return
  43. }
  44. func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO {
  45. return MerchantProductDTO{
  46. Id: product.ID,
  47. Title: product.Title,
  48. CoverSrc: product.CoverSrc,
  49. Description: product.Description,
  50. ProductTile: product.Title,
  51. Price: product.Price,
  52. RiskLevel: "",
  53. Type: string(product.Type),
  54. TypeCN: typeTransfer[product.Type],
  55. IsPermanent: product.IsPermanent,
  56. ValidDays: product.ValidDays,
  57. SourceId: product.SourceID,
  58. }
  59. }
  60. func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) {
  61. productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType)
  62. if err != nil {
  63. if errors.Is(err, gorm.ErrRecordNotFound) {
  64. logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType)
  65. } else {
  66. logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err)
  67. }
  68. return
  69. }
  70. product = convertToDTO(productDao)
  71. return
  72. }