merchant_product.go 1.9 KB

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