package merchant import ( "errors" logger "eta/eta_mini_ht_api/common/component/log" merchantDao "eta/eta_mini_ht_api/models/merchant" "github.com/shopspring/decimal" "gorm.io/gorm" ) var ( typeTransfer = map[merchantDao.MerchantProductType]string{ merchantDao.Report: "单篇报告", merchantDao.Video: "单个视频", merchantDao.Audio: "单个音频", merchantDao.Package: "套餐", } ) type MerchantProductDTO struct { Id int Title string CoverSrc string Description string Price decimal.Decimal RiskLevel string Type string IsPermanent bool ValidDays int } func GetMerchantProductById(id int) (product MerchantProductDTO, err error) { productDao, err := merchantDao.GetMerchantProductById(id) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("商品信息不存在[productId:%d]", id) } else { logger.Error("获取商品信息失败[productId:%d],err:", id, err) } } product = convertToDTO(productDao) return } func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO { return MerchantProductDTO{ Id: product.ID, Title: product.Title, CoverSrc: product.CoverSrc, Description: product.Description, Price: product.Price, RiskLevel: product.RiskLevel, Type: typeTransfer[product.Type], IsPermanent: product.IsPermanent, ValidDays: product.ValidDays, } } func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) { productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType) } else { logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err) } return } product = convertToDTO(productDao) return }