package accounts

import (
	"context"
	"time"

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

type AccountHistoryInfo struct {
	Id          int
	AccountUid  string
	AccountName string
	Type        string
	Amount      float64
	Balance     float64
	UpdateTime  time.Time
	CreateTime  time.Time
}

const ACCOUNT_HISTORY_INFO = "account_history_info"

func GetAccountHistoryLenByMap(ctx context.Context, params map[string]string) int {
	o := orm.NewOrm()
	qs := o.QueryTable(ACCOUNT_HISTORY_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 account history len by map fail: ", err)
	}
	return int(cnt)
}

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