merchant.active.group.buy.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "errors"
  5. "github.com/shopspring/decimal"
  6. "time"
  7. )
  8. type MerchantActiveGroupBuy struct {
  9. ID int64 `gorm:"column:id;type:bigint(20);primary_key" json:"id"`
  10. ActiveConfigID int64 `gorm:"column:active_config_id;type:bigint(20)" json:"active_config_id"` // 关联活动
  11. GroupBuyName string `gorm:"column:group_buy_name;type:varchar(255)" json:"group_buy_name"` // 拼团名
  12. GroupBuyMode int `gorm:"column:group_buy_mode;type:int(11)" json:"group_buy_mode"` // 拼团模式 1-真实拼团 2-虚拟拼团
  13. MerchantOpenID string `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"`
  14. OriginalPrice decimal.Decimal `gorm:"column:original_price;type:decimal(10,2)" json:"original_price"` // 原价
  15. GroupBuyOneNum int `gorm:"column:group_buy_one_num;type:int(11)" json:"group_buy_one_num"` // 人数
  16. GroupBuyOnePrice decimal.Decimal `gorm:"column:group_buy_one_price;type:decimal(10,2)" json:"group_buy_one_price"` // 价格
  17. GroupBuyTwoNum int `gorm:"column:group_buy_two_num;type:int(11)" json:"group_buy_two_num"` // 人数
  18. GroupBuyTwoPrice decimal.Decimal `gorm:"column:group_buy_two_price;type:decimal(10,2)" json:"group_buy_two_price"` // 价格
  19. GroupBuyThreeNum int `gorm:"column:group_buy_three_num;type:int(11)" json:"group_buy_three_num"`
  20. GroupBuyThreePrice decimal.Decimal `gorm:"column:group_buy_three_price;type:decimal(10,2)" json:"group_buy_three_price"`
  21. GroupBuyFourNum int `gorm:"column:group_buy_four_num;type:int(11)" json:"group_buy_four_num"`
  22. GroupBuyFourPrice decimal.Decimal `gorm:"column:group_buy_four_price;type:decimal(10,2)" json:"group_buy_four_price"`
  23. GroupBuyUrl string `gorm:"column:group_buy_url;type:varchar(255)" json:"group_buy_url"` // 图片
  24. GroupBuyBigUrl string `gorm:"column:group_buy_big_url;type:varchar(255)" json:"group_buy_big_url"` // 大图
  25. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  26. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  27. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  28. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  29. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  30. RebateRate int `gorm:"column:rebate_rate;type:int(11)" json:"rebate_rate"` // 佣金比例
  31. }
  32. func (m *MerchantActiveGroupBuy) TableName() string {
  33. return "merchant_active_group_buy"
  34. }
  35. func (u *MerchantActiveGroupBuy) Create() (MerchantActiveGroupBuy, error) {
  36. var doc MerchantActiveGroupBuy
  37. var err error
  38. doc = *u
  39. err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
  40. if err != nil {
  41. return doc, err
  42. }
  43. return doc, nil
  44. }
  45. func (u *MerchantActiveGroupBuy) UpdateConfigId(groupBuyId int64) error {
  46. res := orm.ShMysql.Table(u.TableName()).Where("id = ? and active_config_id = 0", groupBuyId).Updates(
  47. map[string]interface{}{
  48. "active_config_id": u.ActiveConfigID,
  49. "updated_at": time.Now(),
  50. })
  51. if res.Error != nil {
  52. return res.Error
  53. }
  54. if res.RowsAffected <= 0 {
  55. return errors.New("未更新拼团数据")
  56. }
  57. return nil
  58. }
  59. // 中奖商品列表
  60. func (m *MerchantActiveGroupBuy) GetGroupBuyList() ([]MerchantActiveGroupBuy, int, error) {
  61. var doc []MerchantActiveGroupBuy
  62. table := orm.ShMysql.Table(m.TableName())
  63. table = table.Where("active_config_id = ? ", m.ActiveConfigID)
  64. var count int
  65. if err := table.Select("*").Order("id desc").Find(&doc).Error; err != nil {
  66. return nil, 0, err
  67. }
  68. table.Count(&count)
  69. return doc, count, nil
  70. }
  71. func (m *MerchantActiveGroupBuy) GetGroupBuyListByOpenId(pageSize int, pageIndex int) ([]MerchantActiveGroupBuy, int, error) {
  72. var doc []MerchantActiveGroupBuy
  73. table := orm.ShMysql.Table(m.TableName())
  74. table = table.Where("merchant_open_id = ? and active_config_id = 0", m.MerchantOpenID)
  75. var count int
  76. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  77. return nil, 0, err
  78. }
  79. table.Count(&count)
  80. return doc, count, nil
  81. }
  82. func (m *MerchantActiveGroupBuy) GetMerchantActiveGroupBuyById() (MerchantActiveGroupBuy, error) {
  83. var doc MerchantActiveGroupBuy
  84. table := orm.ShMysql.Table(m.TableName())
  85. table = table.Where("id = ? ", m.ID)
  86. if err := table.Select("*").Order("id desc").First(&doc).Error; err != nil {
  87. return doc, err
  88. }
  89. return doc, nil
  90. }