mtfosbot/router/api/line.go

101 lines
1.8 KiB
Go
Raw Normal View History

2018-10-14 13:16:58 +00:00
package api
import (
2018-10-15 08:39:06 +00:00
"fmt"
2018-10-14 13:16:58 +00:00
"strconv"
"git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/module/utils"
)
// GetLineList -
func GetLineList(c *context.Context) {
ls, err := model.GetLineGroupList()
if err != nil {
c.ServerError(nil)
return
}
list := make([]map[string]interface{}, 0)
if ls != nil {
2018-10-14 13:26:18 +00:00
for _, v := range ls {
if v == nil {
continue
}
2018-10-14 13:16:58 +00:00
list = append(list, utils.ToMap(v))
}
}
c.Success(map[string]interface{}{
"list": list,
})
}
// GetLineMessageLog -
func GetLineMessageLog(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", "")
u := c.DefaultQuery("user", "")
2018-12-05 12:35:24 +00:00
order := c.DefaultQuery("order", "desc")
if order != "asc" && order != "desc" {
order = "desc"
}
2018-10-14 13:16:58 +00:00
2018-10-15 08:36:06 +00:00
where := make(map[string]string)
if len(g) > 0 {
where["group"] = g
}
if len(u) > 0 {
where["user"] = u
}
count, err := model.GetLineMessageLogCount(where)
2018-10-14 13:16:58 +00:00
if err != nil {
2018-10-15 08:39:06 +00:00
fmt.Println("count error :::: ", err)
2018-10-14 13:16:58 +00:00
c.ServerError(nil)
return
}
page := utils.CalcPage(count, numP, numMax)
2018-12-05 12:35:24 +00:00
if numP == -1 {
numP = page.Total
}
page = utils.CalcPage(count, numP, numMax)
2018-10-14 13:16:58 +00:00
2018-12-05 12:35:24 +00:00
logs, err := model.GetLineMessageLogList(g, u, page.Offset, page.Limit, order)
2018-10-14 13:16:58 +00:00
if err != nil {
c.ServerError(nil)
return
}
resMap := make([]map[string]interface{}, 0)
for _, v := range logs {
m := utils.ToMap(v.LineMessageLog)
m["group_name"] = v.GroupName
m["user_name"] = v.UserName
resMap = append(resMap, m)
}
c.Success(map[string]interface{}{
"list": resMap,
"page": map[string]interface{}{
"cur": page.Page,
"total": page.Total,
},
})
}