merchant_product.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package models
  2. import (
  3. "errors"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type SaleStatus string
  8. type MerchantProductType string
  9. const (
  10. OnSale SaleStatus = "on_sale" //上架
  11. OffSale SaleStatus = "off_sale" //下架
  12. ProductPackage MerchantProductType = "package"
  13. ProductReport MerchantProductType = "report"
  14. ProductVideo MerchantProductType = "video"
  15. ProductAudio MerchantProductType = "audio"
  16. )
  17. type MerchantProduct struct {
  18. Id int `description:"column:id;primary_key;autoIncrement;comment:主键"`
  19. SourceId int `description:"column:source_id;type:int(11);comment:单品或者套餐对应的主键"`
  20. Title string `description:"column:title;type:varchar(255);comment:标题"`
  21. CoverSrc int `description:"column:cover_src;type:int(11);comment:封面图片资源库id"`
  22. CoverUrl string `description:"column:cover_url;type:varchar(255);comment:封面图片url"`
  23. Description string `description:"column:description;type:varchar(255);comment:描述"`
  24. Price string `description:"column:price;type:decimal(10,2);comment:价格"`
  25. Type MerchantProductType `description:"column:type;type:enum('report','video','audio','package');not null;comment:类型"`
  26. IsPermanent bool `description:"column:is_permanent;type:int(1);not null;default:0;comment:是否永久"`
  27. ValidDays int `description:"column:valid_days;type:int(11);comment:有效期天数"`
  28. Creator string `description:"创建人"`
  29. SaleStatus SaleStatus `description:"column:sale_status;type:enum('on_sale','off_sale');not null;default:'on_sale';comment:上架/下架状态"`
  30. Deleted int `description:"column:deleted;type:tinyint(1);not null;default:0;comment:是否删除"`
  31. CreatedTime time.Time `description:"column:created_time;type:datetime;comment:创建时间"`
  32. UpdatedTime time.Time `description:"column:updated_time;type:datetime;comment:更新时间;default:CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"`
  33. }
  34. func (m *MerchantProduct) TableName() string {
  35. return "merchant_products"
  36. }
  37. func (m *MerchantProduct) Insert() (err error) {
  38. o := orm.NewOrm()
  39. _, err = o.Insert(m)
  40. return
  41. }
  42. func (m *MerchantProduct) InsertPackage() (err error) {
  43. o := orm.NewOrm()
  44. //tx := o.Begin()
  45. //merchantPackage := MERCHAT
  46. //tx.Insert()
  47. //tx.Rollback()
  48. //tx.Commit()
  49. _, err = o.Insert(m)
  50. return
  51. }
  52. func GetProductSourceIdsByProductType(productType string) (ids []int, err error) {
  53. var condition string
  54. switch productType {
  55. case "report":
  56. condition = "and type ='report'"
  57. case "media":
  58. condition = "and type in ('audio','video')"
  59. default:
  60. err = errors.New("不支持的产品类型查询")
  61. return
  62. }
  63. o := orm.NewOrm()
  64. _, err = o.Raw("select source_id from merchant_products where deleted = 0 " + condition).QueryRows(&ids)
  65. return
  66. }
  67. func GetProductByProductType(sourceId int, productType MerchantProductType) (product MerchantProduct, err error) {
  68. o := orm.NewOrm()
  69. err = o.Raw("select * from merchant_products where source_id =? and type=? and deleted = 0 ", sourceId, productType).QueryRow(&product)
  70. return
  71. }
  72. func (mp *MerchantProduct) AddProduct() (err error) {
  73. o := orm.NewOrm()
  74. _, err = o.Insert(mp)
  75. return
  76. }
  77. func (mp *MerchantProduct) EditProduct() (err error) {
  78. o := orm.NewOrm()
  79. mp.UpdatedTime = time.Now()
  80. if mp.Type == ProductPackage {
  81. _, err = o.Update(mp, "updated_time", "price", "cover_src", "description", "valid_days", "title")
  82. } else {
  83. _, err = o.Update(mp, "updated_time", "price")
  84. }
  85. return
  86. }
  87. func (mp *MerchantProduct) UpdateProductSaleStatus() (err error) {
  88. o := orm.NewOrm()
  89. var dbProduct MerchantProduct
  90. err = o.Raw("select * from merchant_products where id =? and deleted=0", mp.Id).QueryRow(&dbProduct)
  91. if err != nil {
  92. if err.Error() == "<QuerySeter> no row found" {
  93. return errors.New("产品不存在")
  94. }
  95. return
  96. }
  97. _, err = o.Update(mp, "sale_status", "updated_time")
  98. return
  99. }
  100. func (mp *MerchantProduct) Delete() (err error) {
  101. o := orm.NewOrm()
  102. _, err = o.Update(mp, "deleted", "updated_time")
  103. return
  104. }
  105. func GetProductByCondition(condition string, sortCondition string, startSize int, pageSize int) (list []*MerchantProduct, err error) {
  106. o := orm.NewOrm()
  107. sql := `select * from merchant_products where deleted=0`
  108. if condition != "" {
  109. sql += condition
  110. }
  111. if sortCondition != "" {
  112. sql += sortCondition
  113. }
  114. sql += ` LIMIT ?,?`
  115. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  116. return
  117. }
  118. func GetProductCountByCondition(condition string) (total int, err error) {
  119. o := orm.NewOrm()
  120. err = o.Raw("select count(*) from merchant_products where deleted=0" + condition).QueryRow(&total)
  121. return
  122. }