merchant_product.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if len(productType) > 0 {
  57. if len(productType) == 1 {
  58. err = db.Select(detailColumns).Where("source_id =? and type = ? and deleted =?", sourceId, productType[0], false).First(&product).Error
  59. } else {
  60. err = db.Select(detailColumns).Where("source_id =? and type in (?) and deleted =?", sourceId, productType, false).First(&product).Error
  61. }
  62. } else {
  63. err = db.Select(detailColumns).Where("source_id =? and deleted =?", sourceId, false).First(&product).Error
  64. }
  65. return
  66. }
  67. func GetProductListBySourceIds(ids []int, productType ...MerchantProductType) (productList []MerchantProduct, err error) {
  68. db := models.Main()
  69. if len(productType) > 0 {
  70. if len(productType) == 1 {
  71. err = db.Select(sourceIdColumn).Where("source_id in ? and type = ? and deleted =? ", ids, productType[0], false).Find(&productList).Error
  72. } else {
  73. err = db.Select(sourceIdColumn).Where("source_id in ? and type in (?) and deleted =? ", ids, productType, false).Find(&productList).Error
  74. }
  75. } else {
  76. err = db.Select(sourceIdColumn).Where("source_id in ? and deleted =? ", ids, productType, false).Find(&productList).Error
  77. }
  78. return
  79. }