package merchant import ( "eta/eta_mini_ht_api/models" "github.com/shopspring/decimal" "gorm.io/gorm" "time" ) type SaleStatus string type MerchantProductType string const ( detailColumns = "id,title,price,is_permanent,valid_days,type,risk_level" sourceIdColumn = "id,risk_level,source_id" ) const ( OnSale SaleStatus = "on_sale" //上架 OffSale SaleStatus = "off_sale" //下架 Package MerchantProductType = "package" Report MerchantProductType = "report" Video MerchantProductType = "video" Audio MerchantProductType = "audio" ) // MerchantProduct 商户产品信息结构体 type MerchantProduct struct { ID int `gorm:"column:id;primary_key;autoIncrement;comment:主键"` SourceID int `gorm:"column:source_id;type:int(11);comment:单品或者套餐对应的主键"` Title string `gorm:"column:title;type:varchar(255);comment:标题"` CoverSrc string `gorm:"column:cover_src;type:varchar(255);comment:封面图片"` Description string `gorm:"column:description;type:varchar(255);comment:描述"` Price decimal.Decimal `gorm:"column:price;type:decimal(10,2);comment:价格"` RiskLevel string `gorm:"column:risk_level;type:varchar(255);not null;comment:风险等级"` Type MerchantProductType `gorm:"column:type;type:enum('report','video','audio','package');not null;comment:类型"` IsPermanent bool `gorm:"column:is_permanent;type:int(1);not null;default:0;comment:是否永久"` ValidDays int `gorm:"column:valid_days;type:int(11);comment:有效期天数"` SaleStatus SaleStatus `gorm:"column:sale_status;type:enum('on_sale','off_sale');not null;default:'on_sale';comment:上架/下架状态"` Deleted bool `gorm:"column:deleted;type:tinyint(1);not null;default:0;comment:是否删除"` CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"` UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;comment:更新时间;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"` } // TableName 指定表名 func (MerchantProduct) TableName() string { return "merchant_products" } func (m *MerchantProduct) BeforeCreate(_ *gorm.DB) (err error) { m.CreatedTime = time.Now() m.Deleted = false m.SaleStatus = OffSale return } func GetMerchantProductById(id int) (product MerchantProduct, err error) { db := models.Main() err = db.Select(detailColumns).Where("id = ? and deleted =?", id, false).First(&product).Error return } func GetMerchantProductBySourceId(sourceId int, productType MerchantProductType) (product MerchantProduct, err error) { db := models.Main() err = db.Select(detailColumns).Where("source_id =? and type = ? and deleted =?", sourceId, productType, false).First(&product).Error return } func GetProductListBySourceIds(ids []int, productType MerchantProductType) (productList []MerchantProduct, err error) { db := models.Main() err = db.Select(sourceIdColumn).Where("source_id in ? and type = ? and deleted =? ", ids, productType, false).Find(&productList).Error return }