package merchant

import (
	"context"
	"time"

	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
)

type MerchantInfo struct {
	Id                   int
	Status               string
	BelongAgentUid       string
	BelongAgentName      string
	MerchantName         string
	MerchantUid          string
	MerchantKey          string
	MerchantSecret       string
	LoginPassword        string
	LoginAccount         string
	AutoSettle           string
	AutoPayFor           string
	WhiteIps             string
	Remark               string
	SinglePayForRoadUid  string
	SinglePayForRoadName string
	RollPayForRoadCode   string
	RollPayForRoadName   string
	PayforFee            float64
	UpdateTime           time.Time
	CreateTime           time.Time
}

const MERCHANT_INFO = "merchant_info"

func IsExistByMerchantName(ctx context.Context, merchantName string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(MERCHANT_INFO).Filter("merchant_name", merchantName).ExistWithCtx(ctx)

	return exist
}

func IsExistByMerchantUid(ctx context.Context, uid string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(MERCHANT_INFO).Filter("merchant_uid", uid).ExistWithCtx(ctx)

	return exist
}

func IsExistMerchantByAgentUid(ctx context.Context, uid string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(MERCHANT_INFO).Filter("belong_agent_uid", uid).ExistWithCtx(ctx)

	return exist
}

func IsExistByMerchantPhone(ctx context.Context, phone string) bool {
	o := orm.NewOrm()
	exist := o.QueryTable(MERCHANT_INFO).Filter("LoginAccount", phone).ExistWithCtx(ctx)

	return exist
}

func InsertMerchantInfo(ctx context.Context, merchantInfo MerchantInfo) bool {
	o := orm.NewOrm()
	_, err := o.InsertWithCtx(ctx, &merchantInfo)
	if err != nil {
		logs.Error("insert merchant fail: ", err)
		return false
	}
	return true
}

func GetMerchantLenByMap(ctx context.Context, params map[string]string) int {
	o := orm.NewOrm()
	qs := o.QueryTable(MERCHANT_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}
	cnt, err := qs.CountWithCtx(ctx)
	if err != nil {
		logs.Error("get merchant len by map fail: ", err)
	}
	return int(cnt)
}

func GetMerchantListByMap(ctx context.Context, params map[string]string, displayCount, offset int) []MerchantInfo {
	o := orm.NewOrm()
	qs := o.QueryTable(MERCHANT_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}
	var merchantList []MerchantInfo
	_, err := qs.Limit(displayCount, offset).OrderBy("-update_time").AllWithCtx(ctx, &merchantList)
	if err != nil {
		logs.Error("get merchant list by map fail: ", err)
	}
	return merchantList
}

func GetAllMerchant(ctx context.Context) []MerchantInfo {
	o := orm.NewOrm()
	var merchantList []MerchantInfo

	_, err := o.QueryTable(MERCHANT_INFO).Limit(-1).AllWithCtx(ctx, &merchantList)
	if err != nil {
		logs.Error("get all merchant fail：", err)
	}

	return merchantList
}

func GetMerchantByParams(ctx context.Context, params map[string]string, displayCount, offset int) []MerchantInfo {
	o := orm.NewOrm()
	var merchantList []MerchantInfo
	qs := o.QueryTable(MERCHANT_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}

	qs.Limit(displayCount, offset).AllWithCtx(ctx, &merchantList)

	return merchantList
}

func GetMerchantLenByParams(ctx context.Context, params map[string]string) int {
	o := orm.NewOrm()
	qs := o.QueryTable(MERCHANT_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}

	cnt, err := qs.Limit(-1).CountWithCtx(ctx)

	if err != nil {
		logs.Error("get merchant len by params fail: ", err)
	}
	return int(cnt)
}

func GetMerchantByUid(ctx context.Context, merchantUid string) MerchantInfo {
	o := orm.NewOrm()
	var merchantInfo MerchantInfo
	_, err := o.QueryTable(MERCHANT_INFO).Filter("merchant_uid", merchantUid).Limit(1).AllWithCtx(ctx, &merchantInfo)
	if err != nil {
		logs.Error("get merchant info fail: ", err)
	}
	return merchantInfo
}

func UpdateMerchant(ctx context.Context, merchantInfo MerchantInfo) bool {
	o := orm.NewOrm()
	_, err := o.UpdateWithCtx(ctx, &merchantInfo)

	if err != nil {
		logs.Error("update merchant fail: ", err)
		return false
	}

	return true
}

func DeleteMerchantByUid(ctx context.Context, merchantUid string) bool {
	o := orm.NewOrm()
	_, err := o.QueryTable(MERCHANT_INFO).Filter("merchant_uid", merchantUid).DeleteWithCtx(ctx)
	if err != nil {
		logs.Error("delete merchant fail: ", err)
		return false
	}
	return true
}
