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" "time" ) var ( typeTransfer = map[merchantDao.MerchantProductType]string{ merchantDao.Report: "单篇报告", merchantDao.Video: "单个视频", merchantDao.Audio: "单个音频", merchantDao.Package: "套餐", } typeKindTransfer = map[string]merchantDao.MerchantProductType{ "report": merchantDao.Report, "video": merchantDao.Video, "audio": merchantDao.Audio, "package": merchantDao.Package, } ) type MerchantProductDTO struct { Id int Title string CoverSrc string Description string Price decimal.Decimal ProductTile string RiskLevel string Type string TypeCN string IsPermanent bool ValidDays int SaleStatus string SourceId int CreatedTime time.Time } func GetMerchantProductById(id int) (productDTO MerchantProductDTO, err error) { product, 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) } } productDTO = convertToDTO(product) return } func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO { return MerchantProductDTO{ Id: product.ID, Title: product.Title, CoverSrc: product.CoverSrc, Description: product.Description, ProductTile: product.Title, Price: product.Price, RiskLevel: "", Type: string(product.Type), TypeCN: typeTransfer[product.Type], IsPermanent: product.IsPermanent, ValidDays: product.ValidDays, SourceId: product.SourceID, CreatedTime: product.CreatedTime, } } 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 } func GetProductListBySourceIds(ids []int, productType string) (productDTOS []MerchantProductDTO, err error) { productList, err := merchantDao.GetProductListBySourceIds(ids, typeKindTransfer[productType]) if err != nil { logger.Error("获取商品列表失败[sourceIds:%v,type:%v],err:%v", ids, productType, err) return } for _, product := range productList { productDTO := convertToDTO(product) productDTOS = append(productDTOS, productDTO) } return }