package groupPower_m import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" "gorm.io/gorm" "gorm.io/gorm/clause" "hilo-group/_const/enum/groupPower_e" "time" ) type GroupPowerExp struct { Date string GroupPowerId mysql.ID Exp int64 CreatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"` } type GroupPowerGrade struct { GroupPowerId mysql.ID Exp int64 Grade groupPower_e.GroupPowerGrade ExpiredAt time.Time CreatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"` } // 增加家族经验 // 达到经验值之后升级 // 单进程同步执行,不考虑并发 func IncrGroupPowerExp(txModel *domain.Model, groupPowerId mysql.ID, exp int64) error { var err error defer func() { if err != nil { txModel.Log.Errorf("IncrGroupPowerExp fail,id:%v,exp:%v,err:%v", groupPowerId, exp, err) } }() // 增加家族经验-天 date := time.Now().Format("2006-01-02") gpe := &GroupPowerExp{ Date: date, GroupPowerId: groupPowerId, Exp: exp, } if err = txModel.DB().Model(GroupPowerExp{}).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "date"}, {Name: "group_power_id"}}, DoUpdates: clause.Assignments(map[string]interface{}{ "exp": gorm.Expr("exp + ?", gpe.Exp)})}).Create(gpe).Error; err != nil { return err } // 增加家族经验-总 gpg := &GroupPowerGrade{ GroupPowerId: groupPowerId, Exp: exp, Grade: 0, ExpiredAt: time.Time{}, } if err = txModel.DB().Model(GroupPowerGrade{}).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "group_power_id"}}, DoUpdates: clause.Assignments(map[string]interface{}{ "exp": gorm.Expr("exp + ?", gpg.Exp)})}).Create(gpg).Error; err != nil { return err } // 达到经验值之后升级 latestGrade := new(GroupPowerGrade) if err = txModel.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", groupPowerId).First(latestGrade).Error; err != nil { return err } for grade := groupPower_e.GroupPowerGradeMax; grade >= groupPower_e.GroupPowerGrade0; grade-- { if latestGrade.Exp > groupPower_e.GroupPowerGradeExp[grade] { if latestGrade.Grade != grade { if err = txModel.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", latestGrade.GroupPowerId).UpdateColumn("grade", grade).Error; err != nil { return err } } break } } return nil }