dorisbot/models/public/command.go

136 lines
3.3 KiB
Go

package public
import (
"dorisbot/models"
"errors"
"time"
)
// CommandsModel -
type CommandModel struct {
Key string `db:"key"`
Platform string `db:"platform"`
Binding string `db:"binding"`
RequireManage bool `db:"require_manage"`
RequireInit bool `db:"require_init"`
Value string `db:"value"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewCommand -
func NewCommand(key, value, platform, binding string, requireManage, requireInit bool) (c *CommandModel, err error) {
if len(key) == 0 || len(value) == 0 {
return nil, errors.New("key, server or value is empty")
}
c = &CommandModel{
Key: key,
Value: value,
Platform: platform,
Binding: binding,
RequireInit: requireInit,
RequireManage: requireManage,
}
return
}
// GetCommandListByPlatformBinding -
func GetCommandListByPlatformBinding(binding, platform string) (list []*CommandModel, err error) {
if len(binding) == 0 || len(platform) == 0 {
return nil, errors.New("server id or platform is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"binding" = $1
and "platform" = $2`
err = db.Select(&list, query, binding, platform)
if err != nil {
return nil, err
}
return
}
// GetCommandByPlatformBinding -
func GetCommandByPlatformBinding(key, binding, platform string) (list []*CommandModel, err error) {
if len(key) == 0 || len(binding) == 0 || len(platform) == 0 {
return nil, errors.New("key, server id or platform is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"key" = $1
and (
("binding" = $2 and "platform" = $3)
or ("binding" = '' and "platform" = '')
)
order by "binding" desc`
err = db.Select(&list, query, key, binding, platform)
if err != nil {
return nil, err
}
return
}
// GetCommandByKey -
func GetCommandByKey(key string) (list []*CommandModel, err error) {
if len(key) == 0 {
return nil, errors.New("key is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"key" = $1`
err = db.Select(&list, query, key)
if err != nil {
return nil, err
}
return
}
// GetGlobalCommand -
func GetGlobalCommand(key string) (list []*CommandModel, err error) {
if len(key) == 0 {
return nil, errors.New("key is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"key" = $1
and "platform" = ''
and "binding" = ''`
err = db.Select(&list, query, key)
if err != nil {
return nil, err
}
return
}
// Write -
func (p *CommandModel) Write() (err error) {
db := models.GetConn()
query := `insert into "command" ("key", "paltform", "binding", "value", "require_init", "require_manage") values
(:key, :platform, :binding, :value, :require_init, :require_manage) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
defer row.Close()
if row.Next() {
err = row.StructScan(p)
if err != nil {
return err
}
}
return
}