package notify

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

type NotifyInfo struct {
	Id              int
	Type            string //订单-order，代付-payfor
	BankOrderId     string
	MerchantOrderId string
	Status          string
	Times           int
	Url             string
	Response        string
	UpdateTime      time.Time
	CreateTime      time.Time
}

const NOTIFYINFO = "notify_info"

func GetNotifyInfoByBankOrderId(bankOrderId string) NotifyInfo {
	o := orm.NewOrm()
	var notifyInfo NotifyInfo
	_, err := o.QueryTable(NOTIFYINFO).Filter("bank_order_id", bankOrderId).OrderBy("-create_time").All(&notifyInfo)
	if err != nil {
		logs.Error("get notify info by bankOrderId fail: ", err)
	}
	return notifyInfo
}

func GetNotifyBankOrderIdListByParams(params map[string]string) []string {
	o := orm.NewOrm()
	qs := o.QueryTable(NOTIFYINFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}
	var notifyList []NotifyInfo
	qs.Limit(-1).All(&notifyList)
	var list []string
	for _, n := range notifyList {
		list = append(list, n.BankOrderId)
	}

	return list
}
