pg_company_reponsitory.go 3.0 KB
package repository

import (
	"errors"
	"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,
		Enable:         m.Enable,
		CreateAt:       m.CreateAt,
		UpdateAt:       m.UpdateAt,
		Abbreviation:   m.Abbreviation,
	}, 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,
		Enable:         m.Enable,
		CreateAt:       m.CreateAt,
		UpdateAt:       m.UpdateAt,
		Abbreviation:   m.Abbreviation,
	}
	_, 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,
		Enable:         m.Enable,
		CreateAt:       m.CreateAt,
		DeleteAt:       m.DeleteAt,
		UpdateAt:       m.UpdateAt,
		Abbreviation:   m.Abbreviation,
	}
	_, err = tx.Model(&companyModel).WherePK().Update()
	return err
}

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

	query := tx.Model(&m)
	if queryOptions.Id > 0 {
		hasCondition = true
		query = query.Where("id=?", queryOptions.Id)
	}
	if queryOptions.AdminCompanyId > 0 {
		hasCondition = true
		query = query.Where("admin_company_id=?", queryOptions.Id)
	}
	if !hasCondition {
		return domain.Company{}, errors.New("findOne 必须要有查询条件")
	}
	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
}