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 {
|
|
|
|
LineMessageLog
|
|
|
|
GroupName string `db:"group_name" cc:"group_name"`
|
|
|
|
UserName string `db:"user_name" cc:"user_name"`
|
|
|
|
}
|
|
|
|
|
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 -
|
|
|
|
func GetLineMessageLogCount() (c int, err error) {
|
|
|
|
err = x.Get(&c, `select count(*) as c from "public"."line_message_log"`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLineMessageLogList -
|
|
|
|
func GetLineMessageLogList(g, u string, offset, limit int) (logs []*LineMessageLogWithUG, err error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
order := `order by m.ctime desc`
|
|
|
|
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
|
|
|
|
}
|