123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package models
- import (
- "errors"
- "github.com/beego/beego/v2/client/orm"
- "time"
- )
- type SaleStatus string
- type MerchantProductType string
- const (
- OnSale SaleStatus = "on_sale" //上架
- OffSale SaleStatus = "off_sale" //下架
- ProductPackage MerchantProductType = "package"
- ProductReport MerchantProductType = "report"
- ProductVideo MerchantProductType = "video"
- ProductAudio MerchantProductType = "audio"
- )
- type MerchantProduct struct {
- Id int `description:"column:id;primary_key;autoIncrement;comment:主键"`
- SourceId int `description:"column:source_id;type:int(11);comment:单品或者套餐对应的主键"`
- Title string `description:"column:title;type:varchar(255);comment:标题"`
- CoverSrc int `description:"column:cover_src;type:int(11);comment:封面图片资源库id"`
- CoverUrl string `description:"column:cover_url;type:varchar(255);comment:封面图片url"`
- Description string `description:"column:description;type:varchar(255);comment:描述"`
- Price string `description:"column:price;type:decimal(10,2);comment:价格"`
- Type MerchantProductType `description:"column:type;type:enum('report','video','audio','package');not null;comment:类型"`
- IsPermanent bool `description:"column:is_permanent;type:int(1);not null;default:0;comment:是否永久"`
- ValidDays int `description:"column:valid_days;type:int(11);comment:有效期天数"`
- Creator string `description:"创建人"`
- SaleStatus SaleStatus `description:"column:sale_status;type:enum('on_sale','off_sale');not null;default:'on_sale';comment:上架/下架状态"`
- Deleted int `description:"column:deleted;type:tinyint(1);not null;default:0;comment:是否删除"`
- CreatedTime time.Time `description:"column:created_time;type:datetime;comment:创建时间"`
- UpdatedTime time.Time `description:"column:updated_time;type:datetime;comment:更新时间;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"`
- }
- func (m *MerchantProduct) TableName() string {
- return "merchant_products"
- }
- func (m *MerchantProduct) Insert() (err error) {
- o := orm.NewOrm()
- _, err = o.Insert(m)
- return
- }
- func (m *MerchantProduct) InsertPackage() (err error) {
- o := orm.NewOrm()
- //tx := o.Begin()
- //merchantPackage := MERCHAT
- //tx.Insert()
- //tx.Rollback()
- //tx.Commit()
- _, err = o.Insert(m)
- return
- }
- func GetProductSourceIdsByProductType(productType string) (ids []int, err error) {
- var condition string
- switch productType {
- case "report":
- condition = "and type ='report'"
- case "media":
- condition = "and type in ('audio','video')"
- default:
- err = errors.New("不支持的产品类型查询")
- return
- }
- o := orm.NewOrm()
- _, err = o.Raw("select source_id from merchant_products where deleted = 0 " + condition).QueryRows(&ids)
- return
- }
- func GetProductByProductType(sourceId int, productType MerchantProductType) (product MerchantProduct, err error) {
- o := orm.NewOrm()
- err = o.Raw("select * from merchant_products where source_id =? and type=? and deleted = 0 ", sourceId, productType).QueryRow(&product)
- return
- }
- func (mp *MerchantProduct) AddProduct() (err error) {
- o := orm.NewOrm()
- _, err = o.Insert(mp)
- return
- }
- func (mp *MerchantProduct) EditProduct() (err error) {
- o := orm.NewOrm()
- mp.UpdatedTime = time.Now()
- if mp.Type == ProductPackage {
- _, err = o.Update(mp, "updated_time", "price", "cover_src", "description", "valid_days", "title")
- } else {
- _, err = o.Update(mp, "updated_time", "price")
- }
- return
- }
- func (mp *MerchantProduct) UpdateProductSaleStatus() (err error) {
- o := orm.NewOrm()
- var dbProduct MerchantProduct
- err = o.Raw("select * from merchant_products where id =? and deleted=0", mp.Id).QueryRow(&dbProduct)
- if err != nil {
- if err.Error() == "<QuerySeter> no row found" {
- return errors.New("产品不存在")
- }
- return
- }
- _, err = o.Update(mp, "sale_status", "updated_time")
- return
- }
- func (mp *MerchantProduct) Delete() (err error) {
- o := orm.NewOrm()
- _, err = o.Update(mp, "deleted", "updated_time")
- return
- }
- func GetProductByCondition(condition string, sortCondition string, startSize int, pageSize int) (list []*MerchantProduct, err error) {
- o := orm.NewOrm()
- sql := `select * from merchant_products where deleted=0`
- if condition != "" {
- sql += condition
- }
- if sortCondition != "" {
- sql += sortCondition
- }
- sql += ` LIMIT ?,?`
- _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
- return
- }
- func GetProductCountByCondition(condition string) (total int, err error) {
- o := orm.NewOrm()
- err = o.Raw("select count(*) from merchant_products where deleted=0" + condition).QueryRow(&total)
- return
- }
|