package sys_config_dict

import (
	"context"
	"kami/internal/consts"
	"kami/internal/dao"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/utility/config"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/util/gconv"
)

// SetIsAllowRedeemCardDifferentAmount 京东拿卡策略，金额异议是否兑换
func (d *sSysConfigDict) SetIsAllowRedeemCardDifferentAmount(ctx context.Context, category consts.CardRedeemAccountCategory, isAllow bool) (err error) {
	isStealStr := "0"
	if isAllow {
		isStealStr = "1"
	}
	_, err = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardDifferentAmount.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemCardDifferentAmount.Key(gconv.String(category)),
		Name:  "金额异议配置",
		Value: isStealStr,
	})
	return
}

func (d *sSysConfigDict) GetIsAllowRedeemCardDifferentAmount(ctx context.Context, category consts.CardRedeemAccountCategory) (isAllow bool, err error) {
	isAllow = true
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardDifferentAmount.Key(gconv.String(category))).Scan(&data)
	if data.Value != "1" {
		isAllow = true
	}
	return
}

// SetIsAllowRedeemCardMaxCount 京东拿卡策略，金额异议是否兑换
func (d *sSysConfigDict) SetIsAllowRedeemCardMaxCount(ctx context.Context, category consts.CardRedeemAccountCategory, amount float64) (err error) {
	_, err = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeAccountMaxRechargeCount.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeAccountMaxRechargeCount.Key(gconv.String(category)),
		Name:  "金额异议配置",
		Value: gconv.String(amount),
	})
	return
}

// GetIsAllowRedeemCardMaxCount 设置账号最大充值次数
func (d *sSysConfigDict) GetIsAllowRedeemCardMaxCount(ctx context.Context, category consts.CardRedeemAccountCategory) (amount float64, err error) {
	//isAllow = false
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeAccountMaxRechargeCount.Key(gconv.String(category))).Scan(&data)
	amount = gconv.Float64(data.Value)
	return
}

// GetRedeemCardSucceedCallbackIsAllow 成功的金额异议是否允许回调
func (d *sSysConfigDict) GetRedeemCardSucceedCallbackIsAllow(ctx context.Context, category consts.CardRedeemAccountCategory) (isAllow bool) {
	isAllow = true
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardDifferentSucceedCallbackAllow.Key(gconv.String(category))).Scan(&data)
	if data.Value != "1" {
		isAllow = true
	}
	return
}

// SetRedeemCardSucceedCallbackIsAllow 京东拿卡策略，金额异议是否兑换
func (d *sSysConfigDict) SetRedeemCardSucceedCallbackIsAllow(ctx context.Context, category consts.CardRedeemAccountCategory, isAllow bool) (err error) {
	isStealStr := "0"
	if isAllow {
		isStealStr = "1"
	}
	_, err = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardDifferentSucceedCallbackAllow.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemCardDifferentSucceedCallbackAllow.Key(gconv.String(category)),
		Name:  "是否允许回调(金额异议——成功)：" + category.Name(),
		Value: isStealStr,
	})
	return
}

// GetRedeemCardFailCallbackIsAllow 成功的金额异议是否允许回调
func (d *sSysConfigDict) GetRedeemCardFailCallbackIsAllow(ctx context.Context, category consts.CardRedeemAccountCategory) (isAllow bool) {
	isAllow = true
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardDifferentFailCallbackAllow.Key(gconv.String(category))).Scan(&data)
	if data.Value != "1" {
		isAllow = false
	}
	return
}

// SetRedeemCardFailCallbackIsAllow 京东拿卡策略，金额异议是否兑换
func (d *sSysConfigDict) SetRedeemCardFailCallbackIsAllow(ctx context.Context, category consts.CardRedeemAccountCategory, isAllow bool) (err error) {
	isStealStr := "0"
	if isAllow {
		isStealStr = "1"
	}
	_, err = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardDifferentFailCallbackAllow.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemCardDifferentFailCallbackAllow.Key(gconv.String(category)),
		Name:  "是否允许回调(金额异议——失败)：" + category.Name(),
		Value: isStealStr,
	})
	return
}

// GetRedeemCardMinAmount 获取最小充值金额
func (d *sSysConfigDict) GetRedeemCardMinAmount(ctx context.Context, category consts.CardRedeemAccountCategory) (amount int64) {
	//isAllow = false
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardMinAmount.Key(gconv.String(category))).Scan(&data)
	amount = gconv.Int64(data.Value)
	return
}

// SetRedeemCardMinAmount 设置最小充值金额
func (d *sSysConfigDict) SetRedeemCardMinAmount(ctx context.Context, category consts.CardRedeemAccountCategory, amount int64) (err error) {
	_, _ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardMinAmount.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemCardMinAmount.Key(gconv.String(category)),
		Name:  "最小充值金额：" + category.Name(),
		Value: gconv.String(amount),
	})
	return
}

// SetRedeemCardCompensatedAutoCallback 补卡是否自动回调
func (d *sSysConfigDict) SetRedeemCardCompensatedAutoCallback(ctx context.Context, category consts.CardRedeemAccountCategory, isAllow bool) (err error) {
	isStealStr := "0"
	if isAllow {
		isStealStr = "1"
	}
	_, err = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardCompensatedAutoCallback.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemCardCompensatedAutoCallback.Key(gconv.String(category)),
		Name:  "补卡是否自动回调：" + category.Name(),
		Value: isStealStr,
	})
	return
}

// GetRedeemCardCompensatedAutoCallback 补卡是否自动回调
func (d *sSysConfigDict) GetRedeemCardCompensatedAutoCallback(ctx context.Context, category consts.CardRedeemAccountCategory) (isAllow bool) {
	isAllow = true
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardCompensatedAutoCallback.Key(gconv.String(category))).Scan(&data)
	if data.Value != "1" {
		isAllow = false
	}
	return
}

// GetRedeemCardRate 设置单位内充值速率
func (d *sSysConfigDict) GetRedeemCardRate(ctx context.Context, category consts.CardRedeemAccountCategory) (rate int) {
	rate = 100
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardRate.Key(gconv.String(category))).Scan(&data)
	if !pointer.IsNil(data) && data.Value != "" {
		rate = gconv.Int(data.Value)
	}
	return
}

func (d *sSysConfigDict) GetMaxAccountRedeemCount(ctx context.Context, category consts.CardRedeemAccountCategory) (count int) {
	count = 20
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemRedeemMaxCountLimit.Key(gconv.String(category))).Scan(&data)
	if !pointer.IsNil(data) && data.Value != "" {
		count = gconv.Int(data.Value)
	}
	return
}

func (d *sSysConfigDict) SetMaxAccountRedeemCount(ctx context.Context, category consts.CardRedeemAccountCategory, count int) (isAllow bool) {
	_, _ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemRedeemMaxCountLimit.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemRedeemMaxCountLimit.Key(gconv.String(category)),
		Name:  "最大充值账号次数：" + category.Name(),
		Value: gconv.String(count),
	})
	return
}

// SetIsAllowedRepeated 获取是否允许重复上传
func (d *sSysConfigDict) SetIsAllowedRepeated(ctx context.Context, category consts.CardRedeemAccountCategory, isAllow bool) (err error) {
	isStealStr := "0"
	if isAllow {
		isStealStr = "1"
	}
	_, err = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemAllowRepeated.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemAllowRepeated.Key(gconv.String(category)),
		Name:  "允许重复上传：" + category.Name(),
		Value: isStealStr,
	})
	return
}

// GetIsAllowedRepeated 获取是否允许重复上传
func (d *sSysConfigDict) GetIsAllowedRepeated(ctx context.Context, category consts.CardRedeemAccountCategory) (isAllow bool) {
	isAllow = false
	data := &entity.V1SysConfigDict{}
	_ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemAllowRepeated.Key(gconv.String(category))).Scan(&data)
	if data.Value == "1" {
		isAllow = true
	}
	return
}

func (d *sSysConfigDict) SetRedeemCardRate(ctx context.Context, category consts.CardRedeemAccountCategory, rate int) {
	_, _ = dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemCardRate.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemCardRate.Key(gconv.String(category)),
		Name:  "单位内充值速率：" + category.Name(),
		Value: gconv.String(rate),
	})
}

// SetRedeemScheduleStrategy 设置调度策略
func (d *sSysConfigDict) SetRedeemScheduleStrategy(ctx context.Context, category consts.CardRedeemAccountCategory, strategy consts.RedeemCardScheduleStrategyType, tx gdb.TX) (err error) {
	m := dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}

	_, err = m.Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemScheduleStrategy.Key(gconv.String(category))).Save(do.V1SysConfigDict{
		Key:   consts.SysConfigDictTypeRedeemScheduleStrategy.Key(gconv.String(category)),
		Name:  "调度策略：" + category.Name(),
		Value: gconv.String(strategy),
	})
	return
}

// GetRedeemScheduleStrategy 获取调度策略
func (d *sSysConfigDict) GetRedeemScheduleStrategy(ctx context.Context, category consts.CardRedeemAccountCategory, tx gdb.TX) (strategy consts.RedeemCardScheduleStrategyType, err error) {
	strategy = consts.RedeemCardScheduleStrategyTypeNormal
	data := &entity.V1SysConfigDict{}
	m := dao.V1SysConfigDict.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	err = m.Where(dao.V1SysConfigDict.Columns().Key, consts.SysConfigDictTypeRedeemScheduleStrategy.Key(gconv.String(category))).Scan(&data)
	if consts.RedeemCardScheduleStrategyType(data.Value) == consts.RedeemCardScheduleStrategyTypeNormal {
		strategy = consts.RedeemCardScheduleStrategyTypeNormal
	}
	if consts.RedeemCardScheduleStrategyType(data.Value) == consts.RedeemCardScheduleStrategyTypeRandom {
		strategy = consts.RedeemCardScheduleStrategyTypeRandom
	}
	return
}
