f
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"tyapi-server/internal/domains/user/entities"
|
||||
"tyapi-server/internal/domains/user/repositories"
|
||||
@@ -107,7 +108,48 @@ func (r *GormUserRepository) ExistsByUnifiedSocialCode(ctx context.Context, unif
|
||||
}
|
||||
|
||||
func (r *GormUserRepository) Update(ctx context.Context, user entities.User) error {
|
||||
return r.UpdateEntity(ctx, &user)
|
||||
db := r.GetDB(ctx)
|
||||
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// 避免 GORM 自动保存关联触发 ON CONFLICT(受历史库索引差异影响)
|
||||
if err := tx.WithContext(ctx).Omit(clause.Associations).Save(&user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 企业信息单独按 user_id 做更新或创建,避免关联 upsert 依赖冲突约束
|
||||
if user.EnterpriseInfo == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
enterpriseInfo := *user.EnterpriseInfo
|
||||
enterpriseInfo.UserID = user.ID
|
||||
enterpriseInfo.User = nil
|
||||
|
||||
var count int64
|
||||
if err := tx.WithContext(ctx).
|
||||
Model(&entities.EnterpriseInfo{}).
|
||||
Where("user_id = ?", user.ID).
|
||||
Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
updates := map[string]interface{}{
|
||||
"company_name": enterpriseInfo.CompanyName,
|
||||
"unified_social_code": enterpriseInfo.UnifiedSocialCode,
|
||||
"legal_person_name": enterpriseInfo.LegalPersonName,
|
||||
"legal_person_id": enterpriseInfo.LegalPersonID,
|
||||
"legal_person_phone": enterpriseInfo.LegalPersonPhone,
|
||||
"enterprise_address": enterpriseInfo.EnterpriseAddress,
|
||||
}
|
||||
return tx.WithContext(ctx).
|
||||
Model(&entities.EnterpriseInfo{}).
|
||||
Where("user_id = ?", user.ID).
|
||||
Updates(updates).Error
|
||||
}
|
||||
|
||||
return tx.WithContext(ctx).Create(&enterpriseInfo).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (r *GormUserRepository) CreateBatch(ctx context.Context, users []entities.User) error {
|
||||
|
||||
Reference in New Issue
Block a user