pg_company_repository.go 3.1 KB
package repository

import (
	"github.com/go-pg/pg/v10"
	"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.Model(m).Returning("*").Insert(); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if _, err = tx.Model(m).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")
	query.SetWhere(`"company".status = ?`, "status")
	query.SetWhere(`"company".enable = ?`, "enable")
	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)
	if companies, ok := queryOptions["inCompanyIds"]; ok {
		companyIds, _ := companies.([]int64)
		query.Where("id in (?)", pg.In(companyIds))
	}
	//query.SetWhere("status = ?", "status")
	query.SetWhere(`enable = ?`, "enable")
	query.SetOrder(`create_at`, "sortByCreateTime")
	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
}