@@ -21,8 +21,8 @@ const (
21
21
var (
22
22
// ctdb 聊天时长数据库全局变量
23
23
ctdb * chattimedb
24
- // leveler 水群提醒时间提醒段,单位分钟
25
- leveler = NewLeveler ([] int { 60 , 120 , 180 , 240 , 300 } )
24
+ // l 水群提醒时间提醒段,单位分钟
25
+ l = newLeveler ( 60 , 120 , 180 , 240 , 300 )
26
26
)
27
27
28
28
// chattimedb 聊天时长数据库结构体
@@ -92,11 +92,14 @@ func (ctdb *chattimedb) updateChatTime(gid, uid int64) (remindTime int64, remind
92
92
ts , ok := ctdb .userTimestampMap .Load (keyword )
93
93
if ! ok {
94
94
ctdb .userTimestampMap .Store (keyword , now .Unix ())
95
+ ctdb .userTodayMessageMap .Store (keyword , 1 )
95
96
return
96
97
}
97
98
lastTime := time .Unix (ts , 0 )
98
99
todayTime , _ := ctdb .userTodayTimeMap .Load (keyword )
99
100
totayMessage , _ := ctdb .userTodayMessageMap .Load (keyword )
101
+ //这个消息数是必须统计的
102
+ ctdb .userTodayMessageMap .Store (keyword , totayMessage + 1 )
100
103
st := chatTime {
101
104
GroupID : gid ,
102
105
UserID : uid ,
@@ -127,9 +130,8 @@ func (ctdb *chattimedb) updateChatTime(gid, uid int64) (remindTime int64, remind
127
130
// 当聊天时间在一定范围内的话,则计入时长
128
131
if userChatTime < chatInterval {
129
132
ctdb .userTodayTimeMap .Store (keyword , todayTime + userChatTime )
130
- ctdb .userTodayMessageMap .Store (keyword , totayMessage + 1 )
131
133
remindTime = (todayTime + userChatTime ) / 60
132
- remindFlag = leveler . Level (int ((todayTime + userChatTime )/ 60 )) > leveler . Level (int (todayTime / 60 ))
134
+ remindFlag = l . level (int ((todayTime + userChatTime )/ 60 )) > l . level (int (todayTime / 60 ))
133
135
}
134
136
ctdb .userTimestampMap .Store (keyword , now .Unix ())
135
137
return
@@ -175,24 +177,24 @@ func (ctdb *chattimedb) getChatRank(gid int64) (chatTimeList []chatTime) {
175
177
TodayMessage : todayMessage ,
176
178
})
177
179
}
178
- sort .Sort (ByTotalTimeDescMessageDesc (chatTimeList ))
180
+ sort .Sort (sortChatTime (chatTimeList ))
179
181
return
180
182
}
181
183
182
- // Leveler 结构体,包含一个 levelArray 字段
183
- type Leveler struct {
184
+ // leveler 结构体,包含一个 levelArray 字段
185
+ type leveler struct {
184
186
levelArray []int
185
187
}
186
188
187
- // NewLeveler 构造函数,用于创建 Leveler 实例
188
- func NewLeveler (levels [] int ) * Leveler {
189
- return & Leveler {
189
+ // newLeveler 构造函数,用于创建 Leveler 实例
190
+ func newLeveler (levels ... int ) * leveler {
191
+ return & leveler {
190
192
levelArray : levels ,
191
193
}
192
194
}
193
195
194
- // Level 方法,封装了 getLevel 函数的逻辑
195
- func (l * Leveler ) Level (t int ) int {
196
+ // level 方法,封装了 getLevel 函数的逻辑
197
+ func (l * leveler ) level (t int ) int {
196
198
for i := len (l .levelArray ) - 1 ; i >= 0 ; i -- {
197
199
if t >= l .levelArray [i ] {
198
200
return i + 1
@@ -201,23 +203,23 @@ func (l *Leveler) Level(t int) int {
201
203
return 0
202
204
}
203
205
204
- // ByTotalTimeDescMessageDesc chatTime排序数组
205
- type ByTotalTimeDescMessageDesc []chatTime
206
+ // sortChatTime chatTime排序数组
207
+ type sortChatTime []chatTime
206
208
207
209
// Len 实现 sort.Interface
208
- func (a ByTotalTimeDescMessageDesc ) Len () int {
210
+ func (a sortChatTime ) Len () int {
209
211
return len (a )
210
212
}
211
213
212
214
// Less 实现 sort.Interface,按 TodayTime 降序,TodayMessage 降序
213
- func (a ByTotalTimeDescMessageDesc ) Less (i , j int ) bool {
215
+ func (a sortChatTime ) Less (i , j int ) bool {
214
216
if a [i ].TodayTime == a [j ].TodayTime {
215
217
return a [i ].TodayMessage > a [j ].TodayMessage
216
218
}
217
219
return a [i ].TodayTime > a [j ].TodayTime
218
220
}
219
221
220
222
// Swap 实现 sort.Interface
221
- func (a ByTotalTimeDescMessageDesc ) Swap (i , j int ) {
223
+ func (a sortChatTime ) Swap (i , j int ) {
222
224
a [i ], a [j ] = a [j ], a [i ]
223
225
}
0 commit comments