|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"github.com/linmadan/egglib-go/utils/snowflake"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
|
|
|
)
|
|
|
|
|
|
type AccountDestroyRecordRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *AccountDestroyRecordRepository) nextIdentify() (int64, error) {
|
|
|
IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
id, err := IdWorker.NextId()
|
|
|
return id, err
|
|
|
}
|
|
|
func (repository *AccountDestroyRecordRepository) Save(accountDestroyRecord *domain.AccountDestroyRecord) (*domain.AccountDestroyRecord, error) {
|
|
|
sqlBuildFields := []string{
|
|
|
"user_base_id",
|
|
|
"account_destroy_record_id",
|
|
|
"destroy_time",
|
|
|
"account_after",
|
|
|
"account_before",
|
|
|
}
|
|
|
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "account_destroy_record_id"))
|
|
|
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "account_destroy_record_id"))
|
|
|
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "account_destroy_record_id")
|
|
|
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if accountDestroyRecord.Identify() == nil {
|
|
|
accountDestroyRecordId, err := repository.nextIdentify()
|
|
|
if err != nil {
|
|
|
return accountDestroyRecord, err
|
|
|
} else {
|
|
|
accountDestroyRecord.AccountDestroyRecordId = accountDestroyRecordId
|
|
|
}
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&accountDestroyRecord.UserBaseId,
|
|
|
&accountDestroyRecord.AccountDestroyRecordId,
|
|
|
&accountDestroyRecord.DestroyTime,
|
|
|
&accountDestroyRecord.AccountAfter,
|
|
|
&accountDestroyRecord.AccountBefore,
|
|
|
),
|
|
|
fmt.Sprintf("INSERT INTO users.account_destroy_records (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
accountDestroyRecord.UserBaseId,
|
|
|
accountDestroyRecord.DestroyTime,
|
|
|
accountDestroyRecord.AccountAfter,
|
|
|
accountDestroyRecord.AccountBefore,
|
|
|
); err != nil {
|
|
|
return accountDestroyRecord, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&accountDestroyRecord.UserBaseId,
|
|
|
&accountDestroyRecord.AccountDestroyRecordId,
|
|
|
&accountDestroyRecord.DestroyTime,
|
|
|
&accountDestroyRecord.AccountAfter,
|
|
|
&accountDestroyRecord.AccountBefore,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE users.account_destroy_records SET %s WHERE account_destroy_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
accountDestroyRecord.UserBaseId,
|
|
|
accountDestroyRecord.DestroyTime,
|
|
|
accountDestroyRecord.AccountAfter,
|
|
|
accountDestroyRecord.AccountBefore,
|
|
|
accountDestroyRecord.Identify(),
|
|
|
); err != nil {
|
|
|
return accountDestroyRecord, err
|
|
|
}
|
|
|
}
|
|
|
return accountDestroyRecord, nil
|
|
|
}
|
|
|
func (repository *AccountDestroyRecordRepository) Remove(accountDestroyRecord *domain.AccountDestroyRecord) (*domain.AccountDestroyRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
accountDestroyRecordModel := new(models.AccountDestroyRecord)
|
|
|
accountDestroyRecordModel.AccountDestroyRecordId = accountDestroyRecord.Identify().(int64)
|
|
|
if _, err := tx.Model(accountDestroyRecordModel).WherePK().Delete(); err != nil {
|
|
|
return accountDestroyRecord, err
|
|
|
}
|
|
|
return accountDestroyRecord, nil
|
|
|
}
|
|
|
func (repository *AccountDestroyRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.AccountDestroyRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
accountDestroyRecordModel := new(models.AccountDestroyRecord)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(accountDestroyRecordModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("account_destroy_record.account_destroy_record_id = ?", "accountDestroyRecordId")
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if accountDestroyRecordModel.AccountDestroyRecordId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return transform.TransformToAccountDestroyRecordDomainModelFromPgModels(accountDestroyRecordModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *AccountDestroyRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.AccountDestroyRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var accountDestroyRecordModels []*models.AccountDestroyRecord
|
|
|
accountDestroyRecords := make([]*domain.AccountDestroyRecord, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(&accountDestroyRecordModels), queryOptions)
|
|
|
query.SetWhereByQueryOption("account_before = ?", "accountBefore")
|
|
|
query.SetOffsetAndLimit(20)
|
|
|
query.SetOrderDirect("account_destroy_record_id", "DESC")
|
|
|
if count, err := query.SelectAndCount(); err != nil {
|
|
|
return 0, accountDestroyRecords, err
|
|
|
} else {
|
|
|
for _, accountDestroyRecordModel := range accountDestroyRecordModels {
|
|
|
if accountDestroyRecord, err := transform.TransformToAccountDestroyRecordDomainModelFromPgModels(accountDestroyRecordModel); err != nil {
|
|
|
return 0, accountDestroyRecords, err
|
|
|
} else {
|
|
|
accountDestroyRecords = append(accountDestroyRecords, accountDestroyRecord)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), accountDestroyRecords, nil
|
|
|
}
|
|
|
}
|
|
|
func NewAccountDestroyRecordRepository(transactionContext *pgTransaction.TransactionContext) (*AccountDestroyRecordRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &AccountDestroyRecordRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|