merchant_product.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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,cover_url,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 int `gorm:"column:cover_src;type:int(11);comment:封面图片资源库id"`
  29. CoverUrl string `gorm:"column:cover_url;type:varchar(255);comment:封面图片url"`
  30. Description string `gorm:"column:description;type:varchar(255);comment:描述"`
  31. Price decimal.Decimal `gorm:"column:price;type:decimal(10,2);comment:价格"`
  32. RiskLevel string `gorm:"-"`
  33. Type MerchantProductType `gorm:"column:type;type:enum('report','video','audio','package');not null;comment:类型"`
  34. IsPermanent bool `gorm:"column:is_permanent;type:int(1);not null;default:0;comment:是否永久"`
  35. ValidDays int `gorm:"column:valid_days;type:int(11);comment:有效期天数"`
  36. SaleStatus SaleStatus `gorm:"column:sale_status;type:enum('on_sale','off_sale');not null;default:'on_sale';comment:上架/下架状态"`
  37. Deleted bool `gorm:"column:deleted;type:tinyint(1);not null;default:0;comment:是否删除"`
  38. CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"`
  39. UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;comment:更新时间;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"`
  40. }
  41. // TableName 指定表名
  42. func (MerchantProduct) TableName() string {
  43. return "merchant_products"
  44. }
  45. func (m *MerchantProduct) BeforeCreate(_ *gorm.DB) (err error) {
  46. m.CreatedTime = time.Now()
  47. m.Deleted = false
  48. m.SaleStatus = OffSale
  49. return
  50. }
  51. func GetMerchantProductById(id int) (product MerchantProduct, err error) {
  52. db := models.Main()
  53. err = db.Select(detailColumns).Where("id = ? and deleted =?", id, false).First(&product).Error
  54. return
  55. }
  56. func GetMerchantProductBySourceId(sourceId int, productType ...MerchantProductType) (product MerchantProduct, err error) {
  57. db := models.Main()
  58. if len(productType) > 0 {
  59. if len(productType) == 1 {
  60. err = db.Select(detailColumns).Where("source_id =? and type = ? and deleted =?", sourceId, productType[0], false).First(&product).Error
  61. } else {
  62. err = db.Select(detailColumns).Where("source_id =? and type in (?) and deleted =?", sourceId, productType, false).First(&product).Error
  63. }
  64. } else {
  65. err = db.Select(detailColumns).Where("source_id =? and deleted =?", sourceId, false).First(&product).Error
  66. }
  67. return
  68. }
  69. func GetProductListBySourceIds(ids []int, detail bool, productType ...MerchantProductType) (productList []MerchantProduct, err error) {
  70. db := models.Main()
  71. var columns string
  72. if productType == nil {
  73. err = errors.New("productType参数不能为空")
  74. return
  75. }
  76. if detail {
  77. columns = detailColumns
  78. } else {
  79. columns = sourceIdColumn
  80. }
  81. if len(productType) > 0 {
  82. if len(productType) == 1 {
  83. err = db.Select(columns).Where("source_id in ? and type = ? and deleted =? order by created_time desc", ids, productType[0], false).Find(&productList).Error
  84. } else {
  85. err = db.Select(columns).Where("source_id in ? and type in (?) and deleted =? by created_time desc", ids, productType, false).Find(&productList).Error
  86. }
  87. } else {
  88. err = db.Select(columns).Where("source_id in ? and deleted =? by created_time desc", ids, productType, false).Find(&productList).Error
  89. }
  90. return
  91. }
  92. func GetTotalPageCountByProductType(productType MerchantProductType) (total int64, latestId int64) {
  93. db := models.Main()
  94. _ = db.Model(&MerchantProduct{}).Select("count(*)").Where("type=? and deleted =?", productType, false).Scan(&total).Error
  95. _ = db.Model(&MerchantProduct{}).Select("max(id)").Where("type=? and deleted =?", productType, false).Scan(&latestId).Error
  96. return
  97. }
  98. func GetProductListByProductType(productType MerchantProductType, id int64, offset int, limit int) (list []MerchantProduct, err error) {
  99. db := models.Main()
  100. if productType == "" {
  101. err = errors.New("productType参数不能为空")
  102. return
  103. }
  104. err = db.Select(detailColumns).Where("id <= ? and type = ? and deleted =? order by created_time desc limit ?,? ", id, productType, false, offset, limit).Find(&list).Error
  105. return
  106. }