@@ -2,6 +2,7 @@ package chatcount
2
2
3
3
import (
4
4
"os"
5
+ "sync"
5
6
"time"
6
7
7
8
"github.com/jinzhu/gorm"
@@ -14,8 +15,10 @@ const (
14
15
var (
15
16
// ctdb 聊天时长数据库全局变量
16
17
ctdb * chattimedb
17
- // 水群提醒时间提醒段,单位分钟
18
+ // levelArray 水群提醒时间提醒段,单位分钟
18
19
levelArray = [... ]int {15 , 30 , 60 , 120 , 240 }
20
+ // chatmu 读写添加锁
21
+ chatmu sync.Mutex
19
22
)
20
23
21
24
// chattimedb 聊天时长数据库结构体
@@ -61,8 +64,10 @@ func (ChatTime) TableName() string {
61
64
return "chat_time"
62
65
}
63
66
64
- // sleep 更新发言时间,todayTime的单位是分钟
67
+ // updateChatTime 更新发言时间,todayTime的单位是分钟
65
68
func (ctdb * chattimedb ) updateChatTime (gid , uid int64 ) (todayTime int64 , remindFlag bool ) {
69
+ chatmu .Lock ()
70
+ defer chatmu .Unlock ()
66
71
db := (* gorm .DB )(ctdb )
67
72
now := time .Now ()
68
73
st := ChatTime {
@@ -108,6 +113,8 @@ func (ctdb *chattimedb) updateChatTime(gid, uid int64) (todayTime int64, remindF
108
113
109
114
// getChatTime 获得用户聊天时长,todayTime,totalTime的单位是分钟
110
115
func (ctdb * chattimedb ) getChatTime (gid , uid int64 ) (todayTime int64 , totalTime int64 ) {
116
+ chatmu .Lock ()
117
+ defer chatmu .Unlock ()
111
118
db := (* gorm .DB )(ctdb )
112
119
st := ChatTime {}
113
120
db .Model (& ChatTime {}).Where ("group_id = ? and user_id = ?" , gid , uid ).First (& st )
@@ -118,6 +125,8 @@ func (ctdb *chattimedb) getChatTime(gid, uid int64) (todayTime int64, totalTime
118
125
119
126
// getChatRank 获得水群排名
120
127
func (ctdb * chattimedb ) getChatRank (gid int64 ) (chatTimeList []ChatTime ) {
128
+ chatmu .Lock ()
129
+ defer chatmu .Unlock ()
121
130
db := (* gorm .DB )(ctdb )
122
131
db .Model (& ChatTime {}).Where ("group_id = ?" , gid ).Order ("today_time DESC" ).Find (& chatTimeList )
123
132
return
0 commit comments