package common

import (
	"dbweb/core"
	"dbweb/lib/bill"
	"strings"
)

//该单据适用于任意的表，第一个主键值被识别成表名
type GenericBill struct{}

func init() {
	core.RegisterBill("generic", new(GenericBill), "")
}
func (g *GenericBill) Bill(p *core.ElementHandleArgs) *bill.Bill {
	q := p.Req.URL.Query()
	dbName := p.User.DecodeQueryValue(q.Get("db"))
	names := strings.Split(p.User.DecodeQueryValue(q.Get("tables")), ",")
	b, err := bill.OpenBill(core.LoadOuterDB(p.DB, dbName), names[0], names[1:]...)
	if err != nil {
		core.LOG.Panic(err)
	}
	return b
}
func (g *GenericBill) DBName(p *core.ElementHandleArgs) string {
	q := p.Req.URL.Query()
	return p.User.DecodeQueryValue(q.Get("db"))
}
