merchant_product.go 4.0 KB

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