pg_company_reponsitory.go 2.4 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 map[string]interface{}) (domain.Company, error) {
	return domain.Company{}, nil
}

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