pg_position_repository.go 3.0 KB
package repository

import (
	"time"

	"github.com/go-pg/pg/v10"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)

type PositionRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewPositionRepository(tx *pgTransaction.TransactionContext) *PositionRepository {
	return &PositionRepository{
		transactionContext: tx,
	}
}

// Insert 新增职位
func (repository *PositionRepository) Insert(position *domain.Position) (*domain.Position, error) {
	model := &models.Position{
		Id:        position.Id,
		CompanyId: position.CompanyId,
		Name:      position.Name,
		ParentId:  position.ParentId,
		Path:      position.Path,
		Level:     position.Level,
	}
	_, err := repository.transactionContext.PgTx.Model(model).Insert()
	return position, err
}

// Update 更新
func (repository *PositionRepository) Update(position *domain.Position) (*domain.Position, error) {
	model := &models.Position{
		Id:        position.Id,
		CompanyId: position.CompanyId,
		Name:      position.Name,
		ParentId:  position.ParentId,
		Path:      position.Path,
		Level:     position.Level,
		UpdatedAt: time.Now(),
	}
	_, err := repository.transactionContext.PgTx.Model(model).WherePK().Update()
	return position, err
}

// Remove 删除
func (repository *PositionRepository) Remove(ids []int64) error {
	_, err := repository.transactionContext.PgTx.Model(&models.Position{}).Where("id in (?)", pg.In(ids)).Delete()
	return err
}

func (repository *PositionRepository) FindOne(queryOption map[string]interface{}) (*domain.Position, error) {
	position := &models.Position{}
	query := repository.transactionContext.PgTx.Model(position)
	if id, ok := queryOption["id"]; ok {
		query.Where("id = ?", id)
	}
	err := query.First()
	if err != nil {
		return nil, err
	}
	return repository.transferToDomain(position), nil
}

func (repo *PositionRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Position, error) {
	tx := repo.transactionContext.PgTx
	positionModel := []models.Position{}
	query := tx.Model(&positionModel)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in(?)", pg.In(v))
	}
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id=?", v)
	}
	if v, ok := queryOptions["offset"]; ok {
		query.Offset(v.(int))
	}
	if v, ok := queryOptions["limit"]; ok {
		query.Limit(v.(int))
	}
	cnt, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	arrays := make([]*domain.Position, 0)
	for i := range positionModel {
		result := repo.transferToDomain(&positionModel[i])
		arrays = append(arrays, result)
	}
	return cnt, arrays, nil
}

func (repository *PositionRepository) transferToDomain(model *models.Position) *domain.Position {
	return &domain.Position{
		Id:        model.Id,
		CompanyId: model.CompanyId,
		Name:      model.Name,
		ParentId:  model.ParentId,
		Path:      model.Path,
		Level:     model.Level,
	}
}