merchant_product.go 1.9 KB

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