/
base.go
146 lines (123 loc) · 3.07 KB
/
base.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
package mysql
import (
"time"
"github.com/astaxie/beego"
"github.com/i2eco/ecology/appgo/pkg/mus"
)
type period string
const (
PeriodDay period = "day"
PeriodWeek period = "week"
PeriodLastWeek period = "last-week"
PeriodMonth period = "month"
PeriodLastMoth period = "last-month"
PeriodAll period = "all"
PeriodYear period = "year"
)
const dateFormat = "20060102"
var cacheTime = beego.AppConfig.DefaultFloat("CacheTime", 60) // 1 分钟
func Init() {
initAPI()
initAdsCache()
//NewSign().UpdateSignRule() // 更新签到规则的全局变量
//NewReadRecord().UpdateReading Rule() // 更新阅读计时规则的全局变量
}
type SitemapDocs struct {
DocumentId int
DocumentName string
Identify string
BookId int
}
// 统计书籍分类
var counting = false
type Count struct {
Cnt int
CategoryId int
}
func CountCategory() {
if counting {
return
}
counting = true
defer func() {
counting = false
}()
var count []Count
sql := "select count(bc.id) cnt, bc.category_id from " + BookCategory{}.TableName() + " bc left join " + Book{}.TableName() + " b on b.book_id=bc.book_id where b.privately_owned=0 group by bc.category_id"
mus.Db.Raw(sql).Scan(&count)
if len(count) == 0 {
return
}
var cates []Category
mus.Db.Select("id,pid,cnt").Find(&cates)
if len(cates) == 0 {
return
}
var err error
db := mus.Db.Begin()
defer func() {
if err != nil {
db.Rollback()
} else {
db.Commit()
}
}()
db.Model(Category{}).Updates(Ups{"cnt": 0})
cateChild := make(map[int]int)
for _, item := range count {
if item.Cnt > 0 {
cateChild[item.CategoryId] = item.Cnt
err = db.Model(Category{}).Where("id=?", item.CategoryId).Updates(Ups{"cnt": item.Cnt}).Error
if err != nil {
return
}
}
}
}
func getTimeRange(t time.Time, prd period) (start, end string) {
switch prd {
case PeriodWeek:
start, end = getWeek(t)
case PeriodLastWeek:
start, end = getWeek(t.AddDate(0, 0, -7))
case PeriodMonth:
start, end = getMonth(t)
case PeriodLastMoth:
start, end = getMonth(t.AddDate(0, -1, 0))
case PeriodAll:
start = "20060102"
end = "20401231"
case PeriodDay:
start = t.Format(dateFormat)
end = start
case PeriodYear:
start, end = getYear(t.AddDate(-1, 0, 0))
default:
start = t.Format(dateFormat)
end = start
}
return
}
func getWeek(t time.Time) (start, end string) {
if t.Weekday() == 0 {
start = t.Add(-7 * 24 * time.Hour).Format(dateFormat)
end = t.Format(dateFormat)
} else {
s := t.Add(-time.Duration(t.Weekday()-1) * 24 * time.Hour)
start = s.Format(dateFormat)
end = s.Add(6 * 24 * time.Hour).Format(dateFormat)
}
return
}
func getYear(t time.Time) (start, end string) {
month := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.Local)
start = month.Format(dateFormat)
end = month.AddDate(0, 12, 0).Add(-24 * time.Hour).Format(dateFormat)
return
}
func getMonth(t time.Time) (start, end string) {
month := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
start = month.Format(dateFormat)
end = month.AddDate(0, 1, 0).Add(-24 * time.Hour).Format(dateFormat)
return
}