package gameServerMgr

import (
	"Framework/managecenterModel"
	"goutil/stringUtil"
	"testing"
)

func TestChargeUtil_GetChargeConfigList(t *testing.T) {
	/*
		渠道充值配置：[{"ProductId":"fzxh_00060","ChargePoint":6,"GamePoint":60,"Ratio":10,"GiveGamePoint":6,"FirstGiveGamePoint":60,"GiveRatio":0.1,"VipLv":0,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00300","ChargePoint":30,"GamePoint":300,"Ratio":10,"GiveGamePoint":30,"FirstGiveGamePoint":300,"GiveRatio":0.1,"VipLv":0,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00500","ChargePoint":50,"GamePoint":550,"Ratio":11,"GiveGamePoint":110,"FirstGiveGamePoint":550,"GiveRatio":0.2,"VipLv":1,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00980","ChargePoint":98,"GamePoint":1470,"Ratio":15,"GiveGamePoint":441,"FirstGiveGamePoint":1470,"GiveRatio":0.3,"VipLv":5,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_restriction_18","ChargePoint":18,"GamePoint":90,"Ratio":5,"GiveGamePoint":0,"FirstGiveGamePoint":0,"GiveRatio":0,"VipLv":3,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":true},{"ProductId":"fzxh_restriction_50","ChargePoint":50,"GamePoint":300,"Ratio":6,"GiveGamePoint":0,"FirstGiveGamePoint":0,"GiveRatio":0,"VipLv":1,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":true}]
	*/

	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001
	var partnerId int32 = 1001

	//激活器服务器，获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	type VipRange struct {
		MinVip byte
		MaxVip byte
	}

	var isMonthCard = false
	var maxVip byte = 20

	vipMap := make(map[VipRange][]float64)
	vipMap[VipRange{MinVip: 0, MaxVip: 0}] = []float64{6, 30}
	vipMap[VipRange{MinVip: 1, MaxVip: 4}] = []float64{6, 30, 50}
	vipMap[VipRange{MinVip: 5, MaxVip: maxVip}] = []float64{6, 30, 50, 98}

	isExistsChargeConfig := func(configs []*managecenterModel.ChargeConfig, point float64) bool {
		for _, item := range configs {
			if item.ChargePoint == point {
				return true
			}
		}

		return false
	}

	//每个VIP等级都获取一遍充值档位
	for vipRange, points := range vipMap {
		for vip := vipRange.MinVip; vip <= vipRange.MaxVip; vip++ {
			showChargeList, exists, error := ChargeUtilObj.GetChargeConfigList(partnerId, vip, isMonthCard, func(config *managecenterModel.ChargeConfig) bool {
				if config.IfFirstShow == 1 && config.IfSecondShow == 1 {
					return true
				}

				return false
			})

			if error != nil {
				t.Fatalf("获取充值配置列表出现错误：%v", error)
			}

			if !exists {
				t.Fatalf("PartnerId[%d],Vip[%d],IsMonthCard[%v],未找到充值配置列表", partnerId, vip, isMonthCard)
			}

			for _, point := range points {
				if !isExistsChargeConfig(showChargeList, point) {
					t.Fatalf("PartnerId[%d],Vip[%d],IsMonthCard[%v],金额[%f]未找到充值配置", partnerId, vip, isMonthCard, point)
				}
			}
		}
	}
}

// 测试生成订单号
func TestChargeUtil_GenerateOrderId(t *testing.T) {
	var chargeServerUrl = "https://chargetest-xht.moqikaka.com/API/GenerateOrderId.ashx"
	var serverGroupId int32 = 20001
	var partnerId int32 = 1001
	var productId = "fzxh_00060"
	var userId = stringUtil.GetNewGUID()
	var playerId = stringUtil.GetNewGUID()
	var isMonthCard = true

	//生成订单号
	orderId, error := ChargeUtilObj.GenerateOrderId(
		chargeServerUrl, productId, partnerId, serverGroupId, userId, playerId, "macmacmacmac", "idfa", "ip", "imei", "extra", isMonthCard)
	if error != nil {
		t.Fatalf("生成订单号[GenerateOrderId]出现错误：%v", error)
	}

	t.Logf("生成订单号[GenerateOrderId]:%s", orderId)
}

// 测试验证充值回调签名
func TestChargeUtil_CheckChargeSign(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001
	var partnerId int32 = 1001
	var playerId = "04bade21-d7f7-4188-bc7c-8914c5330a23"
	var orderId = "1001_20001_1590394423_2"
	var productId = "fzxh_00060"
	var deviceIdentifier = "deviceIdentifier"
	//var loginKey = "a0482eaf-14e8-4a65-950e-864214f62da5"

	//激活器服务器，获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//充值服务器生成的签名
	var targetSign = "02bd33319fc0f520a3e801f725973017"

	//验证充值回调签名是否正确
	if !ChargeUtilObj.CheckChargeSign(partnerId, serverGroupId, playerId, orderId, productId, deviceIdentifier, targetSign) {
		t.Fatalf("验证充值回调签名失败！目标签名：%s", targetSign)
	}
}

// 测试获取充值配置
func TestChargeUtil_GetChargeConfigItem(t *testing.T) {
	/*
		渠道充值配置：[{"ProductId":"fzxh_00060","ChargePoint":6,"GamePoint":60,"Ratio":10,"GiveGamePoint":6,"FirstGiveGamePoint":60,"GiveRatio":0.1,"VipLv":0,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00300","ChargePoint":30,"GamePoint":300,"Ratio":10,"GiveGamePoint":30,"FirstGiveGamePoint":300,"GiveRatio":0.1,"VipLv":0,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00500","ChargePoint":50,"GamePoint":550,"Ratio":11,"GiveGamePoint":110,"FirstGiveGamePoint":550,"GiveRatio":0.2,"VipLv":1,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00980","ChargePoint":98,"GamePoint":1470,"Ratio":15,"GiveGamePoint":441,"FirstGiveGamePoint":1470,"GiveRatio":0.3,"VipLv":5,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_restriction_18","ChargePoint":18,"GamePoint":90,"Ratio":5,"GiveGamePoint":0,"FirstGiveGamePoint":0,"GiveRatio":0,"VipLv":3,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":true},{"ProductId":"fzxh_restriction_50","ChargePoint":50,"GamePoint":300,"Ratio":6,"GiveGamePoint":0,"FirstGiveGamePoint":0,"GiveRatio":0,"VipLv":1,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":true}]
	*/

	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001
	var partnerId int32 = 1001
	var maxVip byte = 20

	//激活器服务器，获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	type VipRange struct {
		MinVip byte
		MaxVip byte
	}

	//#region 测试获取普通充值配置

	isMonthCard := false
	vipMap := make(map[VipRange][]float64)
	vipMap[VipRange{MinVip: 0, MaxVip: 0}] = []float64{6, 30}
	vipMap[VipRange{MinVip: 1, MaxVip: 4}] = []float64{6, 30, 50}
	vipMap[VipRange{MinVip: 5, MaxVip: maxVip}] = []float64{6, 30, 50, 98}

	//每个VIP等级都获取一遍充值档位
	for vipRange, points := range vipMap {
		for vip := vipRange.MinVip; vip <= vipRange.MaxVip; vip++ {
			for _, point := range points {
				//获取充值配置
				chargeConfig, exists, error := ChargeUtilObj.GetChargeConfigItem(partnerId, vip, isMonthCard, point)
				if error != nil {
					t.Fatalf("普通充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]获取充值档位出现错误:%v", vip, point, error)
				}

				if !exists {
					t.Fatalf("普通充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]未找到充值档位配置", vip, point)
				}

				if chargeConfig == nil {
					t.Fatalf("普通充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]找到的充值档位配置为nil", vip, point)
				}

				if chargeConfig.ChargePoint != point {
					t.Fatalf("普通充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]找到的充值档位ChargePoint[%f]错误", vip, point, chargeConfig.ChargePoint)
				}
			}
		}
	}

	//#endregion

	//#region 测试获取月卡充值配置

	isMonthCard = true
	vipMap = make(map[VipRange][]float64)
	vipMap[VipRange{MinVip: 1, MaxVip: 1}] = []float64{50}
	vipMap[VipRange{MinVip: 3, MaxVip: maxVip}] = []float64{18, 50}

	//每个VIP等级都获取一遍充值档位
	for vipRange, points := range vipMap {
		for vip := vipRange.MinVip; vip <= vipRange.MaxVip; vip++ {
			for _, point := range points {
				//获取充值配置
				chargeConfig, exists, error := ChargeUtilObj.GetChargeConfigItem(partnerId, vip, isMonthCard, point)
				if error != nil {
					t.Fatalf("月卡充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]获取充值档位出现错误:%v", vip, point, error)
				}

				if !exists {
					t.Fatalf("月卡充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]未找到充值档位配置", vip, point)
				}

				if chargeConfig == nil {
					t.Fatalf("月卡充值，Vip[%d]，Point[%f]找到的充值档位配置为nil", vip, point)
				}

				if chargeConfig.ChargePoint != point {
					t.Fatalf("月卡充值，获取充值配置[GetChargeConfigItem]，Vip[%d]，Point[%f]找到的充值档位ChargePoint[%f]错误", vip, point, chargeConfig.ChargePoint)
				}
			}
		}
	}

	//#endregion
}

// 测试计算充值获得的游戏货币
func TestChargeUtil_CalcChargeAllGamePoint(t *testing.T) {
	/*
		渠道充值配置：[{"ProductId":"fzxh_00060","ChargePoint":6,"GamePoint":60,"Ratio":10,"GiveGamePoint":6,"FirstGiveGamePoint":60,"GiveRatio":0.1,"VipLv":0,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00300","ChargePoint":30,"GamePoint":300,"Ratio":10,"GiveGamePoint":30,"FirstGiveGamePoint":300,"GiveRatio":0.1,"VipLv":0,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00500","ChargePoint":50,"GamePoint":550,"Ratio":11,"GiveGamePoint":110,"FirstGiveGamePoint":550,"GiveRatio":0.2,"VipLv":1,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_00980","ChargePoint":98,"GamePoint":1470,"Ratio":15,"GiveGamePoint":441,"FirstGiveGamePoint":1470,"GiveRatio":0.3,"VipLv":5,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":false},{"ProductId":"fzxh_restriction_18","ChargePoint":18,"GamePoint":90,"Ratio":5,"GiveGamePoint":0,"FirstGiveGamePoint":0,"GiveRatio":0,"VipLv":3,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":true},{"ProductId":"fzxh_restriction_50","ChargePoint":50,"GamePoint":300,"Ratio":6,"GiveGamePoint":0,"FirstGiveGamePoint":0,"GiveRatio":0,"VipLv":1,"IfFirstShow":1,"IfSecondShow":1,"IsMonthCard":true}]
	*/

	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001
	var partnerId int32 = 1001

	//激活器服务器，获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//输入订单信息
	type OrderItem struct {
		Vip           byte
		Money         float64
		ActivityMoney float64
		ProductId     string
		IsFirstCharge bool
		IsMonthCard   bool
	}

	//计算结果
	type GamePointResult struct {
		ChargeGamePoint   int
		GiveGamePoint     int
		ActivityGamePoint int
		TotalGamePoint    int
	}

	testOrderMap := make(map[OrderItem]GamePointResult)

	//#region 普通充值

	testOrderMap[OrderItem{
		Vip:           0,
		Money:         6,
		ActivityMoney: 1,
		ProductId:     "fzxh_00060",
		IsFirstCharge: true,
		IsMonthCard:   false,
	}] = GamePointResult{
		ChargeGamePoint:   60, //GamePoint
		GiveGamePoint:     60, //FirstGiveGamePoint
		ActivityGamePoint: 10, //ActivityMoney*Ratio = 1*10
		TotalGamePoint:    130,
	} //首充6元,活动金额1元

	testOrderMap[OrderItem{
		Vip:           0,
		Money:         6,
		ActivityMoney: 1,
		ProductId:     "fzxh_00060",
		IsFirstCharge: false,
		IsMonthCard:   false,
	}] = GamePointResult{
		ChargeGamePoint:   60, //GamePoint
		GiveGamePoint:     6,  //GiveGamePoint 6
		ActivityGamePoint: 10, //ActivityMoney*Ratio = 1*10
		TotalGamePoint:    76,
	} //非首充6元,活动金额1元

	testOrderMap[OrderItem{
		Vip:           0,
		Money:         0,
		ActivityMoney: 1,
		ProductId:     "fzxh_00060",
		IsFirstCharge: false,
		IsMonthCard:   false,
	}] = GamePointResult{
		ChargeGamePoint:   60, //Money*Ratio = 6*10
		GiveGamePoint:     6,  //GiveGamePoint 6
		ActivityGamePoint: 10, //ActivityMoney*Ratio = 1*10
		TotalGamePoint:    76,
	} //无金额匹配6元,活动金额1元

	testOrderMap[OrderItem{
		Vip:           0,
		Money:         10,
		ActivityMoney: 3,
		ProductId:     "",
		IsFirstCharge: false,
		IsMonthCard:   false,
	}] = GamePointResult{
		ChargeGamePoint:   100, //Money*Ratio = 10*10
		GiveGamePoint:     10,  //Money*Ratio*GiveRatio 10 * 10 * 0.1
		ActivityGamePoint: 30,  //ActivityMoney*Ratio = 3*10
		TotalGamePoint:    140,
	} //无对应档位配置,模糊匹配10元,活动金额1元,实际匹配到6元档位

	testOrderMap[OrderItem{
		Vip:           3,
		Money:         98,
		ActivityMoney: 3,
		ProductId:     "",
		IsFirstCharge: false,
		IsMonthCard:   false,
	}] = GamePointResult{
		ChargeGamePoint:   1078, //Money*Ratio = 98*11
		GiveGamePoint:     216,  //Money*Ratio*GiveRatio 98 * 11 * 0.2 = 215.6
		ActivityGamePoint: 33,   //ActivityMoney*Ratio = 3*11
		TotalGamePoint:    1327,
	} //Vip限制,匹配98元,实际匹配到50档位,活动金额3元

	//#endregion

	for order, result := range testOrderMap {
		//计算充值获得的游戏币
		chargeGamePoint, giveGamePoint, activityGamePoint, totalGamePoint, exists, err := ChargeUtilObj.CalcChargeAllGamePoint(
			partnerId, order.Vip, order.IsMonthCard, order.Money, order.ActivityMoney, order.ProductId, order.IsFirstCharge)

		if err != nil {
			t.Fatalf("普通充值,计算充值获得游戏币[CalcChargeAllGamePoint],金额[%f] error:%v", order.Money, err)
		}

		if !exists {
			t.Fatalf("普通充值,计算充值获得游戏币[CalcChargeAllGamePoint],金额[%f]未找到充值配置:%v", order.Money, exists)
		}

		if chargeGamePoint != result.ChargeGamePoint {
			t.Fatalf("普通充值,计算充值获得游戏币[CalcChargeAllGamePoint],ChargeGamePoint计算结果不正确,金额[%f],期望[%d],结果[%d]", order.Money, result.ChargeGamePoint, chargeGamePoint)
		}

		if giveGamePoint != result.GiveGamePoint {
			t.Fatalf("普通充值,计算充值获得游戏币[CalcChargeAllGamePoint],GiveGamePoint计算结果不正确,金额[%f],期望[%d],结果[%d]", order.Money, result.GiveGamePoint, giveGamePoint)
		}

		if activityGamePoint != result.ActivityGamePoint {
			t.Fatalf("普通充值,计算充值获得游戏币[CalcChargeAllGamePoint],ActivityGamePoint计算结果不正确,金额[%f],期望[%d],结果[%d]", order.Money, result.ActivityGamePoint, activityGamePoint)
		}

		if totalGamePoint != result.TotalGamePoint {
			t.Fatalf("普通充值,计算充值获得游戏币[CalcChargeAllGamePoint],TotalGamePoint计算结果不正确,金额[%f],期望[%d],结果[%d]", order.Money, result.TotalGamePoint, totalGamePoint)
		}
	}

	//#region 月卡充值

	testOrderMap[OrderItem{
		Vip:         3,
		Money:       18,
		ProductId:   "fzxh_restriction_18",
		IsMonthCard: true,
	}] = GamePointResult{
		ChargeGamePoint: 90, //GamePoint
	} //18元月卡

	testOrderMap[OrderItem{
		Vip:         0,
		Money:       50,
		ProductId:   "fzxh_restriction_50",
		IsMonthCard: true,
	}] = GamePointResult{
		ChargeGamePoint: 250, //Money * Ratio = 50 * 5
	} //VIP限制，匹配50元月卡，实际匹配到18元月卡

	testOrderMap[OrderItem{
		Vip:         1,
		Money:       0,
		ProductId:   "fzxh_restriction_50",
		IsMonthCard: true,
	}] = GamePointResult{
		ChargeGamePoint: 300, //GamePoint
	} //无金额匹配50元月卡

	testOrderMap[OrderItem{
		Vip:         0,
		Money:       0,
		ProductId:   "fzxh_restriction_50",
		IsMonthCard: true,
	}] = GamePointResult{
		ChargeGamePoint: 0, //匹配不成功
	} //VIP限制，无金额匹配50元月卡

	for order, result := range testOrderMap {
		//计算充值获得的游戏币
		chargeGamePoint, exists, err := ChargeUtilObj.CalcChargeGamePoint(
			partnerId, order.Vip, order.IsMonthCard, order.Money, order.ProductId)

		if err != nil {
			t.Fatalf("月卡充值,计算充值获得游戏币[CalcChargeGamePoint],金额[%f] error:%v", order.Money, err)
		}

		if !exists {
			t.Fatalf("月卡充值,计算充值获得游戏币[CalcChargeGamePoint],金额[%f]未找到充值配置:%v", order.Money, exists)
		}

		if chargeGamePoint != result.ChargeGamePoint {
			t.Fatalf("月卡充值,计算充值获得游戏币[CalcChargeGamePoint],ChargeGamePoint计算结果不正确,金额[%f],期望[%d],结果[%d]", order.Money, result.ChargeGamePoint, chargeGamePoint)
		}
	}

	//#endregion
}
