package data

import (
	"context"
	"errors"
	"fmt"
	"google.golang.org/protobuf/types/known/durationpb"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/hints"
	"sync"
	"testing"
	"time"
	"voucher/internal/biz/vo"
	"voucher/internal/conf"
	"voucher/internal/data/model"
)

var source = "voucher:Lsxd@2024@tcp(voucher.rwlb.cn-chengdu.rds.aliyuncs.com:3306)/voucher?parseTime=True&loc=Local"

func Test_gorm_db_create(t *testing.T) {

	gormDB, err := gorm.Open(
		mysql.Open(source),
		&gorm.Config{
			Logger:                 logger.Default.LogMode(logger.Info),
			SkipDefaultTransaction: true,
		},
	)
	if err != nil {
		t.Fatal("failed to connect database " + err.Error())
	}

	sqlDB, err := gormDB.DB()
	if err != nil {
		t.Fatal("failed to gormDB " + err.Error())
	}

	sqlDB.SetMaxIdleConns(20)
	sqlDB.SetMaxOpenConns(100)

	start2 := time.Now()

	concurrency := 1000 // 调整并发数
	errCount := 0

	stats := sqlDB.Stats()
	fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle)

	go func() {
		ticker := time.NewTicker(50 * time.Millisecond)
		defer ticker.Stop()
		for range ticker.C {
			fmt.Printf("监控: 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle)
		}
	}()

	var wg sync.WaitGroup
	wg.Add(concurrency)

	for i := 0; i < concurrency; i++ {
		go func(i int) {
			defer wg.Done()

			ctx := context.Background()

			now := time.Now()

			info := &model.Order{
				OrderNo:     fmt.Sprintf("test_create_10_%d", i),
				OutBizNo:    fmt.Sprintf("374252390990605ngywYrSAGE8310_%d", i),
				ProductNo:   "001",
				BatchNo:     "001",
				Account:     "oO3vO5K2nE131-9uMoeYymLhlbYk",
				AccountType: 1,
				Status:      2,
				Type:        1,
				AppID:       "",
				MerchantNo:  "wx9ed74283ad25bca1",
				Channel:     1,
				NotifyUrl:   "https://sandbox.cdcc.cmbchina.com/AccessGateway/transIn/updateCodeStatus.json",
				Attach:      "{}",
				CreateTime:  &now,
				UpdateTime:  &now,
			}

			if info.ProductNo == "001" {
				info.VoucherNo = info.OrderNo
				info.Status = vo.OrderStatusSuccess.GetValue()
			}

			ddd := gormDB.WithContext(ctx).Model(model.Order{})
			tx := ddd.Create(info)

			if tx.Error != nil {
				fmt.Printf("写入错误: %v\n", tx.Error)
				errCount += 1
			}

			sqlDBx, _ := ddd.DB()
			fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", sqlDBx.Stats().OpenConnections, sqlDBx.Stats().Idle)
		}(i)
	}

	fmt.Printf("order create 当前打开连接数:%d, 空闲连接数:%d\n", stats.OpenConnections, stats.Idle)

	wg.Wait()
	t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
}

func Test_gorm_db(t *testing.T) {
	// 镜像mysql
	//data := &conf.Data_Database{
	//	Driver:      "mysql",
	//	Source:      "root:lsxddb123.@tcp(47.108.53.72:3306)/voucher?parseTime=True&loc=Local",
	//	MaxIdle:     20,                 // 空闲连接池
	//	MaxOpen:     200,                // 最大连接数
	//	MaxLifetime: durationpb.New(60), // 5分钟
	//	IsDebug:     false,
	//}
	// 测试
	//data := &conf.Data_Database{
	//	Driver:      "mysql",
	//	Source:      "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local",
	//	MaxIdle:     2,  // 空闲连接池
	//	MaxOpen:     10, // 最大连接数
	//	MaxLifetime: durationpb.New(60),
	//	IsDebug:     false,
	//}
	data := &conf.Data_Database{
		Driver:      "mysql",
		Source:      "voucher:Lsxd@2024@tcp(voucher.rwlb.cn-chengdu.rds.aliyuncs.com:3306)/voucher?parseTime=True&loc=Local",
		MaxIdle:     20,                 // 空闲连接池
		MaxOpen:     200,                // 最大连接数
		MaxLifetime: durationpb.New(60), // 5分钟
		IsDebug:     false,
	}
	gormDb := db(data)

	start2 := time.Now()

	concurrency := 10000 // 调整并发数
	errCount := 0

	var wg sync.WaitGroup
	wg.Add(concurrency)

	for i := 0; i < concurrency; i++ {
		go func(i int) {
			defer wg.Done()

			//ctx := context.Background()
			var info model.Order
			tx := gormDb.
				//WithContext(ctx).
				Model(model.Order{}).
				//Clauses(hints.UseIndex("udx_out_biz_no_type")).
				Where(model.Order{Type: vo.OrderTypeCmb.GetValue(), OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e1%d", i)}).
				First(&info)

			if tx.Error != nil {
				if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
					t.Errorf("未找到记录")
				} else {
					fmt.Printf("请求错误: %v\n", tx.Error)
					errCount += 1
				}
			}
			time.Sleep(1 * time.Microsecond)
		}(i)
	}

	wg.Wait()
	t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
}

func Test_db(t *testing.T) {
	maxLifetime := durationpb.New(300) // 5分钟
	data := &conf.Data_Database{
		Driver:      "mysql",
		Source:      "root:lansexiongdi6,@tcp(47.97.27.195:3306)/voucher?parseTime=True&loc=Local",
		MaxIdle:     2,  // 空闲连接池
		MaxOpen:     10, // 最大连接数
		MaxLifetime: maxLifetime,
		IsDebug:     false,
	}
	gormDb := db(data)

	start2 := time.Now()

	errCount := 0
	const concurrency = 1

	for i := 0; i < concurrency; i++ {
		ctx := context.Background()

		var info model.Order
		tx := gormDb.
			WithContext(ctx).
			Model(model.Order{}).
			Clauses(hints.ForceIndex("udx_out_biz_no_type")).
			Where(model.Order{Type: vo.OrderTypeCmb.GetValue(), OutBizNo: fmt.Sprintf("174252390990605ngywYrSAGE83e1%d", i)}).
			First(&info)
		if tx.Error != nil {
			if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
				t.Errorf("未找到记录")
			} else {
				fmt.Printf("请求错误: %v\n", tx.Error)
				errCount += 1
			}
		}
	}

	t.Logf("\n--------------连接请求,总请求耗时: %v,总数: %d, 失败次数: %d--------------\n", time.Since(start2), concurrency, errCount)
}
