-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
176 lines (135 loc) · 3.37 KB
/
history.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package db
import (
"fmt"
"strconv"
"strings"
"github.com/juju/errors"
//"github.com/zssky/log"
)
type Lottery struct {
ID int `json:"id"`
Expect string `json:"expect"`
Red string `json:"red"`
Blue int `json:"blue"`
OpenTime string `json:"opentime"`
OpenTimestamp string `json:"opentimestamp"`
}
func (s *Sqlite3) GetAllHistory(query map[string]string, limit int) ([]Lottery, error) {
sql := "SELECT * FROM history"
var where string
for key, value := range query {
where += fmt.Sprintf("%v=%v AND", key, value)
}
if strings.HasSuffix(where, "AND") {
where = where[:len(where)-len("AND")]
}
if where != "" {
sql += " WHERE " + where
sql += " ORDER BY expect DESC "
}
if limit > 0 {
sql += fmt.Sprintf("LIMI %v", limit)
}
//log.Debugf("sql:%v", sql)
rows, err := s.Query(sql)
if err != nil {
return nil, errors.Trace(err)
}
list := make([]Lottery, 0)
defer rows.Close()
for rows.Next() {
var l Lottery
if err := rows.Scan(&l.ID, &l.Expect, &l.Red, &l.Blue, &l.OpenTime, &l.OpenTimestamp); err != nil {
return nil, errors.Trace(err)
}
list = append(list, l)
}
return list, nil
}
func (s *Sqlite3) GetRedList(where string, limit int) ([][]int, error) {
sql := "SELECT red FROM history"
if where != "" {
sql += " WHERE " + where
}
if limit > 0 {
sql += fmt.Sprintf(" ORDER BY expect DESC LIMIT %v", limit)
}
//log.Debugf("sql:%v", sql)
rows, err := s.Query(sql)
if err != nil {
return nil, errors.Trace(err)
}
list := make([][]int, 0)
defer rows.Close()
for rows.Next() {
var r string
var l []int
if err := rows.Scan(&r); err != nil {
return nil, errors.Trace(err)
}
for _, s := range strings.Split(r, ",") {
num, _ := strconv.Atoi(s)
l = append(l, num)
}
list = append(list, l)
}
return list, nil
}
func (s *Sqlite3) GetBlueList(where string, limit int) ([]int, error) {
sql := "SELECT blue FROM history"
if where != "" {
sql += " WHERE " + where
}
if limit > 0 {
sql += fmt.Sprintf(" ORDER BY expect DESC LIMIT %v", limit)
}
//log.Debugf("sql:%v", sql)
rows, err := s.Query(sql)
if err != nil {
return nil, errors.Trace(err)
}
list := make([]int, 0)
defer rows.Close()
for rows.Next() {
var b int
if err := rows.Scan(&b); err != nil {
return nil, errors.Trace(err)
}
list = append(list, b)
}
return list, nil
}
func (s *Sqlite3) AddHistory(value Lottery) error {
sql := "INSERT INTO history(expect, red, blue, opentime, opentimestamp) VALUES(?, ?, ?, ?, ?)"
_, err := s.Exec(sql, value.Expect, value.Red, value.Blue, value.OpenTime, value.OpenTimestamp)
if err != nil {
return errors.Trace(err)
}
return nil
}
func (s *Sqlite3) BatchAddHistory(list []Lottery) error {
tx, err := s.Begin()
if err != nil {
tx.Rollback()
return err
}
sql := "INSERT INTO history(expect, red, blue, opentime, opentimestamp) VALUES(?, ?, ?, ?, ?)"
for _, item := range list {
if _, err := tx.Exec(sql, item.Expect, item.Red, item.Blue, item.OpenTime, item.OpenTimestamp); err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
}
func (s *Sqlite3) RemoveAllHistory() error {
sql := "DELETE FROM history"
if _, err := s.Exec(sql); err != nil {
return errors.Trace(err)
}
sql = "update sqlite_sequence set seq=0 where name='history'"
if _, err := s.Exec(sql); err != nil {
return errors.Trace(err)
}
return nil
}