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