|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
|
|
|
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
|
|
|
)
|
|
|
|
|
|
type PartnerCategoryInfoRepository struct {
|
|
|
transactionContext *transaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *PartnerCategoryInfoRepository) Save(dm *domain.PartnerCategoryInfo) (*domain.PartnerCategoryInfo, error) {
|
|
|
var (
|
|
|
err error
|
|
|
m = &models.PartnerCategoryInfo{}
|
|
|
tx = repository.transactionContext.PgTx
|
|
|
)
|
|
|
if err = GobModelTransform(m, dm); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if dm.Identify() == nil {
|
|
|
if err = tx.Insert(m); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return dm, nil
|
|
|
}
|
|
|
if err = tx.Update(m); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return dm, nil
|
|
|
}
|
|
|
|
|
|
func (repository *PartnerCategoryInfoRepository) Remove(PartnerCategoryInfo *domain.PartnerCategoryInfo) (*domain.PartnerCategoryInfo, error) {
|
|
|
var (
|
|
|
tx = repository.transactionContext.PgTx
|
|
|
PartnerCategoryInfoModel = &models.PartnerCategoryInfo{Id: PartnerCategoryInfo.Identify().(int64)}
|
|
|
)
|
|
|
if _, err := tx.Model(PartnerCategoryInfoModel).Where("id = ?", PartnerCategoryInfo.Id).Delete(); err != nil {
|
|
|
return PartnerCategoryInfo, err
|
|
|
}
|
|
|
return PartnerCategoryInfo, nil
|
|
|
}
|
|
|
|
|
|
func (repository *PartnerCategoryInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.PartnerCategoryInfo, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
PartnerCategoryInfoModel := new(models.PartnerCategoryInfo)
|
|
|
query := NewQuery(tx.Model(PartnerCategoryInfoModel), queryOptions)
|
|
|
query.SetWhere("id = ?", "id")
|
|
|
if err := query.First(); err != nil {
|
|
|
return nil, fmt.Errorf("query row not found")
|
|
|
}
|
|
|
if PartnerCategoryInfoModel.Id == 0 {
|
|
|
return nil, fmt.Errorf("query row not found")
|
|
|
}
|
|
|
return repository.transformPgModelToDomainModel(PartnerCategoryInfoModel)
|
|
|
}
|
|
|
|
|
|
func (repository *PartnerCategoryInfoRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerCategoryInfo, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var PartnerCategoryInfoModels []*models.PartnerCategoryInfo
|
|
|
PartnerCategoryInfos := make([]*domain.PartnerCategoryInfo, 0)
|
|
|
query := NewQuery(tx.Model(&PartnerCategoryInfoModels), queryOptions).
|
|
|
SetOrder("create_time", "sortByCreateTime").
|
|
|
SetOrder("update_time", "sortByUpdateTime")
|
|
|
var err error
|
|
|
if query.AffectRow, err = query.SelectAndCount(); err != nil {
|
|
|
return 0, PartnerCategoryInfos, err
|
|
|
}
|
|
|
for _, PartnerCategoryInfoModel := range PartnerCategoryInfoModels {
|
|
|
if PartnerCategoryInfo, err := repository.transformPgModelToDomainModel(PartnerCategoryInfoModel); err != nil {
|
|
|
return 0, PartnerCategoryInfos, err
|
|
|
} else {
|
|
|
PartnerCategoryInfos = append(PartnerCategoryInfos, PartnerCategoryInfo)
|
|
|
}
|
|
|
}
|
|
|
return int64(query.AffectRow), PartnerCategoryInfos, nil
|
|
|
}
|
|
|
|
|
|
func (repository *PartnerCategoryInfoRepository) transformPgModelToDomainModel(PartnerCategoryInfoModel *models.PartnerCategoryInfo) (*domain.PartnerCategoryInfo, error) {
|
|
|
m := &domain.PartnerCategoryInfo{}
|
|
|
err := GobModelTransform(m, PartnerCategoryInfoModel)
|
|
|
return m, err
|
|
|
}
|
|
|
|
|
|
func NewPartnerCategoryInfoRepository(transactionContext *transaction.TransactionContext) (*PartnerCategoryInfoRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
}
|
|
|
return &PartnerCategoryInfoRepository{transactionContext: transactionContext}, nil
|
|
|
} |
...
|
...
|
|