Implement rebate calculation, idempotent record creation, frozen release handling, and tests for rebate configuration behavior.
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package model
|
|
|
|
import "time"
|
|
|
|
type RebateRecord struct {
|
|
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
|
|
InviterId int `json:"inviter_id" gorm:"index;uniqueIndex:idx_rebate_order_invite,priority:3;not null"`
|
|
InviteeId int `json:"invitee_id" gorm:"index;uniqueIndex:idx_rebate_order_invite,priority:4;not null"`
|
|
OrderId int `json:"order_id" gorm:"index;uniqueIndex:idx_rebate_order_invite,priority:2"`
|
|
OrderType string `json:"order_type" gorm:"type:varchar(16);uniqueIndex:idx_rebate_order_invite,priority:1;not null"`
|
|
OrderAmount int `json:"order_amount" gorm:"not null"`
|
|
RebateAmount int `json:"rebate_amount" gorm:"not null"`
|
|
RatePercent int `json:"rate_percent" gorm:"not null"`
|
|
Status string `json:"status" gorm:"type:varchar(16);not null;default:'frozen';index"`
|
|
FrozenUntil *time.Time `json:"frozen_until" gorm:"index"`
|
|
ReleasedAt *time.Time `json:"released_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (RebateRecord) TableName() string {
|
|
return "rebate_records"
|
|
}
|
|
|
|
func (r *RebateRecord) Insert() error {
|
|
return DB.Create(r).Error
|
|
}
|
|
|
|
func (r *RebateRecord) Update() error {
|
|
return DB.Save(r).Error
|
|
}
|
|
|
|
func GetFrozenRebatesDue() ([]RebateRecord, error) {
|
|
var records []RebateRecord
|
|
err := DB.Where("status = ? AND frozen_until IS NOT NULL AND frozen_until <= ?", "frozen", time.Now()).
|
|
Order("id ASC").
|
|
Find(&records).Error
|
|
return records, err
|
|
}
|
|
|
|
func GetRebateRecords(page int, pageSize int, status string, userId int) ([]RebateRecord, int64, error) {
|
|
var records []RebateRecord
|
|
var total int64
|
|
query := DB.Model(&RebateRecord{})
|
|
if status != "" {
|
|
query = query.Where("status = ?", status)
|
|
}
|
|
if userId > 0 {
|
|
query = query.Where("inviter_id = ? OR invitee_id = ?", userId, userId)
|
|
}
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
err := query.Order("id DESC").Offset((page - 1) * pageSize).Limit(pageSize).Find(&records).Error
|
|
return records, total, err
|
|
}
|