/
data_block.go
70 lines (63 loc) · 2.28 KB
/
data_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package database
import (
"fmt"
"time"
"github.com/2mf8/QQBotOffical/config"
"github.com/2mf8/QQBotOffical/public"
_ "github.com/denisenkom/go-mssqldb"
"github.com/tencent-connect/botgo/log"
_ "gopkg.in/guregu/null.v3/zero"
)
type PBlock struct {
Id int `json:"id"`
GuildId string `json:"guild_id"`
UserId string `json:"user_id"`
IsPBlock bool `json:"ispblock"`
AdminId string `json:"admin_id"`
GmtModified time.Time `json:"gmt_modified"`
}
func PBlockGet(guildId, userId string) (p PBlock, err error) {
pblock := PBlock{}
statment := fmt.Sprintf("select ID, guild_id, user_id, admin_id, gmt_modified, ispblock from [%s].[dbo].[guild_pblock] where user_id = $1 and ispblock = $2 and guild_id = $3", config.Conf.DatabaseName)
err = Db.QueryRow(statment, userId, true, guildId).Scan(&pblock.Id, &pblock.GuildId, &pblock.UserId, &pblock.AdminId, &pblock.GmtModified, &pblock.IsPBlock)
info := fmt.Sprintf("%s", err)
if public.StartsWith(info, "sql") || public.StartsWith(info, "unable") {
if public.StartsWith(info, "unable") {
log.Warn(info)
}
return
}
return
}
func (pBlock *PBlock) PBlockCreate() (err error) {
statement := fmt.Sprintf("insert into [%s].[dbo].[guild_pblock] values ($1, $2, $3, $4, $5) select @@identity", config.Conf.DatabaseName)
stmt, err := Db.Prepare(statement)
if err != nil {
return
}
defer stmt.Close()
err = stmt.QueryRow(pBlock.GuildId, pBlock.UserId, pBlock.AdminId, pBlock.GmtModified, pBlock.IsPBlock).Scan(&pBlock.Id)
return
}
func (pBlock *PBlock) PBlockUpdate() (err error) {
statment := fmt.Sprintf("update [%s].[dbo].[guild_pblock] set guild_id = $2, user_id = $3, ispblock = $4, admin_id = $5, gmt_modified = $6 where ID = $1", config.Conf.DatabaseName)
_, err = Db.Exec(statment, pBlock.Id, pBlock.GuildId, pBlock.UserId, pBlock.IsPBlock, pBlock.AdminId, pBlock.GmtModified)
return
}
func PBlockSave(guildId, userId, adminId string, ispblock bool, gmtModified time.Time) (err error) {
pblock := PBlock{
GuildId: guildId,
UserId: userId,
IsPBlock: ispblock,
AdminId: adminId,
GmtModified: gmtModified,
}
pblock_get, err := PBlockGet(guildId, userId)
if err != nil {
pblock.PBlockCreate()
return
}
pblock_get.IsPBlock = ispblock
pblock_get.PBlockUpdate()
return
}