merchant_card.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "duoduo/tools"
  5. "time"
  6. )
  7. type MerchantCard struct {
  8. ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
  9. MerchantCardName string `gorm:"column:merchant_card_name;type:varchar(255)" json:"merchant_card_name"` // 商户卡名称
  10. MerchantOpenID string `gorm:"column:merchant_open_id;type:varchar(255);NOT NULL" json:"merchant_open_id"` // openid
  11. CardProjectData string `gorm:"column:card_project_data;type:json" json:"card_project_data"` // 项目
  12. CardPrice string `gorm:"column:card_price;type:decimal(10,2)" json:"card_price"` // 单价
  13. ActivityEndTime int64 `gorm:"column:activity_end_time;type:bigint(20)" json:"activity_end_time"` // 活动结束时间
  14. ActivityStartTime int64 `gorm:"column:activity_start_time;type:bigint(20)" json:"activity_start_time"` // 活动开始时间
  15. MerchantCardTime int64 `gorm:"column:merchant_card_time;type:bigint(20)" json:"merchant_card_time"` // 商户卡有效期
  16. Inventory int64 `gorm:"column:inventory;type:bigint(20)" json:"inventory"` // 库存数量
  17. QuotaNum int64 `gorm:"column:quota_num;type:int(11)" json:"quota_num"` // 限购数量
  18. UseRule string `gorm:"column:use_rule;type:varchar(255)" json:"use_rule"` // 使用规则
  19. Picture string `gorm:"column:picture;type:varchar(255)" json:"picture"` // 图片
  20. RebateRate int64 `gorm:"column:rebate_rate;type:int(11)" json:"rebate_rate"` // 佣金比例
  21. CancelNumber int `gorm:"column:cancel_number;type:int(11)" json:"cancel_number"` //
  22. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  23. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  24. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  25. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  26. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  27. BackgroundImage string `gorm:"column:background_image;type:varchar(255)" json:"background_image"` //背景图
  28. W string `gorm:"column:w;type:varchar(255);default:null" json:"w"` // w
  29. H string `gorm:"column:h;type:varchar(255);default:null" json:"h"` // h
  30. X string `gorm:"column:x;type:varchar(255);default:null" json:"x"` // x
  31. Y string `gorm:"column:y;type:varchar(255);default:null" json:"y"` // y
  32. CardTotalPrice string `gorm:"column:card_total_price;type:decimal(10,2)" json:"card_total_price"` // 总价
  33. }
  34. func (m *MerchantCard) TableName() string {
  35. return "merchant_card"
  36. }
  37. func (u *MerchantCard) Create() (MerchantCard, error) {
  38. var doc MerchantCard
  39. var err error
  40. doc = *u
  41. err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
  42. if err != nil {
  43. return doc, err
  44. }
  45. return doc, nil
  46. }
  47. func (m *MerchantCard) GetMerchantCard() (MerchantCard, error) {
  48. var doc MerchantCard
  49. table := orm.ShMysql.Table(m.TableName())
  50. table = table.Where("id = ? ", m.ID)
  51. if err := table.Select("*").First(&doc).Error; err != nil {
  52. return doc, err
  53. }
  54. return doc, nil
  55. }
  56. // list 接口使用
  57. func (m *MerchantCard) GetOpenIdList(pageSize int, pageIndex int, listType int) ([]MerchantCard, int, error) {
  58. var doc []MerchantCard
  59. timeNow := time.Now().Unix()
  60. table := orm.ShMysql.Table(m.TableName())
  61. if listType == 1 { //待开始
  62. table = table.Where("activity_start_time > ?", timeNow)
  63. } else if listType == 2 { //进行中
  64. table = table.Where("activity_start_time < ? and activity_end_time > ?", timeNow, timeNow)
  65. } else if listType == 3 { //已结束
  66. table = table.Where("activity_end_time < ?", timeNow)
  67. }
  68. //if m.ActivityStartTime != 0
  69. table = table.Where("merchant_open_id = ? ", m.MerchantOpenID)
  70. var count int
  71. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  72. return nil, 0, err
  73. }
  74. table.Count(&count)
  75. return doc, count, nil
  76. }
  77. // list 接口使用
  78. func (m *MerchantCard) GetMerchantByOpenId() ([]MerchantCard, error) {
  79. var doc []MerchantCard
  80. table := orm.ShMysql.Table(m.TableName())
  81. table = table.Where("merchant_open_id = ? ", m.MerchantOpenID)
  82. err := table.Select("id,merchant_card_name,merchant_card_time,rebate_rate").Find(&doc).Error
  83. if err != nil {
  84. return nil, err
  85. }
  86. return doc, nil
  87. }
  88. // list 接口使用
  89. func (m *MerchantCard) ClientGetOpenIdList(pageSize int, pageIndex int, listType int) ([]MerchantCard, int, error) {
  90. var doc []MerchantCard
  91. timeNow := time.Now().Unix()
  92. table := orm.ShMysql.Table(m.TableName())
  93. if listType == 1 { //待开始
  94. table = table.Where("activity_start_time > ?", timeNow)
  95. } else if listType == 2 { //进行中
  96. table = table.Where("activity_start_time < ? and activity_end_time > ?", timeNow, timeNow)
  97. } else if listType == 3 { //已结束
  98. table = table.Where("activity_end_time < ?", timeNow)
  99. }
  100. //if m.ActivityStartTime != 0
  101. var count int
  102. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  103. return nil, 0, err
  104. }
  105. table.Count(&count)
  106. return doc, count, nil
  107. }
  108. func (m *MerchantCard) UpdateMerchantCard() error {
  109. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  110. map[string]interface{}{
  111. "project_name": m.ActivityEndTime,
  112. "activity_start_time": m.ActivityStartTime,
  113. "inventory": m.Inventory,
  114. "quota_num": m.QuotaNum,
  115. "use_rule": m.UseRule,
  116. "picture": m.Picture,
  117. "updated_at": tools.GetCurrntTimeStr()}).Error; err != nil {
  118. return err
  119. }
  120. return nil
  121. }
  122. func (m *MerchantCard) UpdateMerchantWHXY() error {
  123. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  124. map[string]interface{}{
  125. "w": m.W,
  126. "h": m.H,
  127. "x": m.X,
  128. "y": m.Y,
  129. "updated_at": tools.GetCurrntTimeStr()}).Error; err != nil {
  130. return err
  131. }
  132. return nil
  133. }