Use Upsert

This commit is contained in:
殷勇 2023-08-24 11:50:40 +08:00
parent 73339156e4
commit ba703c871b
3 changed files with 80 additions and 48 deletions

View File

@ -16,7 +16,9 @@ func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string,
{"receiver_account_id", account2Id},
{"is_friendship", isFriendship},
}
updateKv := insertKv
updateKv := [][]string{
{"is_friendship", isFriendship},
}
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_friend_pending_request",
@ -38,13 +40,14 @@ func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isD
{"account1_id", account1Id},
{"account2_id", account2Id},
}
fields := [][]string{
insertKv := [][]string{
{"account1_id", account1Id},
{"account2_id", account2Id},
{"is_delete_friendship", q5.ToString(isDeleteFriendship)},
}
insertKv := fields
updateKv := fields
updateKv := [][]string{
{"is_delete_friendship", q5.ToString(isDeleteFriendship)},
}
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_friend_ships",
@ -90,7 +93,9 @@ func (fm *FriendsMgr) upsertBlacklist(account1Id string, account2Id string, isRe
{"blocked_account_id", account2Id},
{"is_removed", q5.ToString(isRemoved)},
}
updateKv := insertKv
updateKv := [][]string{
{"is_removed", q5.ToString(isRemoved)},
}
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_friend_blacklist",

View File

@ -159,19 +159,33 @@ func (gm *GuildMgr) updateGuild(g *Guild, fields [][]string) {
)
}
// insertPendingReqs 添加申请加入公会记录
func (gm *GuildMgr) insertPendingReqs(guildId int64, applicantAccountId string) {
fields := [][]string{
// insertPendingReqs 添加申请加入公会记录, 存在则更新记录
func (gm *GuildMgr) upsertPendingReqs(guildId int64, accountId string, isJoinGuild string) {
where := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", applicantAccountId},
{"account_id", accountId},
}
insertKv := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", accountId},
{"is_join_guild", "0"},
}
f5.GetJsStyleDb().Replace(
updateKv := [][]string{
{"is_join_guild", isJoinGuild},
}
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_guild_pending_request",
fields,
func(error, int64, int64) {},
)
where,
updateKv,
insertKv,
func(err error, lastInsertId int64, rowsAffected int64) {
if err != nil {
fmt.Printf("error:%v\n", err)
}
fmt.Printf("lastInsertId:%d\n", lastInsertId)
fmt.Printf("rowsAffected:%d\n", rowsAffected)
})
}
// updatePendingReqs 更新所有申请加入公会记录
@ -198,13 +212,13 @@ func (gm *GuildMgr) updateAllPendingReqs(guildId int64, isJoinGuild int) {
}
// updatePendingReqs 更新申请加入公会记录
func (gm *GuildMgr) updatePendingReqs(guildId int64, applicantAccountId string, isJoinGuild int) {
func (gm *GuildMgr) updatePendingReqs(guildId int64, accountId string, isJoinGuild string) {
fields := [][]string{
{"is_join_guild", q5.ToString(isJoinGuild)},
{"is_join_guild", isJoinGuild},
}
where := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", applicantAccountId},
{"account_id", accountId},
}
f5.GetJsStyleDb().Update(
FRIEND_DB,
@ -222,17 +236,24 @@ func (gm *GuildMgr) updatePendingReqs(guildId int64, applicantAccountId string,
}
// insertPendingReqs 添加公会成员
func (gm *GuildMgr) insertGuildMember(guildId int64, member *GuildMember) {
fields := [][]string{
func (gm *GuildMgr) upsertGuildMember(guildId int64, member *GuildMember) {
where := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", member.AccountId},
}
insertKv := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", member.AccountId},
{"level", q5.ToString(member.Level)},
{"is_leave_guild", "0"},
}
f5.GetJsStyleDb().Replace(
updateKv := insertKv
f5.GetJsStyleDb().Upsert(
FRIEND_DB,
"t_guild_members",
fields,
where,
updateKv,
insertKv,
func(err error, lastInsertId int64, rowsAffected int64) {
if err != nil {
fmt.Printf("error:%v\n", err)
@ -291,7 +312,6 @@ func (gm *GuildMgr) insertGuildLog(log *GuildLog) {
{"account_id", log.AccountId},
{"log_type", q5.ToString(int(log.LogType))},
{"content", log.Content}}
f5.GetJsStyleDb().Insert(
FRIEND_DB,
"t_guild_logs",

View File

@ -35,9 +35,9 @@ func (gm *GuildMgr) init() {
// CreateGuild 创建公会
func (gm *GuildMgr) CreateGuild(name string, leaderID string) (int64, error) {
// check join guild
otherGuildId, isJoin := gm.checkJoinGuild(leaderID)
if isJoin {
// Check joined guild
otherGuildId, isJoined := gm.checkJoinGuild(leaderID)
if isJoined {
errMsg := fmt.Sprintf("Player:%s has joined other guild:%d", leaderID, otherGuildId)
return 0, fmt.Errorf(errMsg)
}
@ -58,9 +58,9 @@ func (gm *GuildMgr) CreateGuild(name string, leaderID string) (int64, error) {
newLevel := 1 // 会长
newMember := GuildMember{AccountId: leaderID, Level: newLevel}
_ = guild.AddMember(&newMember)
gm.insertGuildMember(guildId, &newMember)
gm.upsertGuildMember(guildId, &newMember)
gm.Guilds[guildId] = guild
return guildId, nil
}
@ -81,7 +81,7 @@ func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error
return fmt.Errorf("guild is full")
}
// IF exists, then replace it
gm.insertPendingReqs(guildId, applicantAccountId)
gm.upsertPendingReqs(guildId, applicantAccountId, "0")
gm.pendingReqs[guildId][applicantAccountId] = true
return nil
}
@ -116,10 +116,9 @@ func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string)
if err != nil {
return err
}
gm.insertGuildMember(guildId, &newMember)
gm.upsertGuildMember(guildId, &newMember)
delete(gm.pendingReqs[guildId], accountId)
gm.updatePendingReqs(guildId, accountId, 1)
gm.updatePendingReqs(guildId, accountId, "1")
logContent := fmt.Sprintf("Approve[%d-%s-%s]", guildId, operatorAccountId, accountId)
gm.WriteLog(guildId, accountId, LogTypeApprove, logContent)
@ -136,26 +135,34 @@ func (gm *GuildMgr) Reject(guildId int64, applicantAccountId string) error {
//delete(gm.pendingReqs[guildId], applicantAccountId)
gm.pendingReqs[guildId][applicantAccountId] = false
gm.updatePendingReqs(guildId, applicantAccountId, 2)
gm.updatePendingReqs(guildId, applicantAccountId, "2")
return nil
}
// JoinGuild 可直接加入公会
//func (gm *GuildMgr) JoinGuild(guildId int64, accountId string) error {
// guild, exists := gm.Guilds[guildId]
// if !exists {
// return fmt.Errorf("guild not found")
// }
//
// if len(guild.Members) >= guild.MaxMembers {
// return fmt.Errorf("guild is full")
// }
//
// newMember := GuildMember{AccountId: accountId, Level: 3}
// guild.Members = append(guild.Members, newMember)
// return nil
//}
// JoinGuild 直接加入公会
func (gm *GuildMgr) JoinGuild(guildId int64, accountId string) error {
guild, exists := gm.Guilds[guildId]
if !exists {
return fmt.Errorf("guild not found")
}
if len(guild.Members) >= guild.MaxMembers {
return fmt.Errorf("guild is full")
}
newMember := GuildMember{AccountId: accountId, Level: 3}
err := guild.AddMember(&newMember)
if err != nil {
return err
}
gm.upsertGuildMember(guildId, &newMember)
delete(gm.pendingReqs[guildId], accountId)
gm.updatePendingReqs(guildId, accountId, "1")
return nil
}
// LeaveGuild 离开公会
func (gm *GuildMgr) LeaveGuild(guildId int64, accountId string) error {
@ -320,8 +327,8 @@ func (gm *GuildMgr) Disband(guildId int64, accountId string) error {
fields := [][]string{{"is_leave_guild", q5.ToString(1)}}
gm.updateGuildMembers(guild, fields)
is_join_guild := 3
gm.updateAllPendingReqs(guildId, is_join_guild)
isJoinGuild := 3
gm.updateAllPendingReqs(guildId, isJoinGuild)
// 确保在删除之前没有其他地方引用了公会对象的指针, 以避免空指针异常
gm.Guilds[guildId] = nil