-
Notifications
You must be signed in to change notification settings - Fork 347
/
consume.go
55 lines (42 loc) · 1.05 KB
/
consume.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
package db
import (
"github.com/lonng/nanoserver/db/model"
"github.com/lonng/nanoserver/protocol"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)
func InsertConsume(entity *model.CardConsume) error {
_, err := database.Insert(entity)
if err != nil {
log.Error(err)
}
return err
}
//消耗统计
func ConsumeStats(from, to int64) ([]*protocol.CardConsume, error) {
fn := func(from, to int64) *protocol.CardConsume {
mQuery, err := database.Query("SELECT SUM(card_count) AS cards FROM card_consume WHERE consume_at BETWEEN ? AND ?; ",
from,
to)
cc := &protocol.CardConsume{
Date: from,
}
if len(mQuery) < 1 || err != nil {
return cc
}
temp := string(mQuery[0]["cards"])
if temp != "" {
cc.Value, err = strconv.ParseInt(temp, 10, 0)
}
return cc
}
begin := time.Unix(from, 0)
var ret []*protocol.CardConsume
t := time.Date(begin.Year(), begin.Month(), begin.Day(), 0, 0, 0, 0, time.Local)
for i := t.Unix(); i < to; i += dayInSecond {
cc := fn(i, i+dayInSecond-1)
ret = append(ret, cc)
}
return ret, nil
}