96 lines
3.2 KiB
Go
96 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
DbDSN = "root:bindbox2025kdy@tcp(150.158.78.154:3306)/dev_game?charset=utf8mb4&parseTime=True&loc=Local"
|
|
)
|
|
|
|
type UserCoupons struct {
|
|
ID int64 `gorm:"column:id"`
|
|
UserID int64 `gorm:"column:user_id"`
|
|
CouponID int64 `gorm:"column:coupon_id"` // System Coupon ID
|
|
Status int32 `gorm:"column:status"` // 1: Unused, 2: Used, 3: Expired
|
|
ValidStart time.Time `gorm:"column:valid_start"`
|
|
ValidEnd time.Time `gorm:"column:valid_end"`
|
|
UsedAt *time.Time `gorm:"column:used_at"`
|
|
CreatedAt time.Time `gorm:"column:created_at"`
|
|
}
|
|
|
|
type SystemCoupons struct {
|
|
ID int64 `gorm:"column:id"`
|
|
Name string `gorm:"column:name"`
|
|
DiscountType int32 `gorm:"column:discount_type"` // 1: Direct, 2: Threshold, 3: Discount
|
|
DiscountValue int64 `gorm:"column:discount_value"` // Value in cents
|
|
MinOrderAmount int64 `gorm:"column:min_order_amount"`
|
|
}
|
|
|
|
type Orders struct {
|
|
ID int64 `gorm:"column:id"`
|
|
OrderNo string `gorm:"column:order_no"`
|
|
ActualAmount int64 `gorm:"column:actual_amount"`
|
|
DiscountAmount int64 `gorm:"column:discount_amount"`
|
|
CouponID int64 `gorm:"column:coupon_id"` // Refers to system_coupons.id or user_coupons.id? usually user_coupons.id in many systems, need to check query.
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func (UserCoupons) TableName() string { return "user_coupons" }
|
|
func (SystemCoupons) TableName() string { return "system_coupons" }
|
|
func (Orders) TableName() string { return "orders" }
|
|
|
|
func main() {
|
|
db, err := gorm.Open(mysql.Open(DbDSN), &gorm.Config{})
|
|
if err != nil {
|
|
log.Fatalf("DB connection failed: %v", err)
|
|
}
|
|
|
|
userID := 9090
|
|
|
|
fmt.Printf("--- Querying Coupons for User %d ---\n", userID)
|
|
var userCoupons []UserCoupons
|
|
db.Where("user_id = ?", userID).Order("created_at DESC").Find(&userCoupons)
|
|
|
|
for _, uc := range userCoupons {
|
|
var sc SystemCoupons
|
|
db.First(&sc, uc.CouponID)
|
|
|
|
statusStr := "Unknown"
|
|
switch uc.Status {
|
|
case 1:
|
|
statusStr = "Unused"
|
|
case 2:
|
|
statusStr = "Used"
|
|
case 3:
|
|
statusStr = "Expired"
|
|
}
|
|
|
|
fmt.Printf("\n[UserCoupon ID: %d]\n", uc.ID)
|
|
fmt.Printf(" Status: %d (%s)\n", uc.Status, statusStr)
|
|
fmt.Printf(" Name: %s\n", sc.Name)
|
|
fmt.Printf(" Type: %d, Value: %d (cents), Threshold: %d\n", sc.DiscountType, sc.DiscountValue, sc.MinOrderAmount)
|
|
fmt.Printf(" Valid: %v to %v\n", uc.ValidStart.Format("2006-01-02 15:04"), uc.ValidEnd.Format("2006-01-02 15:04"))
|
|
|
|
if uc.Status == 2 {
|
|
// Find order used
|
|
var order Orders
|
|
// Note: orders table usually links to user_coupons ID via `coupon_id` column in this system (based on previous files).
|
|
// Let's verify if `orders.coupon_id` matches `user_coupons.id` or `system_coupons.id`. previous logs hinted `user_coupons.id`.
|
|
err := db.Where("coupon_id = ?", uc.ID).First(&order).Error
|
|
if err == nil {
|
|
fmt.Printf(" USED IN ORDER: %s (ID: %d)\n", order.OrderNo, order.ID)
|
|
fmt.Printf(" Order Total (Actual): %d cents\n", order.ActualAmount)
|
|
fmt.Printf(" Discount Applied: %d cents\n", order.DiscountAmount)
|
|
} else {
|
|
fmt.Printf(" WARNING: Status Used but Order not found linked to this UserCoupon ID.\n")
|
|
}
|
|
}
|
|
}
|
|
}
|