merchant_card.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. }
  33. func (m *MerchantCard) TableName() string {
  34. return "merchant_card"
  35. }
  36. func (u *MerchantCard) Create() (MerchantCard, error) {
  37. var doc MerchantCard
  38. var err error
  39. doc = *u
  40. err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
  41. if err != nil {
  42. return doc, err
  43. }
  44. return doc, nil
  45. }
  46. func (m *MerchantCard) GetMerchantCard() (MerchantCard, error) {
  47. var doc MerchantCard
  48. table := orm.ShMysql.Table(m.TableName())
  49. table = table.Where("id = ? ", m.ID)
  50. if err := table.Select("*").First(&doc).Error; err != nil {
  51. return doc, err
  52. }
  53. return doc, nil
  54. }
  55. // list 接口使用
  56. func (m *MerchantCard) GetOpenIdList(pageSize int, pageIndex int, listType int) ([]MerchantCard, int, error) {
  57. var doc []MerchantCard
  58. timeNow := time.Now().Unix()
  59. table := orm.ShMysql.Table(m.TableName())
  60. if listType == 1 { //待开始
  61. table = table.Where("activity_start_time > ?", timeNow)
  62. } else if listType == 2 { //进行中
  63. table = table.Where("activity_start_time < ? and activity_end_time > ?", timeNow, timeNow)
  64. } else if listType == 3 { //已结束
  65. table = table.Where("activity_end_time < ?", timeNow)
  66. }
  67. //if m.ActivityStartTime != 0
  68. table = table.Where("merchant_open_id = ? ", m.MerchantOpenID)
  69. var count int
  70. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  71. return nil, 0, err
  72. }
  73. table.Count(&count)
  74. return doc, count, nil
  75. }
  76. // list 接口使用
  77. func (m *MerchantCard) GetMerchantByOpenId() ([]MerchantCard, error) {
  78. var doc []MerchantCard
  79. table := orm.ShMysql.Table(m.TableName())
  80. table = table.Where("merchant_open_id = ? ", m.MerchantOpenID)
  81. err := table.Select("id,merchant_card_name,merchant_card_time,rebate_rate").Find(&doc).Error
  82. if err != nil {
  83. return nil, err
  84. }
  85. return doc, nil
  86. }
  87. // list 接口使用
  88. func (m *MerchantCard) ClientGetOpenIdList(pageSize int, pageIndex int, listType int) ([]MerchantCard, int, error) {
  89. var doc []MerchantCard
  90. timeNow := time.Now().Unix()
  91. table := orm.ShMysql.Table(m.TableName())
  92. if listType == 1 { //待开始
  93. table = table.Where("activity_start_time > ?", timeNow)
  94. } else if listType == 2 { //进行中
  95. table = table.Where("activity_start_time < ? and activity_end_time > ?", timeNow, timeNow)
  96. } else if listType == 3 { //已结束
  97. table = table.Where("activity_end_time < ?", timeNow)
  98. }
  99. //if m.ActivityStartTime != 0
  100. var count int
  101. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  102. return nil, 0, err
  103. }
  104. table.Count(&count)
  105. return doc, count, nil
  106. }
  107. func (m *MerchantCard) UpdateMerchantCard() error {
  108. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  109. map[string]interface{}{
  110. "project_name": m.ActivityEndTime,
  111. "activity_start_time": m.ActivityStartTime,
  112. "inventory": m.Inventory,
  113. "quota_num": m.QuotaNum,
  114. "use_rule": m.UseRule,
  115. "picture": m.Picture,
  116. "updated_at": tools.GetCurrntTimeStr()}).Error; err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. func (m *MerchantCard) UpdateMerchantWHXY() error {
  122. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
  123. map[string]interface{}{
  124. "w": m.W,
  125. "h": m.H,
  126. "x": m.X,
  127. "y": m.Y,
  128. "updated_at": tools.GetCurrntTimeStr()}).Error; err != nil {
  129. return err
  130. }
  131. return nil
  132. }