package biz

import (
	"sync"
	"voucher/internal/biz/cmb"
	"voucher/internal/biz/mixrepos"
	"voucher/internal/biz/repo"
	"voucher/internal/biz/wechatrepo"
	"voucher/internal/conf"
	"voucher/internal/data"
)

type VoucherBiz struct {
	bc                          *conf.Bootstrap
	rdb                         *data.Rdb
	Cmb                         *cmb.Cmb
	ProductRepo                 repo.ProductRepo
	OrderRepo                   repo.OrderRepo
	OrderBakRepo                repo.OrderBakRepo
	OrderNotifyRepo             repo.OrderNotifyRepo
	WechatNotifyRegisterTagRepo repo.WechatNotifyRegisterTagRepo
	MqSendMixRepo               mixrepos.MQSendMixRepo
	GenerateMixRepo             mixrepos.GenerateMixRepo
	WechatCpnRepo               wechatrepo.WechatCpnRepo
	BankMultiActivityRepo       wechatrepo.BankMultiActivityRepo
	DingMixRepo                 mixrepos.DingMixRepo
	CmbMixRepo                  mixrepos.CmbMixRepo
	SmsMixRepo                  mixrepos.SmsMixRepo
	MultiBiz                    *MultiBiz

	mu       sync.RWMutex
	queryMap map[string]bool
}

func NewVoucherBiz(
	bc *conf.Bootstrap,
	rdb *data.Rdb,
	Cmb *cmb.Cmb,
	ProductRepo repo.ProductRepo,
	OrderRepo repo.OrderRepo,
	OrderBakRepo repo.OrderBakRepo,
	OrderNotifyRepo repo.OrderNotifyRepo,
	WechatNotifyRegisterTagRepo repo.WechatNotifyRegisterTagRepo,
	MqSendMixRepo mixrepos.MQSendMixRepo,
	GenerateMixRepo mixrepos.GenerateMixRepo,
	WechatCpnRepo wechatrepo.WechatCpnRepo,
	BankMultiActivityRepo wechatrepo.BankMultiActivityRepo,
	DingMixRepo mixrepos.DingMixRepo,
	CmbMixRepo mixrepos.CmbMixRepo,
	SmsMixRepo mixrepos.SmsMixRepo,
	MultiBiz *MultiBiz,
) *VoucherBiz {
	return &VoucherBiz{
		bc:                          bc,
		rdb:                         rdb,
		Cmb:                         Cmb,
		ProductRepo:                 ProductRepo,
		OrderRepo:                   OrderRepo,
		OrderBakRepo:                OrderBakRepo,
		OrderNotifyRepo:             OrderNotifyRepo,
		WechatNotifyRegisterTagRepo: WechatNotifyRegisterTagRepo,
		MqSendMixRepo:               MqSendMixRepo,
		GenerateMixRepo:             GenerateMixRepo,
		WechatCpnRepo:               WechatCpnRepo,
		BankMultiActivityRepo:       BankMultiActivityRepo,
		DingMixRepo:                 DingMixRepo,
		CmbMixRepo:                  CmbMixRepo,
		SmsMixRepo:                  SmsMixRepo,
		MultiBiz:                    MultiBiz,

		queryMap: make(map[string]bool),
	}
}

func (this *VoucherBiz) Get(uid string) bool {
	this.mu.Lock()
	defer this.mu.Unlock()

	if _, ok := this.queryMap[uid]; ok {
		return ok
	}

	return false
}

func (this *VoucherBiz) Add(uid string) {
	this.mu.Lock()
	defer this.mu.Unlock()

	this.queryMap[uid] = true
}

func (this *VoucherBiz) Remove(uid string) {

	this.mu.Lock()
	defer this.mu.Unlock()

	if _, ok := this.queryMap[uid]; ok {
		delete(this.queryMap, uid)
	}
}
