123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package merchant
- import (
- "errors"
- "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,sale_status,cover_src,cover_url,description,source_id,created_time,updated_time"
- sourceIdColumn = "id,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 int `gorm:"column:cover_src;type:int(11);comment:封面图片资源库id"`
- CoverUrl string `gorm:"column:cover_url;type:varchar(255);comment:封面图片url"`
- Description string `gorm:"column:description;type:varchar(255);comment:描述"`
- Price decimal.Decimal `gorm:"column:price;type:decimal(10,2);comment:价格"`
- RiskLevel string `gorm:"-"`
- 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()
- if len(productType) > 0 {
- if len(productType) == 1 {
- err = db.Select(detailColumns).Where("source_id =? and type = ? and deleted =?", sourceId, productType[0], false).First(&product).Error
- } else {
- err = db.Select(detailColumns).Where("source_id =? and type in (?) and deleted =?", sourceId, productType, false).First(&product).Error
- }
- } else {
- err = db.Select(detailColumns).Where("source_id =? and deleted =?", sourceId, false).First(&product).Error
- }
- return
- }
- func GetProductListBySourceIds(ids []int, detail bool, productType ...MerchantProductType) (productList []MerchantProduct, err error) {
- db := models.Main()
- var columns string
- if productType == nil {
- err = errors.New("productType参数不能为空")
- return
- }
- if detail {
- columns = detailColumns
- } else {
- columns = sourceIdColumn
- }
- if len(productType) > 0 {
- if len(productType) == 1 {
- err = db.Select(columns).Where("source_id in ? and type = ? and deleted =? order by created_time desc", ids, productType[0], false).Find(&productList).Error
- } else {
- err = db.Select(columns).Where("source_id in ? and type in (?) and deleted =? by created_time desc", ids, productType, false).Find(&productList).Error
- }
- } else {
- err = db.Select(columns).Where("source_id in ? and deleted =? by created_time desc", ids, productType, false).Find(&productList).Error
- }
- return
- }
- func GetTotalPageCountByProductType(productType MerchantProductType) (total int64, latestId int64) {
- db := models.Main()
- _ = db.Model(&MerchantProduct{}).Select("count(*)").Where("type=? and deleted =?", productType, false).Scan(&total).Error
- _ = db.Model(&MerchantProduct{}).Select("max(id)").Where("type=? and deleted =?", productType, false).Scan(&latestId).Error
- return
- }
- func GetProductListByProductType(productType MerchantProductType, id int64, offset int, limit int) (list []MerchantProduct, err error) {
- db := models.Main()
- if productType == "" {
- err = errors.New("productType参数不能为空")
- return
- }
- err = db.Select(detailColumns).Where("id <= ? and type = ? and deleted =? order by created_time desc limit ?,? ", id, productType, false, offset, limit).Find(&list).Error
- return
- }
|