pg_customer_service_repository.go 3.3 KB
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 CustomerServiceRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *CustomerServiceRepository) Save(dm *domain.CustomerService) (*domain.CustomerService, error) {
	var (
		err error
		m   = &models.CustomerService{}
		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 *CustomerServiceRepository) Remove(CustomerService *domain.CustomerService) (*domain.CustomerService, error) {
	var (
		tx                   = repository.transactionContext.PgTx
		CustomerServiceModel = &models.CustomerService{Id: CustomerService.Identify().(int64)}
	)
	if _, err := tx.Model(CustomerServiceModel).Where("id = ?", CustomerService.Id).Delete(); err != nil {
		return CustomerService, err
	}
	return CustomerService, nil
}

func (repository *CustomerServiceRepository) FindOne(queryOptions map[string]interface{}) (*domain.CustomerService, error) {
	tx := repository.transactionContext.PgTx
	CustomerServiceModel := new(models.CustomerService)
	query := NewQuery(tx.Model(CustomerServiceModel), queryOptions)
	query.SetWhere("id = ?", "id")
	query.SetWhere("user_id = ?", "user_id")
	if err := query.First(); err != nil {
		return nil, domain.QueryNoRow
	}
	if CustomerServiceModel.Id == 0 {
		return nil, domain.QueryNoRow
	}
	return repository.transformPgModelToDomainModel(CustomerServiceModel)
}

func (repository *CustomerServiceRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CustomerService, error) {
	tx := repository.transactionContext.PgTx
	var CustomerServiceModels []*models.CustomerService
	CustomerServices := make([]*domain.CustomerService, 0)
	query := NewQuery(tx.Model(&CustomerServiceModels), queryOptions).
		SetOrder("create_time", "sortByCreateTime").
		SetOrder("update_time", "sortByUpdateTime").
		SetOrder("id", "sortById")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, CustomerServices, err
	}
	for _, CustomerServiceModel := range CustomerServiceModels {
		if CustomerService, err := repository.transformPgModelToDomainModel(CustomerServiceModel); err != nil {
			return 0, CustomerServices, err
		} else {
			CustomerServices = append(CustomerServices, CustomerService)
		}
	}
	return int64(query.AffectRow), CustomerServices, nil
}

func (repository *CustomerServiceRepository) transformPgModelToDomainModel(CustomerServiceModel *models.CustomerService) (*domain.CustomerService, error) {
	m := &domain.CustomerService{}
	err := GobModelTransform(m, CustomerServiceModel)
	return m, err
}

func NewCustomerServiceRepository(transactionContext *transaction.TransactionContext) (*CustomerServiceRepository, error) {
	if transactionContext == nil {
		return nil, ERR_EMPTY_TC
	}
	return &CustomerServiceRepository{transactionContext: transactionContext}, nil
}