This commit is contained in:
Jay 2018-12-06 17:44:37 +08:00
parent a066ea63c1
commit 874ab24f6b
2 changed files with 103 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package model
import (
"errors"
"fmt"
"regexp"
"time"
)
@ -14,6 +16,12 @@ type Commands struct {
Mtime time.Time `db:"mtime" cc:"ctime"`
}
// CommandsWithGroup -
type CommandsWithGroup struct {
Commands
GroupName string `db:"group_name" cc:"group_name"`
}
// GetAllCommands -
func GetAllCommands() (cmds []*Commands, err error) {
err = x.Select(&cmds, `select * from "public"."commands"`)
@ -39,3 +47,77 @@ func GetGroupCommand(c, g string) (cmd *Commands, err error) {
}
return
}
// GetCommandCount -
func GetCommandCount(where ...interface{}) (c int, err error) {
query := `select count(*) as c from "public"."commands"`
values := make([]interface{}, 0)
if len(where) > 0 {
if whereMap, ok := where[0].(map[string]string); ok {
str := ""
idx := 1
for k, v := range whereMap {
if len(str) > 0 {
str += " and "
}
str += fmt.Sprintf(` "%s" = $%d `, k, idx)
idx++
values = append(values, v)
}
if len(str) > 0 {
query += ` where ` + str
}
}
}
err = x.Get(&c, query, values)
return
}
// GetCommands -
func GetCommands(where map[string]string, offset, limit int, order map[string]string) (cmds []*Commands, err error) {
query := `select c.*, (case when g.name is null then '' else g.name end) as group_name from "public"."commands"
left join "public"."line_group" g
on g.id = c.group `
values := make([]interface{}, (len(where) + len(order)))
idx := 1
if len(where) > 0 {
str := ""
for k, v := range where {
if len(str) > 0 {
str += " and "
}
str += fmt.Sprintf(` "%s" = $%d `, k, idx)
idx++
values = append(values, v)
}
if len(str) > 0 {
query += ` where ` + str
}
}
if offset >= 0 {
query += fmt.Sprintf(" offset %d ", offset)
}
if limit > 0 {
query += fmt.Sprintf(" limit %d ", limit)
}
if len(order) > 0 {
regex, err := regexp.Compile("(?i)(desc|asc)")
if err != nil {
return nil, err
}
str := ""
for k, v := range order {
if !regex.Match([]byte(v)) {
continue
}
if len(str) > 0 {
str += " , "
}
str += fmt.Sprintf(` "%s" %s `, k, v)
}
}
return
}

View File

@ -98,3 +98,24 @@ func GetLineMessageLog(c *context.Context) {
},
})
}
// GetCommands -
func GetCommandList(c *context.Context) {
numP := 1
if p, ok := c.GetQuery("p"); ok {
if i, err := strconv.Atoi(p); err == nil {
numP = i
}
}
numMax := 20
if max, ok := c.GetQuery("max"); ok {
if m, err := strconv.Atoi(max); err == nil {
numMax = m
}
}
g := c.DefaultQuery("group", "")
cmd := c.DefaultQuery("cmd", "")
}