From 9d77177e61ebd0be7a2ec5a62a66835b210ea2e8 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Mon, 24 Jul 2023 14:59:30 +0800 Subject: [PATCH] insert or update --- domain/model/groupPower_m/group_grade.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/domain/model/groupPower_m/group_grade.go b/domain/model/groupPower_m/group_grade.go index d65a403..2a71932 100644 --- a/domain/model/groupPower_m/group_grade.go +++ b/domain/model/groupPower_m/group_grade.go @@ -83,6 +83,7 @@ func IncrGroupPowerExp(txModel *domain.Model, groupPowerId mysql.ID, exp mysql.N } // 增加家族经验-总 gpg := new(GroupPowerGrade) + // insert if err := txModel.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", groupPowerId).First(gpg).Error; err != nil { if err != gorm.ErrRecordNotFound { return err @@ -97,10 +98,12 @@ func IncrGroupPowerExp(txModel *domain.Model, groupPowerId mysql.ID, exp mysql.N if err = txModel.DB().Create(gpg).Error; err != nil { return err } - } - if err := txModel.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", groupPowerId). - UpdateColumn("exp", gorm.Expr("exp + ?", exp)).Error; err != nil { - return err + } else { + // or update + if err := txModel.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", groupPowerId). + UpdateColumn("exp", gorm.Expr("exp + ?", exp)).Error; err != nil { + return err + } } //if err = txModel.DB().Model(GroupPowerGrade{}).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "group_power_id"}}, // DoUpdates: clause.Assignments(map[string]interface{}{ -- 2.22.0