作者 yangfu

账号注销、注销记录

@@ -10,7 +10,7 @@ import ( @@ -10,7 +10,7 @@ import (
10 10
11 type DestroyAccountCommand struct { 11 type DestroyAccountCommand struct {
12 // 用户Id 用户唯一标识 12 // 用户Id 用户唯一标识
13 - UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"` 13 + UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"`
14 } 14 }
15 15
16 func (destroyAccountCommand *DestroyAccountCommand) Valid(validation *validation.Validation) { 16 func (destroyAccountCommand *DestroyAccountCommand) Valid(validation *validation.Validation) {
@@ -10,7 +10,7 @@ import ( @@ -10,7 +10,7 @@ import (
10 10
11 type PhoneAuthChangePasswordCommand struct { 11 type PhoneAuthChangePasswordCommand struct {
12 // 用户Id 用户唯一标识 12 // 用户Id 用户唯一标识
13 - UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"` 13 + UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"`
14 // 旧密码 14 // 旧密码
15 OldPassword string `cname:"旧密码" json:"oldPassword" valid:"Required"` 15 OldPassword string `cname:"旧密码" json:"oldPassword" valid:"Required"`
16 // 新密码 16 // 新密码
@@ -72,10 +72,21 @@ func (authService *AuthService) DestroyAccount(destroyAccountCommand *command.De @@ -72,10 +72,21 @@ func (authService *AuthService) DestroyAccount(destroyAccountCommand *command.De
72 defer func() { 72 defer func() {
73 transactionContext.RollbackTransaction() 73 transactionContext.RollbackTransaction()
74 }() 74 }()
  75 +
  76 + accountDestroyService, err := factory.CreatePgAuthAccountDestroyService(map[string]interface{}{
  77 + "transactionContext": transactionContext,
  78 + })
  79 + if err != nil {
  80 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  81 + }
  82 + if err := accountDestroyService.DestroyAccount(nil, destroyAccountCommand.UserId); err != nil {
  83 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  84 + }
  85 +
75 if err := transactionContext.CommitTransaction(); err != nil { 86 if err := transactionContext.CommitTransaction(); err != nil {
76 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 87 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
77 } 88 }
78 - return nil, nil 89 + return struct{}{}, nil
79 } 90 }
80 91
81 // 修改密码 92 // 修改密码
@@ -93,10 +104,54 @@ func (authService *AuthService) PhoneAuthChangePassword(phoneAuthChangePasswordC @@ -93,10 +104,54 @@ func (authService *AuthService) PhoneAuthChangePassword(phoneAuthChangePasswordC
93 defer func() { 104 defer func() {
94 transactionContext.RollbackTransaction() 105 transactionContext.RollbackTransaction()
95 }() 106 }()
  107 + var userRepository domain.UserRepository
  108 + if value, err := factory.CreateUserRepository(map[string]interface{}{
  109 + "transactionContext": transactionContext,
  110 + }); err != nil {
  111 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  112 + } else {
  113 + userRepository = value
  114 + }
  115 + var userBaseId int64
  116 + if user, err := userRepository.FindOne(map[string]interface{}{"userId": phoneAuthChangePasswordCommand.UserId}); err != nil {
  117 + if err == domain.ErrorNotFound {
  118 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
  119 + }
  120 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  121 + } else {
  122 + userBaseId = user.UserBaseId
  123 + }
  124 +
  125 + var userBaseRepository domain.UserBaseRepository
  126 + if value, err := factory.CreateUserBaseRepository(map[string]interface{}{
  127 + "transactionContext": transactionContext,
  128 + }); err != nil {
  129 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  130 + } else {
  131 + userBaseRepository = value
  132 + }
  133 + userBase, err := userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBaseId})
  134 + if err != nil {
  135 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  136 + }
  137 + if err == domain.ErrorNotFound {
  138 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
  139 + }
  140 + if err := userBase.CheckAccountPassword(userBase.Account, phoneAuthChangePasswordCommand.OldPassword); err != nil {
  141 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  142 + }
  143 + if err := userBase.ResetPassword(userBase.Account, phoneAuthChangePasswordCommand.NewPassword); err != nil {
  144 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  145 + }
  146 + if _, err = userBaseRepository.Save(userBase); err != nil {
  147 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  148 + }
  149 +
96 if err := transactionContext.CommitTransaction(); err != nil { 150 if err := transactionContext.CommitTransaction(); err != nil {
97 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 151 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
98 } 152 }
99 - return nil, nil 153 + return struct {
  154 + }{}, nil
100 } 155 }
101 156
102 // 手机账号密码检查 157 // 手机账号密码检查
@@ -237,7 +292,7 @@ func (authService *AuthService) RefreshIM(refreshIMCommand *command.RefreshIMCom @@ -237,7 +292,7 @@ func (authService *AuthService) RefreshIM(refreshIMCommand *command.RefreshIMCom
237 return nil, nil 292 return nil, nil
238 } 293 }
239 294
240 -// 用户信息 (暂时没有使用) 295 +// 用户信息
241 func (authService *AuthService) UserInfo(userInfoQuery *query.UserInfoQuery) (interface{}, error) { 296 func (authService *AuthService) UserInfo(userInfoQuery *query.UserInfoQuery) (interface{}, error) {
242 if err := userInfoQuery.ValidateQuery(); err != nil { 297 if err := userInfoQuery.ValidateQuery(); err != nil {
243 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 298 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -21,3 +21,11 @@ func CreatePgAuthResetPhoneService(options map[string]interface{}) (service.PgAu @@ -21,3 +21,11 @@ func CreatePgAuthResetPhoneService(options map[string]interface{}) (service.PgAu
21 } 21 }
22 return domainService.NewPgAuthResetPhoneService(transactionContext) 22 return domainService.NewPgAuthResetPhoneService(transactionContext)
23 } 23 }
  24 +
  25 +func CreatePgAuthAccountDestroyService(options map[string]interface{}) (service.PgAuthAccountDestroyService, error) {
  26 + var transactionContext *pgTransaction.TransactionContext
  27 + if value, ok := options["transactionContext"]; ok {
  28 + transactionContext = value.(*pgTransaction.TransactionContext)
  29 + }
  30 + return domainService.NewPgAuthAccountDestroyService(transactionContext)
  31 +}
@@ -61,3 +61,11 @@ func CreateCustomizeMenuRepository(options map[string]interface{}) (domain.Custo @@ -61,3 +61,11 @@ func CreateCustomizeMenuRepository(options map[string]interface{}) (domain.Custo
61 } 61 }
62 return repository.NewCustomizeMenuRepository(transactionContext) 62 return repository.NewCustomizeMenuRepository(transactionContext)
63 } 63 }
  64 +
  65 +func CreateAccountDestroyRecordRepository(options map[string]interface{}) (domain.AccountDestroyRecordRepository, error) {
  66 + var transactionContext *pg.TransactionContext
  67 + if value, ok := options["transactionContext"]; ok {
  68 + transactionContext = value.(*pg.TransactionContext)
  69 + }
  70 + return repository.NewAccountDestroyRecordRepository(transactionContext)
  71 +}
  1 +package domain
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +)
  7 +
  8 +// 账号注销记录
  9 +type AccountDestroyRecord struct {
  10 + // 用户基础数据id
  11 + UserBaseId int64 `json:"userBaseId,string"`
  12 + // 账号注销记录ID
  13 + AccountDestroyRecordId int64 `json:"accountDestroyRecordId,string"`
  14 + // 注销时间
  15 + DestroyTime time.Time `json:"destroyTime"`
  16 + // 账号修改后
  17 + AccountAfter string `json:"accountAfter"`
  18 + // 账号修改前
  19 + AccountBefore string `json:"accountBefore"`
  20 +}
  21 +
  22 +type AccountDestroyRecordRepository interface {
  23 + Save(accountDestroyRecord *AccountDestroyRecord) (*AccountDestroyRecord, error)
  24 + Remove(accountDestroyRecord *AccountDestroyRecord) (*AccountDestroyRecord, error)
  25 + FindOne(queryOptions map[string]interface{}) (*AccountDestroyRecord, error)
  26 + Find(queryOptions map[string]interface{}) (int64, []*AccountDestroyRecord, error)
  27 +}
  28 +
  29 +func (accountDestroyRecord *AccountDestroyRecord) Identify() interface{} {
  30 + if accountDestroyRecord.AccountDestroyRecordId == 0 {
  31 + return nil
  32 + }
  33 + return accountDestroyRecord.AccountDestroyRecordId
  34 +}
  35 +
  36 +func (accountDestroyRecord *AccountDestroyRecord) Update(data map[string]interface{}) error {
  37 + //if userBaseId, ok := data["userBaseId"]; ok {
  38 + // accountDestroyRecord.UserBaseId = userBaseId.(int64)
  39 + //}
  40 + //if accountDestroyRecordId, ok := data["accountDestroyRecordId"]; ok {
  41 + // accountDestroyRecord.AccountDestroyRecordId = accountDestroyRecordId.(int64)
  42 + //}
  43 + //if destroyTime, ok := data["destroyTime"]; ok {
  44 + // accountDestroyRecord.DestroyTime = destroyTime.(string)
  45 + //}
  46 + //if accountAfter, ok := data["accountAfter"]; ok {
  47 + // accountDestroyRecord.AccountAfter = accountAfter.(string)
  48 + //}
  49 + //if accountBefore, ok := data["accountBefore"]; ok {
  50 + // accountDestroyRecord.AccountBefore = accountBefore.(string)
  51 + //}
  52 + return nil
  53 +}
  54 +
  55 +func NewAccountDestroyRecord(userBaseId int64, num int64, account string) *AccountDestroyRecord {
  56 + return &AccountDestroyRecord{
  57 + UserBaseId: userBaseId,
  58 + DestroyTime: time.Now(),
  59 + AccountBefore: account,
  60 + AccountAfter: fmt.Sprintf("%v-%v", account, num+1),
  61 + }
  62 +}
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  4 +
  5 +// PgAuthAccountDestroyService 账号注销服务
  6 +type PgAuthAccountDestroyService interface {
  7 + DestroyAccount(optUser *domain.User, userId int64) error
  8 +}
@@ -155,3 +155,11 @@ func (user *User) Update(data map[string]interface{}) error { @@ -155,3 +155,11 @@ func (user *User) Update(data map[string]interface{}) error {
155 type UserStatus int 155 type UserStatus int
156 156
157 /***** 1.基础模块函数 *****/ 157 /***** 1.基础模块函数 *****/
  158 +// DestroyAccount 注销账号
  159 +//
  160 +// accountAfter 变更后的账号
  161 +func (user *User) DestroyAccount(accountAfter string) error {
  162 + user.EnableStatus = int(UserStatusDestroy)
  163 + user.Ext.Phone = accountAfter
  164 + return nil
  165 +}
@@ -136,6 +136,10 @@ func (userBase *UserBase) ResetPassword(account, password string) error { @@ -136,6 +136,10 @@ func (userBase *UserBase) ResetPassword(account, password string) error {
136 return nil 136 return nil
137 } 137 }
138 138
  139 +// ResetPhone 重置手机号
  140 +//
  141 +// oldPhone 旧手机号
  142 +// newPhone 新手机号
139 func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error { 143 func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error {
140 if userBase.Status != int(UserStatusEnable) { 144 if userBase.Status != int(UserStatusEnable) {
141 return fmt.Errorf("该用户不存在") 145 return fmt.Errorf("该用户不存在")
@@ -147,3 +151,16 @@ func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error { @@ -147,3 +151,16 @@ func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error {
147 userBase.UserInfo.Phone = newPhone 151 userBase.UserInfo.Phone = newPhone
148 return nil 152 return nil
149 } 153 }
  154 +
  155 +// DestroyAccount 注销账号
  156 +//
  157 +// p1 p1_desc
  158 +func (userBase *UserBase) DestroyAccount(accountAfter string) error {
  159 + if userBase.Status != int(UserStatusEnable) {
  160 + return fmt.Errorf("账号已注销")
  161 + }
  162 + userBase.Status = int(UserStatusDestroy)
  163 + userBase.Account = accountAfter
  164 + userBase.UserInfo.Phone = accountAfter
  165 + return nil
  166 +}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
  7 +
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 +)
  10 +
  11 +// PgAuthAccountDestroyService 账号注销服务
  12 +type PgAuthAccountDestroyService struct {
  13 + transactionContext *pgTransaction.TransactionContext
  14 +}
  15 +
  16 +func (ptr *PgAuthAccountDestroyService) DestroyAccount(optUser *domain.User, userId int64) error {
  17 + // 1.查询账号记录
  18 + userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
  19 + var userBaseId int64
  20 + if user, err := userRepository.FindOne(map[string]interface{}{"userId": userId}); err != nil {
  21 + if err == domain.ErrorNotFound {
  22 + return fmt.Errorf("该用户不存在")
  23 + }
  24 + return err
  25 + } else {
  26 + userBaseId = user.UserBaseId
  27 + }
  28 +
  29 + userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
  30 + userBase, err := userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBaseId})
  31 + if err != nil {
  32 + return err
  33 + }
  34 + if err == domain.ErrorNotFound {
  35 + return fmt.Errorf("该用户不存在")
  36 + }
  37 +
  38 + // 2.查询历史账号注销记录,生成当前账号的注销记录
  39 + accountDestroyRecordRepository, _ := repository.NewAccountDestroyRecordRepository(ptr.transactionContext)
  40 + var total int64
  41 + if total, _, err = accountDestroyRecordRepository.Find(map[string]interface{}{"accountBefore": userBase.Account}); err != nil {
  42 + return err
  43 + }
  44 + record := domain.NewAccountDestroyRecord(userBase.UserBaseId, total, userBase.Account)
  45 + if _, err := accountDestroyRecordRepository.Save(record); err != nil {
  46 + return err
  47 + }
  48 +
  49 + // 3.注销相关联的账号
  50 + for i := 0; i < len(userBase.RelatedUsers); i++ {
  51 + userId := userBase.RelatedUsers[i]
  52 + if user, _ := userRepository.FindOne(map[string]interface{}{"userId": userId}); user != nil {
  53 + user.DestroyAccount(record.AccountAfter)
  54 + if _, err := userRepository.Save(user); err != nil {
  55 + return err
  56 + }
  57 + }
  58 + }
  59 +
  60 + // 4.注销账号
  61 + if err = userBase.DestroyAccount(record.AccountAfter); err != nil {
  62 + return err
  63 + }
  64 + if _, err = userBaseRepository.Save(userBase); err != nil {
  65 + return err
  66 + }
  67 + return nil
  68 +}
  69 +
  70 +func NewPgAuthAccountDestroyService(transactionContext *pgTransaction.TransactionContext) (*PgAuthAccountDestroyService, error) {
  71 + if transactionContext == nil {
  72 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  73 + } else {
  74 + return &PgAuthAccountDestroyService{
  75 + transactionContext: transactionContext,
  76 + }, nil
  77 + }
  78 +}
@@ -36,7 +36,7 @@ func init() { @@ -36,7 +36,7 @@ func init() {
36 (*models.Role)(nil), 36 (*models.Role)(nil),
37 (*models.User)(nil), 37 (*models.User)(nil),
38 (*models.UserBase)(nil), 38 (*models.UserBase)(nil),
39 - //(*models.User)(nil), 39 + (*models.AccountDestroyRecord)(nil),
40 } { 40 } {
41 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 41 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
42 Temp: false, 42 Temp: false,
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type AccountDestroyRecord struct {
  6 + tableName string `comment:"账号注销记录" pg:"users.account_destroy_records,alias:account_destroy_record"`
  7 + // 用户基础数据id
  8 + UserBaseId int64 `comment:"用户基础数据id"`
  9 + // 账号注销记录ID
  10 + AccountDestroyRecordId int64 `comment:"账号注销记录ID" pg:"pk:account_destroy_record_id"`
  11 + // 注销时间
  12 + DestroyTime time.Time `comment:"注销时间"`
  13 + // 账号修改后
  14 + AccountAfter string `comment:"账号修改后"`
  15 + // 账号修改前
  16 + AccountBefore string `comment:"账号修改前"`
  17 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToAccountDestroyRecordDomainModelFromPgModels(accountDestroyRecordModel *models.AccountDestroyRecord) (*domain.AccountDestroyRecord, error) {
  9 + return &domain.AccountDestroyRecord{
  10 + UserBaseId: accountDestroyRecordModel.UserBaseId,
  11 + AccountDestroyRecordId: accountDestroyRecordModel.AccountDestroyRecordId,
  12 + DestroyTime: accountDestroyRecordModel.DestroyTime,
  13 + AccountAfter: accountDestroyRecordModel.AccountAfter,
  14 + AccountBefore: accountDestroyRecordModel.AccountBefore,
  15 + }, nil
  16 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 +
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "github.com/linmadan/egglib-go/utils/snowflake"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type AccountDestroyRecordRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *AccountDestroyRecordRepository) nextIdentify() (int64, error) {
  20 + IdWorker, err := snowflake.NewIdWorker(1)
  21 + if err != nil {
  22 + return 0, err
  23 + }
  24 + id, err := IdWorker.NextId()
  25 + return id, err
  26 +}
  27 +func (repository *AccountDestroyRecordRepository) Save(accountDestroyRecord *domain.AccountDestroyRecord) (*domain.AccountDestroyRecord, error) {
  28 + sqlBuildFields := []string{
  29 + "user_base_id",
  30 + "account_destroy_record_id",
  31 + "destroy_time",
  32 + "account_after",
  33 + "account_before",
  34 + }
  35 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "account_destroy_record_id"))
  36 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "account_destroy_record_id"))
  37 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  38 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "account_destroy_record_id")
  39 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  40 + tx := repository.transactionContext.PgTx
  41 + if accountDestroyRecord.Identify() == nil {
  42 + accountDestroyRecordId, err := repository.nextIdentify()
  43 + if err != nil {
  44 + return accountDestroyRecord, err
  45 + } else {
  46 + accountDestroyRecord.AccountDestroyRecordId = accountDestroyRecordId
  47 + }
  48 + if _, err := tx.QueryOne(
  49 + pg.Scan(
  50 + &accountDestroyRecord.UserBaseId,
  51 + &accountDestroyRecord.AccountDestroyRecordId,
  52 + &accountDestroyRecord.DestroyTime,
  53 + &accountDestroyRecord.AccountAfter,
  54 + &accountDestroyRecord.AccountBefore,
  55 + ),
  56 + fmt.Sprintf("INSERT INTO users.account_destroy_records (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  57 + accountDestroyRecord.UserBaseId,
  58 + accountDestroyRecord.DestroyTime,
  59 + accountDestroyRecord.AccountAfter,
  60 + accountDestroyRecord.AccountBefore,
  61 + ); err != nil {
  62 + return accountDestroyRecord, err
  63 + }
  64 + } else {
  65 + if _, err := tx.QueryOne(
  66 + pg.Scan(
  67 + &accountDestroyRecord.UserBaseId,
  68 + &accountDestroyRecord.AccountDestroyRecordId,
  69 + &accountDestroyRecord.DestroyTime,
  70 + &accountDestroyRecord.AccountAfter,
  71 + &accountDestroyRecord.AccountBefore,
  72 + ),
  73 + fmt.Sprintf("UPDATE users.account_destroy_records SET %s WHERE account_destroy_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  74 + accountDestroyRecord.UserBaseId,
  75 + accountDestroyRecord.DestroyTime,
  76 + accountDestroyRecord.AccountAfter,
  77 + accountDestroyRecord.AccountBefore,
  78 + accountDestroyRecord.Identify(),
  79 + ); err != nil {
  80 + return accountDestroyRecord, err
  81 + }
  82 + }
  83 + return accountDestroyRecord, nil
  84 +}
  85 +func (repository *AccountDestroyRecordRepository) Remove(accountDestroyRecord *domain.AccountDestroyRecord) (*domain.AccountDestroyRecord, error) {
  86 + tx := repository.transactionContext.PgTx
  87 + accountDestroyRecordModel := new(models.AccountDestroyRecord)
  88 + accountDestroyRecordModel.AccountDestroyRecordId = accountDestroyRecord.Identify().(int64)
  89 + if _, err := tx.Model(accountDestroyRecordModel).WherePK().Delete(); err != nil {
  90 + return accountDestroyRecord, err
  91 + }
  92 + return accountDestroyRecord, nil
  93 +}
  94 +func (repository *AccountDestroyRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.AccountDestroyRecord, error) {
  95 + tx := repository.transactionContext.PgTx
  96 + accountDestroyRecordModel := new(models.AccountDestroyRecord)
  97 + query := sqlbuilder.BuildQuery(tx.Model(accountDestroyRecordModel), queryOptions)
  98 + query.SetWhereByQueryOption("account_destroy_record.account_destroy_record_id = ?", "accountDestroyRecordId")
  99 + if err := query.First(); err != nil {
  100 + if err.Error() == "pg: no rows in result set" {
  101 + return nil, fmt.Errorf("没有此资源")
  102 + } else {
  103 + return nil, err
  104 + }
  105 + }
  106 + if accountDestroyRecordModel.AccountDestroyRecordId == 0 {
  107 + return nil, nil
  108 + } else {
  109 + return transform.TransformToAccountDestroyRecordDomainModelFromPgModels(accountDestroyRecordModel)
  110 + }
  111 +}
  112 +func (repository *AccountDestroyRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.AccountDestroyRecord, error) {
  113 + tx := repository.transactionContext.PgTx
  114 + var accountDestroyRecordModels []*models.AccountDestroyRecord
  115 + accountDestroyRecords := make([]*domain.AccountDestroyRecord, 0)
  116 + query := sqlbuilder.BuildQuery(tx.Model(&accountDestroyRecordModels), queryOptions)
  117 + query.SetWhereByQueryOption("account_before = ?", "accountBefore")
  118 + query.SetOffsetAndLimit(20)
  119 + query.SetOrderDirect("account_destroy_record_id", "DESC")
  120 + if count, err := query.SelectAndCount(); err != nil {
  121 + return 0, accountDestroyRecords, err
  122 + } else {
  123 + for _, accountDestroyRecordModel := range accountDestroyRecordModels {
  124 + if accountDestroyRecord, err := transform.TransformToAccountDestroyRecordDomainModelFromPgModels(accountDestroyRecordModel); err != nil {
  125 + return 0, accountDestroyRecords, err
  126 + } else {
  127 + accountDestroyRecords = append(accountDestroyRecords, accountDestroyRecord)
  128 + }
  129 + }
  130 + return int64(count), accountDestroyRecords, nil
  131 + }
  132 +}
  133 +func NewAccountDestroyRecordRepository(transactionContext *pgTransaction.TransactionContext) (*AccountDestroyRecordRepository, error) {
  134 + if transactionContext == nil {
  135 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  136 + } else {
  137 + return &AccountDestroyRecordRepository{
  138 + transactionContext: transactionContext,
  139 + }, nil
  140 + }
  141 +}
@@ -11,12 +11,17 @@ import ( @@ -11,12 +11,17 @@ import (
11 ) 11 )
12 12
13 var _ = Describe("注销账号 (添加用户时重新激活)", func() { 13 var _ = Describe("注销账号 (添加用户时重新激活)", func() {
14 - return  
15 var Id int64 14 var Id int64
16 BeforeEach(func() { 15 BeforeEach(func() {
17 _, err := pG.DB.QueryOne( 16 _, err := pG.DB.QueryOne(
18 pg.Scan(&Id), 17 pg.Scan(&Id),
19 - "INSERT INTO s () VALUES () RETURNING _id", 18 + "INSERT INTO \"users\".\"user_base\"(\"user_base_id\", \"user_info\", \"account\", \"password\", \"im\", \"related_user\", \"status\", \"created_at\", \"updated_at\") VALUES (999, '{\"phone\": \"phone\", \"userName\": \"string\"}', 'phone', 'string', '{\"accid\": \"\", \"imToken\": \"\", \"csAccountId\": \"\"}', '{999}', 1, '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08') RETURNING user_base_id;",
  19 + )
  20 + Expect(err).NotTo(HaveOccurred())
  21 +
  22 + _, err = pG.DB.QueryOne(
  23 + pg.Scan(&Id),
  24 + "INSERT INTO \"users\".\"user\"(\"user_id\", \"company_id\", \"user_base_id\", \"user_type\", \"user_code\", \"organization_id\", \"department_id\", \"user_org\", \"user_role\", \"favorite_menus\", \"cooperation_info\", \"enable_status\", \"ext\", \"created_at\", \"updated_at\") VALUES (999, 5, 999, 1025, 'ADMIN01', 5, 5, '[{\"orgId\": 5, \"orgName\": \"string1\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"deletedAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[{\"ext\": {\"orgName\": \"string1\"}, \"roleId\": 5, \"roleName\": \"企业管理员\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[]', '{\"cooperationCompany\": \"\", \"cooperationDeadline\": \"0001-01-01T00:00:00Z\"}', 1, '{\"phone\": \"18860183031\", \"depName\": \"string1\", \"orgName\": \"string1\"}', '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08');\n",
20 ) 25 )
21 Expect(err).NotTo(HaveOccurred()) 26 Expect(err).NotTo(HaveOccurred())
22 }) 27 })
@@ -25,7 +30,7 @@ var _ = Describe("注销账号 (添加用户时重新激活)", func() { @@ -25,7 +30,7 @@ var _ = Describe("注销账号 (添加用户时重新激活)", func() {
25 It("", func() { 30 It("", func() {
26 httpExpect := httpexpect.New(GinkgoT(), server.URL) 31 httpExpect := httpexpect.New(GinkgoT(), server.URL)
27 body := map[string]interface{}{ 32 body := map[string]interface{}{
28 - "userId": "int64", 33 + "userId": 999,
29 } 34 }
30 httpExpect.POST("/auth/destroy-account"). 35 httpExpect.POST("/auth/destroy-account").
31 WithJSON(body). 36 WithJSON(body).
@@ -40,7 +45,9 @@ var _ = Describe("注销账号 (添加用户时重新激活)", func() { @@ -40,7 +45,9 @@ var _ = Describe("注销账号 (添加用户时重新激活)", func() {
40 }) 45 })
41 }) 46 })
42 AfterEach(func() { 47 AfterEach(func() {
43 - _, err := pG.DB.Exec("DELETE FROM s WHERE true") 48 + _, err := pG.DB.Exec("DELETE FROM users.user_base WHERE user_base_id = 999")
  49 + Expect(err).NotTo(HaveOccurred())
  50 + _, err = pG.DB.Exec(`DELETE FROM users."user" WHERE user_id = 999`)
44 Expect(err).NotTo(HaveOccurred()) 51 Expect(err).NotTo(HaveOccurred())
45 }) 52 })
46 }) 53 })
@@ -11,12 +11,17 @@ import ( @@ -11,12 +11,17 @@ import (
11 ) 11 )
12 12
13 var _ = Describe("修改密码", func() { 13 var _ = Describe("修改密码", func() {
14 - return  
15 var Id int64 14 var Id int64
16 BeforeEach(func() { 15 BeforeEach(func() {
17 _, err := pG.DB.QueryOne( 16 _, err := pG.DB.QueryOne(
18 pg.Scan(&Id), 17 pg.Scan(&Id),
19 - "INSERT INTO s () VALUES () RETURNING _id", 18 + "INSERT INTO \"users\".\"user_base\"(\"user_base_id\", \"user_info\", \"account\", \"password\", \"im\", \"related_user\", \"status\", \"created_at\", \"updated_at\") VALUES (999, '{\"phone\": \"phone\", \"userName\": \"string\"}', 'phone', 'string', '{\"accid\": \"\", \"imToken\": \"\", \"csAccountId\": \"\"}', '{999}', 1, '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08') RETURNING user_base_id;",
  19 + )
  20 + Expect(err).NotTo(HaveOccurred())
  21 +
  22 + _, err = pG.DB.QueryOne(
  23 + pg.Scan(&Id),
  24 + "INSERT INTO \"users\".\"user\"(\"user_id\", \"company_id\", \"user_base_id\", \"user_type\", \"user_code\", \"organization_id\", \"department_id\", \"user_org\", \"user_role\", \"favorite_menus\", \"cooperation_info\", \"enable_status\", \"ext\", \"created_at\", \"updated_at\") VALUES (999, 5, 999, 1025, 'ADMIN01', 5, 5, '[{\"orgId\": 5, \"orgName\": \"string1\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"deletedAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[{\"ext\": {\"orgName\": \"string1\"}, \"roleId\": 5, \"roleName\": \"企业管理员\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[]', '{\"cooperationCompany\": \"\", \"cooperationDeadline\": \"0001-01-01T00:00:00Z\"}', 1, '{\"phone\": \"18860183031\", \"depName\": \"string1\", \"orgName\": \"string1\"}', '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08');\n",
20 ) 25 )
21 Expect(err).NotTo(HaveOccurred()) 26 Expect(err).NotTo(HaveOccurred())
22 }) 27 })
@@ -25,9 +30,9 @@ var _ = Describe("修改密码", func() { @@ -25,9 +30,9 @@ var _ = Describe("修改密码", func() {
25 It("", func() { 30 It("", func() {
26 httpExpect := httpexpect.New(GinkgoT(), server.URL) 31 httpExpect := httpexpect.New(GinkgoT(), server.URL)
27 body := map[string]interface{}{ 32 body := map[string]interface{}{
28 - "userId": "int64", 33 + "userId": 999,
29 "oldPassword": "string", 34 "oldPassword": "string",
30 - "newPassword": "string", 35 + "newPassword": "string999",
31 } 36 }
32 httpExpect.POST("/auth/change-password"). 37 httpExpect.POST("/auth/change-password").
33 WithJSON(body). 38 WithJSON(body).
@@ -42,7 +47,9 @@ var _ = Describe("修改密码", func() { @@ -42,7 +47,9 @@ var _ = Describe("修改密码", func() {
42 }) 47 })
43 }) 48 })
44 AfterEach(func() { 49 AfterEach(func() {
45 - _, err := pG.DB.Exec("DELETE FROM s WHERE true") 50 + _, err := pG.DB.Exec("DELETE FROM users.user_base WHERE user_base_id = 999")
  51 + Expect(err).NotTo(HaveOccurred())
  52 + _, err = pG.DB.Exec(`DELETE FROM users."user" WHERE user_id = 999`)
46 Expect(err).NotTo(HaveOccurred()) 53 Expect(err).NotTo(HaveOccurred())
47 }) 54 })
48 }) 55 })