pg_customer_service_repository.go 3.4 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 ImCustomerServiceRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *ImCustomerServiceRepository) Save(dm *domain.ImCustomerService) (*domain.ImCustomerService, error) {
	var (
		err error
		m   = &models.ImCustomerService{}
		tx  = repository.transactionContext.PgTx
	)
	if err = GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if _, err = tx.Model(m).Returning("id").Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if _, err = tx.Model(m).Where("id=?", m.Id).Update(m); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *ImCustomerServiceRepository) Remove(ImCustomerService *domain.ImCustomerService) (*domain.ImCustomerService, error) {
	var (
		tx                     = repository.transactionContext.PgTx
		ImCustomerServiceModel = &models.ImCustomerService{Id: ImCustomerService.Identify().(int64)}
	)
	if _, err := tx.Model(ImCustomerServiceModel).Where("id = ?", ImCustomerService.Id).Delete(); err != nil {
		return ImCustomerService, err
	}
	return ImCustomerService, nil
}

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

func (repository *ImCustomerServiceRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ImCustomerService, error) {
	tx := repository.transactionContext.PgTx
	var ImCustomerServiceModels []*models.ImCustomerService
	ImCustomerServices := make([]*domain.ImCustomerService, 0)
	query := NewQuery(tx.Model(&ImCustomerServiceModels), 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, ImCustomerServices, err
	}
	for _, ImCustomerServiceModel := range ImCustomerServiceModels {
		if ImCustomerService, err := repository.transformPgModelToDomainModel(ImCustomerServiceModel); err != nil {
			return 0, ImCustomerServices, err
		} else {
			ImCustomerServices = append(ImCustomerServices, ImCustomerService)
		}
	}
	return int64(query.AffectRow), ImCustomerServices, nil
}

func (repository *ImCustomerServiceRepository) transformPgModelToDomainModel(ImCustomerServiceModel *models.ImCustomerService) (*domain.ImCustomerService, error) {
	m := &domain.ImCustomerService{}
	err := GobModelTransform(m, ImCustomerServiceModel)
	return m, err
}

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