审查视图

pkg/infrastructure/repository/pg_position_repository.go 3.1 KB
庄敏学 authored
1 2 3
package repository

import (
Your Name authored
4 5
	"time"
庄敏学 authored
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	"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
}
Your Name authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
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
	}
	var resultList []*domain.Position
	for i := range positionModel {
		result := repo.transferToDomain(&positionModel[i])
		resultList = append(resultList, result)
	}
	return cnt, resultList, nil
}
庄敏学 authored
101 102 103 104 105 106 107 108 109 110
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,
	}
}