|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"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 CompanyRepository struct {
|
|
|
transactionContext *transaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *CompanyRepository) Save(dm *domain.Company) (*domain.Company, error) {
|
|
|
var (
|
|
|
err error
|
|
|
m = &models.Company{}
|
|
|
tx = repository.transactionContext.PgTx
|
|
|
)
|
|
|
if err = GobModelTransform(m, dm); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if dm.Id == 0 {
|
|
|
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 *CompanyRepository) Remove(Company *domain.Company) (*domain.Company, error) {
|
|
|
var (
|
|
|
tx = repository.transactionContext.PgTx
|
|
|
CompanyModel = &models.Company{Id: Company.Id}
|
|
|
)
|
|
|
if _, err := tx.Model(CompanyModel).Where("id = ?", Company.Id).Delete(); err != nil {
|
|
|
return Company, err
|
|
|
}
|
|
|
return Company, nil
|
|
|
}
|
|
|
|
|
|
func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
CompanyModel := new(models.Company)
|
|
|
query := NewQuery(tx.Model(CompanyModel), queryOptions)
|
|
|
query.SetWhere(`"company".id = ?`, "id")
|
|
|
if err := query.First(); err != nil {
|
|
|
return nil, query.HandleError(err, "没有此订单")
|
|
|
}
|
|
|
if CompanyModel.Id == 0 {
|
|
|
return nil, nil
|
|
|
}
|
|
|
return repository.transformPgModelToDomainModel(CompanyModel)
|
|
|
}
|
|
|
|
|
|
func (repository *CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Company, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var CompanyModels []*models.Company
|
|
|
Companys := make([]*domain.Company, 0)
|
|
|
query := NewQuery(tx.Model(&CompanyModels), queryOptions)
|
|
|
var err error
|
|
|
if query.AffectRow, err = query.SelectAndCount(); err != nil {
|
|
|
return 0, Companys, err
|
|
|
}
|
|
|
for _, CompanyModel := range CompanyModels {
|
|
|
if Company, err := repository.transformPgModelToDomainModel(CompanyModel); err != nil {
|
|
|
return 0, Companys, err
|
|
|
} else {
|
|
|
Companys = append(Companys, Company)
|
|
|
}
|
|
|
}
|
|
|
return int64(query.AffectRow), Companys, nil
|
|
|
}
|
|
|
|
|
|
func (repository *CompanyRepository) transformPgModelToDomainModel(CompanyModel *models.Company) (*domain.Company, error) {
|
|
|
m := &domain.Company{}
|
|
|
err := GobModelTransform(m, CompanyModel)
|
|
|
return m, err
|
|
|
}
|
|
|
|
|
|
func NewCompanyRepository(transactionContext *transaction.TransactionContext) (*CompanyRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, ERR_EMPTY_TC
|
|
|
}
|
|
|
return &CompanyRepository{transactionContext: transactionContext}, nil
|
|
|
} |
...
|
...
|
|