merchant_card.go 6.7 KB

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