2018-09-12 10:07:49 +00:00
|
|
|
package model
|
|
|
|
|
2018-09-21 13:37:33 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
2018-09-12 10:07:49 +00:00
|
|
|
|
|
|
|
// LineMessageLog -
|
|
|
|
type LineMessageLog struct {
|
|
|
|
ID string `db:"id" cc:"id"`
|
|
|
|
Group string `db:"group" cc:"group"`
|
|
|
|
User string `db:"user" cc:"user"`
|
|
|
|
Message string `db:"message" cc:"message"`
|
2018-10-01 16:11:32 +00:00
|
|
|
Type string `db:"type" cc:"type"`
|
2018-09-12 10:07:49 +00:00
|
|
|
Ctime time.Time `db:"ctime" cc:"ctime"`
|
|
|
|
Mtime time.Time `db:"mtime" cc:"mtime"`
|
|
|
|
}
|
2018-09-19 16:17:00 +00:00
|
|
|
|
2018-09-21 13:37:33 +00:00
|
|
|
// LineMessageLogWithUG -
|
|
|
|
type LineMessageLogWithUG struct {
|
2018-12-13 07:30:36 +00:00
|
|
|
LineMessageLog `cc:"-,<<"`
|
|
|
|
GroupName string `db:"group_name" cc:"group_name"`
|
|
|
|
UserName string `db:"user_name" cc:"user_name"`
|
2018-09-21 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 16:17:00 +00:00
|
|
|
// AddLineMessageLog -
|
2018-10-01 16:11:32 +00:00
|
|
|
func AddLineMessageLog(g, u, msg, t string) (msglog *LineMessageLog, err error) {
|
|
|
|
query := `insert into "public"."line_message_log" ("group", "user", "message", "type") values ($1, $2, $3, $4)`
|
2018-09-20 17:14:08 +00:00
|
|
|
msglog = &LineMessageLog{}
|
2018-10-01 16:11:32 +00:00
|
|
|
err = x.Get(msglog, query, g, u, msg, t)
|
2018-09-19 16:17:00 +00:00
|
|
|
return
|
|
|
|
}
|
2018-09-21 13:37:33 +00:00
|
|
|
|
|
|
|
// GetLineMessageLogCount -
|
2018-10-15 08:36:06 +00:00
|
|
|
func GetLineMessageLogCount(where ...interface{}) (c int, err error) {
|
|
|
|
query := `select count(*) as c from "public"."line_message_log"`
|
2018-10-15 08:46:00 +00:00
|
|
|
values := make([]interface{}, 0)
|
2018-10-15 08:36:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 08:46:00 +00:00
|
|
|
err = x.Get(&c, query, values...)
|
2018-09-21 13:37:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLineMessageLogList -
|
2018-12-05 12:35:24 +00:00
|
|
|
func GetLineMessageLogList(g, u string, offset, limit int, orderArg string) (logs []*LineMessageLogWithUG, err error) {
|
|
|
|
if orderArg != "asc" && orderArg != "desc" {
|
|
|
|
orderArg = "desc"
|
|
|
|
}
|
2018-09-21 13:37:33 +00:00
|
|
|
params := struct {
|
|
|
|
Group string `db:"group"`
|
|
|
|
User string `db:"user"`
|
|
|
|
}{}
|
|
|
|
query := `select m.*, g.name as group_name, u.name as user_name from "public"."line_message_log" m
|
|
|
|
left join "public"."line_user" u
|
|
|
|
on u.id = m.user
|
|
|
|
left join "public"."line_group" g
|
|
|
|
on g.id = m.group
|
|
|
|
`
|
|
|
|
where := ""
|
|
|
|
if len(g) > 0 {
|
|
|
|
where = ` where g.id = :group`
|
|
|
|
params.Group = g
|
|
|
|
}
|
|
|
|
if len(u) > 0 {
|
|
|
|
if len(where) > 0 {
|
|
|
|
where += ` and u.id = :user`
|
|
|
|
} else {
|
|
|
|
where += ` where u.id = :user`
|
|
|
|
}
|
|
|
|
params.User = u
|
|
|
|
}
|
2018-12-05 12:35:24 +00:00
|
|
|
order := fmt.Sprintf(`order by m.ctime %s`, orderArg)
|
2018-09-21 13:37:33 +00:00
|
|
|
pager := fmt.Sprintf("offset %d limit %d", offset, limit)
|
|
|
|
|
|
|
|
stmt, err := x.PrepareNamed(fmt.Sprintf("%s %s %s %s", query, where, order, pager))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stmt.Select(&logs, params)
|
|
|
|
return
|
|
|
|
}
|