merchant_product.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package merchant
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "github.com/shopspring/decimal"
  5. "gorm.io/gorm"
  6. "time"
  7. )
  8. type SaleStatus string
  9. type MerchantProductType string
  10. const (
  11. detailColumns = "id,title,price,is_permanent,valid_days,type,risk_level"
  12. sourceIdColumn = "id,risk_level,source_id"
  13. )
  14. const (
  15. OnSale SaleStatus = "on_sale" //上架
  16. OffSale SaleStatus = "off_sale" //下架
  17. Package MerchantProductType = "package"
  18. Report MerchantProductType = "report"
  19. Video MerchantProductType = "video"
  20. Audio MerchantProductType = "audio"
  21. )
  22. // MerchantProduct 商户产品信息结构体
  23. type MerchantProduct struct {
  24. ID int `gorm:"column:id;primary_key;autoIncrement;comment:主键"`
  25. SourceID int `gorm:"column:source_id;type:int(11);comment:单品或者套餐对应的主键"`
  26. Title string `gorm:"column:title;type:varchar(255);comment:标题"`
  27. CoverSrc string `gorm:"column:cover_src;type:varchar(255);comment:封面图片"`
  28. Description string `gorm:"column:description;type:varchar(255);comment:描述"`
  29. Price decimal.Decimal `gorm:"column:price;type:decimal(10,2);comment:价格"`
  30. RiskLevel string `gorm:"column:risk_level;type:varchar(255);not null;comment:风险等级"`
  31. Type MerchantProductType `gorm:"column:type;type:enum('report','video','audio','package');not null;comment:类型"`
  32. IsPermanent bool `gorm:"column:is_permanent;type:int(1);not null;default:0;comment:是否永久"`
  33. ValidDays int `gorm:"column:valid_days;type:int(11);comment:有效期天数"`
  34. SaleStatus SaleStatus `gorm:"column:sale_status;type:enum('on_sale','off_sale');not null;default:'on_sale';comment:上架/下架状态"`
  35. Deleted bool `gorm:"column:deleted;type:tinyint(1);not null;default:0;comment:是否删除"`
  36. CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"`
  37. UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;comment:更新时间;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"`
  38. }
  39. // TableName 指定表名
  40. func (MerchantProduct) TableName() string {
  41. return "merchant_products"
  42. }
  43. func (m *MerchantProduct) BeforeCreate(_ *gorm.DB) (err error) {
  44. m.CreatedTime = time.Now()
  45. m.Deleted = false
  46. m.SaleStatus = OffSale
  47. return
  48. }
  49. func GetMerchantProductById(id int) (product MerchantProduct, err error) {
  50. db := models.Main()
  51. err = db.Select(detailColumns).Where("id = ? and deleted =?", id, false).First(&product).Error
  52. return
  53. }
  54. func GetMerchantProductBySourceId(sourceId int, productType MerchantProductType) (product MerchantProduct, err error) {
  55. db := models.Main()
  56. err = db.Select(detailColumns).Where("source_id =? and type = ? and deleted =?", sourceId, productType, false).First(&product).Error
  57. return
  58. }
  59. func GetProductListBySourceIds(ids []int, productType MerchantProductType) (productList []MerchantProduct, err error) {
  60. db := models.Main()
  61. err = db.Select(sourceIdColumn).Where("source_id in ? and type = ? and deleted =? ", ids, productType, false).Find(&productList).Error
  62. return
  63. }