pg_company_reponsitory.go 2.7 KB
package repository

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type CompanyRepository struct {
	transactionContext *transaction.TransactionContext
}

var (
	_ domain.CompanyRepository = (*CompanyRepository)(nil)
)

func NewCompanyRepository(transactionContext *transaction.TransactionContext) (*CompanyRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &CompanyRepository{transactionContext: transactionContext}, nil
}

func (repository CompanyRepository) transformPgModelToDomainModel(m *models.Company) (domain.Company, error) {
	return domain.Company{
		Id:             m.Id,
		Name:           m.Name,
		Phone:          m.Phone,
		Logo:           m.Logo,
		Remarks:        m.Remarks,
		AdminCompanyId: m.AdminCompanyId,
		Status:         m.Status,
		Enable:         m.Enable,
		CreateAt:       m.CreateAt,
		UpdateAt:       m.UpdateAt,
	}, nil
}

func (reponsitory CompanyRepository) Add(m *domain.Company) error {
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
	)
	companyModel := models.Company{
		Id:             m.Id,
		Name:           m.Name,
		Phone:          m.Phone,
		Logo:           m.Logo,
		Remarks:        m.Remarks,
		AdminCompanyId: m.AdminCompanyId,
		Status:         m.Status,
		Enable:         m.Enable,
		CreateAt:       m.CreateAt,
		UpdateAt:       m.UpdateAt,
	}
	_, err = tx.Model(&companyModel).Insert()
	return err
}

func (reponsitory CompanyRepository) Edit(m *domain.Company) error {
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
	)
	companyModel := models.Company{
		Id:             m.Id,
		Name:           m.Name,
		Phone:          m.Phone,
		Logo:           m.Logo,
		Remarks:        m.Remarks,
		AdminCompanyId: m.AdminCompanyId,
		Status:         m.Status,
		Enable:         m.Enable,
		CreateAt:       m.CreateAt,
		DeleteAt:       m.DeleteAt,
		UpdateAt:       m.UpdateAt,
	}
	_, err = tx.Model(&companyModel).Update()
	return err
}

func (reponsitory CompanyRepository) FindOne(queryOptions domain.CompanyFindOneOptions) (domain.Company, error) {
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
		m   models.Company
	)

	query := tx.Model(&m)
	if queryOptions.Id > 0 {
		query = query.Where("id=?", queryOptions.Id)
	}
	err = query.First()
	if err != nil {
		return domain.Company{}, err
	}
	return reponsitory.transformPgModelToDomainModel(&m)
}

func (reponsitory CompanyRepository) Find(queryOptions domain.CompanyFindOptions) (int64, []domain.Company, error) {
	return 0, nil, nil
}