friend.go
11.2 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package models
import (
db "client/friendSystem/database"
"errors"
"log"
"math/rand"
"strconv"
"time"
)
type FriendRelationship struct {
RelationshipId int
FriendRelationshipAId string
FriendRelationshipBId string
RelationshipValue int
}
type FriendNameList struct {
FriendId string
FriendName string
FriendHeadPortraitUrl string
}
type RandomFriendInfo struct {
Guid string
Username string
AvatorUrl string
}
type FriendAddRespones struct {
Guid string
Username string
AvatorUrl string
RelationshipValue int
}
type FriendRanklistResult struct {
RanklistUserId string
RanklistUserName string
RanklistHeadPortraitUrl string
RanklistScore float64
RanklistIndex int
RanklistUserValueCount map[string]string
}
type FriendRanklistRequest struct {
RanklistGameId string
RanklistName string
RanklistUserId string
RanklistLength int
RanklistFieldIndex int
}
/*发送好友请求*/
func (f *FriendRelationship) AddFriend(addType string) (result interface{}, err error) {
if err = CheckIdExist(f.FriendRelationshipAId); err != nil {
return nil, err
}
if err = CheckIdExist(f.FriendRelationshipBId); err != nil {
return nil, err
}
friendrelationship := make([]FriendRelationship, 0)
sql := ` select relationship_id,relationship_value from ice_friend_relationship where (friend_relationship_a_id=? and friend_relationship_b_id=?) or (friend_relationship_b_id=? and friend_relationship_a_id =?) `
if err = db.Engine.Sql(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.FriendRelationshipAId, f.FriendRelationshipBId).Find(&friendrelationship); err != nil {
return nil, err
}
if len(friendrelationship) != 0 {
// if friendrelationship[0].RelationshipValue == 0 {
// err = errors.New("已向该玩家发送请求")
// } else if friendrelationship[0].RelationshipValue == 1 {
// err = errors.New("该玩家已经是你的好友")
// } else {
// }
// return nil, err
switch friendrelationship[0].RelationshipValue {
case 0:
err = errors.New("已向该玩家发送请求")
break
case 1:
err = errors.New("该玩家已经是你的好友")
break
case 2:
log.Println("该玩家已经拒绝过你的请求")
sql = ` delete from ice_friend_relationship where relationship_id = ? `
_, err = db.Engine.Exec(sql, friendrelationship[0].RelationshipId)
break
case 3:
err = errors.New("该玩家已经拒绝过你的请求")
break
default:
err = errors.New("未知的好友关系类型")
break
}
}
if err != nil {
return nil, err
}
var res string
switch addType {
case "addById":
f.RelationshipValue = 0
res = "成功发送请求"
case "addByQrcode":
f.RelationshipValue = 1
res = "成功添加好友"
default:
err = errors.New("addtype not exists")
return nil, err
}
friendrelationship = make([]FriendRelationship, 0)
sql = ` insert into ice_friend_relationship (friend_relationship_a_id,friend_relationship_b_id,relationship_value) values(?,?,?) `
err = db.Engine.Sql(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.RelationshipValue).Find(&friendrelationship)
if err != nil {
return nil, err
}
return res, err
}
/*响应好友请求*/
func (f *FriendRelationship) RespondFriendRequest(respondResult string) (result interface{}, err error) {
if err = CheckIdExist(f.FriendRelationshipAId); err != nil {
return nil, err
}
if err = CheckIdExist(f.FriendRelationshipBId); err != nil {
return nil, err
}
friendRelationship := make([]FriendRelationship, 0)
sql := ` select relationship_value from ice_friend_relationship where friend_relationship_a_id=? and friend_relationship_b_id = ? limit 1 `
err = db.Engine.Sql(sql, f.FriendRelationshipAId, f.FriendRelationshipBId).Find(&friendRelationship)
if len(friendRelationship) == 0 {
err = errors.New("不存在的好友申请")
return nil, err
}
if friendRelationship[0].RelationshipValue == 1 {
err = errors.New("已存在的好友关系")
return nil, err
}
switch respondResult {
case "accept":
sql = `update ice_friend_relationship set relationship_value = 1 where friend_relationship_a_id=?`
break
case "refuse":
sql = `update ice_friend_relationship set relationship_value = 2 where friend_relationship_a_id=?`
default:
err = errors.New("好友申请响应类型错误")
return nil, err
}
_, err = db.Engine.Exec(sql, f.FriendRelationshipAId)
if err != nil {
return nil, err
}
return
}
/*删除好友*/
func (f *FriendRelationship) DeleteFriend() (result string, err error) {
sql := ` select * from ice_friend_relationship where (friend_relationship_a_id =? and friend_relationship_b_id=?) or (friend_relationship_b_id =? and friend_relationship_a_id = ?) and relationship_value = 2 or relationship_value = 1 `
r, err := db.Engine.Query(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.FriendRelationshipAId, f.FriendRelationshipBId)
if err != nil {
return
}
if len(r) == 0 {
err = errors.New("不存在的好友关系")
return
}
sql = ` delete from ice_friend_relationship where (friend_relationship_a_id =? and friend_relationship_b_id=?) or (friend_relationship_b_id =? and friend_relationship_a_id = ?)`
_, err = db.Engine.Exec(sql, f.FriendRelationshipAId, f.FriendRelationshipBId, f.FriendRelationshipAId, f.FriendRelationshipBId)
if err != nil {
return
}
return "删除好友成功", err
}
/*查询是否有新的好友请求*/
func (f *FriendRelationship) CheckFriendRequest() (result interface{}, err error) {
if err = CheckIdExist(f.FriendRelationshipBId); err != nil {
return nil, err
}
sql := `SELECT guid, username, avator_url FROM users INNER JOIN ice_friend_relationship ON guid = friend_relationship_a_id WHERE friend_relationship_b_id =? and relationship_value =0`
friendInfo := make([]RandomFriendInfo, 0)
err = db.Engine.Sql(sql, f.FriendRelationshipBId).Find(&friendInfo)
if err != nil {
return nil, err
}
return friendInfo, err
}
/*获取二维码图片url*/
func GetQrCodeImg(uuid string) (result string, err error) {
if err = SetQrCodeImage(uuid); err != nil {
return
}
result = "http://iceplay.oss-cn-beijing.aliyuncs.com/userInfo/Qrcode/" + MD5(uuid)
return
}
/*获得随机推荐好友*/
func GetRandomFriend(randomType string, randomLength int) (result interface{}, err error) {
switch randomType {
case "realrandom":
randomFriendInfo := make([]RandomFriendInfo, 0)
sql := `select count(*) from users `
r, err := db.Engine.Query(sql)
if err != nil {
return nil, err
}
count, err := strconv.ParseInt(string(r[0]["count(*)"]), 10, 64)
if err != nil {
return nil, err
}
sql = ` select id from users order by id limit 1 `
r, err = db.Engine.Query(sql)
if err != nil {
return nil, err
}
start, err := strconv.ParseInt(string(r[0]["id"]), 10, 64)
newRand := rand.New(rand.NewSource(time.Now().UnixNano()))
sqlin := " where id in("
for i := 0; i < randomLength; i++ {
randUserId := strconv.FormatInt(newRand.Int63n(count)+start, 10)
sqlin = sqlin + randUserId + ","
}
sqlin = sqlin[:len(sqlin)-1]
sqlin = sqlin + ") "
sql = `select guid,username,avator_url from users` + sqlin
log.Println(sql)
err = db.Engine.Sql(sql).Find(&randomFriendInfo)
if err != nil {
return nil, err
}
return randomFriendInfo, err
default:
err = errors.New("随机类型不存在")
return nil, err
}
}
/*获取好友列表*/
func GetFriendNameList(UserId string) (result interface{}, err error) {
if err = CheckIdExist(UserId); err != nil {
return nil, err
}
friendNameList := make([]FriendNameList, 0)
sql := ` SELECT b.USERNAME AS friend_name,b.GUID AS friend_id ,b.avator_url as friend_head_portrait_url FROM ice_friend_relationship a,users b WHERE(a.friend_relationship_a_id = ? AND a.friend_relationship_b_id = b.GUID and a.relationship_value = 1) OR (a.friend_relationship_b_id = ? AND a.friend_relationship_a_id = b.GUID and a.relationship_value = 1) `
err = db.Engine.Sql(sql, UserId, UserId).Find(&friendNameList)
if err != nil {
return nil, err
}
return friendNameList, err
}
/*获取好友排行榜*/
func (f *FriendRanklistRequest) GetFriendRanklist() (result interface{}, err error) {
/*老版本获取好友排行榜
// tableName, err := GetTableName(f.RanklistGameId) //通过游戏id获取排行榜表名
tableName := "ice_tablename_" + f.RanklistGameId
log.Println(tableName)
fieldName, err := GetTableFieldName(tableName, f.RanklistFieldIndex) //通过索引位置获取要排序的字段
if err != nil {
return nil, err
}
friendList, err := GetFriendList(f.RanklistUserId) //通过用户uuid获取用户好友列表
if err != nil {
return nil, err
}
sql := `select ranklist_user_id,ranklist_user_name,ranklist_head_portrait_url,` + fieldName + ` from ` + tableName + ` where ranklist_user_id ` + friendList + " order by " + fieldName + " desc"
log.Println(sql)
log.Println(fieldName)
r, err := db.Engine.Query(sql)
friendRanklistResult := make([]FriendRanklistResult, len(r))
for k, v := range r {
friendRanklistResult[k].RanklistUserId = string(v["ranklist_user_id"])
friendRanklistResult[k].RanklistUserName = string(v["ranklist_user_name"])
friendRanklistResult[k].RanklistHeadPortraitUrl = string(v["ranklist_head_portrait_url"])
friendRanklistResult[k].RanklistScore, err = strconv.ParseFloat(string(v[fieldName]), 64)
}
if err != nil {
return nil, err
}
return friendRanklistResult, err
*/
//关联陈成排行榜
friendList, err := GetFriendList(f.RanklistUserId) //通过用户uuid获取用户好友列表
if err != nil {
return nil, err
}
log.Println(friendList)
log.Println(f)
sql := ` select users.guid as ranklist_user_id,users.username as ranklist_user_name,users.avator_url as ranklist_head_portrait_url,mykey from ice_gametop,users where guid ` + friendList + ` and GTID=? and GID=? and ice_gametop.uid=users.guid order by cast(mykey as signed) desc`
log.Println(sql, f.RanklistName, f.RanklistGameId)
r, err := db.Engine.Query(sql, f.RanklistName, f.RanklistGameId)
friendRanklistResult := make([]FriendRanklistResult, len(r))
for k, v := range r {
friendRanklistResult[k].RanklistUserId = string(v["ranklist_user_id"])
friendRanklistResult[k].RanklistUserName = string(v["ranklist_user_name"])
friendRanklistResult[k].RanklistHeadPortraitUrl = string(v["ranklist_head_portrait_url"])
friendRanklistResult[k].RanklistScore, err = strconv.ParseFloat(string(v["mykey"]), 64)
friendRanklistResult[k].RanklistIndex = k + 1
}
if err != nil {
return nil, err
}
return friendRanklistResult, err
}
/*根据Gameid创建数据库表结构*/
func CreateDBTable(sql string) (result string, err error) {
_, err = db.Engine.Exec(sql)
if err != nil {
return
}
result = "success"
return
}
/*查看好友申请情况*/
func (f *FriendRelationship) CheckFriendRespones() (result interface{}, err error) {
if err = CheckIdExist(f.FriendRelationshipAId); err != nil {
return nil, err
}
friendAddRespones := make([]FriendAddRespones, 0)
sql := `SELECT guid, username, avator_url, relationship_value FROM users inner join ice_friend_relationship on guid = friend_relationship_b_id WHERE friend_relationship_a_id = ?`
err = db.Engine.Sql(sql, f.FriendRelationshipAId).Find(&friendAddRespones)
if err != nil {
return nil, err
}
return friendAddRespones, err
}