99 lines
3.6 KiB
Go
99 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
func main() {
|
|
// 连接数据库
|
|
dsn := "root:bindbox2025kdy@tcp(150.158.78.154:3306)/dev_game?parseTime=true"
|
|
db, err := sql.Open("mysql", dsn)
|
|
if err != nil {
|
|
log.Fatal("连接失败:", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
fmt.Println("=== 优惠券 ID 275 当前状态 ===")
|
|
var ucID, userID, couponID int64
|
|
var status int32
|
|
var balanceAmount int64
|
|
var usedOrderID sql.NullInt64
|
|
var usedAt sql.NullTime
|
|
err = db.QueryRow("SELECT id, user_id, coupon_id, status, balance_amount, used_order_id, used_at FROM user_coupons WHERE id = 275").Scan(
|
|
&ucID, &userID, &couponID, &status, &balanceAmount, &usedOrderID, &usedAt,
|
|
)
|
|
if err != nil {
|
|
log.Println("查询优惠券失败:", err)
|
|
} else {
|
|
fmt.Printf("用户券ID: %d | 用户ID: %d | 模板ID: %d | 状态: %d | 余额(分): %d | 使用订单ID: %v | 使用时间: %v\n",
|
|
ucID, userID, couponID, status, balanceAmount, usedOrderID, usedAt)
|
|
}
|
|
|
|
// 查询系统券模板
|
|
fmt.Println("\n=== 系统优惠券模板 ===")
|
|
var scID int64
|
|
var scName string
|
|
var discountType int32
|
|
var discountValue int64
|
|
err = db.QueryRow("SELECT id, name, discount_type, discount_value FROM system_coupons WHERE id = ?", couponID).Scan(&scID, &scName, &discountType, &discountValue)
|
|
if err == nil {
|
|
fmt.Printf("模板ID: %d | 名称: %s | 类型: %d | 面值(分): %d\n", scID, scName, discountType, discountValue)
|
|
}
|
|
|
|
fmt.Println("\n=== 优惠券 ID 275 的所有流水记录 ===")
|
|
rows, err := db.Query(`
|
|
SELECT id, user_id, user_coupon_id, change_amount, balance_after, order_id, action, created_at
|
|
FROM user_coupon_ledger
|
|
WHERE user_coupon_id = 275
|
|
ORDER BY created_at DESC
|
|
`)
|
|
if err != nil {
|
|
log.Println("查询流水失败:", err)
|
|
} else {
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var id, userID, userCouponID, changeAmount, balanceAfter, orderID int64
|
|
var action string
|
|
var createdAt sql.NullTime
|
|
rows.Scan(&id, &userID, &userCouponID, &changeAmount, &balanceAfter, &orderID, &action, &createdAt)
|
|
fmt.Printf("流水ID: %d | 变动: %d分 | 余额: %d分 | 订单ID: %d | 动作: %s | 时间: %v\n",
|
|
id, changeAmount, balanceAfter, orderID, action, createdAt)
|
|
}
|
|
}
|
|
|
|
fmt.Println("\n=== order_coupons 表中使用优惠券 275 的记录 ===")
|
|
rows2, err := db.Query(`
|
|
SELECT oc.id, oc.order_id, oc.user_coupon_id, oc.applied_amount, oc.created_at,
|
|
o.order_no, o.status, o.total_amount, o.discount_amount, o.actual_amount
|
|
FROM order_coupons oc
|
|
LEFT JOIN orders o ON o.id = oc.order_id
|
|
WHERE oc.user_coupon_id = 275
|
|
ORDER BY oc.created_at DESC
|
|
`)
|
|
if err != nil {
|
|
log.Println("查询 order_coupons 失败:", err)
|
|
} else {
|
|
defer rows2.Close()
|
|
for rows2.Next() {
|
|
var id, orderID, userCouponID, appliedAmount int64
|
|
var createdAt sql.NullTime
|
|
var orderNo sql.NullString
|
|
var orderStatus sql.NullInt32
|
|
var totalAmount, discountAmount, actualAmount sql.NullInt64
|
|
rows2.Scan(&id, &orderID, &userCouponID, &appliedAmount, &createdAt, &orderNo, &orderStatus, &totalAmount, &discountAmount, &actualAmount)
|
|
fmt.Printf("记录ID: %d | 订单ID: %d | 订单号: %v | 扣减金额: %d分 | 时间: %v\n",
|
|
id, orderID, orderNo.String, appliedAmount, createdAt)
|
|
}
|
|
}
|
|
|
|
// 计算总扣减金额
|
|
var totalApplied int64
|
|
db.QueryRow("SELECT COALESCE(SUM(applied_amount), 0) FROM order_coupons WHERE user_coupon_id = 275").Scan(&totalApplied)
|
|
fmt.Printf("\n=== 统计 ===\n优惠券 275 累计扣减: %d 分 (%.2f 元)\n", totalApplied, float64(totalApplied)/100)
|
|
fmt.Printf("当前余额: %d 分 (%.2f 元)\n", balanceAmount, float64(balanceAmount)/100)
|
|
}
|